Using R for Power Analysis
Presenters: Ding Yu and Jeremy Yagle
Spring 2013
R is a free software downloadable at [Link]
Notes: Code and Output:
1. R Console Setup:
> prompts you for formula or function.
The result appears on the next line(s).
2. Comments begin with #
Anything in the line following a # is a comment. > # This is a comment!
3. Installing a Package # In step 3) you can also
use the command:
To conduct a power or sample size analysis using R the pwr >[Link]("pwr")
package must be installed and loaded.
1) Select “Install Packages” in the dropdown menu “Packages”
at the top of the screen.
2) Select the country, and state that is nearest you.
3) Select the package “pwr” and press “ok”. Step 4) type in
4) Activate the package using the library command. > library(pwr)
All the following functions are also explained in this link.
[Link]
4. the conventional effect size ( Cohen’s Rules of Thumb)
[Link](test = c("p", "t", "r", "anova", "chisq", "f2"), size =
c("small", "medium", "large"))
Arguments:
Test: Choose one statistical test of interest
Size: Choose one effect size: small, medium, or large
5. Determine a medium effect size for a two-samples t-test >[Link](test = c("t"), size = c("medium"))
according to Cohen’s Rules of Thumb
Conventional effect size from Cohen (1982)
Cohen suggests that d values of 0.2, 0.5, and 0.8 represent small, test = t
medium, and large effect sizes respectively. size = medium
[Link] = 0.5
6. Determine a small effect size for multiple regression >[Link](test = c("f2"), size = c("small"))
Conventional effect size from Cohen (1982)
test = f2
size = small
[Link] = 0.02
7. power analysis function for t-tests
[Link](n = , d = , [Link] = , power = ,
type = c("[Link]", "[Link]", "paired"),
alternative = c("[Link]", "less","greater"))
Arguments:
d: Effect size
n: Number of observations in sample
[Link]: Significance level (default= .05)
power: Power of test (usually 0.8)
type: Choose one character string (default= two-sample)
specifying the type of t-test.
alternative: choose one character string (default [Link])
specifying the alternative hypothesis.
8. Find the power for an independent-samples t-test given >[Link](n =30 , d = .5, [Link] =.05)
sample size
Two-sample t test power calculation
For an independent-sample two-sided t-test, with 30 subjects in
each group, and a medium effect size. What is the power? n = 30
d = 0.5
[Link] = 0.05
power = 0.4778965
alternative = [Link]
NOTE: n is number in *each* group
9. Find the required sample size for an independent-samples t- >[Link](n =NULL , d = .2, [Link] =.05, power=.8)
test and a given power
Two-sample t test power calculation
For an independent-sample two-sided t-test, 80% power, and a
small effect size. What is the sample size? n = 393.4057
d = 0.2
[Link] = 0.05
power = 0.8
alternative = [Link]
NOTE: n is number in *each* group
10. Displaying the sample size (n) only [Link](n =NULL , d = .2, [Link] =.05, power=.8)$n
Add “$n” to the end of the [Link] command. [1] 393.4057
11. t-test (two samples with unequal n) example
>[Link](n1 =20 , n2=80 , d =.5 , [Link] = 0.05,
Function: [Link] power =NULL ,alternative = c("[Link]",
"less","greater"))
Example. Find the power for an independent-samples t-test at t test power calculation
the 5% level of significance, medium effect size, with sample sizes
sizes of 20 and 80. n1 = 20
n2 = 80
d = 0.5
[Link] = 0.05
power = 0.5081857
alternative = [Link]
12. Balanced one way ANOVA
Function: [Link]
Arguments:
k: Number of groups
n: Number of observations per group
f: Effect size(Cohen suggests that f values of 0.1, 0.25, and 0.4
represent small, medium, and large effect sizes respectively).
[Link]: Significance level
power: Power of test
13. Balanced one way ANOVA Example > [Link](k =3 , n =NULL , f =.25, [Link] =.05 ,
power =.8)
Example. Find the sample size for a one way ANOVA test, with 3
groups, a medium effect size, 80% power, and at the 5% level of Balanced one-way analysis of variance power calculation
significance.
k=3
n = 52.3966
f = 0.25
[Link] = 0.05
power = 0.8
So n= 53 per group (159 total)
NOTE: n is number in each group
14. Chi Square
Function: [Link]
Arguments:
w: Effect size
N: Total number of observations
df: Degrees of freedom.
Usually (number of rows-1)(number of columns -1)
[Link]: Significance level
power: Power of test
15. Chi Square Example > [Link](w =.3 , N =300 , df =6 , [Link] = 0.05,
Example. Find the power for a Chi Square test for independence power =NULL )
for two categorical variables (one with three levels and one with (Cohen suggests that w values of 0.1, 0.3, and 0.5
four levels), with 300 observation, a medium effect size, and at represent small, medium, and large effect sizes
the 5% level of significance. respectively).
4 levels (columns) Chi squared power calculation
3 levels(rows)
w = 0.3
Age N = 300
Education 20-29 30-39 40-49 50 and older df = 6
G.E.D [Link] = 0.05
A.A power = 0.9872113
B.S
NOTE: N is the number of observations
Note: df = (number of rows-1)(number of columns -1)
= (3-1) (4-1) = 2*3 = 6
16. Multiple Regression
Function: [Link]
Arguments:
u: Numerator degrees of freedom (the number of continuous
predictors plus the number of dummy variables minus one)
v: Denominator (error) degrees of freedom
f2: Effect Size
(Cohen suggests f2 values of 0.02, 0.15, and 0.35 represent small,
medium, and large effect sizes).
[Link]: Significance level
power: Power of test
Notes: every categorical variable with k levels has k-1 dummy
variables because one level serves as the reference category
The sample size(n) is the sum of u and v plus one.
17. Multiple Regression Example >[Link](u = 4, v = NULL, f2 = .15, [Link] = 0.05,
power = 0.8)
Example
Find sample size u=4 Multiple regression power calculation
for multiple regression test i.e.
with two continuous predictor,
* GRE score u=4
* Blood pressure, 2 continuous variables v = 79.44992
and two categorical predictors, f2 = 0.15
Sex (Male or Female), + (2-1) dummy variables [Link] = 0.05
Marital Status (married, single, or other), + (3-1) dummy variables power = 0.8
with a medium effect size, —1
80% power,
4
and at the 5% level of significance.
Note: total sample size = N = 80+4+1=85
18. Example Generating a Table of sample sizes >seq=c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8)
>FindN=array(0,8)
>for (i in 1:8) FindN[i]=[Link](d=seq[i],power=.8,
Example: [Link]=.05, type="[Link]",
Suppose that you are determining the required sample size for a alternative="greater")$n
two-sided upper-tailed independent samples t-test with 80%
power. >[Link](d=seq ,N=ceiling(FindN))
Generate a table showing the required sample size for each of the d N
following 8 effect sizes: 1 0.1 1238
d = 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, and 0.8. 2 0.2 310
3 0.3 139
4 0.4 78
5 0.5 51
6 0.6 36
7 0.7 26
8 0.8 21
19. Generating Power Curve # Plot sample size curves for detecting correlations of
# various sizes.
library(pwr)
Example:
Suppose that you are determining the required sample size for a # range of effect sizes
two-sided upper-tailed independent samples t-test with 80% d <- seq(.1,.8,.01)
power. nd <- length(d)
Generate power curves showing the required sample size for each # power values
of the following 8 effect sizes: p <- seq(.6,.8,.1)
d = 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, and 0.8. np <- length(p)
# obtain sample sizes
On the graph output: samsize <- array(numeric(nd*np), dim=c(nd,np))
for (i in 1:np){
x axis shows the range of effect sizes for (j in 1:nd){
result <- [Link](n = NULL, d = d[j],
y axis shows required samples sizes [Link] = .05, power = p[i],
alternative = "[Link]")
Three power curves are generated (p=.6, .7, or .8). You can find samsize[j,i] <- ceiling(result$n)
the required sample size on the y axis respective to the desired }
effect size and power. }
# set up graph
xrange <- range(d)
yrange <- round(range(samsize))
colors <- rainbow(length(p))
plot(xrange, yrange, type="n",
xlab="Effect sizes (d)",
ylab="Sample Size (n)", ylim=c(0,1600) )
# add power curves
for (i in 1:np){
lines(d, samsize[,i], type="l", lwd=2, col=colors[i])
}
# add annotation (grid lines, title, legend)
abline(v=0, h=seq(0,yrange[2],50), lty=2, col="grey89")
abline(h=0, v=seq(xrange[1],xrange[2],.02), lty=2,
col="grey89")
title("Sample Size Estimation for t-test Studies\n
Sig=0.05 (Two-tailed)")
legend("topright", title="Power", [Link](p),
fill=colors)
Sample Size Estimation for t-test Studies
Sig=0.05 (Two-tailed)
Power
1500
0.6
0.7
0.8
1000
Sample Size (n)
500
0
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8
Effect sizes (d)