SCS 2011: Mixed Models with R

From Wiki1

Jump to: navigation, search

The March 2012 version of the course is here.

Reload the page with your browser to get the latest version


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

Installing spida and p3d

  • After installing R, install the 'car' and 'rgl' packages from CRAN. In R, use the command:
  • In the future you will probably want to install the 'spida' and 'p3d' packages from R-Forge with the following command in R:
        repos = "")
  • 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("", "")
install.packages("", repos = NULL)

download.file("", "")
install.packages("", repos = NULL)
  • On a Mac
download.file("", "spida.beta.tar.gz")
install.packages("spida.beta.tar.gz", repos = NULL)

download.file("", "p3d.beta.tar.gz")
install.packages("p3d.beta.tar.gz", repos = NULL)

If you run into problems, have a look at [ 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("", "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

  • spida.beta: [1]
  • p3d.beta: [2]

Lecture Slides

Preliminary material

Main topics

Asymptotic Functions of Time Part II (under construction)

Additional Notes

Additional links for each day


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

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,


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

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.


Research and more advanced methodology

Sources of multilevel and longitudinal data

Web links

Personal tools