Skip to content

Instantly share code, notes, and snippets.

@baogorek
Last active August 29, 2015 13:56
Show Gist options
  • Save baogorek/9145934 to your computer and use it in GitHub Desktop.
Save baogorek/9145934 to your computer and use it in GitHub Desktop.
Three variations of the same nested model in lmer. See http://lme4.r-forge.r-project.org/book/Ch2.pdf for more detail.
library(lme4)
head(Pastes)
# three casks are nested within each of 10 batches.
# No cask is used twice! (30 total casks)
# Noted that the variable "sample" is a concatination of "batch" and "cask"
lmer1 <- lmer(strength ~ 1 + (1 | batch) + (1 | sample), Pastes, REML=F)
lmer2 <- lmer(strength ~ 1 + (1 | batch) + (1 | batch:cask), Pastes, REML=F)
lmer3 <- lmer(strength ~ 1 + (1 | batch/cask), Pastes, REML = F)
summary(lmer1)
summary(lmer2)
summary(lmer3)
# Leaving out the main effect of batch, notice how the batch:cask variance
# is the sum of the batch and batch:cask variance components from the lmer1-lmer3
lmer2b <- lmer(strength ~ 1 + (1 | batch:cask), Pastes, REML=F)
summary(lmer2b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment