# Mixed Models with R

Under construction

## 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.

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)
```

## 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 = σ2I
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

• 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
```