|
| 1 | +""" |
| 2 | +============================================ |
| 3 | +Tutorial for Testing the MCAR Case |
| 4 | +============================================ |
| 5 | +
|
| 6 | +In this tutorial, we show how to test the MCAR case using the Little's test. |
| 7 | +""" |
| 8 | + |
| 9 | +# %% |
| 10 | +# First import some libraries |
| 11 | +from matplotlib import pyplot as plt |
| 12 | + |
| 13 | +import numpy as np |
| 14 | +import pandas as pd |
| 15 | +from scipy.stats import norm |
| 16 | + |
| 17 | +from qolmat.analysis.holes_characterization import LittleTest |
| 18 | +from qolmat.benchmark.missing_patterns import UniformHoleGenerator |
| 19 | + |
| 20 | +plt.rcParams.update({"font.size": 12}) |
| 21 | + |
| 22 | + |
| 23 | +# %% |
| 24 | +# Generating random data |
| 25 | +# ---------------------- |
| 26 | + |
| 27 | +rng = np.random.RandomState(42) |
| 28 | +data = rng.multivariate_normal(mean=[0, 0], cov=[[1, 0], [0, 1]], size=200) |
| 29 | +df = pd.DataFrame(data=data, columns=["Column 1", "Column 2"]) |
| 30 | + |
| 31 | +q975 = norm.ppf(0.975) |
| 32 | + |
| 33 | +# %% |
| 34 | +# The Little's test |
| 35 | +# --------------------------------------------------------------- |
| 36 | +# First, we need to introduce the concept of a missing pattern. A missing pattern, also called a |
| 37 | +# pattern, is the structure of observed and missing values in a dataset. For example, in a |
| 38 | +# dataset with two columns, the possible patterns are: (0, 0), (1, 0), (0, 1), (1, 1). The value 1 |
| 39 | +# (0) indicates that the column value is missing (observed). |
| 40 | +# |
| 41 | +# The null hypothesis, H0, is: "The means of observations within each pattern are similar.". |
| 42 | +# |
| 43 | +# We choose to use the classic threshold of 5%. If the test p-value is below this threshold, |
| 44 | +# we reject the null hypothesis. |
| 45 | +# |
| 46 | +# This notebook shows how the Little's test performs on a simplistic case and its limitations. We |
| 47 | +# instanciate a test object with a random state for reproducibility. |
| 48 | + |
| 49 | +test_mcar = LittleTest(random_state=rng) |
| 50 | + |
| 51 | +# %% |
| 52 | +# Case 1: MCAR holes (True negative) |
| 53 | +# ================================== |
| 54 | + |
| 55 | + |
| 56 | +hole_gen = UniformHoleGenerator( |
| 57 | + n_splits=1, random_state=rng, subset=["Column 2"], ratio_masked=0.2 |
| 58 | +) |
| 59 | +df_mask = hole_gen.generate_mask(df) |
| 60 | +df_nan = df.where(~df_mask, np.nan) |
| 61 | + |
| 62 | +has_nan = df_mask.any(axis=1) |
| 63 | +df_observed = df.loc[~has_nan] |
| 64 | +df_hidden = df.loc[has_nan] |
| 65 | + |
| 66 | +plt.scatter(df_observed["Column 1"], df_observed[["Column 2"]], label="Fully observed values") |
| 67 | +plt.scatter(df_hidden[["Column 1"]], df_hidden[["Column 2"]], label="Values with missing C2") |
| 68 | + |
| 69 | +plt.legend( |
| 70 | + loc="lower left", |
| 71 | + fontsize=8, |
| 72 | +) |
| 73 | +plt.xlabel("Column 1") |
| 74 | +plt.ylabel("Column 2") |
| 75 | +plt.title("Case 1: MCAR data") |
| 76 | +plt.grid() |
| 77 | +plt.show() |
| 78 | + |
| 79 | +# %% |
| 80 | +result = test_mcar.test(df_nan) |
| 81 | +print(f"Test p-value: {result:.2%}") |
| 82 | +# %% |
| 83 | +# The p-value is larger than 0.05, therefore we don't reject the HO MCAR assumption. In this case |
| 84 | +# this is a true negative. |
| 85 | + |
| 86 | +# %% |
| 87 | +# Case 2: MAR holes with mean bias (True positive) |
| 88 | +# ================================================ |
| 89 | + |
| 90 | +df_mask = pd.DataFrame({"Column 1": False, "Column 2": df["Column 1"] > q975}, index=df.index) |
| 91 | + |
| 92 | +df_nan = df.where(~df_mask, np.nan) |
| 93 | + |
| 94 | +has_nan = df_mask.any(axis=1) |
| 95 | +df_observed = df.loc[~has_nan] |
| 96 | +df_hidden = df.loc[has_nan] |
| 97 | + |
| 98 | +plt.scatter(df_observed["Column 1"], df_observed[["Column 2"]], label="Fully observed values") |
| 99 | +plt.scatter(df_hidden[["Column 1"]], df_hidden[["Column 2"]], label="Values with missing C2") |
| 100 | + |
| 101 | +plt.legend( |
| 102 | + loc="lower left", |
| 103 | + fontsize=8, |
| 104 | +) |
| 105 | +plt.xlabel("Column 1") |
| 106 | +plt.ylabel("Column 2") |
| 107 | +plt.title("Case 2: MAR data with mean bias") |
| 108 | +plt.grid() |
| 109 | +plt.show() |
| 110 | + |
| 111 | +# %% |
| 112 | + |
| 113 | +result = test_mcar.test(df_nan) |
| 114 | +print(f"Test p-value: {result:.2%}") |
| 115 | +# %% |
| 116 | +# The p-value is smaller than 0.05, therefore we reject the HO MCAR assumption. In this case |
| 117 | +# this is a true positive. |
| 118 | + |
| 119 | +# %% |
| 120 | +# Case 3: MAR holes with any mean bias (False negative) |
| 121 | +# ===================================================== |
| 122 | +# |
| 123 | +# The specific case is designed to emphasize the Little's test limits. In the case, we generate |
| 124 | +# holes when the absolute value of the first feature is high. This missingness mechanism is clearly |
| 125 | +# MAR but the means between missing patterns is not statistically different. |
| 126 | + |
| 127 | +df_mask = pd.DataFrame( |
| 128 | + {"Column 1": False, "Column 2": df["Column 1"].abs() > q975}, index=df.index |
| 129 | +) |
| 130 | + |
| 131 | +df_nan = df.where(~df_mask, np.nan) |
| 132 | + |
| 133 | +has_nan = df_mask.any(axis=1) |
| 134 | +df_observed = df.loc[~has_nan] |
| 135 | +df_hidden = df.loc[has_nan] |
| 136 | + |
| 137 | +plt.scatter(df_observed["Column 1"], df_observed[["Column 2"]], label="Fully observed values") |
| 138 | +plt.scatter(df_hidden[["Column 1"]], df_hidden[["Column 2"]], label="Values with missing C2") |
| 139 | + |
| 140 | +plt.legend( |
| 141 | + loc="lower left", |
| 142 | + fontsize=8, |
| 143 | +) |
| 144 | +plt.xlabel("Column 1") |
| 145 | +plt.ylabel("Column 2") |
| 146 | +plt.title("Case 3: MAR data without any mean bias") |
| 147 | +plt.grid() |
| 148 | +plt.show() |
| 149 | + |
| 150 | +# %% |
| 151 | + |
| 152 | +result = test_mcar.test(df_nan) |
| 153 | +print(f"Test p-value: {result:.2%}") |
| 154 | +# %% |
| 155 | +# The p-value is larger than 0.05, therefore we don't reject the HO MCAR assumption. In this case |
| 156 | +# this is a false negative since the missingness mechanism is MAR. |
| 157 | + |
| 158 | +# %% |
| 159 | +# Limitations |
| 160 | +# ----------- |
| 161 | +# In this tutoriel, we can see that Little's test fails to detect covariance heterogeneity between |
| 162 | +# patterns. |
| 163 | +# |
| 164 | +# We also note that the Little's test does not handle categorical data or temporally |
| 165 | +# correlated data. |
0 commit comments