# Repeated-measures error in R ezANOVA using more levels than subjects (balanced design)

I'm having a problem running a repeated-measures ANOVA in R using the ezANOVA function.

I have data from 18 subjects - each subject participated in 3 conditions, and data was collected in each subject/condition combination at 29 electrode sites (1566 total data points in a balanced design with no missing cells).

When I try to run the full ANOVA

model = ezANOVA(data, dv=voltage, wid=subject, within=.(electrode, channel))


I get the following error:

Error in lambda > 0 : invalid comparison with complex values Error in ezANOVA_main(data = data, dv = dv, wid = wid, within = within, :
The car::Anova() function used to compute results and assumption tests seems to have failed. Most commonly this is because you have too few subjects relative to the number of cells in the within-Ss design. It is possible that trying the ANOVA again with "type=1" may yield results (but definitely no assumption tests).

Things work fine though if I include fewer levels of electrode than I have subjects (18 or fewer), but if I add more it fails. Why is this a problem? I wouldn't think that having greater-than-n levels for a within-subjects factor would be a problem if it's a balanced fully repeated-measures design. (SPSS will compute things just fine). Using Type I SS works, but if I select this option I won't give me the sphericity corrected p-values that I need to report.

This issue is described in this post by John Fox - author of the car::Anova() function that is used internally by ezANOVA().

As a workaround, you can use anova() using a multivariate model specification that is described in this article by Peter Dalgaard as well as in this excellent answer by Aaron. Here's a reproducible example with data in wide format:

set.seed(123)  ## make reproducible
N  <- 18       ## number of subjects
P  <- 3        ## number of conditions
Q  <- 29       ## number of sites
voltage <- matrix(round(rnorm(N*P*Q), 2), nrow=N)   ## (N x (PxQ))-matrix with voltages

fit  <- lm(voltage ~ 1)   ## between-subjects design (here: no between factors)
inDf <- expand.grid(channel=gl(P, 1), electrode=gl(Q, 1))  ## within design
library(car)              ## for Anova()
AnRes <- Anova(fit, idata=inDf, idesign=~channel*electrode)
summary(AnRes, multivariate=FALSE, univariate=TRUE)


Due to the singular SSP-matrix, this does not return sphericity-corrected p-values:

Univariate Type III Repeated-Measures ANOVA Assuming Sphericity
SS num Df Error SS den Df      F Pr(>F)
(Intercept)        0.862      1    14.08     17 1.0413 0.3218
channel            1.815      2    42.62     34 0.7237 0.4923
electrode         21.018     28   439.35    476 0.8132 0.7408
channel:electrode 56.375     56   945.04    952 1.0141 0.4484


Instead, use anova() with the multivariate model (shortened output). Test for channel:

> anova(fit, M=~channel, X=~1, idata=inDf, test="Spherical")
Greenhouse-Geisser epsilon: 0.9569
Huynh-Feldt epsilon:        1.0754

Df      F num Df den Df  Pr(>F)  G-G Pr  H-F Pr
(Intercept)  1 0.7237      2     34 0.49225 0.48682 0.49225
Residuals   17


Test for electrode:

> anova(fit, M=~channel + electrode, X=~channel, idata=inDf, test="Spherical")
Greenhouse-Geisser epsilon: 0.3729
Huynh-Feldt epsilon:        1.0126

Df      F num Df den Df  Pr(>F)  G-G Pr  H-F Pr
(Intercept)  1 0.8132     28    476 0.74076 0.62102 0.74076
Residuals   17


Test for channel:electrode interaction:

> anova(fit, M=~channel + electrode + channel:electrode, X=~channel + electrode, idata=inDf, test="Spherical")
Greenhouse-Geisser epsilon: 0.233
Huynh-Feldt epsilon:        1.052

Df      F num Df den Df  Pr(>F) G-G Pr  H-F Pr
(Intercept)  1 1.0141     56    952 0.44836 0.4386 0.44836
Residuals   17


I`ve got another "solution" for your problem: I faced the same error message recently when I analyzed ERPs using a rmANOVA with two factors, one of them being a factor electrode with 26 levels. Fortunately, I remarked that the error message only occurred when I analyzed latencies, but not when I analyzed amplitude values. I took a closer look at my dataset and wondered whether it might be a problem of too many or unequal number of decimal places. I tried to circumvent this problem by simply rounding the decimal places (thus making them all equal). After doing so, ezANOVA worked again.

 data$voltage <- round(data$voltage, digits = 2)