Skip to content

Evaluating Imputation Quality

The previous pages produced completed cohorts and a pooled analysis. This page examines chain traces, observed and imputed distributions, and sensitivity to departures from MAR. Its invented complete cohort also permits comparisons with hidden values and a complete-data regression fit.

mice iterates: each incomplete variable is imputed from the others in turn, and five iterations of that loop make one chain. The trace plot draws the mean of the imputed values per iteration for each of the 20 chains. What you want to see is boring: level noise with no trend and no chain drifting apart from the others.

suppressPackageStartupMessages({
library(ggplot2)
library(mice)
})
dir.create("outputs", showWarnings = FALSE, recursive = TRUE)
complete_data <- read.csv("/opt/data/cohort_complete.csv", check.names = FALSE)
missing_data <- read.csv("/opt/data/cohort_missing.csv", check.names = FALSE)
mask_data <- read.csv("/opt/data/amputation_mask.csv", check.names = FALSE)
factor_levels <- c("low", "medium", "high")
complete_data$activity <- ordered(
complete_data$activity,
levels = factor_levels
)
missing_data$activity <- ordered(
missing_data$activity,
levels = factor_levels
)
complete_data$smoking <- factor(complete_data$smoking, levels = c(0, 1))
missing_data$smoking <- factor(missing_data$smoking, levels = c(0, 1))
predictor_matrix <- make.predictorMatrix(missing_data)
predictor_matrix[, "patient_id"] <- 0
imp <- mice(
missing_data,
m = 20,
maxit = 5,
seed = 20260820,
predictorMatrix = predictor_matrix,
printFlag = FALSE
)
trace_rows <- vector("list", length = 2 * 20 * 5)
trace_index <- 1
for (variable in c("chol", "sbp")) {
for (chain in seq_len(20)) {
for (iteration in seq_len(5)) {
trace_rows[[trace_index]] <- data.frame(
variable = variable,
chain = chain,
iteration = iteration,
chain_mean = imp$chainMean[variable, iteration, chain]
)
trace_index <- trace_index + 1
}
}
}
trace_data <- do.call(rbind, trace_rows)
# A healthy chain shows level noise with no trend across iterations.
trace_plot <- ggplot(
trace_data,
aes(x = iteration, y = chain_mean, group = chain, colour = factor(chain))
) +
geom_line(alpha = 0.7, show.legend = FALSE) +
facet_wrap(~ variable, scales = "free_y") +
labs(x = "Iteration", y = "Mean of imputed values") +
theme_minimal()
ggsave(
"outputs/evaluating_trace_r.png",
plot = trace_plot,
width = 7,
height = 4,
dpi = 150
)

The mean of the imputed values per iteration for cholesterol and systolic blood pressure, one line per chain across five iterations. The twenty chains overlap in a narrow band with no trend, which is what convergence looks like here.

Five iterations are the mice default, and the traces show why that default is enough on this cohort: the chains reach their shared level immediately and stay there. A trend across iterations would mean the chain had not found its level yet, and the remedy would be more iterations, not more data sets.

The second check draws the imputed values against the observed ones. Under MCAR the two densities should coincide. Under MAR they should not: the missing cholesterol belongs to older patients, so the imputed distribution should sit to the right of the observed one. The check is not “are they identical” but “does the difference match the mechanism”.

long_data <- complete(imp, action = "long", include = FALSE)
mask_lookup <- mask_data[match(long_data$patient_id, mask_data$patient_id), ]
long_data$chol_missing <- mask_lookup$chol
density_data <- rbind(
data.frame(
group = "Observed",
chol = missing_data$chol[!mask_data$chol]
),
data.frame(
group = "Imputed",
chol = long_data$chol[long_data$chol_missing]
)
)
density_plot <- ggplot(
density_data,
aes(x = chol, colour = group, fill = group)
) +
geom_density(alpha = 0.25) +
labs(x = "Cholesterol", y = "Density", colour = NULL, fill = NULL) +
theme_minimal()
ggsave(
"outputs/evaluating_density_r.png",
plot = density_plot,
width = 7,
height = 4,
dpi = 150
)
observed_chol <- missing_data$chol[!mask_data$chol]
imputed_chol <- long_data$chol[long_data$chol_missing]
cat(sprintf("Observed chol mean: %.4f\n", mean(observed_chol)))
cat(sprintf("Observed chol SD: %.4f\n", sd(observed_chol)))
cat(sprintf("Imputed chol mean: %.4f\n", mean(imputed_chol)))
cat(sprintf("Imputed chol SD: %.4f\n", sd(imputed_chol)))
Observed chol mean: 4.8782
Observed chol SD: 0.8597
Imputed chol mean: 5.1468
Imputed chol SD: 0.8625

Both engines shift the imputed distribution to the right, 5.1468 in R and 5.2845 in Python against an observed mean of 4.8782, and both keep its shape: the imputed standard deviation is 0.8625 in R and 0.8227 in Python against 0.8597 observed. The shift is the mechanism from the first page doing its work. The mechanisms page measured that cholesterol goes missing on older patients; older patients carry higher cholesterol; the imputation puts that cholesterol back.

The fixture keeps the true values, so this page can compute what no real analysis can: the per-cell error of every imputed value.

mask_lookup <- mask_data[match(long_data$patient_id, mask_data$patient_id), ]
long_data$sbp_missing <- mask_lookup$sbp
long_data$activity_missing <- mask_lookup$activity
long_data$smoking_missing <- mask_lookup$smoking
truth_data <- complete_data[, c(
"patient_id", "activity", "smoking", "chol", "sbp"
)]
names(truth_data)[-1] <- paste0(names(truth_data)[-1], "_true")
mask_for_join <- mask_data[, c(
"patient_id", "activity", "smoking", "chol", "sbp"
)]
names(mask_for_join)[-1] <- paste0(names(mask_for_join)[-1], "_missing")
missing_columns <- names(mask_for_join)[-1]
scoring_data <- long_data[, !names(long_data) %in% missing_columns]
scored_data <- merge(scoring_data, truth_data, by = "patient_id", sort = FALSE)
scored_data <- merge(
scored_data,
mask_for_join,
by = "patient_id",
sort = FALSE
)
chol_errors <- scored_data$chol[scored_data$chol_missing] -
scored_data$chol_true[scored_data$chol_missing]
sbp_errors <- scored_data$sbp[scored_data$sbp_missing] -
scored_data$sbp_true[scored_data$sbp_missing]
activity_match <- scored_data$activity[scored_data$activity_missing] ==
scored_data$activity_true[scored_data$activity_missing]
smoking_match <- scored_data$smoking[scored_data$smoking_missing] ==
scored_data$smoking_true[scored_data$smoking_missing]
mean_chol <- mean(missing_data$chol, na.rm = TRUE)
baseline_errors <- mean_chol - complete_data$chol[mask_data$chol]
cat(sprintf("chol bias: %.4f\n", mean(chol_errors)))
cat(sprintf("chol RMSE: %.4f\n", sqrt(mean(chol_errors ^ 2))))
cat(sprintf("sbp bias: %.4f\n", mean(sbp_errors)))
cat(sprintf("sbp RMSE: %.4f\n", sqrt(mean(sbp_errors ^ 2))))
cat(sprintf("activity accuracy: %.1f%%\n", 100 * mean(activity_match)))
cat(sprintf("smoking accuracy: %.1f%%\n", 100 * mean(smoking_match)))
cat(sprintf(
"Mean-imputation chol RMSE: %.4f\n",
sqrt(mean(baseline_errors ^ 2))
))
chol bias: -0.1874
chol RMSE: 0.9635
sbp bias: -0.5356
sbp RMSE: 16.9025
activity accuracy: 37.8%
smoking accuracy: 60.4%
Mean-imputation chol RMSE: 0.9530

Read the R numbers first, because they carry the lesson. The cholesterol RMSE under mice is 0.9635, and plain mean imputation scored 0.9530: the sophisticated engine lost to the page-2 straw man on per-cell error. That is not a malfunction. Per-cell RMSE is minimised by the conditional mean, and mean imputation is the cheapest conditional mean there is; a proper imputation draws from the conditional distribution instead, which is exactly what restores the spread the mean destroyed. miceforest’s 0.8151 happens to beat both, and activity and smoking accuracy sit near their guess-the-mode baselines in both engines. Per-cell accuracy and valid statistical inference are different objectives. Compare the pooled analysis with the complete-data analysis as well, while keeping the limits of that comparison explicit.

The next table asks whether each pooled interval contains the coefficient fitted to this complete cohort. That fitted coefficient is itself a sample estimate, not the population parameter. Inclusion is a descriptive check on this dataset, not a property that every valid interval must satisfy.

Confidence interval coverage concerns repeated datasets generated under a specified model. To estimate it, repeat the simulation and compare intervals with the known generating parameter. The likelihood lesson demonstrates that distinction for a binomial model.

