# SPIDA 2012: Mixed Models with R

### From Wiki1

**Quick Links**

- Links
- SPIDA home page
- Installing R packages for SPIDA (this is the script used for the lab at York adapted for your personal computer)
- SPIDA lab material (this zipped .zip file contains the SPIDA folder that SPIDA lab participants already copied in the lab)

**Recordings**

**Contents**

## 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. For SPIDA 2012, you should use the latest development versions that can be loaded as shown below.

### Installing R

- Install R from CRAN (see R: Getting Started for help)
- If you would like to explore online tutorials, have a look at R: Tutorials and Courses

### Installing spida and p3d

#### On a PC

# install some required packages and their dependencies install.packages(c('car','rgl','Hmisc','mgcv')) # to install 'spida' download.file("http://blackwell.math.yorku.ca/R/spida.zip", "spida.zip") install.packages("spida.zip", repos = NULL) # to install 'p3d' download.file("http://blackwell.math.yorku.ca/R/p3d.zip", "p3d.zip") install.packages("p3d.zip", repos = NULL)

The commands above only need to be used occasionally to update your version of the packages or when you have installed a new version of R.

At each R session, you load the packages with the R commands:

library(spida) library(p3d)

#### On a Mac

On a Mac, you need to install these packages using the shell (Terminal in Mac)

- First, you need to install developer tools - that is Xcode 3.1 for Mac OS X 10.5 (Leopard) or Xcode 3.2 for Mac OS X 10.6 (Snow Leopard) or Xcode 4 for Mac OS X 10.7 (Lion) Xcode is present on the OS X installation DVD or can be downloaded from http://developer.apple.com/mac/. Xcode 4 is also available for free in the App Store. (If you want Xcode 3.1 or 3.2 but don't want to join Apple Developer, it is available other places if you are a bit resourceful.)
- Note: to install 'p3ddev', you must have R 2.15 or a later version.

## The following 3 lines only need to be run when you start using R and after ## installing a new version of R. ## They install the packages 'spidadev' and 'p3d' in your R libraries ## so they are available to be loaded with the library command. install.packages(c('car','Hmisc','rgl','mgcv')) path <- .libPaths()[1] download.file("http://blackwell.math.yorku.ca/R/spida.tar.gz", paste(path,"/spida.tar.gz", sep="")) download.file("http://blackwell.math.yorku.ca/R/p3d.tar.gz", paste(path,"/p3d.tar.gz", sep="") ) .libPaths()[1]

- This last line of code will print out the file path to the newly downloaded files. Copy the whole path from R.
- Next, go outside of R to
**Finder -> Applications -> Utilities -> Terminal**- In the terminal window type: cd <paste file path>
- In the terminal window type: R CMD INSTALL spida.tar.gz
- In the terminal window type: R CMD INSTALL p3d.tar.gz

The commands above only need to be used occasionally to update your version of the packages or when you have installed a new version of R.

At each R session, you load the packages with the R commands:

library(spida) # note that a number of other packages get loaded at the same time library(p3d)

If you run into problems, have a look at this MacOSX help file. Please let me know if you find a solution.

Here's a very simple package for testing purposes on a Mac:

download.file("http://www.math.yorku.ca/people/georges/Files/R/spida.test.tar.gz", "spida.test.tar.gz") install.packages("spida.test.tar.gz", repos = NULL)

After installing try:

> library(spida.test) > spida.test() [1] "spida.test seems to have been installed correctly"

Let me know whether it worked.

## Day 1

## Day 2

## Day 3

## Day 4

## Day 5

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

## 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
- Lab 2
- Lab 3 Miscellaneous topics and intro to GLMMs
- Lab 4 Missing Data
- Lab 5 Introduction to GLMMs with MCMC

## References

- Tom Snijders and Roel Bosker (2012)
*Multilevel Analysis*(2nd ed.), Sage. web page and data sets - 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*.