# Missing data: MCAR, MAR and MNAR
# https://tidyecology.com/posts/missing-data-mechanisms-mcar-mar-mnar/
#
# The R code of this Tidy Ecology tutorial, chunk by chunk, in the order of the post.
# Run it top to bottom in a fresh R session. Code the post shows but does not run
# is commented out. The prose, and the numbers it quotes, are in the post itself.
# Post last modified: 2026-09-30

## ---- setup ----
library(ggplot2); library(dplyr); library(tidyr)
ink <- "#2b2b2b"; grid_col <- "#d9d5cc"; paper <- "#f5f4ee"
col_full <- "#6f6f6f"; col_obs <- "#356170"
pal <- c(MCAR = "#4a7a8c", MAR = "#c08a3e", MNAR = "#a24b4b")
theme_te <- function(base = 12) {
  theme_minimal(base_size = base) +
    theme(panel.grid.minor = element_blank(),
          panel.grid.major = element_line(colour = grid_col, linewidth = 0.3),
          plot.background = element_rect(fill = paper, colour = NA),
          panel.background = element_rect(fill = paper, colour = NA),
          strip.text = element_text(face = "bold", colour = ink),
          plot.title = element_text(face = "bold", colour = ink),
          axis.title = element_text(colour = ink),
          legend.position = "top")
}

## ---- dgp ----
set.seed(6813)
n <- 600; b0 <- 5; b1 <- 2; sde <- 2
x <- rnorm(n, 0, 1)
y <- b0 + b1 * x + rnorm(n, 0, sde)
full_mean  <- mean(y)
full_slope <- unname(coef(lm(y ~ x))[2])

## impose each mechanism on y, targeting about 40% missing
p_mcar <- rep(0.40, n)                                   # unrelated to anything
a1 <- 1.2                                                # MAR: depends on observed x
a0 <- uniroot(function(a) mean(plogis(a + a1 * x)) - 0.40, c(-5, 5))$root
p_mar <- plogis(a0 + a1 * x)
ys <- as.numeric(scale(y)); g1 <- 1.2                    # MNAR: depends on y itself
g0 <- uniroot(function(g) mean(plogis(g + g1 * ys)) - 0.40, c(-5, 5))$root
p_mnar <- plogis(g0 + g1 * ys)

blank <- function(p) { yo <- y; yo[runif(n) < p] <- NA; yo }
yo_mcar <- blank(p_mcar); yo_mar <- blank(p_mar); yo_mnar <- blank(p_mnar)

cc_mean  <- function(yo) mean(yo, na.rm = TRUE)
cc_slope <- function(yo) { ok <- !is.na(yo); unname(coef(lm(yo[ok] ~ x[ok]))[2]) }

## ---- cc-mean ----
round(c(full = full_mean,
        MCAR = cc_mean(yo_mcar),
        MAR  = cc_mean(yo_mar),
        MNAR = cc_mean(yo_mnar)), 3)

## ---- cc-slope ----
round(c(full = full_slope,
        MCAR = cc_slope(yo_mcar),
        MAR  = cc_slope(yo_mar),
        MNAR = cc_slope(yo_mnar)), 3)

## ---- fig-densities ----
mk <- function(yo, nm) rbind(
  data.frame(y = y, set = "Full data", mech = nm),
  data.frame(y = yo[!is.na(yo)], set = "Observed only", mech = nm))
df1 <- rbind(mk(yo_mcar, "MCAR"), mk(yo_mar, "MAR"), mk(yo_mnar, "MNAR"))
df1$mech <- factor(df1$mech, levels = c("MCAR", "MAR", "MNAR"))
mu1 <- df1 |> group_by(mech, set) |> summarise(m = mean(y), .groups = "drop")
ggplot(df1, aes(y, colour = set, fill = set)) +
  geom_density(alpha = 0.18, linewidth = 0.7) +
  geom_vline(data = mu1, aes(xintercept = m, colour = set),
             linetype = "dashed", linewidth = 0.5, show.legend = FALSE) +
  facet_wrap(~mech) +
  scale_colour_manual(values = c("Full data" = col_full, "Observed only" = col_obs)) +
  scale_fill_manual(values = c("Full data" = col_full, "Observed only" = col_obs)) +
  labs(title = "Complete-case deletion shifts the response distribution",
       subtitle = "MCAR leaves it in place; MAR and MNAR move it",
       x = "Response y", y = "Density", colour = NULL, fill = NULL) +
  theme_te()

