Missing completely at random (MCAR) describes a missingness mechanism in which missingness is unrelated to both observed and unobserved variables. Missing values therefore occur randomly across the dataset, rather than reflecting a feature value or a hidden process. section 6.1.1
Start by investigating why values are missing before choosing a model strategy. Under MCAR, expected-value imputation can work well, while multivariate imputation can use correlations among predictors to reconstruct missing values. Gradient-boosted decision trees can also handle MCAR effectively, although direct imputation may remain easier to inspect and control. section 6.1.1 section 6.1.2
Distinguish MCAR from related mechanisms before processing the data:
- MCAR means missingness is unrelated to observed and unobserved variables. section 6.1.1
- MAR means observed variables explain missingness, while unobserved values do not. section 6.1.1
- MNAR means missingness follows a systematic relationship involving the missing value itself; specialized treatment may be required. section 6.1.1
- Use missing indicators when missingness is not completely random, and investigate the cause rather than relying automatically on GBDT defaults. section 6.1.2 section 7.1.5
Randomly masking continuous features
A practical comparison creates a five-percent random mask over continuous features, fills one copy with SimpleImputer, and fills another with IterativeImputer using a RandomForestRegressor. Mean absolute error can then compare simple and multivariate reconstruction.
Listing 6.2 Multivariate imputation
from sklearn.experimental import (
enable_iterative_imputer
) #1
from sklearn.impute import SimpleImputer, IterativeImputer
from sklearn.ensemble import RandomForestRegressor
Xm = data[continuous].copy() #2
missing_percentage = 0.05
np.random.seed(0)
mask = np.random.rand(*Xm.shape) < missing_percentage #3
Xm[mask] = np.nan
simple_imputer = SimpleImputer()
Xm_si = simple_imputer.fit_transform(Xm) #4
rf = RandomForestRegressor(random_state=0, n_jobs=-1) #5
multivariate_imputer = IterativeImputer(
estimator=rf,
max_iter=1,
tol=0.01
) #6
Xm_mi = multivariate_imputer.fit_transform(Xm) #7
mae = pd.DataFrame(
{
"simple": np.mean(
np.abs(data[continuous] - Xm_si), axis=0
),
"multivariate": np.mean(
np.abs(data[continuous] - Xm_mi), axis=0
)
},
index = continuous
) #8
print(mae)
Run the supplied pandas and scikit-learn listing to compare simple and multivariate imputation on randomly masked continuous data. section 6.1.1
