Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. # Hierarchical models. Estimation only, no NHST testing.
  2.  
  3. library(lme4)
  4. library(lattice)
  5.  
  6. # ---- Look at the data ---
  7. data(sleepstudy)
  8. head(sleepstudy)
  9.  
  10. xyplot(Reaction ~ Days | Subject, data=sleepstudy, type=c("p","r") )
  11.  
  12. ?lmer
  13. ?glmer # for logistic, Poisson and other models
  14.  
  15. # ------ Model fitting ---------
  16. # -- Wrong model which ignores repeated measures
  17. fit0 <- lm(Reaction ~ Days, data=sleepstudy)
  18. summary(fit0)
  19.  
  20. plot(Reaction ~ Days, data=sleepstudy)
  21. abline(coef(fit0))
  22.  
  23. # -- Random intercept, fixed slopes (Days)
  24. fit1 <- lmer(Reaction ~ Days + (1|Subject), data=sleepstudy )
  25. summary(fit1) # "general effect" for (Intercept) = 251.4051
  26. ranef(fit1) # matrix of effects added to "general level" (Intercept)
  27.  
  28. # --- Random intercept, random slopes
  29. fit2 <- lmer(Reaction ~ Days + (1+Days|Subject), data=sleepstudy )
  30. summary(fit2)
  31. ranef(fit2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement