Created
August 18, 2015 20:19
-
-
Save wetherc/e2cca4161becd1844a89 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######################################## | |
######################################## | |
# | |
# Conducting t-tests in R | |
# | |
######################################## | |
######################################## | |
######################################## | |
# One-sample t-tests | |
######################################## | |
set.seed(0) | |
# Say we're looking at tree volumes | |
# in a lumber shipment | |
treeVolume <- c(rnorm(75, mean = 36500, sd = 2000)) | |
# one-sample t-test for equality of means | |
t.test(treeVolume, mu = 39000) # Ho: mu = 39000 | |
# Visualize as a histogram | |
hist(treeVolume, freq = TRUE, density = 150, col = "dodgerblue4", | |
border = "black", breaks = 15, | |
main = "Histogram of Douglas Fir Volumes", | |
xlab = "Volume (cubic feet)", xlim = c(30000, 44000), | |
ylim = c(0, 225)) | |
segments(39000, 0, 39000, 225, lwd = 3, | |
lty = 2) | |
text(41000, 190, "Population mean") | |
text(41000, 175, "volume") | |
segments(mean(treeVolume), 0, mean(treeVolume), 225, | |
lwd = 3, lty = 2, col = "gray70") | |
text(33500, 190, "Sample mean volume", col = "gray35") | |
######################################## | |
# Paired-samples t-test | |
######################################## | |
set.seed(2820) | |
# Say we're looking at pre- and | |
# post-treatment systolic blood | |
# pressures for people taking | |
# a medication to manage | |
# hypertension | |
preTreat <- c(rnorm(1000, mean = 145, sd = 9)) | |
postTreat <- c(rnorm(1000, mean = 138, sd = 8)) | |
# Visualize the pre- and post-treat | |
# differences in distributions | |
x <- seq(from = 110, to = 174, by = 0.5) | |
y1 <- dnorm(x, mean = 145, sd = 9) | |
y2 <- dnorm(x, mean = 138, sd = 8) | |
plot(x, y1, type="l", lwd=2, col="red", | |
main="Systolic Blood Pressure Before and After Treatment", | |
xlab = "Systolic Blood Pressure (mmHg)", | |
ylab = "Frequency", yaxt="n", | |
xlim = c(110, 175), ylim = c(0, 0.05)) | |
lines(x, y2) | |
polygon(c(110,x,175),c(0,y2,0), col="firebrick3", density = 100, | |
border = "black") | |
polygon(c(117,x,175),c(0,y1,0), col="dodgerblue4", density = 100, | |
border = "black") | |
ylab=c(seq(from=0, to=175, by=25)) | |
y=c(seq(from=0, to=0.05, length.out = 8)) | |
axis(2,at=y,labels=ylab) | |
text(x = 120, y = 0.045, "- Pre-Treatment BP", col = "dodgerblue4", cex = 0.9) | |
text(x = 120, y = 0.04, " - Post-Treatment BP", col = "firebrick3", cex = 0.9) | |
points(109, 0.0445, pch = 15, col = "dodgerblue4") | |
points(109, 0.0395, pch = 15, col = "firebrick3") | |
# Conduct a paired-samples t-test | |
t.test(preTreat, postTreat, paired = TRUE) | |
######################################## | |
# Independent samples t-test | |
######################################## | |
# Independent 2-group t-test | |
# where y1 and y2 are numeric | |
set.seed(0) | |
ClevelandSpending <- rnorm(50, mean = 250, sd = 75) | |
NYSpending <- rnorm(50, mean = 300, sd = 80) | |
t.test(ClevelandSpending, NYSpending, var.equal=TRUE) | |
# Independent 2-group t-test | |
# where y1 is numeric and y2 is binary | |
spending <- c(ClevelandSpending, NYSpending) | |
city <- c(rep("Cleveland", 50), rep("New York", 50)) | |
t.test(spending~city, var.equal=TRUE) | |
# Independent 2-group t-test | |
# Equal variances not assumed | |
t.test(ClevelandSpending, NYSpending, var.equal=FALSE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment