Tuesday, March 18, 2014

Linear Mixed Models in R


The Linear Model
To understand mixed-effects models (or simply mixed models), it is helpful to first revisit the normal linear model. The basic linear model can be expressed with the following equation:

yi = β0 + β1x1i + εi

In this basic model, y is a linear expression of a model’s intercept (β0), regression coefficients (β1x1i) and error (εi). The only random effect in this model is the error term and the predictors represent fixed effects. The terms “random” and “fixed” are often used to mean different things (see Andrew Gelman’s blog citing five different definitions), but we will define fixed effects as effects that (1) do not vary across individuals or units and (2) whose values of interested are fully represented in the data.     

Linear Mixed Models
Mixed models are used when additional random effects are included in a model. This is common when data are non-independent as in the case when data are clustered or longitudinal. A common form of a mixed model involves modeling a random intercept. Random intercepts allow for a model’s intercept to vary by subject or cluster, accounting for the fact that mean levels of some outcome vary significantly by subject/cluster. A random intercept model can be expressed with the following two equations:

yi = β0j + β1xij + εii
β0j = γ00 + u0j

The equation for the intercept is composed of a grand mean (γ00) and some variation around this grand mean (u0j).

A Brief Example in R
The lmer package can be used to run mixed models in R and is fairly straightforward to implement. To illustrate this, we will use an example of data in which we wanted to test the overall effect of a relationship across multiple studies. As such, we wanted to model random intercepts in which intercepts were allowed to vary across studies.

Below is a sample of how data can be set up for linear mixed modeling, with the inclusion of a cluster variable, “Study.”


Once, your data is set up and imported in R, a simple command can run a linear mixed model analysis using the lmer package. We have to label our model (mixedmodel) and define it with the lmer command. The dependent variable (MOL) is being predicted (~) by two fixed variables, (NFC, glorification) and includes a random effect of study.  The (1|Study) specification specifices that we want a random intercept to be modeled for the Study effect. We also specify the appropriate dataframe for use (MergedData) and how to handle missing data (na.action = na.omit). After defining our model, we can review our results.

Commands
>mixedmodel <-lmer(MOL ~ NFC + glorification + (1|Study), MergedData, na.action=na.omit)
>summary(mixedmodel)
Output


Our summary gives us information about our fixed effects (regression coefficients and statistical tests for NFC and glorification) as well as the random effects, or variances, that were estimated as part of the model. This model serves as a simple illustration of how to use the lmer package to estimate linear mixed models in R, but many other variations are possible and addressed in lmer resources.
 

No comments:

Post a Comment