I refer to this post
I use the same library and also the same dataset
library(tidyverse)
library(lmer4)
str(sleepstudy)
'data.frame': 180 obs. of 3 variables:
$ Reaction: num 250 259 251 321 357 ...
$ Days : num 0 1 2 3 4 5 6 7 8 9 ...
$ Subject : Factor w/ 18 levels "308","309","310",..: 1 1 1 1 1 1 1 1 1 1 ...
I want to compute a linear mixed model with a random intercept and a random slope:
fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
This is executed without any problems.
However, if I define the within-variable as a factor, I get the familiar error message
sleepstudy <- sleepstudy %>%
mutate(Days = factor(Days))
fm2 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
Error: number of observations (=180) <= number of random effects (=180) for term (Days | Subject); the random-effects parameters and the residual variance (or scale parameter) are probably unidentifiable
In my own data I have a treatment variable with 3 values. I measured the dependent variable at 3 measurement points. So far I have coded both treatment and the measurement as factor. I would like to compute the following model, but this did not work due to the error message shown above
model <- lmer(y ~ treatment + measurement + treatment*measurement + (measurement|subject) + (1|class), data = df)
So I wonder if I need to define the within-variable as a factor or as a numeric variable?
classvariable: are subjects sampled from different classes? – dipetkov Sep 02 '22 at 10:43classis the school class. All subjects belong to a single class. – Fabi Sep 02 '22 at 13:19