The R Project SVN R

Rev

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

Rev 11301 Rev 12256
Line 1... Line 1...
1
 
1
 
2
R : Copyright 2000, The R Development Core Team
2
R : Copyright 2001, The R Development Core Team
3
Version 1.2.0 Under development (unstable) (2000-11-09)
3
Version 1.2.0 Patched (2001-01-06)
4
 
4
 
5
R is free software and comes with ABSOLUTELY NO WARRANTY.
5
R is free software and comes with ABSOLUTELY NO WARRANTY.
6
You are welcome to redistribute it under certain conditions.
6
You are welcome to redistribute it under certain conditions.
7
Type	"?license" or "?licence" for distribution details.
7
Type `license()' or `licence()' for distribution details.
8
 
8
 
9
R is a collaborative project with many contributors.
9
R is a collaborative project with many contributors.
10
Type	"?contributors" for a list.
10
Type `contributors()' for more information.
11
 
11
 
12
Type	"demo()" for some demos, "help()" for on-line help, or
12
Type `demo()' for some demos, `help()' for on-line help, or
13
    	"help.start()" for a HTML browser interface to help.
13
`help.start()' for a HTML browser interface to help.
14
Type	"q()" to quit R.
14
Type `q()' to quit R.
15
 
15
 
16
> ##
16
> ##
17
> ## RNG tests using DKW inequality for rate of convergence
17
> ## RNG tests using DKW inequality for rate of convergence
18
> ##
18
> ##
19
> ## P(sup F_n-F >t)<2exp(-2nt^2)
19
> ## P(sup | F_n - F | > t) < 2 exp(-2nt^2)
20
> ##
20
> ##
21
> ## The 2 in front of exp() was derived by Massart. It is the best possible
21
> ## The 2 in front of exp() was derived by Massart. It is the best possible
22
> ## constant valid uniformly in t,n,F. For large nt^2 this agrees with the
22
> ## constant valid uniformly in t,n,F. For large n*t^2 this agrees with the
23
> ## large-sample approximation to the Kolmogorov-Smirnov statistic.
23
> ## large-sample approximation to the Kolmogorov-Smirnov statistic.
24
> ## 
24
> ## 
25
> 
25
> 
26
> 
26
> 
27
> superror<-function(rfoo,pfoo,sample.size,...){
27
> superror <- function(rfoo,pfoo,sample.size,...) {
28
+     x<-rfoo(sample.size,...)
28
+     x <- rfoo(sample.size,...)
29
+     tx<-table(x)
29
+     tx <- table(x)
30
+     xi<-as.numeric(names(tx))
30
+     xi <- as.numeric(names(tx))
31
+     f<-pfoo(xi,...)
31
+     f <- pfoo(xi,...)
32
+     fhat<-cumsum(tx)/sample.size
32
+     fhat <- cumsum(tx)/sample.size
33
+     max(abs(fhat-f))
33
+     max(abs(fhat-f))
34
+ }
34
+ }
35
> 
35
> 
36
> pdkwbound<-function(n,t) 2*exp(-2*n*t*t)
36
> pdkwbound <- function(n,t) 2*exp(-2*n*t*t)
37
> 
37
> 
38
> qdkwbound<-function(n,p) sqrt(log(p/2)/(-2*n))
38
> qdkwbound <- function(n,p) sqrt(log(p/2)/(-2*n))
39
> 
39
> 
-
 
