Search code examples
rtidyranovarstatix

Spread Error in the two-way repeated measure anova test


I am working on a two-way repeated measure anova using the rstatix package.

library(rstatix)

res.aov <- anova_test(data=kolding, dv=count, wid=id, within=treatment)
get_anova_table(res.aov)

And here's how the data looks like:

    id treatment           species count
1   K1    biohut  Goldsinny Wrasse     0
2   K2    biohut  Goldsinny Wrasse     2
3   K3    biohut  Goldsinny Wrasse     1
4   K4    biohut  Goldsinny Wrasse     1
5   K5    biohut  Goldsinny Wrasse     1
6   K6    biohut  Goldsinny Wrasse     0
7   K1    biohut      Atlantic Cod     0
8   K2    biohut      Atlantic Cod     0
9   K3    biohut      Atlantic Cod     1
10  K4    biohut      Atlantic Cod     1
11  K5    biohut      Atlantic Cod     0
...

I keep getting a spread error that each row of output must be identified by a unique combination of keys, and I don't know how to go about it.

I tried pivot_wider(), but it wouldn't budge.


Solution

  • The problem is that your id variable is reused for different individuals — ie, you have K1, K2, etc, for both goldsinny wrasse and Atlantic cod. You can make a unique identifier by paste()ing id and species.

    library(rstatix)
    
    kolding$unique_id <- paste(kolding$id, kolding$species)
    
    res.aov <- anova_test(data=kolding, dv=count, wid=unique_id, within=treatment)
    get_anova_table(res.aov)
    
    ANOVA Table (type III tests)
    
         Effect DFn DFd    F     p p<.05   ges
    1 treatment   1  10 0.45 0.518       0.034
    

    Example data:

    OP’s sample data initially threw an error because it only includes one level of treatment, so I added another level:

    set.seed(13)
    
    kolding <- rbind(
      kolding, 
      transform(
        kolding, 
        treatment = "control", 
        count = sample(0:2, 11, replace = TRUE)
      )
    )