I want to do a plot of power vs effect size. I have done one of power vs sample size using:
ptab<-cbind(NULL, NULL)
for (i in c(1:4588)){
pwrt<-power.t.test(d=.9,n=c(1:4588),sig.level=.05,type="one.sample",alternative="two.sided")
plot(pwrt$n,pwrt$power,type="b",xlab="sample size",ylab="power")
but can't find any instructions for power vs effect size. Any ideas?
Edit: Just looked at Caracal's answer (thanks mate). Looks amazing, but your coding is totally different to my attempts:
plot(pwrt$d,pwrt$power,type="b",xla开发者_开发知识库b="effect size",ylab="power")
although this just produces a graph with one point directly in the middle. I tried to convert Caracal's coding to my sample size and although it worked, the graph is crazy!
Edit: show power-plot for one-way ANOVA with 3 groups as well as for one-sample t-Test.
P <- 3 # number of groups for ANOVA
fVals <- seq(0, 1.2, length.out=100) # effect sizes f for ANOVA
dVals <- seq(0, 3, length.out=100) # effect sizes d for t-Test
nn <- seq(10, 25, by=5) # group sizes
alpha <- 0.05 # test for level alpha
# function to calculate one-way ANOVA power for given group size
getFPow <- function(n) {
critF <- qf(1-alpha, P-1, P*n - P) # critical F-value
# probabilities of exceeding this F-value given the effect sizes f
# P*n*fVals^2 is the non-centrality parameter
1-pf(critF, P-1, P*n - P, P*n * fVals^2)
}
# function to calculate one-sample t-Test power for given group size
getTPow <- function(n) {
critT <- qt(1-alpha, n-1) # critical t-value
# probabilities of exceeding this t-value given the effect sizes d
# sqrt(n)*d is the non-centrality parameter
1-pt(critT, n-1, sqrt(n)*dVals)
}
powsF <- sapply(nn, getFPow) # ANOVA power for for all group sizes
powsT <- sapply(nn, getTPow) # t-Test power for for all group sizes
dev.new(width=10, height=5)
par(mfrow=c(1, 2))
matplot(fVals, powsF, type="l", lty=1, lwd=2, xlab="effect size f",
ylab="Power", main="Power one-way ANOVA", xaxs="i",
xlim=c(-0.05, 1.1), col=c("blue", "red", "darkgreen", "green"))
legend(x="bottomright", legend=paste("Nj =", c(10, 15, 20, 25)), lwd=2,
col=c("blue", "red", "darkgreen", "green"))
matplot(dVals, powsT, type="l", lty=1, lwd=2, xlab="effect size d",
ylab="Power", main="Power one-sample t-Test", xaxs="i",
xlim=c(-0.05, 1.1), col=c("blue", "red", "darkgreen", "green"))
legend(x="bottomright", legend=paste("N =", c(10, 15, 20, 25)), lwd=2,
col=c("blue", "red", "darkgreen", "green"))
To get a plot of power vs effect size, you do need to fix a sample size. Here is a quick plot for n=40 (note: a line graph would be better, but I stick with your formatting):
pwrt2 <- power.t.test(d=seq(0,3,by=0.1), power=NULL, n=40,
sig.level=.05, type="one.sample", alternative="two.sided")
plot(pwrt2$d, pwrt2$power, type="b", xlab="effect size",ylab="power")
精彩评论