40
> dkwtest <- function(stub = "norm", ...,
-
 
41
+                     sample.size = 10000, pthreshold = 0.001,
40
> dkwtest<-function(stub="norm",sample.size=10000,...,pthreshold=0.001,print.result=TRUE,print.detail=FALSE,stop.on.failure=TRUE){
42
+                     print.result = TRUE, print.detail = FALSE,
-
 
43
+                     stop.on.failure = TRUE)
-
 
44
+ {
41
+     rfoo<-eval(as.name(paste("r",stub,sep="")))
45
+     rfoo <- eval(as.name(paste("r", stub, sep="")))
42
+     pfoo<-eval(as.name(paste("p",stub,sep="")))
46
+     pfoo <- eval(as.name(paste("p", stub, sep="")))
43
+     s<-superror(rfoo,pfoo,sample.size,...)
47
+     s <- superror(rfoo, pfoo, sample.size, ...)
44
+     if (print.result | print.detail){
48
+     if (print.result || print.detail) {
45
+         printargs<-substitute(list(...))
49
+         printargs <- substitute(list(...))
46
+         printargs[[1]]<-as.name(stub)
50
+         printargs[[1]] <- as.name(stub)
47
+         cat(deparse(printargs))
51
+         cat(deparse(printargs))
48
+         if (print.detail)
52
+         if (print.detail)
-
 
53
+             cat("\nsupremum error = ",signif(s,2),
49
+             cat("\nsupremum error = ",signif(s,2)," with p-value=",min(1,round(pdkwbound(sample.size,s),4)),"\n")
54
+                 " with p-value=",min(1,round(pdkwbound(sample.size,s),4)),"\n")
50
+     }
55
+     }
51
+     rval<-(s<qdkwbound(sample.size,pthreshold))
56
+     rval <- (s < qdkwbound(sample.size,pthreshold))
52
+     if (print.result)
57
+     if (print.result)
53
+         cat(c(" FAILED\n"," PASSED\n",)[rval+1])
58
+         cat(c(" FAILED\n"," PASSED\n",)[rval+1])
54
+     if (stop.on.failure & !rval)
59
+     if (stop.on.failure && !rval)
55
+         stop("dkwtest failed")
60
+         stop("dkwtest failed")
56
+     rval
61
+     rval
57
+   }
62
+ }
-
 
63
> 
-
 
64
> .proctime00 <- proc.time() # start timing
58
> 
65
> 
59
> 
66
> 
60
> dkwtest("binom",size=1,prob=0.2)
67
> dkwtest("binom",size =   1,prob = 0.2)
61
binom(size = 1, prob = 0.2) PASSED
68
binom(size = 1, prob = 0.2) PASSED
62
[1] TRUE
69
[1] TRUE
-
 
70
> dkwtest("binom",size =   2,prob = 0.2)
-
 
71
binom(size = 2, prob = 0.2) PASSED
-
 
72
[1] TRUE
63
> dkwtest("binom",size=100,prob=0.2)
73
> dkwtest("binom",size = 100,prob = 0.2)
64
binom(size = 100, prob = 0.2) PASSED
74
binom(size = 100, prob = 0.2) PASSED
65
[1] TRUE
75
[1] TRUE
66
> dkwtest("binom",size=1,prob=0.2)
76
> dkwtest("binom",size = 1e4,prob = 0.2)
67
binom(size = 1, prob = 0.2) PASSED
77
binom(size = 10000, prob = 0.2) PASSED
68
[1] TRUE
78
[1] TRUE
69
> dkwtest("binom",size=1,prob=0.8)
79
> dkwtest("binom",size =   1,prob = 0.8)
70
binom(size = 1, prob = 0.8) PASSED
80
binom(size = 1, prob = 0.8) PASSED
71
[1] TRUE
81
[1] TRUE
72
> dkwtest("binom",size=100,prob=0.8)
82
> dkwtest("binom",size = 100,prob = 0.8)
73
binom(size = 100, prob = 0.8) PASSED
83
binom(size = 100, prob = 0.8) PASSED
74
[1] TRUE
84
[1] TRUE
75
> dkwtest("binom",size=100,prob=0.2)
85
> dkwtest("binom",size = 100,prob = 0.999)
76
binom(size = 100, prob = 0.2) PASSED
86
binom(size = 100, prob = 0.999) PASSED
77
[1] TRUE
-
 
78
> dkwtest("binom",size=1,prob=0.2)
-
 
79
binom(size = 1, prob = 0.2) PASSED
-
 
80
[1] TRUE
87
[1] TRUE
81
> 
88
> 
82
> dkwtest("pois",lambda=9.5)
89
> dkwtest("pois",lambda =  0.095)
83
pois(lambda = 9.5) PASSED
90
pois(lambda = 0.095) PASSED
84
[1] TRUE
91
[1] TRUE
85
> dkwtest("pois",lambda=0.95)
92
> dkwtest("pois",lambda =  0.95)
86
pois(lambda = 0.95) PASSED
93
pois(lambda = 0.95) PASSED
87
[1] TRUE
94
[1] TRUE
88
> dkwtest("pois",lambda=95)
95
> dkwtest("pois",lambda =  9.5)
89
pois(lambda = 95) PASSED
96
pois(lambda = 9.5) PASSED
90
[1] TRUE
97
[1] TRUE
91
> dkwtest("pois",lambda=0.95)
98
> dkwtest("pois",lambda = 95)
92
pois(lambda = 0.95) PASSED
99
pois(lambda = 95) PASSED
93
[1] TRUE
100
[1] TRUE
94
> 
101
> 
95
> dkwtest("nbinom",size=1,prob=0.2)
102
> dkwtest("nbinom",size =   1,prob = 0.2)
96
nbinom(size = 1, prob = 0.2) PASSED
103
nbinom(size = 1, prob = 0.2) PASSED
97
[1] TRUE
104
[1] TRUE
-
 
