makeMissing: Generate MCAR/MAR/MNAR missingness in complete data

View source: R/makeMissing.R

makeMissingR Documentation

Generate MCAR/MAR/MNAR missingness in complete data

Description

Amputation generator for simulation studies (the variable-wise counterpart of mice::ampute()): takes complete data, sets a controlled share of cells to NA under a chosen missingness mechanism, and returns the amputed data together with an attr(., "where") indicator matrix that plugs directly into evaluation(), nrmse() and pfc():

Usage

makeMissing(
  data,
  prop = 0.1,
  mechanism = c("MCAR", "MAR", "MNAR"),
  vars = NULL,
  weights = NULL,
  seed = NULL
)

Arguments

data

A complete data.frame or data.table (no missing values).

prop

Proportion of cells set to missing per target variable, strictly between 0 and 1.

mechanism

One of "MCAR" (default), "MAR", "MNAR".

vars

Character vector of target variables to receive missing values. Default: all columns.

weights

Optional named numeric vector. For "MAR": the driver columns and their weights (numeric columns, excluding the target). For "MNAR": an entry named after a target scales/flips its own-value score. Ignored for "MCAR".

seed

Optional single number: applied via set.seed() before drawing, for reproducible amputation.

Details

  amp <- makeMissing(dat, prop = 0.2, mechanism = "MAR")
  imp <- vimpute(amp)
  evaluation(dat, imp, m = attr(amp, "where"))

Mechanisms (applied per target variable, each receiving exactly round(prop * nrow(data)) missing cells):

"MCAR"

cells are drawn uniformly at random.

"MAR"

the probability of a cell going missing grows with a weighted score of the other (observed) variables: rows are drawn with probabilities plogis(z) where z is the standardized weighted sum of the driver columns. Default drivers: all numeric columns except the target, with equal weights; use weights to select drivers and directions (negative weights make low driver values predictive of missingness).

"MNAR"

as "MAR", but the score is the target's own standardized values, so high values are more often missing (a weights entry named after the target flips or scales the direction). Numeric targets only.

With several target variables the scores are computed on the complete input, so each target's mechanism conditions on the pre-amputation values of its drivers.

Value

The amputed data, classed like the input (data.frame in, data.frame out; data.table in, data.table out), with attributes "where" (logical indicator matrix of the introduced missings), "mechanism" and "prop".

Author(s)

Matthias Templ

See Also

evaluation(), vimpute(), kNN()

Examples

data(sleep)
complete_rows <- na.omit(sleep[, c("BodyWgt", "BrainWgt", "Sleep", "Span")])
amp <- makeMissing(complete_rows, prop = 0.2, mechanism = "MAR",
                   vars = "Sleep", seed = 1)
colSums(is.na(amp))
imp <- kNN(amp, imp_var = FALSE)
evaluation(complete_rows, imp, m = attr(amp, "where"))

VIM documentation built on Sept. 2, 2026, 5:07 p.m.