SCS 2012: Mixed Models with R

From Wiki1

Jump to: navigation, search

This page and the files it links to are under construction

Week 1
March 8
Week 2
March 15
Week 3
March 21
Week 4
March 28

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. The latest developement versions can be loaded from this site as discussed below.

Installing R

Installing spida and p3d

  • After installing R, install the 'car', 'rgl' and 'Hmisc' packages from CRAN. In R, use the command:
    install.packages(c('car','rgl','Hmisc'))
  • 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 development versions that will be updated as we go along. In R, use the commands:

On a PC

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

# to install 'spidadev'
download.file("http://blackwell.math.yorku.ca/R/spidadev.zip", "spidadev.zip")
install.packages("spidadev.zip", repos = NULL)

# to install 'p3ddev'
download.file("http://blackwell.math.yorku.ca/R/p3ddev.zip", "p3ddev.zip")
install.packages("p3ddev.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(spidadev)
library(p3ddev)

On a Mac

On a Mac do the following: (this is not fully debugged, please report problems if you encounter them):

Currently, it seems 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.)
## 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 'p3ddev in your R libraries
## so they are available to be loaded with the library command.
 install.packages(c('car','Hmisc','rgl')) 
 download.file("http://blackwell.math.yorku.ca/R/spidadev.tar.gz", "spidadev.tar.gz")
 download.file("http://blackwell.math.yorku.ca/R/p3ddev.tar.gz", "p3ddev.tar.gz")
Next, go outside of R to Finder -> Applications -> Utilities -> Terminal
  • In the terminal window type: R CMD INSTALL spidadev.tar.gz
  • In the terminal window type: R CMD INSTALL p3ddev.tar.gz

Each time you use R, load the packages with:

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

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.

Access to the internet

Participants external to York will be able to connect with the internet through the York wireless network. Temporary accounts and passwords will be distributed at the first meeting of the course.

Source code

Recent source code for

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

If anyone would like, I can arrange to make the current code available.

Lecture Slides

Preliminary material

Main topics

Asymptotic Functions of Time Part II (under construction)

Additional Notes

Additional links for each day

Labs

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,

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

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.

Courses

Research and more advanced methodology

Sources of multilevel and longitudinal data

Web links

Personal tools