RM Anova

2022-01-23

Introduction

The repeated measures ANOVA makes the following assumptions about the data:

  • No significant outliers in any cell of the design. This can be checked by visualizing the data using box plot methods and by using the function identify_outliers() [rstatix package].

  • Normality: the outcome (or dependent) variable should be approximately normally distributed in each cell of the design. This can be checked using the Shapiro-Wilk normality test (shapiro_test() [rstatix]) or by visual inspection using QQ plot (ggqqplot() [ggpubr package]).

  • Assumption of sphericity: the variance of the differences between groups should be equal. This can be checked using the Mauchly’s test of sphericity, which is automatically reported when using the R function anova_test() [rstatix package].

Note that, if the above assumptions are not met there are a non-parametric alternative (Friedman test) to the one-way repeated measures ANOVA!

Unfortunately, there are no non-parametric alternatives to the two-way and the three-way repeated measures ANOVA. Thus, in the situation where the assumptions are not met, you could consider running the two-way/three-way repeated measures ANOVA on the transformed and non-transformed data to see if there are any meaningful differences.

If both tests lead you to the same conclusions, you might not choose to transform the outcome variable and carry on with the two-way/three-way repeated measures ANOVA on the original data.

It’s also possible to perform robust ANOVA test using the WRS2 R package.

No matter your choice, you should report what you did in your results.

RM Anova in R

Key R functions:

  • anova_test() [rstatix package], a wrapper around car::Anova() for making easy the computation of repeated measures ANOVA. Key arguments for performing repeated measures ANOVA:

    • data: data frame

    • dv: (numeric) the dependent (or outcome) variable name.

    • wid: variable name specifying the case/sample identifier.

    • within: within-subjects factor or grouping variable

  • get_anova_table() [rstatix package]. Extracts the ANOVA table from the output of anova_test(). It returns ANOVA table that is automatically corrected for eventual deviation from the sphericity assumption. The default is to apply automatically the Greenhouse-Geisser sphericity correction to only within-subject factors violating the sphericity assumption (i.e., Mauchly’s test p-value is significant, p <= 0.05). Read more in Chapter @ref(mauchly-s-test-of-sphericity-in-r).

1-way RM Anova

The dataset “selfesteem” contains 10 individuals’ self-esteem score on three time points during a specific diet to determine whether their self-esteem improved.

## # A tibble: 3 x 4
##      id    t1    t2    t3
##   <int> <dbl> <dbl> <dbl>
## 1     1  4.01  5.18  7.11
## 2     2  2.56  6.91  6.31
## 3     3  3.24  4.44  9.78

The one-way repeated measures ANOVA can be used to determine whether the means self-esteem scores are significantly different between the three time points. So let’s convert this data frame into long format:

## # A tibble: 3 x 3
##   id    time  score
##   <fct> <fct> <dbl>
## 1 1     t1     4.01
## 2 2     t1     2.56
## 3 3     t1     3.24

Descriptive statistics

## # A tibble: 3 x 5
##   time  variable     n  mean    sd
##   <fct> <chr>    <dbl> <dbl> <dbl>
## 1 t1    score       10  3.14 0.552
## 2 t2    score       10  4.93 0.863
## 3 t3    score       10  7.64 1.14

Assumptions

Checking if there are any outliers:

## # A tibble: 2 x 5
##   time  id    score is.outlier is.extreme
##   <fct> <fct> <dbl> <lgl>      <lgl>     
## 1 t1    6      2.05 TRUE       FALSE     
## 2 t2    2      6.91 TRUE       FALSE

There are no extreme outliers.

Checking the normality:

## # A tibble: 3 x 4
##   time  variable statistic     p
##   <fct> <chr>        <dbl> <dbl>
## 1 t1    score        0.967 0.859
## 2 t2    score        0.876 0.117
## 3 t3    score        0.923 0.380

From the results of Shapiro test and the plots we can say that our data is distributed normally.

Anova

and checking the sphercity

## ANOVA Table (type III tests)
## 
## $ANOVA
##   Effect DFn DFd      F        p p<.05   ges
## 1   time   2  18 55.469 2.01e-08     * 0.829
## 
## $`Mauchly's Test for Sphericity`
##   Effect     W     p p<.05
## 1   time 0.551 0.092      
## 
## $`Sphericity Corrections`
##   Effect  GGe      DF[GG]    p[GG] p[GG]<.05   HFe      DF[HF]    p[HF]
## 1   time 0.69 1.38, 12.42 2.16e-06         * 0.774 1.55, 13.94 6.03e-07
##   p[HF]<.05
## 1         *
## ANOVA Table (type III tests)
## 
##   Effect DFn DFd      F        p p<.05   ges
## 1   time   2  18 55.469 2.01e-08     * 0.829