benchmark_fit <- lm(sbp ~ age + bmi + chol + smoking, data = complete_data)
pooled_fit <- pool(with(imp, lm(sbp ~ age + bmi + chol + smoking)))
pooled_summary <- summary(pooled_fit, conf.int = TRUE)
benchmark <- coef(benchmark_fit)
coverage_table <- data.frame(
term = sub("smoking1", "smoking", pooled_summary$term),
benchmark_estimate = unname(benchmark[pooled_summary$term]),
pooled_estimate = pooled_summary$estimate,
lower_2.5 = pooled_summary[["2.5 %"]],
upper_97.5 = pooled_summary[["97.5 %"]]
)
coverage_table$covered <- with(
coverage_table,
benchmark_estimate >= lower_2.5 & benchmark_estimate <= upper_97.5
)
cat("Interval coverage of the complete-data fit:\n")
print(coverage_table, row.names = FALSE)
Interval coverage of the complete-data fit:
term benchmark_estimate pooled_estimate lower_2.5 upper_97.5 covered
(Intercept) 61.3338564 61.2131164 51.3036533 71.122579 TRUE
age 0.8968146 0.9265704 0.8085546 1.044586 TRUE
bmi 0.7016504 0.6917429 0.3404333 1.043053 TRUE
chol 3.0369699 2.8645593 1.0365709 4.692548 TRUE
smoking 8.9983663 7.9024109 5.0177961 10.787026 TRUE

Every displayed interval contains the corresponding complete-data estimate in this run. The covered column records that inclusion only. It does not estimate nominal coverage or demonstrate that the imputation method is unbiased. A repeated simulation would need specified population parameters, missingness mechanisms, and Monte Carlo uncertainty.

Observed data alone cannot distinguish MAR from every possible MNAR mechanism. The following sensitivity scenario shifts imputed cholesterol by half a mmol/L in each direction and refits the analysis. That range is an illustration, not a clinically validated range. In a study, justify plausible departures using assay knowledge, external data, and the reasons measurements are missing.

completed_sets <- complete(imp, action = "all")
# The zero line is MAR. Other lines price lower or higher missing cholesterol.
for (delta in c(-0.5, 0, 0.5)) {
sensitivity_fits <- vector("list", length(completed_sets))
for (dataset_index in seq_along(completed_sets)) {
sensitivity_data <- completed_sets[[dataset_index]]
sensitivity_data$chol[mask_data$chol] <-
sensitivity_data$chol[mask_data$chol] + delta
sensitivity_fits[[dataset_index]] <- lm(
sbp ~ age + bmi + chol + smoking,
data = sensitivity_data
)
}
sensitivity_summary <- summary(pool(sensitivity_fits))
chol_row <- sensitivity_summary[sensitivity_summary$term == "chol", ]
cat(sprintf(
"delta = %.1f: pooled chol coefficient %.4f (SE %.4f)\n",
delta,
chol_row$estimate,
chol_row$std.error
))
}
delta = -0.5: pooled chol coefficient 2.9594 (SE 0.8819)
delta = 0.0: pooled chol coefficient 2.8646 (SE 0.9261)
delta = 0.5: pooled chol coefficient 2.4499 (SE 0.9183)

The zero-shift row gives the MAR analysis. Increasing imputed cholesterol lowers the displayed cholesterol coefficient in both implementations; decreasing it raises the coefficient. These shifts describe sensitivity under the specified scenario. The example defines no clinical importance threshold and supports no claim that the changes are clinically negligible.

A study report should state the target coefficient, justify the sensitivity range, and explain which scientific conclusion changes across that range. Similar conclusions across selected scenarios do not prove MAR.

All intervals in the table contain the complete-data estimates. Does this demonstrate 95% confidence interval coverage?

It does not. This is one dataset, and its complete-data estimates are not the population parameters. Coverage requires repeated datasets and a specified parameter to assess whether each interval includes it.

Morris, White, and Crowther explains how to evaluate bias and interval coverage in a simulation study. For real data, report the diagnostics, assumptions, and sensitivity analysis without treating an unavailable complete-data answer as a validation requirement.

Morris, White, and Crowther, 2019 define bias and confidence interval coverage across repeated samples from a stated model. They also describe uncertainty from a finite number of simulation repetitions. Their definitions support the distinction between the displayed complete-data inclusion check and a repeated-study coverage estimate.

van Buuren and Groothuis-Oudshoorn, 2011 describe multivariate imputation by chained equations in the mice package.

The miceforest repository is the software reference for chained-equation imputation with LightGBM, because the project names no paper to cite.

Morris, White, and Crowther’s simulation study guide defines bias, confidence interval coverage, and Monte Carlo uncertainty. Use those definitions to distinguish the complete-data comparison here from performance across repeated datasets.

The miceforest ImputationKernel reference documents retained iteration data and imputation settings. Those settings determine which completed datasets are available for the trace and distribution comparisons.