Skip to content
Prev 821 / 20628 Next

Simulating linear mixed models - the Venables approach

On 4/9/08, Hank Stevens <HStevens at muohio.edu> wrote:
I find the use of within, expand.grid and indexing on the random
effects to be elegant.  Actually, on reexamining the code I think that
Bill should change the first call to rnorm to have mean = 0, not mean
= fph.

The code for the simulation, data display and fit could be collapsed to

library(lme4)
set.seed(7658943)
reH <- rnorm(90, sd = sqrt(0.0002))
dta <- within(data.frame(hospid = gl(90,80)),
              fpi1 <- reH[hospid] + rnorm(length(hospid), 0.4, sqrt(0.04)))
dotplot(reorder(hospid, fpi1) ~ fpi1, dta)
(fm1 <- lmer(fpi1 ~ 1|hospid, dta, verb = TRUE))