## ---- monte-carlo ----
M <- 2000
res <- matrix(NA, M, 6,
  dimnames = list(NULL, c("mean_MCAR", "mean_MAR", "mean_MNAR",
                          "slope_MCAR", "slope_MAR", "slope_MNAR")))
cov <- res
set.seed(20240613)
for (i in 1:M) {
  xx <- rnorm(n, 0, 1); yy <- b0 + b1 * xx + rnorm(n, 0, sde)
  yys <- as.numeric(scale(yy))
  a0i <- uniroot(function(a) mean(plogis(a + a1 * xx)) - 0.40, c(-6, 6))$root
  g0i <- uniroot(function(g) mean(plogis(g + g1 * yys)) - 0.40, c(-6, 6))$root
  ms <- list(MCAR = runif(n) < 0.40,
             MAR  = runif(n) < plogis(a0i + a1 * xx),
             MNAR = runif(n) < plogis(g0i + g1 * yys))
  for (j in seq_along(ms)) {
    yo <- yy; yo[ms[[j]]] <- NA; ok <- !is.na(yo)
    mu <- mean(yo, na.rm = TRUE); se_mu <- sd(yo, na.rm = TRUE) / sqrt(sum(ok))
    res[i, j] <- mu
    cov[i, j] <- (mu - 1.96 * se_mu <= b0) & (b0 <= mu + 1.96 * se_mu)
    fit <- lm(yo[ok] ~ xx[ok]); bs <- unname(coef(fit)[2]); se_b <- summary(fit)$coef[2, 2]
    res[i, j + 3] <- bs
    cov[i, j + 3] <- (bs - 1.96 * se_b <= b1) & (b1 <= bs + 1.96 * se_b)
  }
}
mc <- rbind(bias = colMeans(res) - rep(c(b0, b1), each = 3),
            coverage = colMeans(cov))
round(mc, 3)

## ---- fig-sampling ----
rd <- as.data.frame(res)
df2 <- rbind(
  data.frame(est = rd$mean_MCAR,  mech = "MCAR", target = "Mean of y (truth 5)"),
  data.frame(est = rd$mean_MAR,   mech = "MAR",  target = "Mean of y (truth 5)"),
  data.frame(est = rd$mean_MNAR,  mech = "MNAR", target = "Mean of y (truth 5)"),
  data.frame(est = rd$slope_MCAR, mech = "MCAR", target = "Slope of y on x (truth 2)"),
  data.frame(est = rd$slope_MAR,  mech = "MAR",  target = "Slope of y on x (truth 2)"),
  data.frame(est = rd$slope_MNAR, mech = "MNAR", target = "Slope of y on x (truth 2)"))
df2$mech <- factor(df2$mech, levels = c("MCAR", "MAR", "MNAR"))
tru <- data.frame(target = c("Mean of y (truth 5)", "Slope of y on x (truth 2)"), v = c(5, 2))
ggplot(df2, aes(est, colour = mech, fill = mech)) +
  geom_density(alpha = 0.12, linewidth = 0.7) +
  geom_vline(data = tru, aes(xintercept = v), colour = ink, linetype = "dotted", linewidth = 0.6) +
  facet_wrap(~target, scales = "free") +
  scale_colour_manual(values = pal) + scale_fill_manual(values = pal) +
  labs(title = "The mechanism decides which estimand breaks",
       subtitle = "MAR spares the conditional slope on x; MNAR spares nothing",
       x = "Complete-case estimate", y = "Density", colour = "Mechanism", fill = "Mechanism") +
  theme_te()
