# Power for a t-test is calculated with the conveniently named
# function 'power.t.test'
#
# We'll go through the example in the power tutorial comparing the heights of the
# women in our class to 64 inches.
# Load in the survey data:
rm(list = ls())
survey <-read.csv("http://www.courses.washington.edu/psy315/datasets/Psych315W19survey.csv")
# Get the heights of the women:
x <- survey$height[survey$gender=="Female"]
# Get rid of any NA's
x = x[!is.na(x)]
# Calculate the effect size (d), and sample size (n), comparing the mean to 64 inches.
d <- (mean(x)-64)/sd(x)
n <- length(x)
# set alpha
alpha <- .01
# We're ready to calculate power. Power depends on:
# 1) sample size (n)
# 2) effect size(d)
# 3) alpha
# Plus
# 4) one or two sided
# 5) one or two means (we've only worked with one mean so far)
# If you know #s 1 2 and 3, then you can find power. Or, if you know #2 and #3
# and power, then you can find #1.
# power.t.test works by letting you set 3 of these four things to numbers and
# the fourth to 'NULL'. Whatever you set to 'NULL' will be the thing that is
# calculated.
# Example 1: use power.t.test to determine the observed power based on the
# sample size,effect size, and alpha. We'll set power= NULL so it'll calculate
# the power:
out1 <- power.t.test(n = n,
d = d,
power = NULL,
sig.level =alpha,
alternative = "two.sided",
type ="one.sample" )
# This is our observed power:
out1$power
# Example 2: use power.t.test to find the sample size given our effect size,
# alpha and a desired power of 0.8. We'll set n = NULL so it'll calculate n:
out2 <- power.t.test(n = NULL,
d = d,
power = .8,
sig.level =0.01,
alternative = "two.sided",
type ="one.sample" )
# This is the desired sample size:
out2$n