我希望通过hotellings测试,如果p变量正态随机向量的样本具有理论平均值,我可以证明。但是如果HottelingsT2函数的分布与HottelingsT2-Test使用的测试统计的分布相匹配,则使用ks.test进行交叉检查。这意味着模拟实验的平均值不是0,但显然它们具有。所以在上下文中应该有一些错误。有错误吗?
require(mvtnorm)
require(ICSNP)
subject<-50
treatment<-4
V<-matrix(c(644.03100226056, 184.319025225855, 572.5312199559, 143.106678641056, 184.319025225855, 73.5310268006399, 230.838267981476, 130.977532385651, 572.5312199559, 230.838267981476, 736.378779002912, 429.445506266528, 143.106678641056, 130.977532385651, 429.445506266528, 435.124191935888),treatment,treatment)
experiment<-list()
R<-3000
seed<-split(1:(R*subject),1:R)
for(i in 1:R){
e<-c()
for(j in 1:subject){
set.seed(seed[[i]][j])
e<-c(e,rmvnorm(mean=rep(0,treatment),sigma=V,n=1,method="chol"))
}
experiment<-c(experiment,list(matrix(e,subject,treatment,byrow=T)))
}
p.values<-c()
for(e in experiment){
fit<-lm(e~1)
p.values<-c(p.values,HotellingsT2(e, mu=rep(0,treatment))[["p.value"]])
}
ks.test(p.values, punif,alternative = "two.sided")
答案 0 :(得分:4)
Hong Ooi对set.seed
这个问题是正确的。我在发布时运行了您的代码并获得了以下结果:
> ks.test(p.values, punif,alternative = "two.sided")
One-sample Kolmogorov-Smirnov test
data: p.values
D = 0.0615, p-value = 2.729e-10
alternative hypothesis: two-sided
但是如果你改变你的代码:
... everything the same before here ...
experiment <- list()
R <- 3000 # experiment
set.seed(42) # set new seed
for (i in 1:R) { # for each of 3000 experiments
e <- c() # empty vector
for (j in 1:subject){ # for each of 50 subjects
e <- c(e,rmvnorm(mean=rep(0,treatment),sigma=V,n=1,method="chol"))
}
experiment <- c(experiment,list(matrix(e,subject,treatment,byrow=T)))
}
... everything the same after here ...
然后你得到以下内容:
> ks.test(p.values, punif,alternative = "two.sided")
One-sample Kolmogorov-Smirnov test
data: p.values
D = 0.0122, p-value = 0.7613
alternative hypothesis: two-sided
基本上,通过在每次迭代中继续重新设置随机种子,即使您小心选择不同的值,您仍然会消除连续绘制的独立性。
答案 1 :(得分:2)
我没有检查过代码,但如果这与Klaus的其他帖子Using Kolmogorov Smirnov Test in R中描述的问题相同,我也不会感到惊讶。基本上,不要将set.seed
放在循环的中间:在代码的顶部设置一次,之后不管它。