我正在尝试将我的手动计算与 EnvStats 包 varTest() 函数进行比较。它不匹配,所以我查看了源代码并找到了 varTest() 函数使用的计算。
我不怀疑它为什么不同(见下文),但为什么这是一个合理的替代品。只要 n 相当大(这使得卡方分布更加对称),这种近似似乎是可以接受的。不过,对于小 n 来说,这可能会有些问题。这尤其成问题,因为近似值较小并且可能增加 I 类错误的可能性。
我错过了什么,或者这不是很正确吗?
> # Generate Data
> n <- 100
> x <- rnorm(n)
> (varx<-var(x))
[1] 1.060071
>
> # Test Statistic
> sigmasq0 <- 1^2
> (chisq.obs<-((n-1)*varx)/sigmasq0)
[1] 104.9471
> (chisq.obs.mirror<-((n-1)*(sigmasq0-abs(sigmasq0-varx))/sigmasq0))
[1] 93.05292
>
> # P-value
> (p.value<- pchisq(chisq.obs.mirror,df=n-1) + # lower tail
+ 1-pchisq((chisq.obs),df=n-1)) # upper tail
[1] 0.6727472
>
> # EnvStats
> library(EnvStats)
> varTest(x=x)
Chi-Squared Test on Variance
data: x
Chi-Squared = 104.95, df = 99, p-value = 0.6443
alternative hypothesis: true variance is not equal to 1
95 percent confidence interval:
0.8172049 1.4305552
sample estimates:
variance
1.060071
> 2*min(pchisq(chisq.obs,df=n-1),1-pchisq(chisq.obs,df=n-1))
[1] 0.6443462