# Using power.anova.test to calculate power or sample size in the one-way ANOVA # Suppose r=4, alpha = 0.05, and the required power for the F-test is 0.80. # Suppose we wish to detect when the four treatment means differ by at least 10 (max - min = 10). # And we believe the standard deviation is 5. groupmeans <- c(10, 15, 15, 20) power.anova.test(groups = length(groupmeans), between.var=var(groupmeans), within.var=5^2, sig.level= 0.05, power=.80) # How does the power/sample size change if the treatment population means are more evenly spead out? groupmeans <- c(10, 13.3, 16.7, 20) power.anova.test(groups = length(groupmeans), between.var=var(groupmeans), within.var=5^2, sig.level= 0.05, power=.80) # We can also specify the sample size we have and determine the power of the F-test we will get. groupmeans <- c(10, 15, 15, 20) power.anova.test(groups = length(groupmeans), between.var=var(groupmeans), within.var=5^2, sig.level= 0.05, n=10) groupmeans <- c(10, 13.3, 16.7, 20) power.anova.test(groups = length(groupmeans), between.var=var(groupmeans), within.var=5^2, sig.level= 0.05, n=10) # We can see the relationship between power and sample size by plotting the power for a variety of sample sizes: n.vec <- seq(2,20,by=1) my.power <- rep(0, times = length(n.vec)) groupmeans <- c(10, 15, 15, 20) for (i in 1:length(n.vec)){ my.power[i]<-power.anova.test(groups = length(groupmeans), between.var=var(groupmeans), within.var=5^2, sig.level= 0.05, n=n.vec[i])$power } plot(n.vec, my.power, type='b',xlab='sample size per group', ylab='Power of F-test')