Posthoc Testing in Two-Way Analyses

Introduction

  • Recall that ANOVA allows us to compare the means of three or more groups.

  • In one-way ANOVA, we are only considering one factor (grouping variable).

  • On Monday, we discussed two-way ANOVA, which allows us to consider a second factor (grouping variable).

  • Today, we are discussing profile plots and posthoc tests in the two-way ANOVA setting.

R Setup

library(tidyverse)
library(ssstats)

supplement_heart_rate <- read_csv("https://raw.githubusercontent.com/samanthaseals/STA4173Fa26/refs/heads/main/lectures/data/supplement_heart_rate.csv") 
exercise_recovery <- read_csv("https://raw.githubusercontent.com/samanthaseals/STA4173Fa26/refs/heads/main/lectures/data/exercise_recovery.csv") 

Two-Way ANOVA: Profile Plots

  • We can construct profile plots to display treatment group means.

    • y-axis: always the average outcome

    • x-axis: either factor A or B

    • “lines” on the plot: the factor that was not selected for the x-axis

  • Note that this is just a graph of the means!

    • It’s valid to construct a profile plot regardless of significance.

Two-Way ANOVA: Profile Plots (R)

  • We will use the profile_plot() function from library(ssstats) to construct basic profile plots.
dataset_name |> profile_plot(continuous = continuous_variable,
                              xaxis = variable_on_x,
                              lines = variable_for_lines)

Example 1

  • Fluttershy asks: “Does the supplement affect every pony type in the same way?”

  • We used the supplement_heart_rate dataset to determine if the effect of treatment on resting heart rate depends on pony type.

pony_id treatment pony_type resting_heart_rate_bpm
FS001 Placebo Earth pony 63.7
FS002 Placebo Earth pony 64.1
FS003 Placebo Earth pony 67.5

Example 1

  • We want to work towards a profile plot. The first question we ask is what levels are in each variable?
supplement_heart_rate |> count(pony_type)
pony_type n
Earth pony 24
Pegasus 24
Unicorn 24
supplement_heart_rate |> count(treatment)
treatment n
Placebo 36
Supplement 36

Example 1

  • Neither variable has natural ordering.

    • Treatment: placebo, supplement
    • Pony type: earth, pegasus, unicorn
  • My choice would be:

    • put pony type on the x-axis
    • create lines for treatment
  • Why?

    • When there is no ordering, we then move to thinking about how many lines will be created.

    • It will be much easier to draw conclusions with fewer lines (graph is less busy).

Example 1

  • Constructing the profile plot,
supplement_heart_rate |> profile_plot(outcome = resting_heart_rate_bpm,
                              xaxis = pony_type,
                              lines = treatment)

Example 1

  • What does it look like if we switch xaxis and lines?
supplement_heart_rate |> profile_plot(outcome = resting_heart_rate_bpm,
                              xaxis = treatment,
                              lines = pony_type)

Example 2

  • Twilight Sparkle asks: “Can the Ponyville clinic help ponies recover faster after exercise? Does the treatment work the same way across pony types?”

  • We used the exercise_recovery to test whether the effect of recovery treatment depends on pony type.

pony_id treatment pony_type recovery_time_min
TS001 Standard care Earth pony 27.3
TS002 Standard care Earth pony 35.8
TS003 Standard care Earth pony 33.5

Example 2

  • What levels are in each variable?
exercise_recovery |> count(treatment)
treatment n
Enhanced treatment 36
New treatment 36
Standard care 36
exercise_recovery |> count(pony_type)
pony_type n
Earth pony 36
Pegasus 36
Unicorn 36

Example 2

  • Neither variable has natural ordering.

    • Treatment: enhanced, new, standard
    • Pony type: earth, pegasus, unicorn
  • My choice would be:

    • put treatment on the x-axis

      • impose ordering: standard, new, enhanced
    • create lines for pony type

exercise_recovery <- exercise_recovery |>
  mutate(treatment = factor(treatment,
                            levels = c("Standard care", "New treatment", "Enhanced treatment")))

Example 2

  • Constructing the profile plot,
exercise_recovery |> profile_plot(outcome = recovery_time_min,
                              xaxis = treatment,
                              lines = pony_type)

Example 2

  • What if we switch the x-axis and the lines?
exercise_recovery |> profile_plot(outcome = recovery_time_min,
                              xaxis = pony_type,
                              lines = treatment)

Posthoc Testing

  • Recall our hypotheses for main effects,

    • H_0: \mu_1 = \mu_2 = ... = \mu_a
    • H_1: at least one \mu_i is different
  • We will again turn to posthoc tests to determine pairwise differences.

  • Tying this to the profile plot, posthoc tests are giving us the p-values corresponding to pairwise differences.

Posthoc Testing (R)

  • We will again use the posthoc() function,
dataset_name |> posthoc(outcome = response_variable,
                        A = factor_A,
                        B = factor_B,
                        interaction = TRUE or FALSE,
                        adjust = TRUE or FALSE)

Example 1

  • Fluttershy asks: “Does the supplement affect every pony type in the same way?”

  • We used the supplement_heart_rate dataset to determine if the effect of treatment on resting heart rate depends on pony type.

supplement_heart_rate |> anovas(outcome = resting_heart_rate_bpm,
                                A = pony_type,
                                B = treatment)
Two-way ANOVA (with interaction)

Source               df         SS        MS       F  p-value 
──────────────────────────────────────────────────────── 
pony_type             2  1383.6803  691.8401  49.519  < 0.001 
treatment             1   215.2813  215.2813  15.409  < 0.001 
pony_type:treatment   2   636.6508  318.3254  22.785  < 0.001 
Residuals            66   922.0942   13.9711                  
──────────────────────────────────────────────────────── 


Hypotheses (pony_type:treatment):
H₀: There is no interaction between pony_type and treatment
H₁: There is an interaction between pony_type and treatment
Test statistic: F(2, 66) = 22.785
p-value: < 0.001
Conclusion: Reject the null hypothesis (p = < 0.001 < α = 0.050)

Example 1

  • We can look at the posthoc test of the interaction term.

  • Treating it as confirmatory,

supplement_heart_rate |> posthoc(outcome = resting_heart_rate_bpm,
                                 A = pony_type,
                                 B = treatment,
                                 adjust = TRUE)
Post-hoc analysis: Tukey's HSD

Following: two-way ANOVA (resting_heart_rate_bpm ~ pony_type * treatment)

Main effect: pony_type 
Comparison             Difference  p-value   
───────────────────────────────────────────── 
Pegasus vs Earth pony     10.6792  < 0.001  * 
Unicorn vs Earth pony      6.3125  < 0.001  * 
Unicorn vs Pegasus        -4.3667  < 0.001  * 
───────────────────────────────────────────── 

Main effect: treatment 
Comparison             Difference  p-value   
───────────────────────────────────────────── 
Supplement vs Placebo     -3.4583  < 0.001  * 
───────────────────────────────────────────── 

Interaction: pony_type × treatment 
Comparison                                   Difference  p-value   
─────────────────────────────────────────────────────────────────── 
Pegasus:Placebo vs Earth pony:Placebo            3.6833    0.167   
Unicorn:Placebo vs Earth pony:Placebo            1.0583    0.982   
Earth pony:Supplement vs Earth pony:Placebo    -11.6250  < 0.001  * 
Pegasus:Supplement vs Earth pony:Placebo         6.0500    0.002  * 
Unicorn:Supplement vs Earth pony:Placebo        -0.0583    1.000   
Unicorn:Placebo vs Pegasus:Placebo              -2.6250    0.524   
Earth pony:Supplement vs Pegasus:Placebo       -15.3083  < 0.001  * 
Pegasus:Supplement vs Pegasus:Placebo            2.3667    0.633   
Unicorn:Supplement vs Pegasus:Placebo           -3.7417    0.154   
Earth pony:Supplement vs Unicorn:Placebo       -12.6833  < 0.001  * 
Pegasus:Supplement vs Unicorn:Placebo            4.9917    0.020  * 
Unicorn:Supplement vs Unicorn:Placebo           -1.1167    0.977   
Pegasus:Supplement vs Earth pony:Supplement     17.6750  < 0.001  * 
Unicorn:Supplement vs Earth pony:Supplement     11.5667  < 0.001  * 
Unicorn:Supplement vs Pegasus:Supplement        -6.1083    0.002  * 
─────────────────────────────────────────────────────────────────── 

* p < α = 0.050

Example 2

  • Twilight Sparkle asks: “Can the Ponyville clinic help ponies recover faster after exercise? Does the treatment work the same way across pony types?”

  • We used the exercise_recovery to test whether the effect of recovery treatment depends on pony type.

exercise_recovery |> anovas(outcome = recovery_time_min,
                            A = pony_type,
                            B = treatment)
Two-way ANOVA (with interaction)

Source               df         SS         MS       F  p-value 
───────────────────────────────────────────────────────── 
pony_type             2    23.1424    11.5712   0.630    0.535 
treatment             2  2909.4913  1454.7456  79.154  < 0.001 
pony_type:treatment   4    47.5704    11.8926   0.647    0.630 
Residuals            99  1819.4858    18.3786                  
───────────────────────────────────────────────────────── 


Hypotheses (pony_type:treatment):
H₀: There is no interaction between pony_type and treatment
H₁: There is an interaction between pony_type and treatment
Test statistic: F(4, 99) = 0.647
p-value: 0.630
Conclusion: Fail to reject the null hypothesis (p = 0.630 ≥ α = 0.050)

Example 2

  • Twilight Sparkle asks: “Can the Ponyville clinic help ponies recover faster after exercise? Does the treatment work the same way across pony types?”

  • We then removed the (non-significant) interaction term,

exercise_recovery |> anovas(outcome = recovery_time_min,
                            A = pony_type,
                            B = treatment,
                            interaction = FALSE)
Two-way ANOVA (no interaction)

Source      df         SS         MS       F  p-value 
──────────────────────────────────────────────── 
pony_type    2    23.1424    11.5712   0.638    0.530 
treatment    2  2909.4913  1454.7456  80.254  < 0.001 
Residuals  103  1867.0562    18.1268                  
──────────────────────────────────────────────── 


Hypotheses (pony_type):
H₀: All means across levels of pony_type are equal
H₁: At least one mean across levels of pony_type differs
Test statistic: F(2, 103) = 0.638
p-value: 0.530
Conclusion: Fail to reject the null hypothesis (p = 0.530 ≥ α = 0.050)

Hypotheses (treatment):
H₀: All means across levels of treatment are equal
H₁: At least one mean across levels of treatment differs
Test statistic: F(2, 103) = 80.254
p-value: < 0.001
Conclusion: Reject the null hypothesis (p = < 0.001 < α = 0.050)

Example 2

  • We now look at the posthoc test for only treatment

    • We did not detect a difference between the pony types (p = 0.530).
  • Treating it as exploratory,

exercise_recovery |> posthoc(outcome = recovery_time_min,
                             A = pony_type,
                             B = treatment,
                             interaction = FALSE,
                             adjust = FALSE)
Post-hoc analysis: Fisher's LSD

Following: two-way ANOVA (recovery_time_min ~ pony_type + treatment)

Main effect: pony_type 
Comparison             Difference  p-value   
───────────────────────────────────────────── 
Pegasus vs Earth pony     -0.9861    0.328   
Unicorn vs Earth pony     -0.9778    0.332   
Unicorn vs Pegasus         0.0083    0.993   
───────────────────────────────────────────── 

Main effect: treatment 
Comparison                           Difference  p-value   
─────────────────────────────────────────────────────────── 
New treatment vs Standard care          -5.0806  < 0.001  * 
Enhanced treatment vs Standard care    -12.6333  < 0.001  * 
Enhanced treatment vs New treatment     -7.5528  < 0.001  * 
─────────────────────────────────────────────────────────── 

* p < α = 0.050

Wrap Up

  • In this lecture, we have covered posthoc testing and profile plots in two-way ANOVA.

    • Interactions.

    • Main effects.

  • We can tie the results from posthoc testing to the profile plot.

    • The profile plot is allowing us to visualize the differences between factors or treatment groups.
  • Next week:

    • Monday: R workshop - bring laptop!

    • Wednesday: Test 2 - ANOVA