105
> dkwtest("nbinom",size =   2,prob = 0.2)
-
 
106
nbinom(size = 2, prob = 0.2) PASSED
-
 
107
[1] TRUE
98
> dkwtest("nbinom",size=100,prob=0.2)
108
> dkwtest("nbinom",size = 100,prob = 0.2)
99
nbinom(size = 100, prob = 0.2) PASSED
109
nbinom(size = 100, prob = 0.2) PASSED
100
[1] TRUE
110
[1] TRUE
101
> dkwtest("nbinom",size=1,prob=0.2)
111
> dkwtest("nbinom",size = 1e4,prob = 0.2)
102
nbinom(size = 1, prob = 0.2) PASSED
112
nbinom(size = 10000, prob = 0.2) PASSED
103
[1] TRUE
113
[1] TRUE
104
> dkwtest("nbinom",size=1,prob=0.8)
114
> dkwtest("nbinom",size =   1,prob = 0.8)
105
nbinom(size = 1, prob = 0.8) PASSED
115
nbinom(size = 1, prob = 0.8) PASSED
106
[1] TRUE
116
[1] TRUE
107
> dkwtest("nbinom",size=100,prob=0.8)
117
> dkwtest("nbinom",size = 100,prob = 0.8)
108
nbinom(size = 100, prob = 0.8) PASSED
118
nbinom(size = 100, prob = 0.8) PASSED
109
[1] TRUE
119
[1] TRUE
110
> dkwtest("nbinom",size=100,prob=0.2)
120
> dkwtest("nbinom",size = 100,prob = 0.999)
111
nbinom(size = 100, prob = 0.2) PASSED
121
nbinom(size = 100, prob = 0.999) PASSED
112
[1] TRUE
-
 
113
> dkwtest("nbinom",size=1,prob=0.2)
-
 
114
nbinom(size = 1, prob = 0.2) PASSED
-
 
115
[1] TRUE
122
[1] TRUE
116
> 
123
> 
117
> dkwtest("norm")
124
> dkwtest("norm")
118
norm() PASSED
125
norm() PASSED
119
[1] TRUE
126
[1] TRUE
120
> dkwtest("norm",mean=5,sd=3)
127
> dkwtest("norm",mean = 5,sd = 3)
121
norm(mean = 5, sd = 3) PASSED
128
norm(mean = 5, sd = 3) PASSED
122
[1] TRUE
129
[1] TRUE
123
> 
130
> 
124
> dkwtest("gamma",shape=0.1)
131
> dkwtest("gamma",shape =  0.1)
125
gamma(shape = 0.1) PASSED
132
gamma(shape = 0.1) PASSED
126
[1] TRUE
133
[1] TRUE
127
> dkwtest("gamma",shape=10)
134
> dkwtest("gamma",shape =  0.2)
128
gamma(shape = 10) PASSED
135
gamma(shape = 0.2) PASSED
129
[1] TRUE
136
[1] TRUE
130
> dkwtest("gamma",shape=0.1)
-
 
131
gamma(shape = 0.1) PASSED
-
 
132
[1] TRUE
-
 
133
> dkwtest("gamma",shape=10)
137
> dkwtest("gamma",shape = 10)
134
gamma(shape = 10) PASSED
138
gamma(shape = 10) PASSED
135
[1] TRUE
139
[1] TRUE
-
 
140
> dkwtest("gamma",shape = 20)
-
 
141
gamma(shape = 20) PASSED
-
 
142
[1] TRUE
136
> 
143
> 
137
> dkwtest("hyper",m=40,n=30,k=20)
144
> dkwtest("hyper",m = 40,n = 30,k = 20)
138
hyper(m = 40, n = 30, k = 20) PASSED
145
hyper(m = 40, n = 30, k = 20) PASSED
139
[1] TRUE
146
[1] TRUE
140
> dkwtest("hyper",m=4,n=3,k=2)
147
> dkwtest("hyper",m = 40,n =  3,k = 20)
141
hyper(m = 4, n = 3, k = 2) PASSED
148
hyper(m = 40, n = 3, k = 20) PASSED
142
[1] TRUE
149
[1] TRUE
143
> dkwtest("hyper",m=40,n=30,k=20)
150
> dkwtest("hyper",m =  6,n =  3,k =  2)
144
hyper(m = 40, n = 30, k = 20) PASSED
151
hyper(m = 6, n = 3, k = 2) PASSED
145
[1] TRUE
152
[1] TRUE
146
> dkwtest("hyper",m=4,n=3,k=2)
-
 
