我试图将卡方测试和t检验的幂函数进行比较,得到一个特定的值,我的总体目标是表明t-Test更强大(因为它有一个关于分布的假设)。我用pwr软件包计算了每个函数的功率,然后编写了两个函数并绘制了结果。然而,我并不认为t检验比卡方检验更好,我对结果感到困惑。我花了好几个小时在上面,所以每一次的帮助都是非常感谢的。

代码是错的,我是对权力函数有错误的理解,还是包中有什么错误?
library(pwr)
#mu is the value for which the power is calculated
#no is the number of observations
#function of the power of the t-test with a h0 of .2
g <- function(mu, alpha, no) { #calculate the power of a particular value for the t-test with h0=.2
p <- mu-.20
sigma <- sqrt(.5*(1-.5))
pwr.t.test(n = no, d = p/sigma, sig.level = alpha, type = "one.sample", alternative="greater")$power # d is the effect size p/sigma
}
#chi squared test
h <- function(mu, alpha, no, degree) {#calculate the power of a particular value for the chi squared test
p01 <- .2 # these constructs the effect size (which is a bit different for the chi squared)
p02 <- .8
p11 <-mu
p12 <- 1-p11
effect.size <- sqrt(((p01-p11)^2/p01)+((p02-p12)^2/p02)) # effect size
pwr.chisq.test(N=no, df=degree, sig.level = alpha, w=effect.size)$power
}
#create a diagram
plot(1, 1, type = "n",
xlab = expression(mu),
xlim = c(.00, .75),
ylim = c(0, 1.1),
ylab = expression(1-beta),
axes=T, main="Power function t-Test and Chi-squared-Test")
axis(side = 2, at = c(0.05), labels = c(expression(alpha)), las = 3)
axis(side = 1, at = 3, labels = expression(mu[0]))
abline(h = c(0.05, 1), lty = 2)
legend(.5,.5, # places a legend at the appropriate place
c("t-Test","Chi-square-Test"), # puts text in the legend
lwd=c(2.5,2.5),col=c("black","red"))
curve(h(x, alpha = 0.05, no = 100, degree=1), from = .00, to = .75, add = TRUE, col="red",lwd=c(2.5,2.5) )
curve(g(x, alpha = 0.05, no = 100), from = .00, to = .75, add = TRUE, lwd=c(2.5,2.5))提前谢谢!
发布于 2016-02-01 22:29:56
如果我正确地理解了这个问题,那么您是在测试一个二项分布,它的平均值在null等于0.2,而另一个值大于0.2?如果是这样的话,那么在第2行中,函数g不是应该是sigma <- sqrt(.2*(1-.2))而不是sigma <- sqrt(.5*(1-.5))吗?这样,您的标准偏差就会更小,从而产生更大的测试统计量,从而减小p值,从而导致更高的功率。
https://stackoverflow.com/questions/35136619
复制相似问题