Why does my repeated factorial aov not recognise my sample size?

Viewed 74

I have a sample dataframe that looks like this:

DV IV1 IV2 ID
6 x a 1
3 x a 2
4 x a 3
9 x b 1
2 x b 2
4 x b 3
8 y a 1
4 y a 2
3 y a 3
2 y b 1
1 y b 2
5 y b 3
9 z a 1
7 z a 2
8 z a 3
9 z b 1
4 z b 2
3 z b 3

I tried to run a repeated measures factorial ANOVA using this code:

model <- aov(DV ~ IV1*IV2 + Error(ID/IV1*IV2), data=my_data)

While the code works, the output is wrong. I suspect it is because R does not recognise there are 3 participants in my data(my degrees of freedom for ID is always 1). Consequently, in the case of this sample data, my error df is 6 instead of 4.

May I know what did I do wrong?

2 Answers

It is always useful to transform categorial variables you want to use in aov to factors, this does also work in your case:

  my_data <- readr::read_tsv(
    'DV     IV1     IV2     ID
6   x   a   1
3   x   a   2
4   x   a   3
9   x   b   1
2   x   b   2
4   x   b   3
8   y   a   1
4   y   a   2
3   y   a   3
2   y   b   1
1   y   b   2
5   y   b   3
9   z   a   1
7   z   a   2
8   z   a   3
9   z   b   1
4   z   b   2
3   z   b   3')
  
  my_data$ID <- factor(my_data$ID)
  
  model <- aov(DV ~ IV1*IV2 + Error(ID/(IV1*IV2)), data=my_data)
  summary(model)
#> 
#> Error: ID
#>           Df Sum Sq Mean Sq F value Pr(>F)
#> Residuals  2  43.11   21.56               
#> 
#> Error: ID:IV1
#>           Df Sum Sq Mean Sq F value Pr(>F)  
#> IV1        2 25.444  12.722   8.642 0.0353 *
#> Residuals  4  5.889   1.472                 
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Error: ID:IV2
#>           Df Sum Sq Mean Sq F value Pr(>F)  
#> IV2        1  9.389   9.389   10.56 0.0831 .
#> Residuals  2  1.778   0.889                 
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Error: ID:IV1:IV2
#>           Df Sum Sq Mean Sq F value Pr(>F)
#> IV1:IV2    2  10.11   5.056   0.802   0.51
#> Residuals  4  25.22   6.306

Created on 2021-01-11 by the reprex package (v0.3.0)

By the way, in edition to @Max Teflon's approach, I found that the "ez" package arrives at the same outcome too.

install.packages("ez")
library(ez)
ezANOVA(test, dv=.(dv), wid=.(ID), within=.(iv1,iv2), detailed=TRUE)
Related