The mice package implements a method to deal with missing data. The package creates multiple imputations (replacement values) for multivariate missing data. The method is based on Fully Conditional Specification, where each incomplete variable is imputed by a separate model. The MICE algorithm can impute mixes of continuous, binary, unordered categorical and ordered categorical data. In addition, MICE can impute continuous twolevel data, and maintain consistency between imputations by means of passive imputation. Many diagnostic plots are implemented to inspect the quality of the imputations.
Generates Multivariate Imputations by Chained Equations (MICE)
mice( data, m = 5, method = NULL, predictorMatrix, ignore = NULL, where = NULL, blocks, visitSequence = NULL, formulas, blots = NULL, post = NULL, defaultMethod = c("pmm", "logreg", "polyreg", "polr"), maxit = 5, printFlag = TRUE, seed = NA, data.init = NULL, ... )
data  A data frame or a matrix containing the incomplete data. Missing
values are coded as 

m  Number of multiple imputations. The default is 
method  Can be either a single string, or a vector of strings with
length 
predictorMatrix  A numeric matrix of 
ignore  A logical vector of 
where  A data frame or matrix with logicals of the same dimensions
as 
blocks  List of vectors with variable names per block. List elements
may be named to identify blocks. Variables within a block are
imputed by a multivariate imputation method
(see 
visitSequence  A vector of block names of arbitrary length, specifying the
sequence of blocks that are imputed during one iteration of the Gibbs
sampler. A block is a collection of variables. All variables that are
members of the same block are imputed
when the block is visited. A variable that is a member of multiple blocks
is reimputed within the same iteration.
The default 
formulas  A named list of formula's, or expressions that
can be converted into formula's by 
blots  A named 
post  A vector of strings with length 
defaultMethod  A vector of length 4 containing the default
imputation methods for 1) numeric data, 2) factor data with 2 levels, 3)
factor data with > 2 unordered levels, and 4) factor data with > 2
ordered levels. By default, the method uses

