ranovatwo-way

Two-way repeated measures ANOVA in R


I have been trying to do a two-way repeated measures ANOVA in R on a fictional data set to learn statistics. I have asked this question before, but I had to adapt my data sets because I had made some crucial mistakes. It represents two students who get graded on their tests on two test variants in two years. Even though the exercise is supposed to be straightforward, I keep getting error messages. This is my data set:

   ID   Score   Time  Group
  <fct> <int>  <fct>  <fct>
   1    10      1       1
   1    15      2       1
   2     2      1       1
   2     5      2       1
   3     7      1       1
   3     8      2       1
   4     6      1       2
   4     8      2       2
   5     9      1       2
   5     4      2       2
   6     3      1       2
   6    12      2       2

I have tried several methods:

aov<- df %>% anova_test(Score~ Time*Group+ Error(ID/(Time*Group)))

ezANOVA(
  data = df, dv = Score, wid = ID,
  within = .(Time, Group), type = 3)

resaov <- anova_test(
  data = df, dv = Score, wid = ID,
  within = c(Time, Group))

The error message I got was:

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  0 (non-NA) cases

Should I format my data in a different manner? If so, how is it supposed to look?


Solution

  • The problem is that Group is a between-subject factor (each ID is either in group 1 or 2 but not in both), but you treat it as a within-subject factor in your anova calls. The error messages tell you that the data is incomplete for your specified design.

    It works, if you specify the design with Group as a between-factor.

    #packages
    library(tidyr)
    library(ez)
    library(rstatix)
    
    #create your data frame (with random Score values)
    df <- tibble(ID = as.factor(rep(1:6, each = 2)),
                 Score = sample(1:15, 12, replace = TRUE),
                 Time = as.factor(rep(1:2, times = 6)),
                 Group = as.factor(rep(1:2, each = 6)))
    
    #ezANOVA
    ezANOVA(
      data = df, dv = Score, wid = ID,
      within = Time, between = Group, type = 3)
    
    $ANOVA
          Effect DFn DFd         F         p p<.05        ges
    2      Group   1   4 0.1290323 0.7375971       0.02787456
    3       Time   1   4 4.1290323 0.1119574       0.10289389
    4 Group:Time   1   4 0.5806452 0.4885138       0.01587302
    
    #anova_test #1
    anova_test(data = df, formula = Score ~ Time*Group + Error(ID/Time))
    ANOVA Table (type II tests)
    
          Effect DFn DFd     F     p p<.05   ges
    1      Group   1   4 0.129 0.738       0.028
    2       Time   1   4 4.129 0.112       0.103
    3 Group:Time   1   4 0.581 0.489       0.016
    
    #anova_test #2
    df %>% anova_test(Score~ Time*Group+ Error(ID/Time))
    
    ANOVA Table (type II tests)
    
          Effect DFn DFd     F     p p<.05   ges
    1      Group   1   4 0.129 0.738       0.028
    2       Time   1   4 4.129 0.112       0.103
    3 Group:Time   1   4 0.581 0.489       0.016