I would like to use the update()
function to update the random part of my model, specifically, adding a random effect. Most examples (help("update")
, help("update.formula")
, lme4:mixed effects modeling with R) focus on the fixed part of the model. How would I go from fm0
to fm1
using update()
in the example below?
library(lme4)
(fm0 <- lmer(Reaction ~ Days + (1 | Subject), sleepstudy))
(fm1 <- lmer(Reaction ~ Days + (1 + Days | Subject), sleepstudy))
To accomplish this in LMER just add the variables for which we want to add random slopes to the random part of the input. This means that (1|class) becomes (1+sex+extrav |class) . We can see that all the fixed regression slopes are still significant.
The random effects: (1 + Time | Chick) which allows individual chicks to vary randomly in terms of their intercept (starting weight) and their effect of Time (weight change over time, also called a “random slope”, but I think that terminology can get confusing when fitting models with nonlinear predictors).
The lmer() function is for linear mixed models and the glmer() function is for generalized mixed models.
lmer ): formula : a two-sided linear formula describing both the fixed-effects and random-effects part of the model, with the response on the left of the ~ operator and predictors and random effects on the right-hand side of the ~ operator.
I doubt this will be useful in your case, but you have to remove the random effect and then add the desired on back in:
update(fm0, . ~ . -(1|Subject) + (1 + Days | Subject))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With