maxit  A scalar giving the number of iterations. The default is 5. 
printFlag  If 
seed  An integer that is used as argument by the 
data.init  A data frame of the same size and type as 
...  Named arguments that are passed down to the univariate imputation functions. 
Returns an S3 object of class mids
(multiply imputed data set)
The mice package contains functions to
Inspect the missing data pattern
Impute the missing data m times, resulting in m completed data sets
Diagnose the quality of the imputed values
Analyze each completed data set
Pool the results of the repeated analyses
Store and export the imputed data in various formats
Generate simulated incomplete data
Incorporate custom imputation methods
Generates multiple imputations for incomplete multivariate data by Gibbs sampling. Missing data can occur anywhere in the data. The algorithm imputes an incomplete column (the target column) by generating 'plausible' synthetic values given other columns in the data. Each incomplete column must act as a target column, and has its own specific set of predictors. The default set of predictors for a given target consists of all other columns in the data. For predictors that are incomplete themselves, the most recently generated imputations are used to complete the predictors prior to imputation of the target column.
A separate univariate imputation model can be specified for each column. The default imputation method depends on the measurement level of the target column. In addition to these, several other methods are provided. You can also write their own imputation functions, and call these from within the algorithm.
The data may contain categorical variables that are used in a regressions on other variables. The algorithm creates dummy variables for the categories of these variables, and imputes these from the corresponding categorical variable.
Builtin univariate imputation methods are:
pmm  any  Predictive mean matching 
midastouch  any  Weighted predictive mean matching 
sample  any  Random sample from observed values 
cart  any  Classification and regression trees 
rf  any  Random forest imputations 
mean  numeric  Unconditional mean imputation 
norm  numeric  Bayesian linear regression 
norm.nob  numeric  Linear regression ignoring model error 
norm.boot  numeric  Linear regression using bootstrap 
norm.predict  numeric  Linear regression, predicted values 
lasso.norm  numeric  Lasso linear regression 
lasso.select.norm  numeric  Lasso select + linear regression 
quadratic  numeric  Imputation of quadratic terms 
ri  numeric  Random indicator for nonignorable data 
logreg  binary  Logistic regression 
logreg.boot  binary  Logistic regression with bootstrap 
lasso.logreg  binary  Lasso logistic regression 
lasso.select.logreg  binary  Lasso select + logistic regression 
polr  ordered  Proportional odds model 
polyreg  unordered  Polytomous logistic regression 
lda  unordered  Linear discriminant analysis 
2l.norm  numeric  Level1 normal heteroscedastic 
2l.lmer  numeric  Level1 normal homoscedastic, lmer 
2l.pan  numeric  Level1 normal homoscedastic, pan 
2l.bin  binary  Level1 logistic, glmer 
2lonly.mean  numeric  Level2 class mean 
2lonly.norm  numeric  Level2 class normal 
2lonly.pmm  any  Level2 class predictive mean matching 
These corresponding functions are coded in the mice
library under
names mice.impute.method
, where method
is a string with the
name of the univariate imputation method name, for example norm
. The
method
argument specifies the methods to be used. For the j
'th
column, mice()
calls the first occurrence of
paste('mice.impute.', method[j], sep = '')
in the search path. The
mechanism allows uses to write customized imputation function,
mice.impute.myfunc
. To call it for all columns specify
method='myfunc'
. To call it only for, say, column 2 specify
method=c('norm','myfunc','logreg',...{})
.
Skipping imputation: The user may skip imputation of a column by
setting its entry to the empty method: ""
. For complete columns without
missing data mice
will automatically set the empty method. Setting t
he empty method does not produce imputations for the column, so any missing
cells remain NA
. If column A contains NA
's and is used as
predictor in the imputation model for column B, then mice
produces no
imputations for the rows in B where A is missing. The imputed data
for B may thus contain NA
's. The remedy is to remove column A from
the imputation model for the other columns in the data. This can be done
by setting the entire column for variable A in the predictorMatrix
equal to zero.
Passive imputation: mice()
supports a special builtin method,
called passive imputation. This method can be used to ensure that a data
transform always depends on the most recently generated imputations. In some
cases, an imputation model may need transformed data in addition to the
original data (e.g. log, quadratic, recodes, interaction, sum scores, and so
on).
Passive imputation maintains consistency among different transformations of
the same data. Passive imputation is invoked if ~
is specified as the
first character of the string that specifies the univariate method.
mice()
interprets the entire string, including the ~
character,
as the formula argument in a call to model.frame(formula,
data[!r[,j],])
. This provides a simple mechanism for specifying deterministic
dependencies among the columns. For example, suppose that the missing entries
in variables data$height
and data$weight
are imputed. The body
mass index (BMI) can be calculated within mice
by specifying the
string '~I(weight/height^2)'
as the univariate imputation method for
the target column data$bmi
. Note that the ~
mechanism works
only on those entries which have missing values in the target column. You
should make sure that the combined observed and imputed parts of the target
column make sense. An easy way to create consistency is by coding all entries
in the target as NA
, but for large data sets, this could be
inefficient. Note that you may also need to adapt the default
predictorMatrix
to evade linear dependencies among the predictors that
could cause errors like Error in solve.default()
or Error:
system is exactly singular
. Though not strictly needed, it is often useful
to specify visitSequence
such that the column that is imputed by the
~
mechanism is visited each time after one of its predictors was
visited. In that way, deterministic relation between columns will always be
synchronized.
#'A new argument ls.meth
can be parsed to the lower level
.norm.draw
to specify the method for generating the least squares
estimates and any subsequently derived estimates. Argument ls.meth
takes one of three inputs: "qr"
for QRdecomposition, "svd"
for
singular value decomposition and "ridge"
for ridge regression.
ls.meth
defaults to ls.meth = "qr"
.
Auxiliary predictors in formulas specification:
For a given block, the formulas
specification takes precedence over
the corresponding row in the predictMatrix
argument. This
precedence is, however, restricted to the subset of variables
specified in the terms of the block formula. Any
variables not specified by formulas
are imputed
according to the predictMatrix
specification. Variables with
nonzero type
values in the predictMatrix
will
be added as main effects to the formulas
, which will
act as supplementary covariates in the imputation model. It is possible
to turn off this behavior by specifying the
argument auxiliary = FALSE
.
The main functions are:
mice()  Impute the missing data *m* times 
with()  Analyze completed data sets 
pool()  Combine parameter estimates 
complete()  Export imputed data 
ampute()  Generate missing data 
There is a detailed series of six online vignettes that walk you through solving realistic inference problems with mice.
We suggest going through these vignettes in the following order
#'Van Buuren, S. (2018). Boca Raton, FL.: Chapman & Hall/CRC Press. The book Flexible Imputation of Missing Data. Second Edition. contains a lot of example code.
The mice software was published in the Journal of Statistical Software (Van Buuren and GroothuisOudshoorn, 2011). doi: 10.18637/jss.v045.i03 The first application of the method concerned missing blood pressure data (Van Buuren et. al., 1999). The term Fully Conditional Specification was introduced in 2006 to describe a general class of methods that specify imputations model for multivariate data as a set of conditional distributions (Van Buuren et. al., 2006). Further details on mixes of variables and applications can be found in the book Flexible Imputation of Missing Data. Second Edition. Chapman & Hall/CRC. Boca Raton, FL.
Updating the BLAS can improve speed of R, sometime considerably. The details depend on the operating system. See the discussion in the "R Installation and Administration" guide for further information.
van Buuren, S., Boshuizen, H.C., Knook, D.L. (1999) Multiple imputation of missing blood pressure covariates in survival analysis. Statistics in Medicine, 18, 681694.
van Buuren, S., Brand, J.P.L., GroothuisOudshoorn C.G.M., Rubin, D.B. (2006) Fully conditional specification in multivariate imputation. Journal of Statistical Computation and Simulation, 76, 12, 10491064.
van Buuren, S., GroothuisOudshoorn, K. (2011). mice
:
Multivariate Imputation by Chained Equations in R
. Journal of
Statistical Software, 45(3), 167. doi: 10.18637/jss.v045.i03
Van Buuren, S. (2018). Flexible Imputation of Missing Data. Second Edition. Chapman & Hall/CRC. Boca Raton, FL.
Van Buuren, S., GroothuisOudshoorn, K. (2011). mice
:
Multivariate Imputation by Chained Equations in R
. Journal of
Statistical Software, 45(3), 167.
doi: 10.18637/jss.v045.i03
Van Buuren, S. (2018). Flexible Imputation of Missing Data. Second Edition. Chapman & Hall/CRC. Boca Raton, FL.
Van Buuren, S., Brand, J.P.L., GroothuisOudshoorn C.G.M., Rubin, D.B. (2006) Fully conditional specification in multivariate imputation. Journal of Statistical Computation and Simulation, 76, 12, 10491064.
Van Buuren, S. (2007) Multiple imputation of discrete and continuous data by fully conditional specification. Statistical Methods in Medical Research, 16, 3, 219242.
Van Buuren, S., Boshuizen, H.C., Knook, D.L. (1999) Multiple imputation of missing blood pressure covariates in survival analysis. Statistics in Medicine, 18, 681694.
Brand, J.P.L. (1999) Development, implementation and evaluation of multiple imputation strategies for the statistical analysis of incomplete data sets. Dissertation. Rotterdam: Erasmus University.
Stef van Buuren stef.vanbuuren@tno.nl, Karin GroothuisOudshoorn c.g.m.oudshoorn@utwente.nl, 20002010, with contributions of Alexander Robitzsch, Gerko Vink, Shahab Jolani, Roel de Jong, Jason Turner, Lisa Doove, John Fox, Frank E. Harrell, and Peter Malewski.
# do default multiple imputation on a numeric matrix imp < mice(nhanes) #> #> iter imp variable #> 1 1 bmi hyp chl #> 1 2 bmi hyp chl #> 1 3 bmi hyp chl #> 1 4 bmi hyp chl #> 1 5 bmi hyp chl #> 2 1 bmi hyp chl #> 2 2 bmi hyp chl #> 2 3 bmi hyp chl #> 2 4 bmi hyp chl #> 2 5 bmi hyp chl #> 3 1 bmi hyp chl #> 3 2 bmi hyp chl #> 3 3 bmi hyp chl #> 3 4 bmi hyp chl #> 3 5 bmi hyp chl #> 4 1 bmi hyp chl #> 4 2 bmi hyp chl #> 4 3 bmi hyp chl #> 4 4 bmi hyp chl #> 4 5 bmi hyp chl #> 5 1 bmi hyp chl #> 5 2 bmi hyp chl #> 5 3 bmi hyp chl #> 5 4 bmi hyp chl #> 5 5 bmi hyp chl imp #> Class: mids #> Number of multiple imputations: 5 #> Imputation methods: #> age bmi hyp chl #> "" "pmm" "pmm" "pmm" #> PredictorMatrix: #> age bmi hyp chl #> age 0 1 1 1 #> bmi 1 0 1 1 #> hyp 1 1 0 1 #> chl 1 1 1 0 # list the actual imputations for BMI imp$imp$bmi #> 1 2 3 4 5 #> 1 29.6 26.3 22.0 28.7 28.7 #> 3 30.1 30.1 22.0 28.7 26.3 #> 4 27.2 22.7 20.4 27.4 21.7 #> 6 21.7 20.4 25.5 22.7 26.3 #> 10 29.6 28.7 22.0 25.5 33.2 #> 11 22.5 22.0 27.2 22.0 29.6 #> 12 28.7 24.9 21.7 27.2 30.1 #> 16 33.2 33.2 30.1 22.0 22.5 #> 21 27.2 26.3 26.3 22.0 30.1 # first completed data matrix complete(imp) #> age bmi hyp chl #> 1 1 29.6 1 187 #> 2 2 22.7 1 187 #> 3 1 30.1 1 187 #> 4 3 27.2 1 206 #> 5 1 20.4 1 113 #> 6 3 21.7 1 184 #> 7 1 22.5 1 118 #> 8 1 30.1 1 187 #> 9 2 22.0 1 238 #> 10 2 29.6 1 218 #> 11 1 22.5 1 131 #> 12 2 28.7 2 204 #> 13 3 21.7 1 206 #> 14 2 28.7 2 204 #> 15 1 29.6 1 187 #> 16 1 33.2 1 184 #> 17 3 27.2 2 284 #> 18 2 26.3 2 199 #> 19 1 35.3 1 218 #> 20 3 25.5 2 184 #> 21 1 27.2 1 131 #> 22 1 33.2 1 229 #> 23 1 27.5 1 131 #> 24 3 24.9 1 204 #> 25 2 27.4 1 186 # imputation on mixed data with a different method per column mice(nhanes2, meth = c("sample", "pmm", "logreg", "norm")) #> #> iter imp variable #> 1 1 bmi hyp chl #> 1 2 bmi hyp chl #> 1 3 bmi hyp chl #> 1 4 bmi hyp chl #> 1 5 bmi hyp chl #> 2 1 bmi hyp chl #> 2 2 bmi hyp chl #> 2 3 bmi hyp chl #> 2 4 bmi hyp chl #> 2 5 bmi hyp chl #> 3 1 bmi hyp chl #> 3 2 bmi hyp chl #> 3 3 bmi hyp chl #> 3 4 bmi hyp chl #> 3 5 bmi hyp chl #> 4 1 bmi hyp chl #> 4 2 bmi hyp chl #> 4 3 bmi hyp chl #> 4 4 bmi hyp chl #> 4 5 bmi hyp chl #> 5 1 bmi hyp chl #> 5 2 bmi hyp chl #> 5 3 bmi hyp chl #> 5 4 bmi hyp chl #> 5 5 bmi hyp chl #> Class: mids #> Number of multiple imputations: 5 #> Imputation methods: #> age bmi hyp chl #> "" "pmm" "logreg" "norm" #> PredictorMatrix: #> age bmi hyp chl #> age 0 1 1 1 #> bmi 1 0 1 1 #> hyp 1 1 0 1 #> chl 1 1 1 0 if (FALSE) { # example where we fit the imputation model on the train data # and apply the model to impute the test data set.seed(123) ignore < sample(c(TRUE, FALSE), size = 25, replace = TRUE, prob = c(0.3, 0.7)) # scenario 1: train and test in the same dataset imp < mice(nhanes2, m = 2, ignore = ignore, print = FALSE, seed = 22112) imp.test1 < filter(imp, ignore) imp.test1$data complete(imp.test1, 1) complete(imp.test1, 2) # scenario 2: train and test in separate datasets traindata < nhanes2[!ignore, ] testdata < nhanes2[ignore, ] imp.train < mice(traindata, m = 2, print = FALSE, seed = 22112) imp.test2 < mice.mids(imp.train, newdata = testdata) complete(imp.test2, 1) complete(imp.test2, 2) }