I would like to write a model with random intercepts and random slopes with respect to time. I am not sure if my code is correct.
model4<-lmer(weight~Time Diet Time*Diet (1 Time|Chick), data = Data, REML = TRUE)
summary(model4)
CodePudding user response:
Yes, that is the correct specification for those random effects. You can check this out, by applying a similar model, but temporarily removing the fixed effect on diet and the interaction between time and diet
model4<-lmer(weight~Time (1 Time|Chick), data = ChickWeight, REML = TRUE)
Column bind the original data, plus predictions from this simple model above, and select five random Chicks to plot
weight_hat = predict(model4)
cw = cbind(ChickWeight,weight_hat)
random_chicks = sample(unique(cw$Chick),5)
ggplot(cw[cw$Chick %in% random_chicks,], aes(Time, color=Chick))
geom_point(aes(y=weight), size=2)
geom_line(aes(y=weight_hat), size=1.5)
theme(legend.position="bottom")
guides(color=guide_legend(nrow=1))
You can see that the intercept and slope for each Chick differs.