R version 3.4.4 (2018-03-15) -- "Someone to Lean On" ### Stat222, WEEK 2 Copyright (C) 2018 The R Foundation for Statistical Computing > library(lme4) Loading required package: Matrix ### Sleepstudy descriptives SFYS > data(sleepstudy) # sleep deprivation 3hrs/night truckers > dim(sleepstudy) [1] 180 3 > head(sleepstudy) Reaction Days Subject 1 249.5600 0 308 2 258.7047 1 308 3 250.8006 2 308 4 321.4398 3 308 5 356.8519 4 308 6 414.6901 5 308 ## note initial observation time = 0 > attach(sleepstudy) > table(Subject) Subject 308 309 310 330 331 332 333 334 335 337 349 350 351 352 369 370 371 372 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 > #yes 18 subjects (3hrs sleep) 10 observations on each, page 64 of bates ch4 > > > #lmList has the old nlme syntax, even in lme4 > sleeplmList = lmList(Reaction ~ Days |Subject, data = sleepstudy) > sleeplmList Call: lmList(formula = Reaction ~ Days | Subject, data = sleepstudy) Coefficients: (Intercept) Days 308 244.1927 21.764702 309 205.0549 2.261785 310 203.4842 6.114899 330 289.6851 3.008073 331 285.7390 5.266019 332 264.2516 9.566768 333 275.0191 9.142045 334 240.1629 12.253141 335 263.0347 -2.881034 337 290.1041 19.025974 349 215.1118 13.493933 350 225.8346 19.504017 351 261.1470 6.433498 352 276.3721 13.566549 369 254.9681 11.348109 370 210.4491 18.056151 371 253.6360 9.188445 372 267.0448 11.298073 Degrees of freedom: 180 total; 144 residual Residual standard error: 25.59182 > # note this matches lmer Residual (random eff) > mean(coef(sleeplmList)[,1]) [1] 251.4051 > mean(coef(sleeplmList)[,2]) [1] 10.46729 > #mean int and slope match lmer Fixed effects results > > #### Random -> varies over units (subj); Fixed -> not vary > # heterogeneity across units > var(coef(sleeplmList)[,1]) [1] 838.3423 > var(coef(sleeplmList)[,2]) [1] 43.01034 > #these are too big as they should be, compare with variance random effects lmer > # mle subtracts off wobble in estimated indiv regressions Y on t > # method of moments - SSR/SST, see Review Question > > ## more useful descriptives using lmList results > quantile(coef(sleeplmList)[,1]) 0% 25% 50% 75% 100% 203.4842 229.4167 258.0576 273.0255 290.1041 > > quantile(coef(sleeplmList)[,2]) 0% 25% 50% 75% 100% -2.881034 6.194548 10.432421 13.548395 21.764702 > > stem(coef(sleeplmList)[,2]) The decimal point is 1 digit(s) to the right of the | -0 | 3 0 | 23 0 | 56699 1 | 011234 1 | 89 2 | 02 > ## can also rename quantities > rate = coef(sleeplmList)[,2] > quantile(rate) 0% 25% 50% 75% 100% -2.881034 6.194548 10.432421 13.548395 21.764702 >