Statistical Inference

3-way Anova

MiƂosz Poruba

Published on ƛroda 24 styczeƄ 2024

3-Way ANOVA

The three-way ANOVA is an extension of the two-way ANOVA for assessing whether there is an interaction effect between three independent categorical variables on a continuous outcome variable.

We’ll use the headache dataset [datarium package], which contains the measures of migraine headache episode pain score in 72 participants treated with three different treatments. The participants include 36 males and 36 females. Males and females were further subdivided into whether they were at low or high risk of migraine.

We want to understand how each independent variable (type of treatments, risk of migraine and gender) interact to predict the pain score.

Descriptive statistics

summary(headache)
##        id          gender     risk    treatment   pain_score   
##  Min.   : 1.0   male  :36   high:36   X:24      Min.   : 63.7  
##  1st Qu.:18.8   female:36   low :36   Y:24      1st Qu.: 72.9  
##  Median :36.5                         Z:24      Median : 77.9  
##  Mean   :36.5                                   Mean   : 77.6  
##  3rd Qu.:54.2                                   3rd Qu.: 81.5  
##  Max.   :72.0                                   Max.   :100.0
stats <- headache %>%
  group_by(gender,risk,treatment) %>%
  summarise(
    Mean_Pain_Score = mean(pain_score),
    SD_Pain_Score = sd(pain_score),
    Count = n()
  )
## `summarise()` has grouped output by 'gender', 'risk'. You can override using
## the `.groups` argument.
stats %>%
  kbl() %>%
  kable_material_dark()
gender risk treatment Mean_Pain_Score SD_Pain_Score Count
male high X 92.7 5.12 6
male high Y 82.3 5.00 6
male high Z 79.7 4.05 6
male low X 76.1 3.85 6
male low Y 73.1 4.76 6
male low Z 74.5 4.89 6
female high X 78.9 5.32 6
female high Y 81.2 4.62 6
female high Z 81.0 3.98 6
female low X 74.2 3.69 6
female low Y 68.4 4.08 6
female low Z 69.8 2.72 6
ggboxplot(headache, x = "treatment", y = "pain_score", color = "gender",
          facet.by = "risk", add = "jitter",
          xlab = "Treatment", ylab = "Pain Score",
          caption = "Boxplots of Pain Scores by Treatment, Risk, and Gender") + 
          theme_dark()

### Assumptions

Outliers

outliers <- headache %>%
  group_by(gender, risk, treatment) %>%
  identify_outliers(pain_score)
  
outliers %>%
  kbl() %>%
  kable_material_dark()
gender risk treatment id pain_score is.outlier is.extreme
female high X 57 68.4 TRUE TRUE
female high Y 62 73.1 TRUE FALSE
female high Z 67 75.0 TRUE FALSE
female high Z 71 87.1 TRUE FALSE

Normality

normality <- headache %>%
  group_by(gender, risk, treatment) %>%
  shapiro_test(pain_score)
  normality %>%
    kbl() %>%
    kable_material_dark()
gender risk treatment variable statistic p
male high X pain_score 0.958 0.808
male high Y pain_score 0.902 0.384
male high Z pain_score 0.955 0.784
male low X pain_score 0.982 0.962
male low Y pain_score 0.920 0.507
male low Z pain_score 0.924 0.535
female high X pain_score 0.714 0.009
female high Y pain_score 0.939 0.654
female high Z pain_score 0.971 0.901
female low X pain_score 0.933 0.600
female low Y pain_score 0.927 0.555
female low Z pain_score 0.958 0.801
ggqqplot(headache$pain_score, caption = "Normal Probability Plot of Pain Scores") +
  theme_dark() +
  geom_qq(color = 'red')

ggqqplot(headache, x = "pain_score", color = "gender") +         
  theme_dark() +
  facet_grid(risk ~ gender + treatment, scales = "free")

Homogeneity of variance

levene_test <- levene_test(pain_score ~ treatment * risk * gender, data = headache)
levene_test %>%
  kbl() %>%
  kable_material_dark()
df1 df2 statistic p
11 60 0.179 0.998

Anova

results <- aov(pain_score ~ gender * risk * treatment, data = headache)
anova(results) %>%
  kbl() %>%
  kable_material_dark()
Df Sum Sq Mean Sq F value Pr(>F)
gender 1 313.36 313.36 16.196 0.000
risk 1 1793.56 1793.56 92.699 0.000
treatment 2 283.17 141.58 7.318 0.001
gender:risk 1 2.73 2.73 0.141 0.708
gender:treatment 2 129.18 64.59 3.338 0.042
risk:treatment 2 27.59 13.80 0.713 0.494
gender:risk:treatment 2 286.60 143.30 7.406 0.001
Residuals 60 1160.89 19.35 NA NA
report(results)
## The ANOVA (formula: pain_score ~ gender * risk * treatment) suggests that:
## 
##   - The main effect of gender is statistically significant and large (F(1, 60) =
## 16.20, p < .001; Eta2 (partial) = 0.21, 95% CI [0.08, 1.00])
##   - The main effect of risk is statistically significant and large (F(1, 60) =
## 92.70, p < .001; Eta2 (partial) = 0.61, 95% CI [0.48, 1.00])
##   - The main effect of treatment is statistically significant and large (F(2, 60)
## = 7.32, p = 0.001; Eta2 (partial) = 0.20, 95% CI [0.06, 1.00])
##   - The interaction between gender and risk is statistically not significant and
## very small (F(1, 60) = 0.14, p = 0.708; Eta2 (partial) = 2.35e-03, 95% CI
## [0.00, 1.00])
##   - The interaction between gender and treatment is statistically significant and
## medium (F(2, 60) = 3.34, p = 0.042; Eta2 (partial) = 0.10, 95% CI [2.01e-03,
## 1.00])
##   - The interaction between risk and treatment is statistically not significant
## and small (F(2, 60) = 0.71, p = 0.494; Eta2 (partial) = 0.02, 95% CI [0.00,
## 1.00])
##   - The interaction between gender, risk and treatment is statistically
## significant and large (F(2, 60) = 7.41, p = 0.001; Eta2 (partial) = 0.20, 95%
## CI [0.06, 1.00])
## 
## Effect sizes were labelled following Field's (2013) recommendations.

Post-hoc tests

If there is a significant 3-way interaction effect, you can decompose it into:

  • Simple two-way interaction: run two-way interaction at each level of third variable,
  • Simple simple main effect: run one-way model at each level of second variable,
  • Simple simple pairwise comparisons: run pairwise or other post-hoc comparisons if necessary.

If you do not have a statistically significant three-way interaction, you need to determine whether you have any statistically significant two-way interaction from the ANOVA output. You can follow up a significant two-way interaction by simple main effects analyses and pairwise comparisons between groups if necessary.

Two-way interactions

model  <- lm(pain_score ~ gender*risk*treatment, data = headache)
two_way <- headache %>%
  group_by(gender) %>%
  anova_test(pain_score ~ risk*treatment, error = model)
two_way %>%
  kbl() %>%
  kable_material_dark()
gender Effect DFn DFd F p p<.05 ges
male risk 1 60 50.037 0.000
0.455
male treatment 2 60 10.174 0.000
0.253
male risk:treatment 2 60 5.252 0.008
0.149
female risk 1 60 42.803 0.000
0.416
female treatment 2 60 0.482 0.620 0.016
female risk:treatment 2 60 2.868 0.065 0.087

Main effects

effects <- headache %>%
  group_by(gender, risk) %>%
  anova_test(pain_score ~ treatment, error = model)
effects %>%
  kbl() %>%
  kable_material_dark()
gender risk Effect DFn DFd F p p<.05 ges
male high treatment 2 60 14.77 0.000
0.330
male low treatment 2 60 0.66 0.521 0.022
female high treatment 2 60 0.52 0.597 0.017
female low treatment 2 60 2.83 0.067 0.086

Pairwise comparisons

pairwise <- headache %>%
  group_by(gender, risk) %>%
  emmeans_test(pain_score ~ treatment)
pairwise %>%
  filter(gender=="male",risk=="high") %>%
  kbl() %>%
  kable_material_dark()
gender risk term .y. group1 group2 df statistic p p.adj p.adj.signif
male high treatment pain_score X Y 60 4.09 0.000 0.000 ***
male high treatment pain_score X Z 60 5.14 0.000 0.000 ****
male high treatment pain_score Y Z 60 1.05 0.299 0.897 ns