{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "How to generate missing values in Python? \n", "\n", "**Aude Sportisse with the help of Marine Le Morvan and Boris Muzellec**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n", "\n", "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.\n", "\n", "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](https://rmisstastic.netlify.app/bibliography/) website." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Notations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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$. \n", "\n", "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$.\n", "\n", "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})$." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Definition of the mechanisms \n", "\n", "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$.\n", "\n", "### MCAR \n", "\n", "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:\n", "$$\\mathbb{P}_R(R\\,|\\, X^{obs}, X^{mis}; \\phi) = \\mathbb{P}_R(R) \\qquad \\forall \\, \\phi.$$\n", "\n", "### MAR\n", "\n", "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,\n", "\n", "$$\\mathbb{P}_R(R\\,|\\,X^{obs},X^{mis};\\phi)=\\mathbb{P}_R(R\\,|\\,X^{obs};\\phi) \\qquad \\forall \\,\\phi,\\, \\forall \\, X^{mis}.$$\n", "\n", "### MNAR\n", "\n", "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.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Use of `produce_NA` with default settings\n", "\n", "For now, with the main function `produce_NA`, it is possible to generate missing values only for quantitative data which are complete.\n", "\n", "Missing values can be generated following one or more of the three main missing values mechanisms (see below for details).\n", "\n", "The function is widely based on the code of Boris Muzellec available [here](https://github.com/BorisMuzellec/MissingDataOT)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We generate a small example of observations $\\mathbf{X}$:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "!pip install wget \n", "\n", "import wget\n", "wget.download('https://raw.githubusercontent.com/BorisMuzellec/MissingDataOT/master/utils.py')\n", "\n", "import numpy as np\n", "import pandas as pd\n", "from utils import *\n", "import torch\n", "import seaborn as sns" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Fix the seed ------------------------------------------------------\n", "np.random.seed(0)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | 0 | \n", "1 | \n", "2 | \n", "3 | \n", "
---|---|---|---|---|
0 | \n", "2.380172 | \n", "4.405363 | \n", "1.0 | \n", "F | \n", "
1 | \n", "3.379180 | \n", "2.400869 | \n", "0.0 | \n", "D | \n", "
2 | \n", "1.341582 | \n", "4.944151 | \n", "0.0 | \n", "E | \n", "
3 | \n", "1.450141 | \n", "2.922843 | \n", "0.0 | \n", "D | \n", "
4 | \n", "1.266093 | \n", "0.695726 | \n", "2.0 | \n", "D | \n", "