5 Workshop 5: One-Way ANOVA (Post-hocs, Contrasts, Write-up)

Aims:

  • Practice using a post-hoc test to interpret an ANOVA
  • Practice using a planned contrast to interpret an ANOVA result.
  • Practice writing-up the results from an ANOVA, using APA style.

Part One

5.1 Exercise 1: Import Data and Run Model

Last week you ran a one-way repeated measures ANOVA looking at memory assessment score over time. This week you will need the same dataset and the code (below) to run the initial model.

Remember - you already checked the assumptions and the ANOVA model was significant, in which case you will now practice using a planned contrast.

To save time, use the code below to load necessary packages, import the data, and run the model. Try running ALL of this code at once to save time (highlight it all and run it). Make sure you working directory is set up correctly first to find the mci.csv file.

install.packages("afex") # install if needed
install.packages("car") #install if needed
library(tidyverse)
library(afex)
library(car)

dat <- read_csv("mci.csv")
mydata <- dat %>%

pivot_longer(cols = year1:year4,
               names_to = "time",
               values_to = "memory")
               
mod <- aov_ez(id = "id",
              dv = "memory",
              within = "time",
              type = 3,
              include_aov = TRUE,
              data = mydata)
              
mod$anova_table

5.2 Exercise 2: Estimated Marginal Means (EMMs)

The code above should import the data, switch it to long format, and run the model.

As the model was significant, we need to pull out the estimated marginal means.

We need a specific package for this:

install.packages("emmeans") # install if needed.
library(emmeans)

Next, pull out the EMMs and save them as an object called emms.

emms <- emmeans(mod, ~ time)

Now let’s say that the alternate hypothesis for this study was that there would be a linear decline in memory scores over time, we could use a polynomial contrast. This will look for a trend. Polynomial contrasts are only suitable for an independent variable which could be considered “continuous” in nature, e.g., time point.

Use the code below to run the contrast.

poly <- contrast(emms, method = "poly")

print(poly) # print the results to the console

Look through the output. Remember, you should look for p-values that are less than .05 (p < .05).

Is there a significant linear, quadratic, or cubic trend?

Additionally, use the code below to visualise this.

#let's visualise the data to help our interpretation
ggplot(mydata, aes(x = time, y = memory, fill = time)) +
  stat_summary(fun = mean, geom = "line", color = "black", size = 1, aes(group = 1)) + 
  theme_classic() +
  labs(title = "Plot of Memory Score Across Time",  # add a title
       x = "Time Point",  # X-axis label
       y = "Memory Score")  # Y-axis label

5.3 Part 2 - Exercise 3: Import the Data

Part Two

Part two of this workshop will use the same dataset and ANOVA model from part two of workshop 4 (last week).

Ensure you have the correct data file loaded.

The file is called alert.csv. Import the data file and save it as an object called mydata1 (as to not overwrite anything from Part 1 in case you want to look back).

5.4 Exercise 4: Run the ANOVA Model

Next, run the ANOVA model again and save it as an object.

We will save the model as an object called mod1.

mod1 <- aov_ez(id = "id",
              dv = "score",
              between = "group",
              type = 3,
              include_aov = TRUE,
              data = mydata1)
              
mod1$anova_table

Last week you would have seen that the model was significant (p-value was less than .05). This means we reject the null hypothesis and at least one of the groups are signficiantly different.

5.5 Exercise 5: Post-hoc Pairwise Comparisons

To run post-hoc tests, you first need to pull out the estimated marginal means using emmeans() and apply the necessary p-value adjustment.

For this exercise, use the Holm adjustment.

emmeans(mod1,
        pairwise ~ group,
        adjust = "holm")

Look at the output and interpret it. Which conditions differ from one another? Remember to look for p-values that are less than .05.

Also look for effect sizes, so you can report them alongside the rest of your analysis.

library(rstatix)
cohens_d(mydata1, score ~ group)

Use a box plot to also help your interpretation:

#how about a box plot too

ggplot(mydata1, aes(x = group, y = score, fill = group)) +
  geom_boxplot() +
  theme_classic()

5.6 Exercise 6: Introducting the Violin Plot

Previously, you have looked at density plots and box plots. Ever wondered if there was a handy way to look at both the distributions and the box plot at the same time? No, probably not. Well I will show you anyway.

Use this code to generate a violin plot:

ggplot(mydata1, aes(x = group, y = score, fill = group)) +
  geom_violin(trim = FALSE) +  # violin plot without trimming tails
  geom_boxplot(width = 0.1, color = "black", alpha = 0.5) +  # overlay a boxplot for additional summary statistics
  theme_classic() +  # Classic theme
  labs(title = "Violin Plot of Alertness Score by Group",  # add a title
       x = "Group",  # x-axis label
       y = "Altertness Score")  # y-axis label

Mow answer the questions on the Workshop Question Sheet

Well Done. You have reached the end of the workshop