将t-Test的功率与Chi-sqare-Test的功率进行比较的图表

时间:2016-02-01 17:26:18

标签: r statistics

我尝试将卡方检验和t检验的幂函数与一个特定值进行比较,我的总体目标是证明t检验更强大(因为它对分布有一个假设) )。我使用pwr软件包来计算每个函数的功效,然后编写两个函数并绘制结果。 但是,我没有发现t检验比卡方检验更好,我对结果感到困惑。我花了几个小时,所以每一个帮助都非常感激。

enter image description here

代码是否错误,我对电源功能有错误的了解,或者包装中是否有错误?

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))

提前多多感谢!

1 个答案:

答案 0 :(得分:0)

如果我理解正确的问题,那么您正在测试二项分布,其中null的平均值等于0.2,而替代值为null大于0.2?如果是这样,那么在函数g的第2行,不应该是sigma <- sqrt(.2*(1-.2))而不是sigma <- sqrt(.5*(1-.5))?这样,您的标准偏差将会更小,从而导致更大的测试统计数据,从而导致更小的p值,从而产生更高的功率。