Rename this template, fill out the author and date lines, and knit it once before you start. Submit both the knitted file and the .Rmd on Canvas. Use α = .05 and two-tailed tests unless a question says otherwise.
# 1a: Calculate the test statistic and degrees of freedom
t <- (106 - 100) / (12 / sqrt(9))
df <- 9 - 1
t
## [1] 1.5
df
## [1] 8
t_crit <- qt(0.975, df = 8)
t_crit
## [1] 2.306004
abs(t) > t_crit
## [1] FALSE
Your answer:
Your answer: ## (i) I would use a paired samples t-test for this, because the same participants are completing both conditions. ## (ii) I would use an independent samples t-test, because two separate groups of students are being compared. ## (iii) I would use a one-sample t-test, because the sample mean is compared to a known population mean of 7 hours.
The built-in ToothGrowth data has tooth length (len) for 60 guinea pigs that got vitamin C either as orange juice (supp = “OJ”) or as ascorbic acid (supp = “VC”), 30 in each group. Does tooth length differ between the two supplements?
OJ <- ToothGrowth$len[ToothGrowth$supp == "OJ"]
VC <- ToothGrowth$len[ToothGrowth$supp == "VC"]
mean(OJ)
## [1] 20.66333
sd(OJ)
## [1] 6.605561
length(OJ)
## [1] 30
mean(VC)
## [1] 16.96333
sd(VC)
## [1] 8.266029
length(VC)
## [1] 30
n1 <- length(OJ)
n2 <- length(VC)
s1 <- sd(OJ)
s2 <- sd(VC)
sp <- sqrt(((n1 - 1) * s1^2 + (n2 - 1) * s2^2) / (n1 + n2 - 2))
t <- (mean(OJ) - mean(VC)) / (sp * sqrt(1/n1 + 1/n2))
df <- n1 + n2 - 2
sp
## [1] 7.482001
t
## [1] 1.915268
df
## [1] 58
t_crit <- qt(0.975, df = df)
t_crit
## [1] 2.001717
abs(t) > t_crit
## [1] FALSE
Your answer: ## The critical t is 2.001717. We fail to reject H0, because there is a statistically significant difference in tooth length between the orange juice and ascorbic acid groups. t = 1.915 is smaller than the critical value (approximately 2.002), and p = .061 is greater than .05. The orange juice group has a higher mean tooth length.
t.test(len ~ supp, data = ToothGrowth, var.equal = TRUE)
##
## Two Sample t-test
##
## data: len by supp
## t = 1.9153, df = 58, p-value = 0.06039
## alternative hypothesis: true difference in means between group OJ and group VC is not equal to 0
## 95 percent confidence interval:
## -0.1670064 7.5670064
## sample estimates:
## mean in group OJ mean in group VC
## 20.66333 16.96333
Your answer: ## The independent samples t-test results in approximately t=1/915, df=58, and p=0.0604, with no statistically significant difference in tooth length between the orange juice and ascorbic acid groups. The 95% confidence interval includes 0, and this is consistent with failing to reject the H0.
Twelve students rated their stress (0–40) before and after a 4-week mindfulness program. Run the chunk below to create the data.
stress <- data.frame(
before = c(14, 16, 16, 22, 10, 23, 18, 29, 30, 32, 29, 24),
after = c( 8, 8, 15, 17, 7, 16, 18, 27, 26, 28, 31, 20)
)
Your answer: ## A paired samples t-test is the right choice here, because the same 12 students were measured before and after the mindfulness program. The observations are dependent.
stress$diff <- stress$after - stress$before
mean_diff <- mean(stress$diff)
sd_diff <- sd(stress$diff)
n <- length(stress$diff)
t <- mean_diff / (sd_diff / sqrt(n))
df <- n - 1
mean_diff
## [1] -3.5
sd_diff
## [1] 2.90767
t
## [1] -4.169784
df
## [1] 11
t.test(stress$after, stress$before, paired = TRUE)
##
## Paired t-test
##
## data: stress$after and stress$before
## t = -4.1698, df = 11, p-value = 0.001563
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
## -5.347445 -1.652555
## sample estimates:
## mean difference
## -3.5
Your answer: ## The test results in t = -4.170, df = 11, and p=0.00156. We reject H0. There is a statistically significant difference in stress scores before and after the mindfulness program. The stress scores decreased after the program.
t.test(stress$diff, mu = 0)
##
## One Sample t-test
##
## data: stress$diff
## t = -4.1698, df = 11, p-value = 0.001563
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
## -5.347445 -1.652555
## sample estimates:
## mean of x
## -3.5
Your answer: ## The results are identical to those in 3c because a paired samples t-test calculates the differences between each participant’s two scores and tests whether the mean difference is significantly different from 0.
d2 <- (mean(OJ) - mean(VC)) / sp
d3 <- mean(stress$diff) / sd(stress$diff)
d2
## [1] 0.4945201
d3
## [1] -1.203713
Your answer: ## The Cohen’s d for the first test is 0.4945201 (a medium effect) and the Cohen’s d for the second test is -1.203713 (a large effect).
Your answer: ## Question 2: An independent samples t-test resulted in no statistically significant difference in tooth length between the orange juice and ascorbic acid groups, t(58) = 1.92, p = 0.061, d = 0.50.
t.test(stress$after, stress$before, var.equal = TRUE)
##
## Two Sample t-test
##
## data: stress$after and stress$before
## t = -1.1094, df = 22, p-value = 0.2792
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -10.042826 3.042826
## sample estimates:
## mean of x mean of y
## 18.41667 21.91667
Your answer: ## The independent samples t-test results are t= -1.109, df = 22, p = 0.279. Unlike the paired samples test, this result is NOT statistically significant. The paired test takes into account the relationship between each participant’s before/after scores. This reduces variability and makes it eaiser to identify the change in stress.