Let's say I have an experiment where I repeatedly measure something over Time (1:10), say 10 days.
Here I simulate some data...
set.seed(101) N = 10 # number of repeats n = 10 # number of individuals # Data Time = rep(1:N,n) # measured over time
Note that I set the fixed effect of time to 0.
# Fixed effects X = matrix(c(Time), ncol = 1) B = c(0) # Set fixed effect of Time's coefficient to 0 # Random effects id = factor(c(0L, cumsum(diff(Time) < 0))) # make id for each block of Time Z = model.matrix(~id+id:Time) # create design matrix u = c(rnorm(n),rnorm(n,2,1)) # random slopes and intercepts e = rnorm(N*n) # error y = X%*%B + Z%*%u + e # Model
Now let's say I fit two models:
library(lme4) fit1 = lmer(y ~ 1 + Time + (1 + Time|id)) fit2 = lmer(y ~ 1 + (1+ Time|id))
Comparing the two with anova()
anova(fit1,fit2) Models: fit2: y ~ 1 + (1 + Time | id) fit1: y ~ 1 + Time + (1 + Time | id) Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq) fit2 5 380.80 393.83 -185.40 370.80 fit1 6 329.41 345.05 -158.71 317.41 53.389 1 2.736e-13 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Here we see that
fit1, which includes
Time as a fixed effect, performs much better, but why is this true when its coefficient is set to 0?
I have a feeling it has something to do with this line.
Z = model.matrix(~id+id:Time) # create design matrix
but my intuition is failing me.