Missing values: how much, where and why
Read missingness patterns and learn the three mechanisms that decide which fixes are safe.
What runs in CodeSplash
- Missing-data patterns and MCAR tests Verified in Python and R
You will be able to
- Measure missingness per column and per row
- Tell MCAR, MAR and MNAR apart in a real scenario
- Explain why dropping incomplete rows can bias a result
Count the gaps
ReadNew York's 1973 air-quality data has 153 days. The column with the most gaps is Ozone, missing on 24.2% of them.
New York air quality, 1973A few percent per column can still cost you many rows, because gaps in different columns land on different rows.
How many complete rows?
PredictPredict first
Ozone is missing on 37 days and solar radiation on 7. Every other column is complete. On how many of the days are all values present?
Make your guess between 0 and 153 days, then reveal.
Reveal
111 days. Two days are missing both values, so the total lost is 42, not 44.
See the pattern
In the appRun missing-data patterns on this dataset. The card lists each combination of gaps and how often it occurs.
χ²(14) = 35.11, p = .001
- Complete rows
- 111
- Patterns
- 4
- Little's MCAR p
- .001
Plots
View data
| Ozone | Solar.R | Wind | Temp | Month | Day |
|---|---|---|---|---|---|
| 41 | 190 | 7.400 | 67 | 5 | 1 |
| 36 | 118 | 8 | 72 | 5 | 2 |
| 12 | 149 | 12.600 | 74 | 5 | 3 |
| 18 | 313 | 11.500 | 62 | 5 | 4 |
| — | — | 14.300 | 56 | 5 | 5 |
| 28 | — | 14.900 | 66 | 5 | 6 |
| 23 | 299 | 8.600 | 65 | 5 | 7 |
| 19 | 99 | 13.800 | 59 | 5 | 8 |
| 8 | 19 | 20.100 | 61 | 5 | 9 |
| — | 194 | 8.600 | 69 | 5 | 10 |
| 7 | — | 6.900 | 74 | 5 | 11 |
| 16 | 256 | 9.700 | 69 | 5 | 12 |
| 11 | 290 | 9.200 | 66 | 5 | 13 |
| 14 | 274 | 10.900 | 68 | 5 | 14 |
| 18 | 65 | 13.200 | 58 | 5 | 15 |
| 14 | 334 | 11.500 | 64 | 5 | 16 |
| 34 | 307 | 12 | 66 | 5 | 17 |
| 6 | 78 | 18.400 | 57 | 5 | 18 |
| 30 | 322 | 11.500 | 68 | 5 | 19 |
| 11 | 44 | 9.700 | 62 | 5 | 20 |
| 1 | 8 | 9.700 | 59 | 5 | 21 |
| 11 | 320 | 16.600 | 73 | 5 | 22 |
| 4 | 25 | 9.700 | 61 | 5 | 23 |
| 32 | 92 | 12 | 61 | 5 | 24 |
| — | 66 | 16.600 | 57 | 5 | 25 |
| — | 266 | 14.900 | 58 | 5 | 26 |
| — | — | 8 | 57 | 5 | 27 |
| 23 | 13 | 12 | 67 | 5 | 28 |
| 45 | 252 | 14.900 | 81 | 5 | 29 |
| 115 | 223 | 5.700 | 79 | 5 | 30 |
| 37 | 279 | 7.400 | 76 | 5 | 31 |
| — | 286 | 8.600 | 78 | 6 | 1 |
| — | 287 | 9.700 | 74 | 6 | 2 |
| — | 242 | 16.100 | 67 | 6 | 3 |
| — | 186 | 9.200 | 84 | 6 | 4 |
| — | 220 | 8.600 | 85 | 6 | 5 |
| — | 264 | 14.300 | 79 | 6 | 6 |
| 29 | 127 | 9.700 | 82 | 6 | 7 |
| — | 273 | 6.900 | 87 | 6 | 8 |
| 71 | 291 | 13.800 | 90 | 6 | 9 |
| 39 | 323 | 11.500 | 87 | 6 | 10 |
| — | 259 | 10.900 | 93 | 6 | 11 |
| — | 250 | 9.200 | 92 | 6 | 12 |
| 23 | 148 | 8 | 82 | 6 | 13 |
| — | 332 | 13.800 | 80 | 6 | 14 |
| — | 322 | 11.500 | 79 | 6 | 15 |
| 21 | 191 | 14.900 | 77 | 6 | 16 |
| 37 | 284 | 20.700 | 72 | 6 | 17 |
| 20 | 37 | 9.200 | 65 | 6 | 18 |
| 12 | 120 | 11.500 | 73 | 6 | 19 |
| 13 | 137 | 10.300 | 76 | 6 | 20 |
| — | 150 | 6.300 | 77 | 6 | 21 |
| — | 59 | 1.700 | 76 | 6 | 22 |
| — | 91 | 4.600 | 76 | 6 | 23 |
| — | 250 | 6.300 | 76 | 6 | 24 |
| — | 135 | 8 | 75 | 6 | 25 |
| — | 127 | 8 | 78 | 6 | 26 |
| — | 47 | 10.300 | 73 | 6 | 27 |
| — | 98 | 11.500 | 80 | 6 | 28 |
| — | 31 | 14.900 | 77 | 6 | 29 |
First 60 of 153 rows.
View data
| Ozone | Solar.R | Wind | Temp | Month | Day |
|---|---|---|---|---|---|
| 41 | 190 | 7.400 | 67 | 5 | 1 |
| 36 | 118 | 8 | 72 | 5 | 2 |
| 12 | 149 | 12.600 | 74 | 5 | 3 |
| 18 | 313 | 11.500 | 62 | 5 | 4 |
| — | — | 14.300 | 56 | 5 | 5 |
| 28 | — | 14.900 | 66 | 5 | 6 |
| 23 | 299 | 8.600 | 65 | 5 | 7 |
| 19 | 99 | 13.800 | 59 | 5 | 8 |
| 8 | 19 | 20.100 | 61 | 5 | 9 |
| — | 194 | 8.600 | 69 | 5 | 10 |
| 7 | — | 6.900 | 74 | 5 | 11 |
| 16 | 256 | 9.700 | 69 | 5 | 12 |
| 11 | 290 | 9.200 | 66 | 5 | 13 |
| 14 | 274 | 10.900 | 68 | 5 | 14 |
| 18 | 65 | 13.200 | 58 | 5 | 15 |
| 14 | 334 | 11.500 | 64 | 5 | 16 |
| 34 | 307 | 12 | 66 | 5 | 17 |
| 6 | 78 | 18.400 | 57 | 5 | 18 |
| 30 | 322 | 11.500 | 68 | 5 | 19 |
| 11 | 44 | 9.700 | 62 | 5 | 20 |
| 1 | 8 | 9.700 | 59 | 5 | 21 |
| 11 | 320 | 16.600 | 73 | 5 | 22 |
| 4 | 25 | 9.700 | 61 | 5 | 23 |
| 32 | 92 | 12 | 61 | 5 | 24 |
| — | 66 | 16.600 | 57 | 5 | 25 |
| — | 266 | 14.900 | 58 | 5 | 26 |
| — | — | 8 | 57 | 5 | 27 |
| 23 | 13 | 12 | 67 | 5 | 28 |
| 45 | 252 | 14.900 | 81 | 5 | 29 |
| 115 | 223 | 5.700 | 79 | 5 | 30 |
| 37 | 279 | 7.400 | 76 | 5 | 31 |
| — | 286 | 8.600 | 78 | 6 | 1 |
| — | 287 | 9.700 | 74 | 6 | 2 |
| — | 242 | 16.100 | 67 | 6 | 3 |
| — | 186 | 9.200 | 84 | 6 | 4 |
| — | 220 | 8.600 | 85 | 6 | 5 |
| — | 264 | 14.300 | 79 | 6 | 6 |
| 29 | 127 | 9.700 | 82 | 6 | 7 |
| — | 273 | 6.900 | 87 | 6 | 8 |
| 71 | 291 | 13.800 | 90 | 6 | 9 |
| 39 | 323 | 11.500 | 87 | 6 | 10 |
| — | 259 | 10.900 | 93 | 6 | 11 |
| — | 250 | 9.200 | 92 | 6 | 12 |
| 23 | 148 | 8 | 82 | 6 | 13 |
| — | 332 | 13.800 | 80 | 6 | 14 |
| — | 322 | 11.500 | 79 | 6 | 15 |
| 21 | 191 | 14.900 | 77 | 6 | 16 |
| 37 | 284 | 20.700 | 72 | 6 | 17 |
| 20 | 37 | 9.200 | 65 | 6 | 18 |
| 12 | 120 | 11.500 | 73 | 6 | 19 |
| 13 | 137 | 10.300 | 76 | 6 | 20 |
| — | 150 | 6.300 | 77 | 6 | 21 |
| — | 59 | 1.700 | 76 | 6 | 22 |
| — | 91 | 4.600 | 76 | 6 | 23 |
| — | 250 | 6.300 | 76 | 6 | 24 |
| — | 135 | 8 | 75 | 6 | 25 |
| — | 127 | 8 | 78 | 6 | 26 |
| — | 47 | 10.300 | 73 | 6 | 27 |
| — | 98 | 11.500 | 80 | 6 | 28 |
| — | 31 | 14.900 | 77 | 6 | 29 |
First 60 of 153 rows.
Missing values by variable
| Variable | Kind | Missing | % |
|---|---|---|---|
| Ozone | numeric | 37 | 24.2% |
| Solar.R | numeric | 7 | 4.6% |
| Wind | numeric | 0 | 0.0% |
| Temp | numeric | 0 | 0.0% |
| Month | numeric | 0 | 0.0% |
| Day | numeric | 0 | 0.0% |
Missingness patterns
| Missing variables | Rows | % | Variables missing |
|---|---|---|---|
| (complete) | 111 | 72.5% | 0 |
| Ozone | 35 | 22.9% | 1 |
| Solar.R | 5 | 3.3% | 1 |
| Ozone, Solar.R | 2 | 1.3% | 2 |
Tests of missing completely at random
| Test | Statistic | df | p | |
|---|---|---|---|---|
| Little's MCAR test | 35.106 | 14 | 0.0014 | small p: not MCAR |
Normality and equal covariances by pattern (Hawkins)
| Missing variables | Rows | Neyman N₄ | p | p from |
|---|
Do rows with missing values differ?
| Missing in | Compared variable | n missing | n observed | Mean when missing | Mean when observed | SMD | |
|---|---|---|---|---|---|---|---|
| Ozone | Solar.R | 37 | 116 | 189.51 | 184.80 | 0.0527 | |
| Ozone | Wind | 37 | 116 | 10.257 | 9.862 | 0.1134 | |
| Ozone | Temp | 37 | 116 | 77.919 | 77.871 | 0.0051 | |
| Ozone | Month | 37 | 116 | 6.351 | 7.198 | −0.6765 | |SMD| > 0.2 |
| Ozone | Day | 37 | 116 | 16.649 | 15.534 | 0.1235 | |
| Solar.R | Ozone | 7 | 146 | 42.800 | 42.099 | 0.0225 | |
| Solar.R | Wind | 7 | 146 | 9 | 10.003 | −0.2678 | |SMD| > 0.2 |
| Solar.R | Temp | 7 | 146 | 73 | 78.116 | −0.4411 | |SMD| > 0.2 |
| Solar.R | Month | 7 | 146 | 6.286 | 7.027 | −0.4921 | |SMD| > 0.2 |
| Solar.R | Day | 7 | 146 | 9.143 | 16.123 | −0.8212 | |SMD| > 0.2 |
- Little's test compares each pattern's means with EM estimates (multivariate normal, numeric variables). A small p is evidence against missing completely at random (MCAR); a large p does not prove MCAR. Complete-case analysis is unbiased only under MCAR.
- SMD > 0.2 in absolute value: rows missing one variable differ on another, so missingness depends on observed data (MAR-like) — consider multiple imputation.
The exact R script · R R version 4.5.1 (2025-06-13)
# Missing-data patterns and MCAR tests
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method missing.patterns 1.1.1 · spec 97315f9f1884aa9d
# Dataset: airquality (version 1)
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
library(codesplashr)
# %% Data (no rows are dropped: missing values are what this analysis is about)
variables <- c("Ozone", "Solar.R", "Wind", "Temp", "Month", "Day")
kinds <- c(
"Ozone" = "numeric",
"Solar.R" = "numeric",
"Wind" = "numeric",
"Temp" = "numeric",
"Month" = "numeric",
"Day" = "numeric"
)
df <- as.data.frame(nanoparquet::read_parquet("tests/parity/fixtures/airquality.parquet", col_select = variables))
cs_check_finite_columns(df, variables) # infinite values stop the run (registry/errors.yaml)
n <- nrow(df)
mcar <- TRUE
mcar_tests <- "little"
min_n <- as.integer(6)
reps <- as.integer(999)
seed <- as.integer(1)
alpha <- 0.05
run_little <- mcar && mcar_tests %in% c("little", "all")
run_hawkins <- mcar && mcar_tests %in% c("hawkins", "jamshidian_jalal", "all")
run_ad <- mcar && mcar_tests %in% c("jamshidian_jalal", "all")
smd_on <- TRUE
max_patterns <- as.integer(20)
warnings <- list()
miss <- is.na(as.matrix(df))
colnames(miss) <- variables
# %% Per variable and patterns
values <- list(n_rows = n, n_vars = length(variables))
var_rows <- list()
for (j in seq_along(variables)) {
v <- variables[j]
k <- sum(miss[, j])
var_rows[[j]] <- list(variable = v, kind = kinds[[v]], n_missing = k, pct_missing = if (n) k / n else NaN)
values[[sprintf("missing[%s]", v)]] <- k
values[[sprintf("pct_missing[%s]", v)]] <- if (n) k / n else NaN
}
complete <- sum(!apply(miss, 1, any))
pats <- cs_missing_patterns(miss)
pat_rows <- list()
for (i in seq_len(nrow(pats))) {
code <- pats$code[i]
missing_vars <- variables[strsplit(code, "")[[1]] == "1"]
values[[sprintf("pattern_n[%s]", code)]] <- pats$n[i]
if (length(pat_rows) < max_patterns) {
pat_rows[[length(pat_rows) + 1]] <- list(pattern = if (length(missing_vars)) paste(missing_vars, collapse = ", ") else "(complete)",
code = code, n = pats$n[i], pct = pats$n[i] / n, n_missing_vars = length(missing_vars))
}
}
counts <- colSums(miss)
worst <- which.max(counts)
values$n_complete <- complete
values$pct_complete <- if (n) complete / n else NaN
values$n_patterns <- nrow(pats)
values$n_cells_missing <- sum(miss)
values$pct_cells_missing <- if (length(miss)) mean(miss) else NaN
values$most_missing <- variables[worst]
values$most_missing_pct <- counts[[worst]] / n
# %% Little's MCAR test (numeric variables; EM estimates of the mean and covariance)
numeric <- Filter(function(v) kinds[[v]] == "numeric" && sum(!is.na(df[[v]])) > 1 && isTRUE(stats::sd(df[[v]], na.rm = TRUE) > 0), variables)
mcar_res <- NULL
if (run_little && length(numeric) >= 2 && n > length(numeric)) {
mcar_res <- cs_little_mcar(as.matrix(df[numeric]))
} else if (run_little && length(numeric) >= 2) {
# With no more rows than variables the estimated covariance matrix is singular.
warnings <- c(warnings, "Little's test needs more rows than numeric variables; it was not run.")
} else if (run_little) {
warnings <- c(warnings, "Little's test needs at least two numeric variables with variation.")
}
values$mcar_done <- !is.null(mcar_res)
values$mcar_vars <- if (is.null(mcar_res)) 0 else length(numeric)
values["mcar_statistic"] <- list(if (is.null(mcar_res)) NULL else mcar_res$statistic)
values["mcar_df"] <- list(if (is.null(mcar_res)) NULL else mcar_res$df)
values["mcar_p"] <- list(if (is.null(mcar_res)) NULL else mcar_res$p)
values["mcar_patterns"] <- list(if (is.null(mcar_res)) NULL else mcar_res$patterns)
# %% Hawkins and Jamshidian–Jalal tests (normality and equal covariances across patterns after one imputation)
jj <- NULL
if (run_hawkins && length(numeric) >= 2) {
jj <- cs_jamshidian_jalal(as.matrix(df[numeric]), min_n, reps, seed, alpha, nonparametric = run_ad)
if (!jj$done) {
warnings <- c(warnings, sprintf("The Hawkins and Jamshidian–Jalal tests were not run: %s.", jj$reason))
jj <- NULL
} else if (jj$n_dropped > 0) {
warnings <- c(warnings, sprintf("%d rows in patterns with fewer than %d rows were left out of the Hawkins and Jamshidian–Jalal tests.", jj$n_dropped, min_n))
}
} else if (run_hawkins) {
warnings <- c(warnings, "The Hawkins and Jamshidian–Jalal tests need at least two numeric variables with variation.")
}
decisions <- c(
consistent = "No evidence against MCAR: after imputation the data look normal with equal covariances across the missingness patterns.",
not_mcar = "Covariances differ across the missingness patterns (Anderson–Darling): evidence against MCAR.",
non_normal = "The data do not look normal, but covariances do not differ across patterns: no evidence against MCAR."
)
has_ad <- !is.null(jj) && !is.null(jj$ad_statistic)
values$hawkins_done <- !is.null(jj)
values["hawkins_statistic"] <- list(if (is.null(jj)) NULL else jj$hawkins_statistic)
values["hawkins_df"] <- list(if (is.null(jj)) NULL else jj$hawkins_df)
values["hawkins_p"] <- list(if (is.null(jj)) NULL else jj$hawkins_p)
values["hawkins_patterns"] <- list(if (is.null(jj)) NULL else jj$n_patterns)
values["hawkins_n"] <- list(if (is.null(jj)) NULL else jj$n_used)
values$ad_done <- has_ad
values["ad_statistic"] <- list(if (has_ad) jj$ad_statistic else NULL)
values["ad_p"] <- list(if (has_ad) jj$ad_p else NULL)
values["jj_decision"] <- list(if (has_ad) jj$decision else NULL)
jj_rows <- list()
if (!is.null(jj)) {
for (g in jj$groups) {
flags <- strsplit(g$code, "")[[1]] == "1"
values[[sprintf("neyman_n4[%s]", g$code)]] <- g$n4
values[[sprintf("neyman_p[%s]", g$code)]] <- g$p
jj_rows[[length(jj_rows) + 1]] <- list(pattern = if (any(flags)) paste(numeric[flags], collapse = ", ") else "(complete)",
n = g$n, n4 = g$n4, p = g$p, method = if (g$n < 30) "Monte-Carlo" else "χ²(4)")
}
}
test_rows <- list()
if (!is.null(mcar_res)) {
test_rows[[length(test_rows) + 1]] <- list(test = "Little's MCAR test", statistic = mcar_res$statistic, df = mcar_res$df, p = mcar_res$p,
reading = "small p: not MCAR")
}
if (!is.null(jj)) {
test_rows[[length(test_rows) + 1]] <- list(test = "Hawkins: normality and equal covariances", statistic = jj$hawkins_statistic,
df = jj$hawkins_df, p = jj$hawkins_p, reading = "Fisher's combination of the pattern p-values")
if (has_ad) {
test_rows[[length(test_rows) + 1]] <- list(test = "Jamshidian–Jalal: equal covariances (k-sample Anderson–Darling)", statistic = jj$ad_statistic,
df = NULL, p = jj$ad_p, reading = sprintf("permutation p (%s relabelings)", format(reps, big.mark = ",")))
}
}
# %% Missingness vs other variables (SMD)
smd_rows <- list()
if (smd_on) {
for (v in variables) {
r <- is.na(df[[v]])
if (sum(r) < 2 || sum(!r) < 2) next
for (w in variables) {
if (w == v || kinds[[w]] != "numeric") next
xw <- as.numeric(df[[w]])
d <- cs_missing_smd(xw[r], xw[!r])
if (!is.finite(d)) next
values[[sprintf("smd[%s|%s]", v, w)]] <- d
smd_rows[[length(smd_rows) + 1]] <- list(missing = v, variable = w, n_missing = sum(r), n_observed = sum(!r),
mean_missing = mean(xw[r], na.rm = TRUE), mean_observed = mean(xw[!r], na.rm = TRUE),
smd = d, flag = if (abs(d) > 0.2) "|SMD| > 0.2" else "")
}
}
}
values$n_smd_flagged <- sum(vapply(smd_rows, function(s) abs(s$smd) > 0.2, logical(1)))
# %% Result
rows <- if (n) unique(round(seq(1, n, length.out = min(n, 5000)))) else integer(0)
chart_cols <- lapply(variables, function(v) {
if (kinds[[v]] == "numeric") cs_col(v, as.numeric(df[[v]][rows]), kind = "numeric") else cs_col(v, as.character(df[[v]][rows]), kind = "categorical")
})
cs_publish(list(
method = "missing.patterns",
methodVersion = "1.1.1",
title = "Missing-data patterns",
engine = cs_engine_info("stats", "nanoparquet"),
n = n,
summary = paste0(sprintf("%d of %d rows are complete; %d missingness patterns.", complete, n, nrow(pats)),
if (!is.null(mcar_res)) sprintf(" Little's MCAR test: χ²(%d) = %.2f, %s.", mcar_res$df, mcar_res$statistic, cs_fmt_p(mcar_res$p)) else ""),
apa = if (!is.null(mcar_res)) sprintf("χ²(%d) = %s, %s", mcar_res$df, cs_fmt_num(mcar_res$statistic), cs_fmt_p(mcar_res$p)) else "",
statistics = c(list(list(key = "n_complete", label = "Complete rows", value = complete),
list(key = "n_patterns", label = "Patterns", value = nrow(pats)),
list(key = "mcar_p", label = "Little's MCAR p", value = if (is.null(mcar_res)) NULL else mcar_res$p, format = "p")),
if (!is.null(jj)) list(list(key = "hawkins_p", label = "Hawkins p", value = jj$hawkins_p, format = "p")),
if (has_ad) list(list(key = "ad_p", label = "Jamshidian–Jalal p", value = jj$ad_p, format = "p"))),
tables = list(
list(key = "variables", title = "Missing values by variable", columns = list(
list(key = "variable", label = "Variable", type = "string"), list(key = "kind", label = "Kind", type = "string"),
list(key = "n_missing", label = "Missing", type = "integer"), list(key = "pct_missing", label = "%", type = "percent")), rows = var_rows),
list(key = "patterns", title = "Missingness patterns", columns = list(
list(key = "pattern", label = "Missing variables", type = "string"), list(key = "n", label = "Rows", type = "integer"),
list(key = "pct", label = "%", type = "percent"), list(key = "n_missing_vars", label = "Variables missing", type = "integer")),
rows = pat_rows, note = if (nrow(pats) > max_patterns) sprintf("The %d most common patterns.", max_patterns) else NULL),
list(key = "mcar_tests", title = "Tests of missing completely at random", columns = list(
list(key = "test", label = "Test", type = "string"), list(key = "statistic", label = "Statistic", type = "number"),
list(key = "df", label = "df", type = "integer"), list(key = "p", label = "p", type = "p"), list(key = "reading", label = "", type = "string")),
rows = test_rows, note = if (has_ad) unname(decisions[jj$decision]) else NULL),
list(key = "neyman", title = "Normality and equal covariances by pattern (Hawkins)", columns = list(
list(key = "pattern", label = "Missing variables", type = "string"), list(key = "n", label = "Rows", type = "integer"),
list(key = "n4", label = "Neyman N₄", type = "number"), list(key = "p", label = "p", type = "p"), list(key = "method", label = "p from", type = "string")),
rows = jj_rows, note = if (!is.null(jj)) sprintf("After one normal imputation from the EM estimate (seed %d); uniform tail probabilities mean normal data with equal covariances.", seed) else NULL),
list(key = "smd", title = "Do rows with missing values differ?", columns = list(
list(key = "missing", label = "Missing in", type = "string"), list(key = "variable", label = "Compared variable", type = "string"),
list(key = "n_missing", label = "n missing", type = "integer"), list(key = "n_observed", label = "n observed", type = "integer"),
list(key = "mean_missing", label = "Mean when missing", type = "number"), list(key = "mean_observed", label = "Mean when observed", type = "number"),
list(key = "smd", label = "SMD", type = "number"), list(key = "flag", label = "", type = "string")), rows = smd_rows)
),
effectSizes = list(),
assumptions = list(),
warnings = warnings,
notes = Filter(Negate(is.null), list(
paste("Little's test compares each pattern's means with EM estimates (multivariate normal, numeric variables). A small p is evidence against",
"missing completely at random (MCAR); a large p does not prove MCAR. Complete-case analysis is unbiased only under MCAR."),
"SMD > 0.2 in absolute value: rows missing one variable differ on another, so missingness depends on observed data (MAR-like) — consider multiple imputation.",
if (!is.null(jj)) paste("Hawkins and Jamshidian–Jalal: patterns are imputed once from the EM estimate. Hawkins' test rejects when the data are not normal or covariances differ",
"across patterns; the nonparametric Anderson–Darling test separates the two. A rejection is about this imputed data set — repeat with another seed to see how stable it is.")
)),
charts = list(cs_chart("matrix", "missing-matrix", "Missing values by row", list(vars = chart_cols)),
cs_chart("patterns", "upset", "Missingness patterns", list(vars = chart_cols))),
values = values
))
χ²(14) = 35.11, p = .001
- Complete rows
- 111
- Patterns
- 4
- Little's MCAR p
- .001
To keep this page light, the plots are shown once, under the R tab.
Missing values by variable
| Variable | Kind | Missing | % |
|---|---|---|---|
| Ozone | numeric | 37 | 24.2% |
| Solar.R | numeric | 7 | 4.6% |
| Wind | numeric | 0 | 0.0% |
| Temp | numeric | 0 | 0.0% |
| Month | numeric | 0 | 0.0% |
| Day | numeric | 0 | 0.0% |
Missingness patterns
| Missing variables | Rows | % | Variables missing |
|---|---|---|---|
| (complete) | 111 | 72.5% | 0 |
| Ozone | 35 | 22.9% | 1 |
| Solar.R | 5 | 3.3% | 1 |
| Ozone, Solar.R | 2 | 1.3% | 2 |
Tests of missing completely at random
| Test | Statistic | df | p | |
|---|---|---|---|---|
| Little's MCAR test | 35.106 | 14 | 0.0014 | small p: not MCAR |
Normality and equal covariances by pattern (Hawkins)
| Missing variables | Rows | Neyman N₄ | p | p from |
|---|
Do rows with missing values differ?
| Missing in | Compared variable | n missing | n observed | Mean when missing | Mean when observed | SMD | |
|---|---|---|---|---|---|---|---|
| Ozone | Solar.R | 37 | 116 | 189.51 | 184.80 | 0.0527 | |
| Ozone | Wind | 37 | 116 | 10.257 | 9.862 | 0.1134 | |
| Ozone | Temp | 37 | 116 | 77.919 | 77.871 | 0.0051 | |
| Ozone | Month | 37 | 116 | 6.351 | 7.198 | −0.6765 | |SMD| > 0.2 |
| Ozone | Day | 37 | 116 | 16.649 | 15.534 | 0.1235 | |
| Solar.R | Ozone | 7 | 146 | 42.800 | 42.099 | 0.0225 | |
| Solar.R | Wind | 7 | 146 | 9 | 10.003 | −0.2678 | |SMD| > 0.2 |
| Solar.R | Temp | 7 | 146 | 73 | 78.116 | −0.4411 | |SMD| > 0.2 |
| Solar.R | Month | 7 | 146 | 6.286 | 7.027 | −0.4921 | |SMD| > 0.2 |
| Solar.R | Day | 7 | 146 | 9.143 | 16.123 | −0.8212 | |SMD| > 0.2 |
- Little's test compares each pattern's means with EM estimates (multivariate normal, numeric variables). A small p is evidence against missing completely at random (MCAR); a large p does not prove MCAR. Complete-case analysis is unbiased only under MCAR.
- SMD > 0.2 in absolute value: rows missing one variable differ on another, so missingness depends on observed data (MAR-like) — consider multiple imputation.
The exact Python script · Python Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0
# Missing-data patterns and MCAR tests
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method missing.patterns 1.1.1 · spec 97315f9f1884aa9d
# Dataset: airquality (version 1)
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
import numpy as np
import pandas as pd
import codesplash as cs
from codesplash.stats import missing as csm
# %% Data (no rows are dropped: missing values are what this analysis is about)
variables = ["Ozone", "Solar.R", "Wind", "Temp", "Month", "Day"]
kinds = {"Day":"numeric","Month":"numeric","Ozone":"numeric","Solar.R":"numeric","Temp":"numeric","Wind":"numeric"}
df = pd.read_parquet("tests/parity/fixtures/airquality.parquet", columns=variables)
cs.check_finite_columns(df, variables) # infinite values stop the run (registry/errors.yaml)
n = len(df)
mcar = True
mcar_tests = "little"
min_n = int(6)
reps = int(999)
seed = int(1)
alpha = 0.05
run_little = mcar and mcar_tests in ("little", "all")
run_hawkins = mcar and mcar_tests in ("hawkins", "jamshidian_jalal", "all")
run_ad = mcar and mcar_tests in ("jamshidian_jalal", "all")
smd_on = True
max_patterns = int(20)
warnings = []
miss = df.isna().to_numpy()
# %% Per variable and patterns
values = {"n_rows": n, "n_vars": len(variables)}
var_rows = []
for j, v in enumerate(variables):
k = int(miss[:, j].sum())
var_rows.append({"variable": v, "kind": kinds[v], "n_missing": k, "pct_missing": k / n if n else float("nan")})
values[f"missing[{v}]"] = k
values[f"pct_missing[{v}]"] = k / n if n else float("nan")
complete = int((~miss.any(axis=1)).sum())
pats = csm.patterns(miss)
pat_rows = []
for code, count in pats:
missing_vars = [v for v, c in zip(variables, code) if c == "1"]
values[f"pattern_n[{code}]"] = count
if len(pat_rows) < max_patterns:
pat_rows.append({"pattern": ", ".join(missing_vars) if missing_vars else "(complete)", "code": code, "n": count,
"pct": count / n, "n_missing_vars": len(missing_vars)})
worst = max(var_rows, key=lambda r: (r["n_missing"], -variables.index(r["variable"])))
values.update({
"n_complete": complete, "pct_complete": complete / n if n else float("nan"), "n_patterns": len(pats),
"n_cells_missing": int(miss.sum()), "pct_cells_missing": float(miss.mean()) if miss.size else float("nan"),
"most_missing": worst["variable"], "most_missing_pct": worst["pct_missing"],
})
# %% Little's MCAR test (numeric variables; EM estimates of the mean and covariance)
numeric = [v for v in variables if kinds[v] == "numeric" and df[v].notna().sum() > 1 and df[v].std(ddof=1) > 0]
mcar_res = None
if run_little and len(numeric) >= 2 and n > len(numeric):
mcar_res = csm.little_mcar(df[numeric].to_numpy(dtype=float))
elif run_little and len(numeric) >= 2:
# With no more rows than variables the estimated covariance matrix is singular.
warnings.append("Little's test needs more rows than numeric variables; it was not run.")
elif run_little:
warnings.append("Little's test needs at least two numeric variables with variation.")
values.update({
"mcar_done": mcar_res is not None, "mcar_vars": len(numeric) if mcar_res else 0,
"mcar_statistic": None if mcar_res is None else mcar_res["statistic"], "mcar_df": None if mcar_res is None else mcar_res["df"],
"mcar_p": None if mcar_res is None else mcar_res["p"], "mcar_patterns": None if mcar_res is None else mcar_res["patterns"],
})
# %% Hawkins and Jamshidian–Jalal tests (normality and equal covariances across patterns after one imputation)
jj = None
if run_hawkins and len(numeric) >= 2:
jj = csm.jamshidian_jalal(df[numeric].to_numpy(dtype=float), min_n, reps, seed, alpha, nonparametric=run_ad)
if not jj["done"]:
warnings.append(f"The Hawkins and Jamshidian–Jalal tests were not run: {jj['reason']}.")
jj = None
elif jj["n_dropped"]:
warnings.append(f"{jj['n_dropped']} rows in patterns with fewer than {min_n} rows were left out of the Hawkins and Jamshidian–Jalal tests.")
elif run_hawkins:
warnings.append("The Hawkins and Jamshidian–Jalal tests need at least two numeric variables with variation.")
DECISIONS = {
"consistent": "No evidence against MCAR: after imputation the data look normal with equal covariances across the missingness patterns.",
"not_mcar": "Covariances differ across the missingness patterns (Anderson–Darling): evidence against MCAR.",
"non_normal": "The data do not look normal, but covariances do not differ across patterns: no evidence against MCAR.",
}
values.update({
"hawkins_done": jj is not None, "hawkins_statistic": jj["hawkins_statistic"] if jj else None, "hawkins_df": jj["hawkins_df"] if jj else None,
"hawkins_p": jj["hawkins_p"] if jj else None, "hawkins_patterns": jj["n_patterns"] if jj else None, "hawkins_n": jj["n_used"] if jj else None,
"ad_done": bool(jj and "ad_statistic" in jj), "ad_statistic": jj.get("ad_statistic") if jj else None, "ad_p": jj.get("ad_p") if jj else None,
"jj_decision": jj.get("decision") if jj else None,
})
jj_rows = []
if jj:
for g in jj["groups"]:
missing_vars = [v for v, c in zip(numeric, g["code"]) if c == "1"]
values[f"neyman_n4[{g['code']}]"] = g["n4"]
values[f"neyman_p[{g['code']}]"] = g["p"]
jj_rows.append({"pattern": ", ".join(missing_vars) if missing_vars else "(complete)", "n": g["n"], "n4": g["n4"], "p": g["p"],
"method": "Monte-Carlo" if g["n"] < 30 else "χ²(4)"})
test_rows = []
if mcar_res:
test_rows.append({"test": "Little's MCAR test", "statistic": mcar_res["statistic"], "df": mcar_res["df"], "p": mcar_res["p"],
"reading": "small p: not MCAR"})
if jj:
test_rows.append({"test": "Hawkins: normality and equal covariances", "statistic": jj["hawkins_statistic"], "df": jj["hawkins_df"],
"p": jj["hawkins_p"], "reading": "Fisher's combination of the pattern p-values"})
if "ad_statistic" in jj:
test_rows.append({"test": "Jamshidian–Jalal: equal covariances (k-sample Anderson–Darling)", "statistic": jj["ad_statistic"], "df": None,
"p": jj["ad_p"], "reading": f"permutation p ({reps:,} relabelings)"})
# %% Missingness vs other variables (SMD)
smd_rows = []
if smd_on:
for v in variables:
r = df[v].isna().to_numpy()
if r.sum() < 2 or (~r).sum() < 2:
continue
for w in variables:
if w == v or kinds[w] != "numeric":
continue
xw = df[w].to_numpy(dtype=float)
d = csm.smd(xw[r], xw[~r])
if not np.isfinite(d):
continue
values[f"smd[{v}|{w}]"] = d
smd_rows.append({"missing": v, "variable": w, "n_missing": int(r.sum()), "n_observed": int((~r).sum()),
"mean_missing": float(np.nanmean(xw[r])), "mean_observed": float(np.nanmean(xw[~r])), "smd": d,
"flag": "|SMD| > 0.2" if abs(d) > 0.2 else ""})
values["n_smd_flagged"] = sum(1 for s in smd_rows if abs(s["smd"]) > 0.2)
# %% Result
rows = np.unique(np.round(np.linspace(0, n - 1, min(n, 5000))).astype(int)) if n else np.array([], dtype=int)
def chart_col(v):
s = df[v].iloc[rows]
if kinds[v] == "numeric":
return cs.col(v, [None if pd.isna(x) else float(x) for x in s], kind="numeric")
return cs.col(v, [None if pd.isna(x) else str(x) for x in s], kind="categorical")
chart_cols = [chart_col(v) for v in variables]
cs.publish({
"method": "missing.patterns",
"methodVersion": "1.1.1",
"title": "Missing-data patterns",
"engine": cs.engine_info("pandas", "numpy", "scipy"),
"n": n,
"summary": f"{complete} of {n} rows are complete; {len(pats)} missingness patterns."
+ (f" Little's MCAR test: χ²({mcar_res['df']}) = {mcar_res['statistic']:.2f}, {cs.fmt_p(mcar_res['p'])}." if mcar_res else ""),
"apa": f"χ²({mcar_res['df']}) = {cs.fmt_num(mcar_res['statistic'])}, {cs.fmt_p(mcar_res['p'])}" if mcar_res else "",
"statistics": [{"key": "n_complete", "label": "Complete rows", "value": complete},
{"key": "n_patterns", "label": "Patterns", "value": len(pats)},
{"key": "mcar_p", "label": "Little's MCAR p", "value": None if mcar_res is None else mcar_res["p"], "format": "p"}]
+ ([{"key": "hawkins_p", "label": "Hawkins p", "value": jj["hawkins_p"], "format": "p"}] if jj else [])
+ ([{"key": "ad_p", "label": "Jamshidian–Jalal p", "value": jj["ad_p"], "format": "p"}] if jj and "ad_p" in jj else []),
"tables": [
{"key": "variables", "title": "Missing values by variable", "columns": [
{"key": "variable", "label": "Variable", "type": "string"}, {"key": "kind", "label": "Kind", "type": "string"},
{"key": "n_missing", "label": "Missing", "type": "integer"}, {"key": "pct_missing", "label": "%", "type": "percent"}], "rows": var_rows},
{"key": "patterns", "title": "Missingness patterns", "columns": [
{"key": "pattern", "label": "Missing variables", "type": "string"}, {"key": "n", "label": "Rows", "type": "integer"},
{"key": "pct", "label": "%", "type": "percent"}, {"key": "n_missing_vars", "label": "Variables missing", "type": "integer"}],
"rows": pat_rows, "note": f"The {max_patterns} most common patterns." if len(pats) > max_patterns else None},
{"key": "mcar_tests", "title": "Tests of missing completely at random", "columns": [
{"key": "test", "label": "Test", "type": "string"}, {"key": "statistic", "label": "Statistic", "type": "number"},
{"key": "df", "label": "df", "type": "integer"}, {"key": "p", "label": "p", "type": "p"}, {"key": "reading", "label": "", "type": "string"}],
"rows": test_rows, "note": DECISIONS.get(jj.get("decision")) if jj else None},
{"key": "neyman", "title": "Normality and equal covariances by pattern (Hawkins)", "columns": [
{"key": "pattern", "label": "Missing variables", "type": "string"}, {"key": "n", "label": "Rows", "type": "integer"},
{"key": "n4", "label": "Neyman N₄", "type": "number"}, {"key": "p", "label": "p", "type": "p"}, {"key": "method", "label": "p from", "type": "string"}],
"rows": jj_rows, "note": f"After one normal imputation from the EM estimate (seed {seed}); uniform tail probabilities mean normal data with equal covariances." if jj else None},
{"key": "smd", "title": "Do rows with missing values differ?", "columns": [
{"key": "missing", "label": "Missing in", "type": "string"}, {"key": "variable", "label": "Compared variable", "type": "string"},
{"key": "n_missing", "label": "n missing", "type": "integer"}, {"key": "n_observed", "label": "n observed", "type": "integer"},
{"key": "mean_missing", "label": "Mean when missing", "type": "number"}, {"key": "mean_observed", "label": "Mean when observed", "type": "number"},
{"key": "smd", "label": "SMD", "type": "number"}, {"key": "flag", "label": "", "type": "string"}], "rows": smd_rows},
],
"effectSizes": [],
"assumptions": [],
"warnings": warnings,
"notes": [
"Little's test compares each pattern's means with EM estimates (multivariate normal, numeric variables). A small p is evidence against "
"missing completely at random (MCAR); a large p does not prove MCAR. Complete-case analysis is unbiased only under MCAR.",
"SMD > 0.2 in absolute value: rows missing one variable differ on another, so missingness depends on observed data (MAR-like) — consider multiple imputation.",
] + (["Hawkins and Jamshidian–Jalal: patterns are imputed once from the EM estimate. Hawkins' test rejects when the data are not normal or covariances differ "
"across patterns; the nonparametric Anderson–Darling test separates the two. A rejection is about this imputed data set — repeat with another seed to see how stable it is."] if jj else []),
"charts": [cs.chart("matrix", "missing-matrix", "Missing values by row", {"vars": chart_cols}),
cs.chart("patterns", "upset", "Missingness patterns", {"vars": chart_cols})],
"values": values,
})
In Insights (⌘5) the Statistician page has the same view under Missing-value patterns. The Charts page draws it three ways:
View data
| age | region | income | last_purchase | satisfaction | nps | comments |
|---|---|---|---|---|---|---|
| 41 | North | 93,500 | 1,769,731,200,000 | 3 | 4 | Great value for a small team, would recommend |
| — | East | 36,700 | 1,708,300,800,000 | — | — | — |
| 51 | East | 138,100 | 1,771,891,200,000 | 4 | 7 | — |
| 39 | North | 32,600 | 1,769,472,000,000 | — | — | — |
| 42 | East | 74,100 | 1,765,497,600,000 | 4 | 8 | Checkout was quick but delivery took longer than promised |
| 32 | West | 56,700 | — | 4 | 5 | Support solved my billing issue within the hour |
| 39 | East | 148,500 | 1,727,913,600,000 | — | — | — |
| 58 | North | 125,800 | 1,756,598,400,000 | 4 | 9 | — |
| 82 | South | 144,800 | 1,721,260,800,000 | 4 | 9 | — |
| 58 | North | — | 1,785,283,200,000 | 4 | 6 | — |
| 53 | South | — | 1,747,267,200,000 | 1 | 1 | — |
| 33 | South | 30,100 | — | 3 | 5 | — |
| 50 | North | 51,500 | 1,791,676,800,000 | 3 | 4 | — |
| 44 | West | 73,800 | 1,728,518,400,000 | 4 | 10 | Search results are often not relevant to my query |
| 26 | North | 15,800 | 1,726,704,000,000 | 3 | 8 | — |
| 28 | West | 36,500 | — | — | — | — |
| 58 | East | 43,100 | 1,726,185,600,000 | — | — | — |
| — | East | 17,100 | 1,738,454,400,000 | — | — | — |
| 25 | East | 30,600 | 1,737,936,000,000 | 3 | 7 | Love the new dashboard, export to Excel still feels slow |
| 27 | South | 31,800 | 1,796,515,200,000 | 3 | 5 | — |
| 44 | East | 79,600 | 1,729,555,200,000 | 2 | 2 | — |
| 50 | West | 58,000 | 1,785,715,200,000 | 4 | 7 | — |
| 26 | North | 42,200 | 1,714,780,800,000 | 2 | 3 | — |
| 41 | South | 29,000 | 1,750,204,800,000 | 5 | 10 | Checkout was quick but delivery took longer than promised |
| 29 | South | 28,100 | 1,737,072,000,000 | 3 | 5 | — |
| 37 | South | 146,500 | 1,774,396,800,000 | 2 | 5 | Great value for a small team, would recommend |
| 44 | East | 34,600 | — | 2 | 3 | Mobile app crashes when uploading large files |
| 39 | East | 63,500 | 1,762,819,200,000 | — | — | — |
| 48 | South | 51,800 | — | 3 | 5 | Checkout was quick but delivery took longer than promised |
| 32 | North | 30,800 | 1,767,139,200,000 | — | — | — |
| 45 | West | 61,900 | 1,777,075,200,000 | 3 | 5 | Pricing is confusing when switching between plans |
| 30 | East | — | — | 3 | 7 | — |
| 35 | South | 46,800 | 1,716,940,800,000 | — | — | — |
| 49 | South | 24,400 | 1,796,169,600,000 | — | — | — |
| 36 | North | 17,600 | 1,734,739,200,000 | — | — | — |
| 18 | North | — | 1,727,136,000,000 | — | — | — |
| 37 | North | 44,900 | 1,760,054,400,000 | — | — | — |
| 34 | North | 61,900 | 1,763,251,200,000 | 3 | 7 | — |
| 53 | West | 47,800 | 1,710,720,000,000 | 4 | 10 | — |
| 26 | West | 28,800 | 1,788,566,400,000 | — | — | — |
| 38 | East | 68,600 | 1,757,203,200,000 | — | — | — |
| 27 | East | 49,700 | 1,740,182,400,000 | 4 | 10 | Love the new dashboard, export to Excel still feels slow |
| 71 | West | 56,400 | — | 3 | 6 | Support solved my billing issue within the hour |
| 44 | West | 90,200 | 1,739,750,400,000 | 3 | 5 | Love the new dashboard, export to Excel still feels slow |
| 41 | South | — | 1,741,132,800,000 | — | — | — |
| 20 | North | 25,100 | 1,775,692,800,000 | 3 | 7 | — |
| 63 | North | 16,500 | — | 4 | 10 | — |
| 45 | North | 92,900 | 1,740,528,000,000 | — | — | — |
| 43 | South | 30,300 | 1,760,659,200,000 | — | — | — |
| 32 | West | 14,400 | 1,753,488,000,000 | 4 | 7 | — |
| 35 | East | 35,100 | — | 2 | 7 | Love the new dashboard, export to Excel still feels slow |
| 43 | North | 15,100 | 1,768,521,600,000 | 4 | 7 | — |
| 39 | North | 24,500 | 1,742,083,200,000 | 4 | 8 | Pricing is confusing when switching between plans |
| 44 | North | 83,500 | 1,770,940,800,000 | 5 | 9 | Great value for a small team, would recommend |
| 50 | South | 23,800 | — | 4 | 9 | Support solved my billing issue within the hour |
| 38 | South | 39,200 | 1,776,470,400,000 | 3 | 7 | Great value for a small team, would recommend |
| 33 | South | 80,000 | 1,719,446,400,000 | — | — | — |
| 36 | South | 48,300 | — | — | — | — |
| 36 | West | 77,900 | 1,722,124,800,000 | 5 | 8 | Checkout was quick but delivery took longer than promised |
| 45 | South | — | 1,765,843,200,000 | — | — | — |
First 60 of 600 rows.
Three reasons a value is missing
Read| Mechanism | Missingness depends on | Example |
|---|---|---|
| MCARMissing completely at randomMissingness unrelated to any value, observed or not; complete-case analysis stays unbiased. | nothing related to the data | a sensor fails at random |
| MARMissing at randomMissingness explained by observed variables; imputation that uses them can remove the bias. | other values you observed | older people skip an online item, and you recorded age |
| MNARMissing not at randomMissingness depends on the missing values themselves (e.g. high earners skip income); needs sensitivity analysis. | the missing value itself | high earners hide income |
The mechanism decides which fixes are safe, and it's a claim about the world. You usually need to know how the data were collected.
Why dropping rows can bias
ReflectDropping incomplete rows is safe under MCAR: you lose precision, not accuracy. Under MAR or MNAR, the rows you keep differ from the rows you lose.
Check your understanding
CheckpointCheck yourself
A depression survey has blanks on the severity item, mostly from the most severely depressed people. Which mechanism is this?
Show the answer
Answer: c
The probability of a blank depends on the unobserved severity. Fixes that assume MAR will understate severity.
Check yourself
Ozone is missing on 37 days and solar radiation on 7. How many cells are missing in total?
Show the answer
Answer: 44
Cells add up even when they share a row: 37 + 7.
Check yourself
Your data show that blood pressure is more often missing for younger patients. You recorded age. Which statement is fair?
Show the answer
Answer: a
A link with age rules out MCAR. It can't show that missingness doesn't also depend on the unseen blood pressure, because that value is what you don't have.
Recap
Recap- Count gaps per column and per row: they don't add up the way you'd expect.
- MCAR, MAR and MNAR describe why values are missing, and that decides what is safe.
- Dropping rows is only harmless under MCAR, and the data alone can't tell you MAR from MNAR.
Concepts in this lesson