# Mixed Models with R

### From Wiki1

**Under construction**

## Contents |

## Installing R and the spida and p3d packages

The 'spida' and 'p3d' packages have been developed specifically to implement some of the methods and techniques that will be used in this course. The packages are not available from CRAN but can be loaded from a local server.

- Install R and RStudio (see R: Getting Started for help)
- If you would like to explore online tutorials, have a look at R: Tutorials and Courses
- Install spida.
- Optionally install p3d.

Installation of R and these packages only needs to be done once initially -- and repeated periodically to get updates. At each R session, you load the packages with the R commands:

library(spida) library(p3d)

## Slides

- Part 1: Introduction and Overview
- Part 2: Hierarchical Models to Mixed Models
- Part 3: Longitudinal Models
- Part 4: Non-linear Models (updated May 27)
- Part 5: General Parametric Splines
- Part 6: Missing Data
- Part 7: GLMMs

## Labs

- Lab 1 Introduction to Mixed Models I
- Lab 2 Introduction to Mixed Models II
- Lab 3 Miscellaneous topics and intro to GLMMs
- Lab 4 Missing Data
- Lab 5 Introduction to GLMMs with MCMC

## Notes

You can discuss questions about this course on our blog. Use the category 'Mixed Models' for postings.

### Package Comparisons

Function Notes lme

in package nlme

Linear mixed effects: normal response

G side and R side modelling

Model syntax:

Y ~ X * W, random = ~ 1 + X | id

For nested effect:

Y ~ X * W, random = ~ 1 + X | higher/lower

or

Y ~ X * W, random = list(higher = ~ 1, lower = ~ 1 + X )

lmer

in package lme4

Linear mixed models for gaussian response with Laplace approximation

G side modeling only, R = σ

^{2}*I*

Model syntax:

Y ~ X * W +(1+X|id)

For nested effect:

Y ~ X * W +(1+X|higher) + (1+X|higher:lower)

For crossed effect:

Y ~ X * W +(1+X|id1) + (1+X|id2)

glmer

in package lme4

Generalized linear mixed models with adaptive Gaussian quadrature

- family: binomial, Gamma, inverse.gaussian, poisson, gaussian

G side only, no R side

Model syntax:

Y ~ X * W +(1+X|id)

For nested effect:

Y ~ X * W +(1+X|higher) + (1+X|higher:lower)

For crossed effect:

Y ~ X * W +(1+X|id1) + (1+X|id2)

glmmPQL

in packages MASS/nlme

Generalized linear mixed models with Penalized Quasi Likelihood

- family: binomial, Gamma, inverse.gaussian, poisson, gaussian

G side and R side as in lme

Model syntax:

Y ~ X * W, random = ~ 1 + X | id

For nested effect:

Y ~ X * W, random = ~ 1 + X | higher/lower

MCMCglmm

in package MCMCglmm

Generalized linear mixed models with MCMC

- family: poisson, categorical, multinomial, ordinal, exponential, geometric, cengaussian, cenpoisson,

cenexponential, zipoisson, zapoisson, ztpoisson, hupoisson, zibinomial (cen=censored, zi=zero-inflated, za=zero-altered, hu=hurdle

G side and R side, R side different from 'lme': no autocorrelation but can be used for multivariate response

Note: 'poisson' potentially overdispersed by default (good), 'binomial' variance for binary variables is unidentified.

Model syntax:

Y ~ X * W, random = ~ us(1 + X):id [Note: id should be a factor, us=unstructured]

For nested effect:

Y ~ X * W, random = ~us(1 + X):higher + us(1 + X):higher:lower

For crossed effect:

Y ~ X * W, random = ~us(1 + X):id1+ us(1 + X):id2

- family: binomial, Gamma, inverse.gaussian, poisson, gaussian

## References

- Tom Snijders and Roel Bosker (2012)
*Multilevel Analysis*(2nd ed.), Sage. web page and data sets - Richard J. Murnane, John B. Willett (2011)
*Methods Matter: Improving Causal Inference in Educational and Social Science Research*Oxford. [http://www.ats.ucla.edu/stat/examples/methods_matter/default.htm data files - Stef van Buuren and Karin Groothuis-Oudshoorn (2011) "mice: Multivariate Imputation by Chained Equations in R",
*Journal of Statistical Software*URL - Jarrod Hadfield (2012)
*MCMCglmm Course Notes*.

## Topics

- /G and R models: package comparisons
- /Priors for variance parameters
- /MCMCglmm
- /Fixed versus random effects
- /Using mixed models instead of ANOVA

## Scripts

- /Multivariate Mixed Models.Rmd
- /glmmPQL.Rmd
- /Mixed Models with Overdispersed Poisson Reponses.Rmd
- /Brief Introdution to MCMCglmm.Rmd

## Links

- A screen capture recording of the last lecture
- Introduction to Mixed Models
- /lme4
- Integrate with Multilevel/Mixed Models
- Additional publicly available resource on mixed models (notes, lecture slides, etc.,) can be found here

See