From the Mauchly’s test for sphericity we can observe that p-value is is bigger than 0.05, hence it is not significant and we can state that variances of group differences are equal. So, all the needed assumptions are met. However, there occures statistially significant difference of the measured score during different times, with F(2,18) = 55.5 and p = 0.0000000201.

Post-hoc tests

To see which pair is significantly different.

## # A tibble: 3 x 10
##   .y.   group1 group2    n1    n2 statistic    df           p p.adj p.adj.signif
## * <chr> <chr>  <chr>  <int> <int>     <dbl> <dbl>       <dbl> <dbl> <chr>       
## 1 score t1     t2        10    10     -4.97     9 0.000772     2e-3 **          
## 2 score t1     t3        10    10    -13.2      9 0.000000334  1e-6 ****        
## 3 score t2     t3        10    10     -4.87     9 0.000886     2e-3 **

p-value is close to zero, so all of them are significantly different.

Conclusions

Hence, we can conclude that the score was statistically significantly different during different times, with the result with F(2,18) = 55.5 and p = 0.0000000201. From the post-hoc tests, when pairwise comparisons were conducted we can state that all the differences between pairs are statistically significant.

2-way RM Anova

For Two-Way Repeated Measures ANOVA, “Two-way” means that there are two factors in the experiment, for example, different treatments and different conditions. “Repeated-measures” means that the same subject received more than one treatment and/or more than one condition. Similar to two-way ANOVA, two-way repeated measures ANOVA can be employed to test for significant differences between the factor level means within a factor and for interactions between factors.

Using a standard ANOVA in this case is not appropriate because it fails to model the correlation between the repeated measures, and the data violates the ANOVA assumption of independence. Two-Way Repeated Measures ANOVA designs can be two repeated measures factors, or one repeated measures factor and one non-repeated factor. If any repeated factor is present, then the repeated measures ANOVA should be used.

Please apply Two-way RM-ANOVA to analyze if any significant interactions (between time and music, time and image, music and image, or music and time and image)! Use the following data set:

set.seed(5250)
myData <- data.frame(PID = rep(seq(from = 1,
                               to = 60, by = 1), 20),
                     stress = sample(x = 1:100,
                                     size = 1200,
                                     replace = TRUE),
                     image = sample(c("Happy", "Angry"),
                                    size = 1200,
                                    replace = TRUE),
                     music = sample(c("Disney", "Horror"),
                                    size = 1200,
                                    replace = TRUE)
)
myData <- within(myData, {
  PID   <- factor(PID)
  image <- factor(image)
  music <- factor(music)
})
myData <- myData[order(myData$PID), ]
head(myData)
##     PID stress image  music
## 1     1     90 Happy Horror
## 61    1      7 Angry Disney
## 121   1     31 Happy Disney
## 181   1     68 Angry Disney
## 241   1      6 Happy Disney
## 301   1     80 Angry Horror

Descriptive statistics

## # A tibble: 4 x 6
##   image music  variable     n  mean    sd
##   <fct> <fct>  <chr>    <dbl> <dbl> <dbl>
## 1 Angry Disney stress     310  48.9  29.4
## 2 Angry Horror stress     305  53.0  28.4
## 3 Happy Disney stress     289  49.5  29.2
## 4 Happy Horror stress     296  47.3  28.4

Assumptions

Checking if there are any outliers:

## [1] image      music      PID        stress     is.outlier is.extreme
## <0 wierszy> (lub 'row.names' o zerowej długości)

There are no outliers.

Checking the normality:

## # A tibble: 4 x 5
##   image music  variable statistic             p
##   <fct> <fct>  <chr>        <dbl>         <dbl>
## 1 Angry Disney stress       0.944 0.00000000171
## 2 Angry Horror stress       0.957 0.0000000924 
## 3 Happy Disney stress       0.952 0.0000000403 
## 4 Happy Horror stress       0.957 0.000000124

The p-values are smaller than 0.05, hence the distribution is not normal.

The plots visualize that the distribution is not normal. Since, as it was stated before “there are no non-parametric alternatives to the two-way and the three-way repeated measures ANOVA”. So, I will continue the report with non-transformed data.

Anova non-transformed

## Coefficient covariances computed by hccm()
## ANOVA Table (type II tests)
## 
##        Effect DFn  DFd     F     p p<.05      ges
## 1       image   1 1196 2.332 0.127       0.002000
## 2       music   1 1196 0.398 0.528       0.000332
## 3 image:music   1 1196 3.597 0.058       0.003000

There are no statistically significant interaction between image and music.