# SCS 2011: Mixed Models with R

### From Wiki1

The March 2012 version of the course is here.

Reload the page with your browser to get the latest version

## 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 yet available from CRAN but can be loaded from R-Forge. The latest beta version can be loaded from this site.

### Installing R

- Install R from CRAN (see R: Getting Started for help)

### Installing spida and p3d

- After installing R, install the 'car' and 'rgl' packages from CRAN. In R, use the command:

install.packages(c('car','rgl'))

- In the future you will probably want to install the 'spida' and 'p3d' packages from R-Forge with the following command in R:

install.packages(c('spida','p3d'), repos = "http://r-forge.r-project.org")

- For this course, you should install beta versions that will be updated as we go along. In R, use the commands:

- On a PC:

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

- On a Mac

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

If you run into problems, have a look at [ http://cran.r-project.org/bin/macosx/RMacOSX-FAQ.html#Installation-of-source-packages 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.

### Source code

The current source code for

## Lecture Slides

### Preliminary material

- Causality and Longitudinal Data Analysis: some very informal notes
- Simple Regression: Visualizing Simple Regression
- Multiple Regression: Visualizing Multiple Regression

### Main topics

- Part 3: Hierarchical Models to Mixed Models
- Part 4: Longitudinal Data Analysis with Mixed Models
- Part 5: Asymptotic Functions of Time Part I (under construction)

- Asymptotic Functions of Time Part II (under construction)

### Additional Notes

#### Additional links for each day

- Day 1:
- Short R script illustrating plotting predicted curves and wald tests: Lab Day 1.R

- Day 3:
- Generalized Linear Mixed Models and Splines
- R script for Generalized Linear Mixed Models and Splines: GLMM.R

- Hierarchical Models: Additional material
- Longitudinal Models: Additional_material
- SAS Mixed Models Course (2006)
- Hierarchical Models to Mixed Models -- with code for graphs

## Labs

- Lab Session 1: Linear Mixed Models
- Lab Session 2: Longitudinal Models
- Lab Session 3: Generalized Linear Mixed Models and Related Topics

### Contents of Labs

#### Lab Session 1: Linear Mixed Models

- First example: Between Sector gap in Math Achievement
- Randomly selecting a subsample of clusters (schools)
- Having a first look at multilevel data
- Creating new Level 2 variables from Level 1 data
- Seeing data in 3d
- A second look at multilevel data: targeted to a model
- Seeing fitted lines in beta space
- Between and within cluster effects
- Fitting a mixed model
- Handling NAs (simplest considerations)
- Non-convergence
- First diagnostics: Hausman test
- Contextual variables to the rescue
- Interpretation of models with contextual effects
- Estimating the compositional (= between) effect
- Alternative equivalent parametrizations for the FE (fixed effects) model.
- Alternative non-equivalent parametrizations for the RE (random effects) model
- Diagnostics based on Level 1 residuals
- Diagnostics based on Level 2 residuals (REs)
- Influence diagnostics
- Plotting the fitted model: hand-made effect plots
- Linking the picture and the numbers
- Formulating and testing linear hypotheses
- Graphs to show confidence bounds for hypotheses

- Second example: Minority status and Math Achievement
- Preliminary diagnostics using Level 1 OLS model
- OLS influence diagnostics
- Scaling Level 1 variables
- Fitting a mixed model
- Dealing with non-convergence
- Building the RE model with a forward stepwise approach
- Simulation to adjust p-values
- Test for contextual effects II
- Simplifying the model
- Using regular expression for easy tests of complex hypotheses
- Some Level 2 diagnostics
- Near-singularity: a pancake in 3D
- Visualizing the model: hand-made effect plots II
- The minority-majority gap
- Comparing different RE models
- More diagnostics
- Marginal and conditional models
- Refining the FE model
- Multilevel R Squared
- Visualizing the model to construct hypotheses

- First example: Between Sector gap in Math Achievement

#### Lab Session 2: Longitudinal Models

- LME model
- Hausman test:
- . Adjusting for time
- Diagnostics: Level 1
- a) Diagnostics for heteroskedasticity
- b) Diagnostics for autocorrelation

- Diagnostics: Level 2
- Dropping observations
- Modeling autocorrelation
- Modeling heteroskedasticity
- Interpreting different kinds of residual plots
- Visualizing the impact of model selection
- Displaying data and fitted values together

#### Lab Session 3: Generalized Linear Mixed Models and Related Topics

- Accelerated Longitudinal Designs and age-period-cohort linear confounding
- Modeling seasonal and periodic effects with Fourier Analysis
- Using general splines to model effect of age or time
- Linear, quadratic, cubic and natural cubic splines
- General spline generator: splines with arbitrary degrees and smoothness
- Defining hypothesis matrices and using Wald tests to explore splines
- Plotting splines and spline features with confidence bounds
- Plotting log-odds or probabilities
- Interpreting hypothesis tests using confidence bounds
- Bonferroni and Scheffe confidence bound adjustment factors
- Testing non-linear cohort effects
- Alternatives to glmmPQL: lmer, glmmML,GLMMGibbs,

## Links

### Special links from each day

- Day 1:
- Short R script illustrating plotting predicted curves and wald tests: Lab Day 1.R

- Day 4:
- R script illustrating estimation of effects using wald tests to estimate effects /Exploring a model.R

### Introductory documents on the web

- John Fox (2002) Linear Mixed Models
- Paul Bliese (2009) Multilevel Modeling in R (2.3): A Brief Introduction to R, the multilevel package and the nlme package

### Books on Mixed Models, Introductory and less introductory

- Paul D. Allison (2005)
*Fixed Effects Regression Methods for Longitudinal Data Using SAS*, SAS Institute.

- Contains a good discussion of the comparison between mixed models and fixed effects models.

- Judith D. Singer and John B. Willett (2003)
*Applied Longitudinal Data Analysis: Modeling Change and Event Occurrence*, Oxford.

- A solid accessible book. The second half deals with the related topic of event history analysis.

- Doug Bates and Jose Pinheiro (2000)
*Mixed-Effects Models in S and S-PLUS*, Springer. - Alain F. Zuur, Elena N. Ieno, Neil J. Walker, Anatoly A. Saveliev, Graham M. Smith (2009)
*Mixed Effects Models and Extensions in Ecology with R*Springer. - Geert Verbeke and Geert Molenberghs (2000)
*Linear Mixed Models for Longitudinal Data.*Springer.