kassambara / rstatix

Pipe-friendly Framework for Basic Statistical Tests in R
https://rpkgs.datanovia.com/rstatix/
432 stars 51 forks source link

anova_test: No Sphericity Corrections for 2x2 Repeated Measures ANOVA #160

Closed ClaudiuPapasteri closed 1 year ago

ClaudiuPapasteri commented 1 year ago

I am not sure if this is intended, but anova_test when having two within factors with only 2 levels does not return Mauchly's Test for Sphericity and Sphericity Corrections. I understand that Mauchly's Test for Sphericity probably is reported ony for effects >2 levels, but I would have been interested in the GG Sphericity Correction. Is there a problem to compute it for 2x2 rmANOVA or is it a bug?

Here is a reprex with two of the datasets you use in your examples.

library(dplyr)
library(rstatix)

ToothGrowth %>% 
  dplyr::mutate(id = rep(1:10, 6)) %>%
  anova_test(dv = len, wid = id, within = c(supp, dose))
#> ANOVA Table (type III tests)
#> 
#> $ANOVA
#>      Effect DFn DFd       F        p p<.05   ges
#> 1      supp   1   9  34.866 2.28e-04     * 0.224
#> 2      dose   2  18 106.470 1.06e-10     * 0.773
#> 3 supp:dose   2  18   2.534 1.07e-01       0.132
#> 
#> $`Mauchly's Test for Sphericity`
#>      Effect     W     p p<.05
#> 1      dose 0.807 0.425      
#> 2 supp:dose 0.934 0.761      
#> 
#> $`Sphericity Corrections`
#>      Effect   GGe      DF[GG]    p[GG] p[GG]<.05   HFe      DF[HF]    p[HF]
#> 1      dose 0.838 1.68, 15.09 2.79e-09         * 1.008 2.02, 18.15 1.06e-10
#> 2 supp:dose 0.938 1.88, 16.88 1.12e-01           1.176 2.35, 21.17 1.07e-01
#>   p[HF]<.05
#> 1         *
#> 2

ToothGrowth %>% 
  dplyr::mutate(id = rep(1:10, 6)) %>%
  dplyr::filter(dose < 2) %>%
  anova_test(dv = len, wid = id, within = c(supp, dose))
#> ANOVA Table (type III tests)
#> 
#>      Effect DFn DFd      F        p p<.05   ges
#> 1      supp   1   9 52.489 4.84e-05     * 0.414
#> 2      dose   1   9 75.163 1.16e-05     * 0.654
#> 3 supp:dose   1   9  0.046 8.35e-01       0.003

ChickWeight %>% 
  anova_test(dv = weight, wid = Chick, within = Time, between = Diet)
#> ANOVA Table (type III tests)
#> 
#> $ANOVA
#>      Effect DFn DFd       F         p p<.05   ges
#> 1      Diet   3  41   5.075  4.00e-03     * 0.161
#> 2      Time  11 451 280.945 6.41e-194     * 0.769
#> 3 Diet:Time  33 451   3.766  9.34e-11     * 0.118
#> 
#> $`Mauchly's Test for Sphericity`
#>      Effect        W         p p<.05
#> 1      Time 2.68e-17 1.03e-251     *
#> 2 Diet:Time 2.68e-17 1.03e-251     *
#> 
#> $`Sphericity Corrections`
#>      Effect   GGe      DF[GG]    p[GG] p[GG]<.05   HFe      DF[HF]    p[HF]
#> 1      Time 0.114 1.26, 51.48 2.01e-24         * 0.116 1.28, 52.34 8.63e-25
#> 2 Diet:Time 0.114 3.77, 51.48 1.00e-02         * 0.116 3.83, 52.34 1.00e-02
#>   p[HF]<.05
#> 1         *
#> 2         *

ChickWeight %>% 
  dplyr::filter(Time < 3) %>%
  anova_test(dv = weight, wid = Chick, within = Time, between = Diet)
#> ANOVA Table (type III tests)
#> 
#>      Effect DFn DFd       F        p p<.05   ges
#> 1      Diet   3  46   2.793 5.10e-02       0.100
#> 2      Time   1  46 361.049 2.06e-23     * 0.755
#> 3 Diet:Time   3  46   6.899 6.22e-04     * 0.150

Created on 2022-09-21 by the reprex package (v2.0.1)

ClaudiuPapasteri commented 1 year ago

Sorry, it was a lapse on my part, sphericity or sphericity correction don't make sense at only 2 levels of the factor.