将 t 检验的功效与卡方检验的功效进行比较的图表
Diagram that compares the power of the t-Test with the power of the Chi-sqare-Test
我尝试比较卡方检验和 t 检验对一个特定值的功效,我的总体目标是证明 t 检验更强大(因为它有一个假设分布)。我使用 R 的 pwr 包来计算每个函数的功率,然后编写了两个函数并绘制了结果。
但是,我没有发现 t 检验比卡方检验好,我对结果感到困惑。我花了几个小时在上面,所以非常感谢每一个帮助。
是代码错了,还是我对power functions的理解有误,还是package有问题?
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))
提前致谢!
如果我对问题的理解正确,那么您正在测试二项分布,空值下的均值等于 0.2,备选方案为空值大于 0.2?如果是这样,那么你函数g
的第2行不应该是sigma <- sqrt(.2*(1-.2))
而不是sigma <- sqrt(.5*(1-.5))
吗?这样,您的标准偏差会更小,从而产生更大的检验统计量,因此更小的 p 值会导致更高的功效。
我尝试比较卡方检验和 t 检验对一个特定值的功效,我的总体目标是证明 t 检验更强大(因为它有一个假设分布)。我使用 R 的 pwr 包来计算每个函数的功率,然后编写了两个函数并绘制了结果。 但是,我没有发现 t 检验比卡方检验好,我对结果感到困惑。我花了几个小时在上面,所以非常感谢每一个帮助。
是代码错了,还是我对power functions的理解有误,还是package有问题?
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))
提前致谢!
如果我对问题的理解正确,那么您正在测试二项分布,空值下的均值等于 0.2,备选方案为空值大于 0.2?如果是这样,那么你函数g
的第2行不应该是sigma <- sqrt(.2*(1-.2))
而不是sigma <- sqrt(.5*(1-.5))
吗?这样,您的标准偏差会更小,从而产生更大的检验统计量,因此更小的 p 值会导致更高的功效。