library(knitr) set.seed(123) options(width=87) opts_chunk$set(background="#ffffff", comment="#", collapse=FALSE, fig.width=9, fig.height=9, warning=FALSE, message=FALSE)

This vignette illustrates the use of `mitml`

for the treatment of missing data at Level 2.
Specifically, the vignette addresses the following topics:

- Specification of the two-level imputation model for missing data at both Level 1 and 2
- Running the imputation procedure

Further information can be found in the other vignettes and the package documentation.

For purposes of this vignette, we make use of the `leadership`

data set, which contains simulated data from 750 employees in 50 groups including ratings on job satisfaction, leadership style, and work load (Level 1) as well as cohesion (Level 2).

The package and the data set can be loaded as follows.

library(mitml) data(leadership)

In the `summary`

of the data, it becomes visible that all variables are affected by missing data.

```
summary(leadership)
```

The following data segment illustrates this fact, including cases with missing data at Level 1 (e.g., job satisfaction) and 2 (e.g., cohesion).

```
leadership[73:78,]
```

In the following, we will employ a two-level model to address missing data at both levels simultaneously.

The specification of the two-level model, involves two components, one pertaining to the variables at each level of the sample (Goldstein, Carpenter, Kenward, & Levin, 2009; for further discussion, see also Enders, Mister, & Keller, 2016; Grund, Lüdtke, & Robitzsch, in press).

Specifically, the imputation model is specified as a list with two components, where the first component denotes the model for the variables at Level 1, and the second component denotes the model for the variables at Level 2.

For example, using the `formula`

interface, an imputation model targeting all variables in the data set can be written as follows.

fml <- list( JOBSAT + NEGLEAD + WLOAD ~ 1 + (1|GRPID) , # Level 1 COHES ~ 1 ) # Level 2

The first component of this list includes the three target variables at Level 1 and a fixed (`1`

) as well as a random intercept (`1|GRPID`

).
The second component includes the target variable at Level 2 with a fixed intercept (`1`

).

From a statistical point of view, this specification corresponds to the following model
$$
\begin{aligned}
\mathbf{y}*{1ij} &= \boldsymbol\mu*{1} + \mathbf{u}*{1j} + \mathbf{e}*{ij} \
\mathbf{y}*{2j} &= \boldsymbol\mu*{2} + \mathbf{u}*{1j} \; ,
\end{aligned}
$$
where $\mathbf{y}*{1ij}$ denotes the target variables at Level 1, $\mathbf{y}_{2j}$ the target variables at Level 2, and the right-hand side of the model contains the fixed effects, random effects, and residual terms as mentioned above.

Note that, even though the two components of the model appear to be separated, they define a single (joint) model for all target variables at both Level 1 and 2. Specifically, this model employs a two-level covariance structure, which allows for relations between variables at both Level 1 (i.e., correlated residuals at Level 1) and 2 (i.e., correlated random effects residuals at Level 2).

Because the data contain missing values at both levels, imputations will be generated with `jomoImpute`

(and not `panImpute`

).
Except for the specification of the two-level model, the syntax is the same as in applications with missing data only at Level 1.

Here, we will run 5,000 burn-in iterations and generate 20 imputations, each 250 iterations apart.

imp <- jomoImpute(leadership, formula = fml, n.burn = 5000, n.iter = 250, m = 20)

By looking at the `summary`

, we can then review the imputation procedure and verify that the imputation model converged.

```
summary(imp)
```

Due to the greater complexity of the two-level model, the output includes more information than in applications with missing data only at Level 1.
For example, the output features the model specification for variables at both Level 1 and 2.
Furthermore, it provides convergence statistics for the additional regression coefficients for the target variables at Level 2 (i.e., `Beta2`

).

Finally, it also becomes visible that the two-level model indeed allows for relations between target variables at Level 1 and 2.
This can be seen from the fact that the potential scale reduction factor ($\hat{R}$) for the covariance matrix at Level 2 (`Psi`

) was largest for `Psi[4,3]`

, which is the covariance between cohesion and the random intercept of work load.

The completed data sets can then be extracted with `mitmlComplete`

.

implist <- mitmlComplete(imp, "all")

When inspecting the completed data, it is easy to verify that the imputations for variables at Level 2 are constant within groups as intended, thus preserving the two-level structure of the data.

```
implist[[1]][73:78,]
```

Enders, C. K., Mistler, S. A., & Keller, B. T. (2016). Multilevel multiple imputation: A review and evaluation of joint modeling and chained equations imputation. *Psychological Methods*, *21*, 222–240. doi: 10.1037/met0000063 (Link)

Goldstein, H., Carpenter, J. R., Kenward, M. G., & Levin, K. A. (2009). Multilevel models with multivariate mixed response types. *Statistical Modelling*, *9*, 173–197. doi: 10.1177/1471082X0800900301 (Link)

Grund, S., Lüdtke, O., & Robitzsch, A. (2018). Multiple imputation of missing data for multilevel models: Simulations and recommendations. *Organizational Research Methods*, *21*(1), 111–149. doi: 10.1177/1094428117703686 (Link)

cat("Author: Simon Grund (simon.grund@uni-hamburg.de)\nDate: ", as.character(Sys.Date()))

**Any scripts or data that you put into this service are public.**

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.