7 Workshop 7: Factorial ANOVA: Different Designs and Assumptions
Aims:
- To practice running and interpreting a factorial ANOVA for a repeated measures design.
- To practice interpreting means for main effects in a 2x2 design.
- To practice evaluating the assumption of normality.
7.1 Exercise 1: Import Data
Before beginning make sure you load the relevant packages.
The data set this week includes data from a study looking at wellbeing scores after exercise. Participants completing a psychological wellbeing measure before and after two different types of exercise: running and swimming. Participants completed all conditions.
Import exercise.csv and inspect the data set, making note of the variable names.
Given this is a repeated measures design, you should double check if the data are in long format…
longd <- mydata %>%
pivot_longer(
cols = starts_with("before") | starts_with("after"), # columns to pivot, anything that starts with "before" or "after"
names_to = c("time", "exercise"), # names of new columns
names_pattern = "(before|after)_(run|swim)", # pattern to separate columns
values_to = "wellbeing") # Name of the value columnOnce you have run this, view() the data file to see what has changed. You should also check the variable names using names()
You also need to ensure any factors are coded as such… As R also puts labels in alphabetical order, it makes sense to place “before” ahead of “after”, so we can use levels = c() to make that switch.
7.2 Exercise 2: Descriptives
Use the code below to generate descriptive statistics.
desc <- longd %>%
group_by(time, exercise) %>%
summarise(m = mean(wellbeing),
sd = sd(wellbeing),
n = n())
view(desc)How about a boxplot too? However, you will need to decide what to place on the x and y axis, and which variable should have a colour fill by replacing NULL below.
ggplot(longd, aes(x = NULL, y = NULL, fill = NULL)) +
geom_boxplot() +
theme_classic()Answer question 2.1 on the worksheet.
7.3 Exercise 3: Run the ANOVA Model
Using aov_ez() run the ANOVA model. You should replace NULL where necessary to match the data set in this workshop.
mod <- aov_ez(id = "NULL",
dv = "NULL",
within = c("NULL", "NULL"),
type = 3,
include_aov = TRUE,
data = NULL)Once you have successfully run the model, an object called mod should now appear in the environment panel. Let’s interpret the model
mod$anova_tableIs the interaction significant? Decide for yourself. If it is, you should use the code from exercises 4-6 from last week’s workshop (workshop 6).
If not, as the two independent variables each only have two levels, you can interpret any main effects by looking at the descriptive statistics and/or plots.
Remember to amend the group_by() line of code in the descriptives to work out the statistics for each independent variable separately (as shown in the lecture).
Use the hint below if needed.
👀 Click for a hint
exercise <- longd %>%
group_by(exercise) %>%
summarise(m = mean(wellbeing),
sd = sd(wellbeing),
n = n())
view(exercise)Now try to amend the code to find the descriptives for the main effect of time.
Answer questions 3.1-3.5 on the worksheet.
7.4 Exercise 4: Check Model Assumptions
The assumption of Sphericity is automatically checked and corrected for (if necessary). However, for a repeated measures design with only two-levels for each independent variable, Sphericity does not apply.
This means for a 2x2 repeated measures design, we will only check normality of residuals.
# testing normality, looking at normal distribution of residuals from the model.
residuals <- residuals(mod) #pulls residuals from the model
qqPlot(residuals) #produces a qq-plot of residuals
hist(residuals, breaks = 20, main = "Histogram of Residuals", xlab = "Residuals", col = "lightblue") #histogram of residuals
shapiro.test(residuals) #Shapiro test for residualsNext week you’ll also check assumptions for a mixed-design, including checking homogeneity of variance.
The next exercises are important to help you continue to develop your understanding of code, research methods, and statistics
7.5 Exercise 5: Data Visualisation
You might notice that many of the plots initially generated by R are not “publication” ready.
The code below is quite long, but have a go at running it and look at the graph it produces.
Keep in mind you will look at data visualisation in a little more detail in a few weeks…
Consider whether a figure like this might be useful for your upcoming lab report.
ggplot(longd, aes(x = time, y = wellbeing, fill = exercise)) +
stat_summary(fun = mean, geom = "bar", position = position_dodge(), color = "black") +
stat_summary(fun.data = mean_sdl, geom = "errorbar",
position = position_dodge(0.9), width = 0.25) +
labs(
title = NULL, # leave this as NULL because APA figures do not use titles.
x = "Time Point",
y = "Mean Wellbeing Score",
fill = "Exercise") +
scale_fill_manual(values = c("run" = "slategray4", "swim" = "slategray2"),
labels = c("Run", "Swim")) +
scale_x_discrete(labels = c("before" = "Before", "after" = "After")) +
# layer 6: add the theme to tidy up
theme_minimal(base_size = 14) +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_rect(color = "black", fill = NA, size = 1),
plot.title = element_text(hjust = 0.5, face = "bold"),
legend.title = element_text(face = "bold"),
axis.title.x = element_text(face = "bold"),
axis.title.y = element_text(face = "bold"))7.6 Exercise 6: Understanding Designs
In the following exercises, the drop down will turn green if correct and red if incorrect.
7.6.1 Look at the code below and answer the questions:
mod <- aov_ez(id = "id",
between = c("group", "handedness"),
dv = "reaction_time",
type = 3,
data = df)Question 1: Based on the code above, what was the design of this study?
Question 2: Based on the code above, what object name was the data set saved as?
7.6.2 Look at the code below and answer the questions.
mod <- aov_ez(id = "id",
between = "group",
within = "time_point",
dv = "anxiety",
type = 3,
data = mydata)Question 3: Based on the code above, what was the design of this study?
Question 4: Based on the code above, what object name was the repeated condition called?
7.7 Exercise 7: “Significant or Non-Significant, That is the question”
Question 5: You see a p-value reported as p < .001.
Significant or non-significant?
Question 6: You see a p-value in RStudio as 4.237e-14
Significant or non-significant?
Question 7: You see a p-value in RStudio as 0.5364
Significant or non-significant?
Question 8: You see a p-value in RStudio as 0.0593
Significant or non-significant?
7.8 Exercise 8: HELP! My Code Won’t Work
A friend is trying to run some code. They want to run a factorial ANOVA and then pull out the estimated marginal means, but it is not working!
Use your expert code skills to identify where the issue is by looking through the code below.
mod <- aov_ez(id = "id",
dv = "score",
between = "treatment",
within = "time",
type = 3,
include_aov = TRUE,
data = mydata)
mod$anova_table
library(emmeans)
emms <- emmeans(mid, ~ treatment*time)
contrasts <- contrast(eemms,
interaction = "pairwise",
simple = "each",
combine = FALSE,
adjust = "holm")Take your time to look through the code and spot two issues and then answer the questions below.
Question 9: Based on the code above, what was the first issue?
Question 10: Based on the code above, what was the second issue?
| Well Done. You have reached the end of the workshop |