# The following the example is the t-test for dependent means, where we compared
# GPA's from high school to GPA's from UW
# Load in the survey data
survey <-read.csv("http://www.courses.washington.edu/psy315/datasets/Psych315W21survey.csv")
# First find the UW GPA's for the male students
x <- survey$GPA_UW[survey$gender == "Male"]
# Then find the high school GPA's for the male students
y <- survey$GPA_HS[survey$gender == "Male"]
# Remove the pairs that have a NA in either x or y:
goodvals = !is.na(x) & !is.na(y)
x <- x[goodvals]
y <- y[goodvals]
# run the t-test. Use 'paired = TRUE' because x and y are dependent
out <- t.test(x,y,
paired = TRUE,
alternative = "two.sided",
var.equal = TRUE)
# The p-pvalue is:
out$p.value
# Displaying the result in APA format:
sprintf('t(%g) = %4.2f, p = %5.4f',out$parameter,out$statistic,out$p.value)
mx <- mean(x)
my <- mean(y)
s = sd(x-y)
n <- length(x)
#effect size
d <- abs(mx-my)/s
d
# Find observed power from d, alpha and n
out <- power.t.test(n =n,
d = d,
sig.level = .05,
power = NULL,
alternative = "two.sided",
type = "one.sample")
out$power
# Example 2: Is there a significant difference between male student's heights and their
# father's heights?
# First find the heights of the male students
x <- survey$height[survey$gender == "Male"]
# Then find the heights of their fathers
y <- survey$pheight[survey$gender == "Male"]
# Remove the pairs that have a NA in either x or y:
goodvals = !is.na(x) & !is.na(y)
x <- x[goodvals]
y <- y[goodvals]
# run the t-test. Use 'paired = TRUE' because x and y are dependent
out <- t.test(x,y,
paired = TRUE,
alternative = "two.sided",
var.equal = TRUE)
# The p-pvalue is:
out$p.value
# Displaying the result in APA format:
sprintf('t(%g) = %4.2f, p = %5.4f',out$parameter,out$statistic,out$p.value)