Solved – lme4: random effects

lme4-nlmerrandom-effects-model

I have a simulated data set of 4 repeated measurements (measure) for 5 subjects (subj), 20 trials (trl) each. I am trying to fit a model with random slopes for age category with subject and trials within subject as random effects and age category as fixed effect.

Here is the reproducible example:

set.seed(1)
# 20 trials 5 subjects 4 rep meas
age <- runif(400, min = 18, max = 60)
measure <- runif(400, min = 8, max = 14)
cat <- seq(1,4,1)
trl <- rep(seq(1,20,1),5)
sub <- c(rep(1,20),rep(2,20),rep(3,20),rep(4,20),rep(5,20))

data <- as.data.frame(cbind(measure, age, cat = rep(cat,100), trl = (rep(trl,4)),        
                      sub = rep(sub,4)))
data[, 'cat'] <- as.factor(data[, 'cat'])
data[, 'sub'] <- as.factor(data[, 'sub'])
data[, 'trl'] <- as.factor(data[, 'trl'])

library(lme4)
model <- lmer(measure ~ cat-1 + (0+cat|sub) + (0+cat|sub/trl),data)

I get an error message as:

Error: number of observations (=400) <= number of random effects (=400) 
  for term (0 + cat | sub); the random-effects parameters and the residual 
  variance (or scale parameter) are probably unidentifiable

I expect to get 100 groups for subj/trl and 5 for subj and 400 observations. So I am confused regarding the error. I am trying to ascertain what went wrong with the model specification. Please help.

Best Answer

While the question appears solved by the OP, I will post the answer for future reference.

The answer is (as suspected) in the error message but the error message itself points to the wrong random structure. The root cause of this message is the number of random factors defined by the structure (cat|sub/trl).

If we simply had the structure (1|sub/trl) we would use the 20 trials and 5 subjects. We would then expect 100 levels due to the interaction sub:trl plus 5 levels due to sub for a total of 105 factors (random intercepts actually). This would be almost fine given one has 400 sample points.

What happens is that as we define the random slope due to cat| to have 4 measurements, we multiply our total number of (random) factors by 4, ie. the random structure we have now has 420 levels. Therefore the random effects design matrix $Z$ is of dimensions $400 \times 420$ and we are having an under-determined system. If one did not use trial (trl) information in the random effects structure (so he had a structure similar to (cat|sub)) the $Z$ matrix would be of dimensions $400 \times 20$ and the estimation procedure would be fine. :)

One can check this later statement by simply defining the associated model and checking the dimensions of $Z$ directly:

  model <-  model <- lmer(measure ~ 1 + (cat|sub),data)
  dim(t(model@pp$Zt)) # [1] 400  20