How to generate missing values in Python?

Aude Sportisse with the help of Marine Le Morvan and Boris Muzellec

Missing values occur in many domains and most datasets contain missing values (due to non-responses, lost records, machine failures, dataset fusions, etc.). These missing values have to be considered before or during analyses of these datasets.

Now, if you have a method that deals with missing values, for instance imputation or estimation with missing values, how can you assess the performance of your method on a given dataset? If the data already contains missing values, than this does not help you since you generally do not have a ground truth for these missing values. So you will have to simulate missing values, i.e. you remove values – which you therefore know to be the ground truth – to generate missing values.

The mechanisms generating missing values can be various but usually they are classified into three main categories defined by (Rubin 1976): missing completely at random (MCAR), missing at random (MAR) and missing not at random (MNAR). The first two are also qualified as ignorable missing values mechanisms, for instance in likelihood-based approaches to handle missing values, whereas the MNAR mechanism generates nonignorable missing values. In the following we will briefly introduce each mechanism (with the definitions used widely in the literature) and propose ways of simulations missing values under these three mechanism assumptions. For more precise definitions we refer to references in the bibliography on the R-miss-tastic website.

# Introduction¶

## Notations¶

Let's denote by $\mathbf{X}\in\mathcal{X_1}\times\dots\times\mathcal{X_p}$ the complete observations. We assume that $\mathbf{X}$ is a concatenation of $p$ columns $X_j\in\mathcal{X_j}$, $j\in\{1,\dots,p\}$, where $dim(\mathcal{X_j})=n$ for all $j$.

The data can be composed of quantitative and/or qualitative values, hence $\mathcal{X_j}$ can be $\mathbb{R}^n$, $\mathbb{Z}^n$ or more generally $\mathcal{S}^n$ for any discrete set $S$.

Missing values are indicated as NA (not available) and we define an indicator matrix $\mathbf{R}\in\{0,1\}^{n\times p}$ such that $R_{ij}=1$ if $X_{ij}$ is observed and $R_{ij}=0$ otherwise. We call this matrix $\mathbf{R}$ the response (or missingness) pattern of the observations $\mathbf{X}$. According to this pattern, we can partition the observations $\mathbf{X}$ into observed and missing: $\mathbf{X} = (\mathbf{X}^{obs}, \mathbf{X}^{mis})$.

## Definition of the mechanisms¶

In order to define the different missing values mechanisms, both $\mathbf{X}$ and $\mathbf{R}$ are modeled as random variables with probability distributions $\mathbb{P}_X$ and $\mathbb{P}_R$ respectively. We parametrize the missingness distribution $\mathbb{P}_R$ by a parameter $\phi$.

### MCAR¶

The observations are said to be Missing Completely At Random (MCAR) if the probability that an observation is missing is independent of the variables and observations: the probability that an observation is missing does not depend on $(\mathbf{X}^{obs},\mathbf{X}^{mis})$. Formally this is: $$\mathbb{P}_R(R\,|\, X^{obs}, X^{mis}; \phi) = \mathbb{P}_R(R) \qquad \forall \, \phi.$$

### MAR¶

The observations are said to be Missing At Random (MAR) if the probability that an observation is missing only depends on the observed data $\mathbf{X}^{obs}$. Formally,

$$\mathbb{P}_R(R\,|\,X^{obs},X^{mis};\phi)=\mathbb{P}_R(R\,|\,X^{obs};\phi) \qquad \forall \,\phi,\, \forall \, X^{mis}.$$

### MNAR¶

The observations are said to be Missing Not At Random (MNAR) in all other cases, i.e. the missingness depends on the missing values and potentially also on the observed values.

# Use of produce_NA with default settings¶

For now, with the main function produce_NA, it is possible to generate missing values only for quantitative data which are complete.

Missing values can be generated following one or more of the three main missing values mechanisms (see below for details).

The function is widely based on the code of Boris Muzellec available here.

We generate a small example of observations $\mathbf{X}$:

## Minimal set of arguments¶

In order to generate missing values for given data, produce_NA requires the following arguments:

• X: the initial data (can be only complete for now) as a matrix or data.frame

• p_miss: proportion of missing values to generate for variables which will have missing values

• mecha: one of "MCAR", "MAR", "MNAR" (default: “MCAR”)

## Value¶

produce_NA returns a list containing three elements:

• X_init: the initial data
• X_incomp: the data with the newly generated missing values
• mask: a matrix indexing the generated missing values

## Example¶

We can plot the data as a color-encoded matrix, cells with missing values are masked (in white).

# Details on all available specifications¶

The main function produce_NA allows generating missing values in various ways. These can be specified through different arguments:

produce_NA(X, p_miss, mecha = "MCAR", opt = None, p_obs = None, q = None)

## Mechanisms¶

### MCAR¶

Missing Completely At Random values are generated using only the desired proportion of missing values p_miss, i.e. each value have the same probability p_miss of being missing. Therefore, we generate missing values using a Bernoulli distribution of parameter p_miss.

### MAR¶

Missing At Random mechanism values are generated by using a logistic model. A subset of fully observed variables (with no missing values) is randomly selected. The remaining variables have missing values according to a logistic model (depending on the fully observed variables only) with random weights, re-scaled so as to attain the desired proportion of missing values on those variables.

There is an additional argument:

• p_obs: the proportion of fully observed variables that will be used for the logistic masking model. Note that at least one variable is chosen to be observed

Note that there exists other ways to generate missing values as described in the Rmarkdown How generate missing values? by using patterns as in the first definition of Rubin (1976).

### MNAR¶

For generating MNAR data, an aditionnal argument should be given:

• opt: it indicates how the missing-data mechanism is generated; using a logistic regression, logistic regression for generating a self-masked MNAR mechanism ("selfmasked") or quantile censorship ("quantile")

#### MNAR with logistic model¶

With opt = "logistic", missing not at random data are generated with a logistic masking model. It implements two mechanisms:

• Missing probabilities are selected with a logistic model, taking all variables as inputs. Hence, values that are inputs can also be missing.
• Variables are split into a set of intputs for a logistic model, and a set whose missing probabilities are determined by the logistic model. Then inputs are then masked MCAR (hence, missing values from the second set will depend on masked values).

In either case, weights are random and the intercept is selected to attain the desired proportion of missing values.

An additional argument is required:

• p_obs: proportion of variables that will be used for the logistic masking model. Note that at the end, these variables are missing, since they are masked MCAR

For self-masked MNAR values, the missingness of the variable $X_j$ only depends on the values of $X_j$.

With opt = "selfmasked", self-masked missing not at random are generated with a logistic self-masking model.

Variables have missing values probabilities given by a logistic model, taking the same variable as input (hence, missingness is independent from one variable to another). The intercepts are selected to attain the desired missing rate.

#### MNAR with quantile censorship¶

With opt = "quantile", missing not at random data are generated by using quantile censorship. First, a subset of variables which will have missing variables is randomly selected. Then, missing values are generated on the q-quantiles at random. Since missingness depends on quantile information, it depends on masked values, hence this is a MNAR mechanism.

Two additional arguments are required:

• p_obs: the proportion of fully observed variables. Note that at least one variable is chosen to be observed.

• q: quantile level at which the cuts should occur.