The R Project SVN R

Rev

Rev 56086 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 56086 Rev 71034
Line 1... Line 1...
1
## some examples of the KS test
1
#### Some examples of the KS and Wilcoxon tests
-
 
2
 
-
 
3
### ------ Kolmogorov Smirnov (KS) --------------
2
 
4
 
3
## unrealistic one of PR#14561
5
## unrealistic one of PR#14561
4
ds1 <- c(1.7,2,3,3,4,4,5,5,6,6)
6
ds1 <- c(1.7,2,3,3,4,4,5,5,6,6)
5
ks.test(ds1, "pnorm", mean = 3.3, sd = 1.55216)
7
ks.test(ds1, "pnorm", mean = 3.3, sd = 1.55216)
6
# how on earth can sigma = 1.55216 be known?
8
# how on earth can sigma = 1.55216 be known?
Line 18... Line 20...
18
ks.test(round(ds2, 2), "pnorm", exact = TRUE)
20
ks.test(round(ds2, 2), "pnorm", exact = TRUE)
19
# now D has doubled, but p-values remain similar (if very different from ds2)
21
# now D has doubled, but p-values remain similar (if very different from ds2)
20
ks.test(round(ds2, 1), "pnorm")
22
ks.test(round(ds2, 1), "pnorm")
21
ks.test(round(ds2, 1), "pnorm", exact = TRUE)
23
ks.test(round(ds2, 1), "pnorm", exact = TRUE)
22
 
24
 
-
 
25
 
-
 
26
### ------ Wilkoxon (Mann Whitney) --------------
-
 
27
 
-
 
28
options(nwarnings = 1000)
-
 
29
(alts <- setNames(, eval(formals(stats:::wilcox.test.default)$alternative)))
-
 
30
x0 <- 0:4
-
 
31
(x.set <- list(s0 = lapply(x0, function(m) 0:m),
-
 
32
               s. = lapply(x0, function(m) c(1e-9, seq_len(m)))))
-
 
33
stats <- setNames(nm = c("statistic", "p.value", "conf.int", "estimate"))
-
 
34
 
-
 
35
## Even with  conf.int = TRUE, do not want errors :
-
 
36
RR <-
-
 
37
    lapply(x.set, ## for all data sets
-
 
38
           function(xs)
-
 
39
               lapply(alts, ## for all three alternatives
-
 
40
                      function(alt)
-
 
41
                          lapply(xs, function(x)
-
 
42
                              ## try(
-
 
43
                              wilcox.test(x, exact=TRUE, conf.int=TRUE, alternative = alt)
-
 
44
                              ## )
-
 
45
                              )))
-
 
46
length(ww <- warnings()) # 52 (or 43 for x0 <- 0:3)
-
 
47
unique(ww) # 4 different ones
-
 
48
 
-
 
49
cc <- lapply(RR, function(A) lapply(A, function(bb) lapply(bb, class)))
-
 
50
table(unlist(cc))
-
 
51
## in R <= 3.3.1,  with try( .. ) above, we got
-
 
52
## htest try-error
-
 
53
##    23         7
-
 
54
uc <- unlist(cc[["s0"]]); noquote(names(uc)[uc != "htest"]) ## these 7 cases :
-
 
55
## two.sided1 two.sided2 two.sided3
-
 
56
## less1      less2
-
 
57
## greater1   greater2
-
 
58
 
-
 
59
##--- How close are the stats of  (0:m)  to those of  (eps, 1:m) ------------
-
 
60
 
-
 
61
## a version that still works with above try(.) and errors there:
-
 
62
getC <- function(L, C) if(inherits(L,"try-error")) c(L) else L[[C]]
-
 
63
stR <- lapply(stats, function(COMP)
-
 
64
           lapply(RR, function(A)
-
 
65
               lapply(A, function(bb)
-
 
66
                   lapply(bb, getC, C=COMP) )))
-
 
67
 
-
 
68
## a) P-value
-
 
69
pv <- stR[["p.value"]]
-
 
70
## only the first is NaN, all others in [0,1]:
-
 
71
sapply(pv$s0, unlist)
-
 
72
sapply(pv$s., unlist) # not really close, but ..
-
 
73
 
-
 
74
pv$s0$two.sided[1] <-  1 ## artificially
-
 
75
stopifnot(all.equal(pv$s0, pv$s., tol = 0.5 + 1e-6), # seen 0.5
-
 
76
	  ## "less" are close:
-
 
77
	  all.equal(unlist(pv[[c("s0","less")]]),
-
 
78
		    unlist(pv[[c("s.","less")]]), tol = 0.03),
-
 
79
	  0 <= unlist(pv), unlist(pv) <= 1) # <- no further NA ..
-
 
80
## b)
-
 
81
sapply(stR[["statistic"]], unlist)
-
 
82
## Conf.int.:
-
 
83
## c)
-
 
84
sapply(stR[["estimate" ]], unlist)
-
 
85
## d) confidence interval
-
 
86
formatCI <- function(ci)
-
 
87
    sprintf("[%g, %g] (%g%%)", ci[[1]], ci[[2]],
-
 
88
	    round(100*attr(ci,"conf.level")))
-
 
89
nx <- length(x0)
-
 
90
noquote(vapply(stR[["conf.int"]], function(ss)
-
 
91
    vapply(ss, function(alt) vapply(alt, formatCI, ""), character(nx)),
-
 
92
    matrix("", nx, length(alts))))
-
 
93
 
-
 
94
 
-
 
95
##-------- 2-sample tests (working unchanged) ------------------
-
 
96
 
-
 
97
R2 <- lapply(alts, ## for all three alternatives
-
 
98
             function(alt)
-
 
99
                 lapply(seq_along(x0), function(k)
-
 
100
                         wilcox.test(x = x.set$s0[[k]], y = x.set$s.[[k]],
-
 
101
                                     exact=TRUE, conf.int=TRUE, alternative = alt)))
-
 
102
length(w2 <- warnings()) # 27
-
 
103
unique(w2) # 3 different ones
-
 
104
 
-
 
105
table(uc2 <- unlist(c2 <- lapply(R2, function(A) lapply(A, class))))
-
 
106
stopifnot(uc2 == "htest")
-
 
107
 
-
 
108
stR2 <- lapply(stats,
-
 
109
               function(COMP)
-
 
110
                   lapply(R2, function(A) lapply(A, getC, C=COMP)))
-
 
111
 
-
 
112
lapply(stats[-3], ## -3: "conf.int" separately
-
 
113
       function(ST) sapply(stR2[[ST]], unlist))
-
 
114
 
-
 
115
noquote(sapply(stR2[["conf.int"]], function(.) vapply(., formatCI, "")))
-
 
116