# Making various mixed effects models

I've tried to create three models (using R): an intercept only linear regression, a simple mixed effects regression and a by-subject effects mixed effects regression.

An intercept only regression models the grand mean of a response variable plus error. In mtcars, the variable drat may be considered a response variable. In the model below, have I correctly modelled the grand mean of drat, plus error?

interceptOnly <- lm(drat ~ 1, data=mtcars)


A simple mixed effects regression models the grand mean of a response variable, plus subject deviation, plus error. In mtcars, drat may be considered a response variable and cyl a subject deviation. In the model below, have I correctly modelled the grand mean of drat, plus the deviation of cyl from drat, plus error?

library(lme4)
simpleMixedEffects <- lmer(drat ~ (1|cyl), data=mtcars)


A by-subject effects mixed effects regression models the grand mean of a response variable, plus subject deviation, plus condition effect, plus error. In mtcars, drat may be considered a response variable, cyl a subject deviation and wt a condition effect. In the model below, have I correctly modelled the grand mean of drat, plus the deviation of cyl from drat, plus the effect of wt, plus error?

bySubjectMixedEffects <- lmer(drat ~ (1|cyl) + wt, data=mtcars)


I have one further question:

How can I model a by-subject varying condition effect model. This is a mixed effects model which models the grand mean of a response variable, plus group deviation from grand mean (random effect), plus condition effect (fixed effect), plus group deviation from condition effect (random effect), plus error. Could someone provide R code that outputs a "by-subject varying condition effect model"?

• More edits made. How can I further improve question? Jun 18, 2013 at 10:35
• As you press me, my personal view is that a good acceptable question will just cite the video as a reference and be focused on questions that other people might ask or difficulties others might express. This looks too much like "Here are some examples I have tried; are they correct?" There are many questions on mixed effects on this site; I can't see this being one of the most useful. Also, you are presupposing some acquaintance with a particular dataset, mtcars. Jun 18, 2013 at 10:41
• Question further edited to remove any necessity of familiarity with mtcars dataset. Jun 18, 2013 at 10:51

Your example models seem to be correct.

Your final question concerns varying condition effects. You can include a vayring slope of mt in the model with the following command:

lmer(drat ~ wt + (1 + wt|cyl), data=mtcars)


This model estimates an intercept and a slope for wt. These are the fixed effects. Additionally, the model estimates the deviations from these fixed effect for the levels of cyl. These are the random effects of the model. Furthermore, a parameter for the correlation between the two random effects is also estimated by the model. If you want to exclude this correlation parameter, you have to change the command to:

lmer(drat ~ wt + (1|cyl) + (0 + wt|cyl), data=mtcars)


Late to this party but I wanted to point out that mtcars is not a suitable dataset for formulating and fitting linear mixed effects models.