knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
Simple experiments:
For example: manipulation of the independent variable involves having an experimental condition and a control.
People used to split variables into high versus low or simply split down the middle.
Reminder:
Between subjects / Independent designs
Repeated measures / within subjects / dependent designs
Independent t-test:
Dependent t-test:
Manipulation
Outcome measured how many mischievous acts participants performed in a week.
library(rio) longdata <- import("data/invisible.csv") str(longdata)
M <- tapply(longdata$Mischief, longdata$Cloak, mean) STDEV <- tapply(longdata$Mischief, longdata$Cloak, sd) N <- tapply(longdata$Mischief, longdata$Cloak, length) M;STDEV;N
Our means appear slightly different. What might have caused those differences?
knitr::include_graphics("pictures/ttests/1.png")
We use the standard error as a gauge of the variability between sample means. - If the difference between the samples we have collected is larger than what we would expect based on the standard error then we can assume one of two interpretations:
$$ t = \frac {\bar{X}_1 - \bar{X}_2}{\sqrt {\frac {s^2_p}{n_1} + \frac {s^2_p}{n_2}}}$$
$$ s^2_p = \frac {(n_1-1)s^2_1 + (n_2-1)s^2_2} {n_1+n_2-2}$$
Assumptions:
t.test(Mischief ~ Cloak, data = longdata, var.equal = TRUE, #assume equal variances paired = FALSE) #independent
var.equal = FALSE
to use this adjustment. t.test(Mischief ~ Cloak, data = longdata, var.equal = FALSE, #assume equal variances paired = FALSE) #independent
Effect size options:
library(MOTE) effect <- d.ind.t(m1 = M[1], m2 = M[2], sd1 = STDEV[1], sd2 = STDEV[2], n1 = N[1], n2 = N[2], a = .05) effect$d
library(pwr) pwr.t.test(n = NULL, #leave NULL d = effect$d, #effect size sig.level = .05, #alpha power = .80, #power type = "two.sample", #independent alternative = "two.sided") #two tailed test
Manipulation
Outcome: We measured how many mischievous acts participants performed in week 1 and week 2.
Note: Same data, but instead the study is dependent. Let's see what happens to our t-test. You would not change the analysis this way, but this example shows how the type of experiment and statistical test can affect power and results.
$$t = \frac {\bar{D} - \mu_D}{S_D/\sqrt N}$$
t.test(Mischief ~ Cloak, data = longdata, var.equal = TRUE, #ignored in dependent t paired = TRUE) #dependent t
Cohen's d: Based on averages
Cohen's d: Based on differences $d_z$
effect2 <- d.dep.t.avg(m1 = M[1], m2 = M[2], sd1 = STDEV[1], sd2 = STDEV[2], n = N[1], a = .05) effect2$d #remember independent t effect$d
You do not normally have to calculate both, just showing how these are different.
Create difference scores, calculate the difference score measures.
diff <- longdata$Mischief[longdata$Cloak == "Cloak"] - longdata$Mischief[longdata$Cloak == "No Cloak"] effect2.1 = d.dep.t.diff(mdiff = mean(diff, na.rm = T), sddiff = sd(diff, na.rm = T), n = length(diff), a = .05) effect2.1$d
pwr.t.test(n = NULL, d = effect2$d, sig.level = .05, power = .80, type = "paired", alternative = "two.sided")
library(ggplot2) cleanup <- theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.background = element_blank(), axis.line.x = element_line(color = "black"), axis.line.y = element_line(color = "black"), legend.key = element_rect(fill = "white"), text = element_text(size = 15)) bargraph <- ggplot(longdata, aes(Cloak, Mischief)) bargraph + cleanup + stat_summary(fun.y = mean, geom = "bar", fill = "White", color = "Black") + stat_summary(fun.data = mean_cl_normal, geom = "errorbar", width = .2, position = "dodge") + xlab("Invisible Cloak Group") + ylab("Average Mischief Acts")
In this lecture, you've learned:
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.