Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Hierarchical models. Estimation only, no NHST testing.
- library(lme4)
- library(lattice)
- # ---- Look at the data ---
- data(sleepstudy)
- head(sleepstudy)
- xyplot(Reaction ~ Days | Subject, data=sleepstudy, type=c("p","r") )
- ?lmer
- ?glmer # for logistic, Poisson and other models
- # ------ Model fitting ---------
- # -- Wrong model which ignores repeated measures
- fit0 <- lm(Reaction ~ Days, data=sleepstudy)
- summary(fit0)
- plot(Reaction ~ Days, data=sleepstudy)
- abline(coef(fit0))
- # -- Random intercept, fixed slopes (Days)
- fit1 <- lmer(Reaction ~ Days + (1|Subject), data=sleepstudy )
- summary(fit1) # "general effect" for (Intercept) = 251.4051
- ranef(fit1) # matrix of effects added to "general level" (Intercept)
- # --- Random intercept, random slopes
- fit2 <- lmer(Reaction ~ Days + (1+Days|Subject), data=sleepstudy )
- summary(fit2)
- ranef(fit2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement