3 Workshop 3: t-tests
Aims:
Practice running and interpreting a two-sample t-test in RStudio.
Practice running and interpreting a paired t-test in RStudio.
Part One
Part one of today’s workshop will involved running a two-sample t-test, which is appropriate for an independent measures design with two groups.
You should use the same data file from last week, as you will be aiming to investigate whether Stroop interference scores (msecs) significantly differ between the Red Cow group and the control group.
You must have completed the week 2 workshop before starting this one.
3.1 Exercise 1: Import the Data
Import the Data File: stroop.csv
Before you begin, you will need the following packages:
install.packages("tidyverse") #install if needed.
install.packages("rstatix") #install if needed.
install.packages("car") #install if needed.
library(tidyverse) #load package.
library(rstatix) #load package.
library(car) #load package.Then import the data file. Make sure it has the int score that you calculated last week. If not, you will need to go back and complete workshop 2.
mydata <- read_csv("stroop.csv")3.2 Exercise 2: Inspect and Check Your Data
Take a look at your newly imported data file
Check the top right panel (the environment) and also use the code below to inspect your data set.
view(mydata) # this will open the data in a new tab.
names(mydata) # this will show the variable names.It is really important to look at the variable names as you’ll be using them in code later on.
3.3 Exercise 3: Check Assumptions
3.3.1 Check Heteroscedasticity with Levene’s Test
We do not need to do this because we will just use a Welch’s t-test instead.
If you need to justify this decision (e.g., for a 3rd Year Project), the following papers might help:
For a gentler explanation, see also this blog post from Daniel Lakens.
3.3.2 Check Normality with Shapiro-Wilk Test and Histograms
We need to check normality for both groups separately. We can filter the groups:
#first create a data set that contains the control group only
control <- mydata %>%
filter(drink == "control")
#then run the test
shapiro.test(control$int)
#then create a data set that contains Red Cow drinkers only
redcow <- mydata %>%
filter(drink == "redcow")
#then run the test
shapiro.test(redcow$int)We use a dollar sign $ to point R to a particular column. For example, when using shapiro.test(redcow$int) you are saying to run the Shapiro Test on the redcow only data set and the specific column called int
Again, we want the p-values to be not significant. A non-significant p-value means the data are roughly normally distributed. If the p-value is significant, this could be an issue as it indicates the data are not normally distributed.
When reporting the Shapiro-Wilk test, you just need to report the test statistics (w = XX) and the p-value. Here is an example what it could look like:
W = 0.98, p = .875
You can also visually check the data with a quick histogram:
Check the plots panel, and use the blue arrow to switch between the two plots.
3.4 Exercise 4: Run the Two-Sample t-Test and ask for Cohen’s d
Run the t-test.
t.test(int ~ drink, data = mydata, var.equal = FALSE, alternative = "two.sided")Ask for Cohen’s d:
cohens_d(data = mydata, int ~ drink, var.equal = FALSE)Interpret your t-test.
Is the test significant?
What is the effect size?
If significant, how do the groups differ (e.g., use descriptive statistics to interpret the difference)?
Hint: re-use the code from last week to find the mean and standard deviation for the two groups.
Answer questions 4.1-4.3 on the worksheet.
3.5 Part 2 - Exercise 5: Import the Data
Part Two Study
Part two of today’s workshop will involved running a paired t-test, which is appropriate for a repeated measures design with two conditions.
Let’s switch it up a bit and use a different scenario and experiment with a new data set.
Here we will look at how someone’s resting heart rate might change as a result of drinking a can of Red Cow.
In this study the resting heart rate in beats per minute (BPM) was measured in a group of participants both before and after consuming a can of Red Cow.
The independent variable is time point: before, after. The dependent variable is BPM.
Import the Data
We will call the object dat (short name for data)
dat <- read_csv("bpm.csv")3.6 Exercise 6: Inspect and Check Your Data
Take a look at your newly imported data file
Check the top right panel (the environment) and also use the code below to inspect your data set.
3.7 Exercise 7: Check Assumptions
We do need to check normality, as heteroscedasiticty does not apply to repeated measures designs. However, we need to ensure the difference score is normally distributed.
diff <- dat$after - dat$before #this will calculate the difference score.
shapiro.test(diff) # run the Shapiro test
hist(diff) # also visually inspect dataYou should interpret and report this in the same way as earlier (exercise 3).
Answer questions 7.1-7.2 on the worksheet.
3.8 Exercise 8: Run the Paired t-Test and ask for Cohen’s d
A paired t-test is a little different compared to the two-sample t-test.
t.test(NULL1, NULL2, paired = TRUE)Change
NULL1to the column with the first condition.Change
NULL2to the column with the second condition.
(Hint: you will need to use the dollar sign $ to specify which data set and column, e.g., dat$NULL1 and dat$NULL2).
Try yourself first, but if you need, check the solution below.
👀 Click for a hint
t.test(dat$before, dat$after, paired = TRUE)Annoyingly, we need to use a different package for Cohen’s d for a paired t-test.
install.packages("effectsize") #install if needed.
library(effectsize)
effectsize::cohens_d(dat$before, dat$after, paired = TRUE)Answer questions 8.1-8.2 on the worksheet. Hint: you will also need the descriptive statistics (see exercise 9 below).
3.9 Exercise 9: Calculate Descriptive Statistics
The final thing to do is to convert the data from wide format to long format. Often with repeated measures when asking for descriptive statistics or plots, we need the data in long format.
longd <- dat %>%
pivot_longer(
before:after,
names_to = "time",
values_to = "bpm"
)Code explanation:
Create a new object called
longdwhere we will store the long data.Base it on the original data set called
datand then%>%The first argument should include the columns which contain your dependent variable. In this case we will use
before:afterwhich will then take all and any columns frombeforethrough toafter(these are the only columns so it will just take the two of them).Next we use
names_to =to tell R what we want to call our independent variable. I have used “time” as it was time point for this study.Then we use
values_to =to tell R what we want to call our dependent variable. We measure beats per minute, so I have just called this “bpm”.Pay careful attention to the lay out of this code. For example, notice where brackets open and close, and the placement of commas to move on the the next line.
Once you have created the new long data set called longd we can use it to calculate descriptive statistics. But first check it to see the difference.
Now adapt the code below to ask for the mean and standard deviation.
Change NULL1 to the name of the independent variable in the long data file, and NULL2 to the dependent variable. If this is tricky, use names(longd) if you need a reminder of the variable names and revisit your notes from last week’s workshop.
Make note of the mean and standard deviation for the before and after conditions.
Try yourself first, but if you need, check the solution below.
👀 Click for a hint
Well Done. You have reached the end of the workshop.