---
webr:
packages: [dplyr, ggplot2, mice]
resources: [R/setup.R, data/adults.rds, data/full.rds]
cell-options:
autorun: true
timelimit: 300
---
{{< include ./_extensions/r-wasm/live/_knitr.qmd >}}
# What to put in the imputation model {#sec-model}
```{webr}
#| include: false
source("R/setup.R")
suppressPackageStartupMessages(library(mice))
```
Whether multiple imputation works correctly depends on the **imputation model** (what missing values are predicted from).
In this chapter we check, with three examples, which variables should go into the imputation model.
## Principle 1: always include the outcome
When an explanatory variable (BMI) is missing and is being imputed, some people hesitate over **whether it is acceptable to use the outcome (blood pressure) for prediction**, because it feels like "peeking at the answer".
Let us actually compare using it and not using it. We make 50% of BMI missing at random (MCAR).
```{webr}
set.seed(1)
d <- full[c("Age", "Gender", "BMI", "BPSysAve")]
d$BMI <- make_missing(full$BMI, 0.5)
pm_without <- make.predictorMatrix(d)
pm_without["BMI", "BPSysAve"] <- 0 # do not use blood pressure to impute BMI
imp_with <- mice(d, m = 10, seed = 1, printFlag = FALSE)
imp_without <- mice(d, m = 10, seed = 1, printFlag = FALSE, predictorMatrix = pm_without)
bmi_coef <- function(imp) {
s <- summary(pool(with(imp, lm(BPSysAve ~ Age + BMI + Gender))))
unlist(s[s$term == "BMI", c("estimate", "std.error")])
}
rbind(before_missing = summary(lm(BPSysAve ~ Age + BMI + Gender, full))$coef["BMI", 1:2],
complete_case = summary(lm(BPSysAve ~ Age + BMI + Gender, d))$coef["BMI", 1:2],
imputed_with_BP = bmi_coef(imp_with),
imputed_without_BP = bmi_coef(imp_without)) |> round(3)
```
When imputed without blood pressure, the BMI coefficient is about **halved**.
The imputed BMI values are generated **independently of** blood pressure, so the relationship between BMI and blood pressure is diluted in proportion to the number of imputed people.
::: {.callout-important}
## Always include the outcome in the imputation model
It is not cheating. The imputation model exists to fill in missing values **while preserving the relationships among variables**.
If the outcome is left out, the relationship between the missing explanatory variable and the outcome is **weakened toward 0**. In this sense, mice's default (using all variables) is the correct setting.
:::
## Principle 2: include variables related to missingness (auxiliary variables)
Suppose missingness in blood pressure is determined by **home blood pressure** (people with normal home blood pressure are less likely to have their blood pressure measured at the check-up).
Home blood pressure is not in the model we want to analyse (blood pressure ~ age + BMI + sex). Such a **variable that is not used in the analysis but helps with imputation** is called an **auxiliary variable**.
```{webr}
set.seed(2)
s <- new_sample(2000)[c("Age", "Gender", "BMI", "BPSysAve", "HomeBP")]
s$BPSysAve <- make_missing(s$BPSysAve, miss_prob(-s$HomeBP, 0.4)) # the lower the home BP, the more missing
imp_noaux <- mice(s[c("Age", "Gender", "BMI", "BPSysAve")], m = 10, seed = 1, printFlag = FALSE)
imp_aux <- mice(s, m = 10, seed = 1, printFlag = FALSE) # also use home BP
mean_pool <- function(imp) unlist(summary(pool(with(imp, lm(BPSysAve ~ 1))))[, c("estimate", "std.error")])
rbind(complete_case = c(mean(s$BPSysAve, na.rm = TRUE), NA),
without_auxiliary = mean_pool(imp_noaux),
with_auxiliary = mean_pool(imp_aux)) |> round(2)
c(true_mean = truth_mean)
```
- Multiple imputation without the auxiliary variable is **biased** almost as much as complete-case analysis. Age, BMI, and sex alone cannot explain the missing-data mechanism (home blood pressure). From the standpoint of age, BMI, and sex, the mechanism is **MNAR**
- Including home blood pressure as an auxiliary variable **almost eliminates the bias**. Conditional on home blood pressure, missingness is random (MAR)
::: {.callout-tip}
## Choosing auxiliary variables
- Variables **related to whether values are missing** (frequency of visits, health status, socioeconomic status…)
- Variables **related to the values of the missing variable** (the same thing measured by another method or at another time). Besides reducing bias, they also improve precision
- Variables related to **both** missingness and the values are the most important
- As a rule of thumb, include variables correlated at 0.4 or more with the variable that has missing values. Too many variables make imputation unstable, so limit them to a few dozen at most
- **Measuring likely auxiliary variables** at the study-planning stage is the most powerful remedy for missing data
:::
## Principle 3: include the relationships used in the analysis in the imputation model too
If the analysis examines an **interaction** (e.g. whether the relationship between age and blood pressure differs by sex), the imputation model must also include the interaction.
Here we create a blood pressure (`BP2`) whose age slope is 0.30 larger in men, and make 40% of it missing.
```{webr}
set.seed(3)
s <- new_sample(5000)
s$BP2 <- round(95 + 0.30 * s$Age + 0.30 * s$Age * (s$Gender == "male") - 8 * (s$Gender == "male") +
0.25 * s$BMI + rnorm(nrow(s), 0, 15)) # the true interaction is 0.30
s <- s[c("Age", "Gender", "BMI", "BP2")]
before <- unname(coef(lm(BP2 ~ Age * Gender + BMI, s))["Age:Gendermale"]) # estimate before creating missingness
s$BP2 <- make_missing(s$BP2, 0.4) # 40% missing at random
int_coef <- function(imp) {
x <- summary(pool(with(imp, lm(BP2 ~ Age * Gender + BMI))))
x$estimate[x$term == "Age:Gendermale"]
}
# (a) mice default (ignores the interaction)
imp_a <- mice(s, m = 10, seed = 1, printFlag = FALSE)
# (b) add the interaction as a separate variable (just another variable)
s_b <- s; s_b$AgeMale <- s_b$Age * (s_b$Gender == "male")
imp_b <- mice(s_b, m = 10, seed = 1, printFlag = FALSE)
# (c) impute separately by sex and combine afterwards
imp_c <- rbind(mice(subset(s, Gender == "female"), m = 10, seed = 1, printFlag = FALSE),
mice(subset(s, Gender == "male"), m = 10, seed = 1, printFlag = FALSE))
c(before_missing = before, complete_case = unname(coef(lm(BP2 ~ Age * Gender + BMI, s))["Age:Gendermale"]),
default = int_coef(imp_a), interaction_as_variable = int_coef(imp_b), imputed_by_sex = int_coef(imp_c)) |> round(3)
```
With mice's default, the interaction is estimated as **too small**. The imputation model fills in values assuming "the age slope is the same for men and women", so the sex difference is diluted in proportion to the number of imputed people.
Adding the interaction as a variable, or imputing separately by sex, brings the estimate close to the pre-missingness value (missingness in this example is MCAR, so complete-case analysis is also unbiased).
::: {.callout-warning}
## Match the imputation model to the analysis model
- If the analysis uses **interactions, squared terms, or transformations** (log, etc.), include them in the imputation model too
- If you analyse by group, **imputing by group** is simple and reliable
- For **variables computed from other variables**, like BMI, either impute height and weight and then compute it (passive imputation, written in mice's `method` as `"~ I(Weight / (Height / 100)^2)"`), or impute BMI itself as a single variable
- The imputation model may be **broader** than the analysis model (containing more variables and relationships). If it is narrower, results are biased
:::
## Try it
::: {.callout-note}
## Practice
1. In the Principle 1 example, check whether the coefficient is also weakened when BMI missingness is **determined by age** (MAR) and blood pressure is not used for imputation
2. In the Principle 2 example, how does the standard error of the mean differ with and without the auxiliary variable?
:::
```{webr}
#| autorun: false
# 1.
set.seed(1)
d2 <- full[c("Age", "Gender", "BMI", "BPSysAve")]
d2$BMI <- make_missing(full$BMI, miss_prob(full$Age, 0.5))
rbind(with_BP = bmi_coef(mice(d2, m = 10, seed = 1, printFlag = FALSE)),
without_BP = bmi_coef(mice(d2, m = 10, seed = 1, printFlag = FALSE, predictorMatrix = pm_without))) |> round(3)
# 2.
rbind(without_auxiliary = mean_pool(imp_noaux), with_auxiliary = mean_pool(imp_aux)) |> round(3)
```
::: {.callout-tip collapse="true"}
## Answers
1. Even under MAR, imputing without blood pressure weakens the BMI coefficient. Regardless of the missing-data mechanism, include the outcome in the imputation model
2. With the auxiliary variable, the standard error is slightly smaller. Home blood pressure predicts the missing blood pressure well, so the uncertainty of imputation (the between-imputation variance B) is smaller
:::
::: {.callout-important}
## Chapter summary
- Always include the **outcome** in the model used to impute explanatory variables. Otherwise relationships are weakened
- Including **auxiliary variables** related to missingness reduces bias and improves precision. Measure them at the study-planning stage
- Include the **interactions and non-linear relationships** used in the analysis in the imputation model too (add them as variables, or impute by group)
- The imputation model may be broader than the analysis model
:::