147
hyper(m = 4, n = 3, k = 2) PASSED
-
 
148
[1] TRUE
-
 
149
> dkwtest("hyper",m=5,n=3,k=2)
153
> dkwtest("hyper",m =  5,n =  3,k =  2)
150
hyper(m = 5, n = 3, k = 2) PASSED
154
hyper(m = 5, n = 3, k = 2) PASSED
151
[1] TRUE
155
[1] TRUE
-
 
156
> dkwtest("hyper",m =  4,n =  3,k =  2)
-
 
157
hyper(m = 4, n = 3, k = 2) PASSED
-
 
158
[1] TRUE
-
 
159
> 
152
> 
160
> 
153
> dkwtest("signrank",n=1)
161
> dkwtest("signrank",n =  1)
154
signrank(n = 1) PASSED
162
signrank(n = 1) PASSED
155
[1] TRUE
163
[1] TRUE
156
> dkwtest("signrank",n=10)
164
> dkwtest("signrank",n =  2)
157
signrank(n = 10) PASSED
165
signrank(n = 2) PASSED
158
[1] TRUE
166
[1] TRUE
159
> dkwtest("signrank",n=1)
167
> dkwtest("signrank",n = 10)
160
signrank(n = 1) PASSED
168
signrank(n = 10) PASSED
161
[1] TRUE
169
[1] TRUE
162
> dkwtest("signrank",n=30)
170
> dkwtest("signrank",n = 30)
163
signrank(n = 30) PASSED
171
signrank(n = 30) PASSED
164
[1] TRUE
172
[1] TRUE
165
> 
173
> 
166
> dkwtest("wilcox",m=40,n=30)
174
> dkwtest("wilcox",m = 40,n = 30)
167
wilcox(m = 40, n = 30) PASSED
175
wilcox(m = 40, n = 30) PASSED
168
[1] TRUE
176
[1] TRUE
169
> dkwtest("wilcox",m=4,n=3)
177
> dkwtest("wilcox",m = 40,n = 10)
170
wilcox(m = 4, n = 3) PASSED
178
wilcox(m = 40, n = 10) PASSED
171
[1] TRUE
179
[1] TRUE
172
> dkwtest("wilcox",m=40,n=30)
180
> dkwtest("wilcox",m =  6,n =  3)
173
wilcox(m = 40, n = 30) PASSED
181
wilcox(m = 6, n = 3) PASSED
174
[1] TRUE
182
[1] TRUE
175
> dkwtest("wilcox",m=4,n=3)
-
 
176
wilcox(m = 4, n = 3) PASSED
-
 
177
[1] TRUE
-
 
178
> dkwtest("wilcox",m=5,n=3)
183
> dkwtest("wilcox",m =  5,n =  3)
179
wilcox(m = 5, n = 3) PASSED
184
wilcox(m = 5, n = 3) PASSED
180
[1] TRUE
185
[1] TRUE
-
 
186
> dkwtest("wilcox",m =  4,n =  3)
-
 
187
wilcox(m = 4, n = 3) PASSED
-
 
188
[1] TRUE
181
> 
189
> 
182
> dkwtest("chisq",df=1)
190
> dkwtest("chisq",df =  1)
183
chisq(df = 1) PASSED
191
chisq(df = 1) PASSED
184
[1] TRUE
192
[1] TRUE
185
> dkwtest("chisq",df=10)
193
> dkwtest("chisq",df = 10)
186
chisq(df = 10) PASSED
194
chisq(df = 10) PASSED
187
[1] TRUE
195
[1] TRUE
188
> 
196
> 
189
> dkwtest("logis")
197
> dkwtest("logis")
190
logis() PASSED
198
logis() PASSED
191
[1] TRUE
199
[1] TRUE
192
> dkwtest("logis",location=4,scale=2)
200
> dkwtest("logis",location = 4,scale = 2)
193
logis(location = 4, scale = 2) PASSED
201
logis(location = 4, scale = 2) PASSED
194
[1] TRUE
202
[1] TRUE
195
> 
203
> 
196
> dkwtest("t",df=1)
204
> dkwtest("t",df =  1)
197
t(df = 1) PASSED
205
t(df = 1) PASSED
198
[1] TRUE
206
[1] TRUE
199
> dkwtest("t",df=10)
207
> dkwtest("t",df = 10)
200
t(df = 10) PASSED
208
t(df = 10) PASSED
201
[1] TRUE
209
[1] TRUE
202
> dkwtest("t",df=40)
210
> dkwtest("t",df = 40)
203
t(df = 40) PASSED
211
t(df = 40) PASSED
204
[1] TRUE
212
[1] TRUE
205
> 
213
> 
206
> dkwtest("beta",shape1=1,shape2=1)
214
> dkwtest("beta",shape1 = 1, shape2 = 1)
207
beta(shape1 = 1, shape2 = 1) PASSED
215
beta(shape1 = 1, shape2 = 1) PASSED
208
[1] TRUE
216
[1] TRUE
209
> dkwtest("beta",shape1=2,shape2=1)
217
> dkwtest("beta",shape1 = 2, shape2 = 1)
210
beta(shape1 = 2, shape2 = 1) PASSED
218
beta(shape1 = 2, shape2 = 1) PASSED
211
[1] TRUE
219
[1] TRUE
212
> dkwtest("beta",shape1=2,shape2=2)
220
> dkwtest("beta",shape1 = 1, shape2 = 2)
213
beta(shape1 = 2, shape2 = 2) PASSED
221
beta(shape1 = 1, shape2 = 2) PASSED
214
[1] TRUE
222
[1] TRUE
215
> dkwtest("beta",shape1=2,shape2=2)
223
> dkwtest("beta",shape1 = 2, shape2 = 2)
216
beta(shape1 = 2, shape2 = 2) PASSED
224
beta(shape1 = 2, shape2 = 2) PASSED
217
[1] TRUE
225
[1] TRUE
218
> dkwtest("beta",shape1=.2,shape2=.2)
226
> dkwtest("beta",shape1 = .2,shape2 = .2)
219
beta(shape1 = 0.2, shape2 = 0.2) PASSED
227
beta(shape1 = 0.2, shape2 = 0.2) PASSED
220
[1] TRUE
228
[1] TRUE
221
> 
229
> 
222
> dkwtest("cauchy")
230
> dkwtest("cauchy")
223
cauchy() PASSED
231
cauchy() PASSED
224
[1] TRUE
232
[1] TRUE
225
> dkwtest("cauchy",location=4,scale=2)
233
> dkwtest("cauchy",location = 4,scale = 2)
226
cauchy(location = 4, scale = 2) PASSED
234
cauchy(location = 4, scale = 2) PASSED
227
[1] TRUE
235
[1] TRUE
228
> 
236
> 
229
> dkwtest("f",df1=1,df2=1)
237
> dkwtest("f",df1 =  1,df2 =  1)
230
f(df1 = 1, df2 = 1) PASSED
238
f(df1 = 1, df2 = 1) PASSED
231
[1] TRUE
239
[1] TRUE
232
> dkwtest("f",df1=1,df2=10)
240
> dkwtest("f",df1 =  1,df2 = 10)
233
f(df1 = 1, df2 = 10) PASSED
241
f(df1 = 1, df2 = 10) PASSED
234
[1] TRUE
242
[1] TRUE
235
> dkwtest("f",df1=10,df2=10)
243
> dkwtest("f",df1 = 10,df2 = 10)
236
f(df1 = 10, df2 = 10) PASSED
244
f(df1 = 10, df2 = 10) PASSED
237
[1] TRUE
245
[1] TRUE
238
> dkwtest("f",df1=30,df2=3)
246
> dkwtest("f",df1 = 30,df2 =  3)
239
f(df1 = 30, df2 = 3) PASSED
247
f(df1 = 30, df2 = 3) PASSED
240
[1] TRUE
248
[1] TRUE
241
> 
249
> 
242
> dkwtest("weibull",shape=1)
250
> dkwtest("weibull",shape = 1)
243
weibull(shape = 1) PASSED
251
weibull(shape = 1) PASSED
244
[1] TRUE
252
[1] TRUE
245
> dkwtest("weibull",shape=4,scale=4)
253
> dkwtest("weibull",shape = 4,scale = 4)
246
weibull(shape = 4, scale = 4) PASSED
254
weibull(shape = 4, scale = 4) PASSED
247
[1] TRUE
255
[1] TRUE
248
> 
256
> 
-
 
257
> 
-
 
258
> cat('Time elapsed: ', proc.time() - .proctime00,'\n')
-
 
259
Time elapsed:  22.67 0.15 22.82 0 0 
-
 
260
> 
-
 
261
>