This article contains solutions to exercises for an article in the mixed models series. For a list of topics covered by this series, see the Introduction.

There is often more than one approach to the exercises. Do not be concerned if your approach is different than the solution provided.

#### Exercise solutions

1. Write the R formula for a model with C as a fixed effect and two unobserved random variables. The random variables are random intercepts. One of the random variables is sampled as A, the other is sampled as B.

y ~ C + (1|A) + (1|B) results in the following model parameters
• (intercept) (mean intercept associated with the groups of A and B at C = 0), $$\beta_0$$
• slope effect associated with C, $$\beta_1$$
• variance of intercept associated with the groups of A, $$\theta_1$$
• variance of intercept associated with the groups of B, $$\theta_2$$
• variance of residuals, $$\sigma^2$$
2. What is the mean and variance for both the unconditional y and the conditional y from problem 1?

Y$$_i$$ $$| (\boldsymbol{B} = [b_j \ b_k ] )$$ will have a mean of $$\beta_0 + \beta_1 c_i + b_{1,j[i]} + b_{2,k[i]}$$ with variance of $$\sigma^2$$, where $$c_i$$ is the value of C from the $$i$$th observation, $$j[i]$$ is the group of model variable A that observation $$i$$ is a member of, $$k[i]$$ is the group of model variable B that observation $$i$$ is a member of, $$b_{1,j[i]}$$ is the intercept effect associated with the $$j$$th sample of the model variable A, $$b_{2,k[i]}$$ is the intercept effect associated with the $$j$$th sample of the model variable B.

Y$$_i$$ will have a mean of $$\beta_0 + \beta_1$$c_i with variance of $$\theta_1 + \theta_2 + \sigma^2$$

3. Write the R formula for a model with a random slope for C and a correlated random intercept. The random slope is sampled as A.

y ~ C +(1+C|A) results in the following model parameters
• (intercept) (mean intercept associated with the groups of A at C = 0), $$\beta_0$$
• slope effect associated with C (mean slope associate with the groups of A), $$\beta_1$$
• variance of intercept associated with A, $$\theta_1$$
• variance of slope of C associated with A, $$\theta_2$$
• covariance of the random intercept and the random slope of C within A, $$\theta_3$$
• variance of residuals, $$\sigma^2$$

This could also have been specified by y ~ C + (C|A).

4. What is the mean and variance for both the unconditional y and the conditional y from problem 3?

Y$$_i$$ $$| (\boldsymbol{B} = \boldsymbol{b}_{j[i]})$$ will have a mean of $$\beta_0 + \beta_1 c_i + b_{1,j[i]} + b_{2,j[i]} c_i$$ with variance of $$\sigma^2$$, where $$c_i$$ is the value of C from the $$i$$th observation, $$j[i]$$ is the group of model variable A that observation $$i$$ is a member of, $$b_{1,j[i]}$$ is the intercept effect associated with the $$j$$th sample of the model variable A, $$b_{2,j[i]}$$ is the slope effect associated with the $$j$$th sample of the model variable A. There are two unobserved variables associated with with the population sampled by A. $$\boldsymbol{B}$$ is the random vector $$[ \boldsymbol{b}_1 \ \boldsymbol{b}_2]$$.

Y$$_i$$ will have a mean of $$\beta_0 + \beta_1$$c$$_i$$ with variance of $$\theta_1 + \theta_2 + 2 \theta_3 + \sigma^2$$

Use the sleepstudy data set for the following exercises.

• This data is load using

library(lme4)
Loading required package: Matrix
data(sleepstudy)
1. Create a model for Reaction time regressing on Days and accounting for the random effect of Subject.

ssc <- lmer(Reaction ~ Days + (1 | Subject), data=sleepstudy)
summary(ssc)
Linear mixed model fit by REML ['lmerMod']
Formula: Reaction ~ Days + (1 | Subject)
Data: sleepstudy

REML criterion at convergence: 1786.5

Scaled residuals:
Min      1Q  Median      3Q     Max
-3.2257 -0.5529  0.0109  0.5188  4.2506

Random effects:
Groups   Name        Variance Std.Dev.
Subject  (Intercept) 1378.2   37.12
Residual              960.5   30.99
Number of obs: 180, groups:  Subject, 18

Fixed effects:
Estimate Std. Error t value
(Intercept) 251.4051     9.7467   25.79
Days         10.4673     0.8042   13.02

Correlation of Fixed Effects:
(Intr)
Days -0.371
2. The median reaction time is 288.7. Create variable for aboveAve. You can use the code provided below. Then create a model regressing on this new dichotomous variable

aboveAve <- ifelse(sleepstudy\$Reaction > 288.7, 1, 0)

ssd <- glmer(aboveAve ~ Days + (1 | Subject), data=sleepstudy, family=binomial)
summary(ssd)
Generalized linear mixed model fit by maximum likelihood (Laplace
Approximation) [glmerMod]
Family: binomial  ( logit )
Formula: aboveAve ~ Days + (1 | Subject)
Data: sleepstudy

AIC      BIC   logLik deviance df.resid
158.6    168.2    -76.3    152.6      177

Scaled residuals:
Min      1Q  Median      3Q     Max
-7.5491 -0.3845  0.0003  0.3080  5.2636

Random effects:
Groups  Name        Variance Std.Dev.
Subject (Intercept) 7.951    2.82
Number of obs: 180, groups:  Subject, 18

Fixed effects:
Estimate Std. Error z value Pr(>|z|)
(Intercept)  -3.4601     0.9370  -3.693 0.000222 ***
Days          0.7426     0.1299   5.719 1.07e-08 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Correlation of Fixed Effects:
(Intr)
Days -0.641