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 two-level 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 = vector("character", length = ncol(data)),
predictorMatrix = (1 - diag(1, ncol(data))), where = is.na(data),
visitSequence = NULL, form = vector("character", length = ncol(data)),
post = vector("character", length = ncol(data)), defaultMethod = c("pmm",
"logreg", "polyreg", "polr"), maxit = 5, diagnostics = TRUE,
printFlag = TRUE, seed = NA, imputationMethod = NULL,
defaultImputationMethod = NULL, data.init = NULL, ...)
A data frame or a matrix containing the incomplete data. Missing
values are coded as NA
.
Number of multiple imputations. The default is m=5
.
Can be either a single string, or a vector of strings with
length ncol(data)
, specifying the univariate imputation method to be
used for each column in data. If specified as a single string, the same
method will be used for all columns. The default imputation method (when no
argument is specified) depends on the measurement level of the target column
and are specified by the defaultMethod
argument. Columns that need
not be imputed have the empty method ''
. See details for more
information.
A square matrix of size ncol(data)
containing
0/1 data specifying the set of predictors to be used for each target column.
Rows correspond to target variables (i.e. variables to be imputed), in the
sequence as they appear in data. A value of '1' means that the column
variable is used as a predictor for the target variable (in the rows). The
diagonal of predictorMatrix
must be zero. The default for
predictorMatrix
is that all other columns are used as predictors
(sometimes called massive imputation). Note: For two-level imputation codes
'2' and '-2' are also allowed.
A data frame or matrix with logicals of the same dimensions
as data
indicating where in the data the imputations should be
created. The default, where = is.na(data)
, specifies that the
missing data should be imputed. The where
argument may be used to
overimpute observed data, or to skip imputations for selected missing values.
A vector of integers of arbitrary length, specifying the
column indices of the visiting sequence. The visiting sequence is the column
order that is used to impute the data during one pass through the data. A
column may be visited more than once. All incomplete columns that are used as
predictors should be visited, or else the function will stop with an error.
The default sequence 1:ncol(data)
implies that columns are imputed
from left to right. It is possible to specify one of the keywords
'roman'
(left to right), 'arabic'
(right to left),
'monotone'
(sorted in increasing amount of missingness) and
'revmonotone'
(reverse of monotone). The keyword should be supplied as
a string and may be abbreviated.
A vector of strings with length ncol(data)
, specifying
formulae. Each string is parsed and executed within the sampler()
function to create terms for the predictor. The default is to do nothing,
indicated by a vector of empty strings ''
. The main value
lies in the easy specification of interaction terms. The user must
ensure that the set of variables in the formula match those in
predictors
.
A vector of strings with length ncol(data)
, specifying
expressions. Each string is parsed and executed within the sampler()
function to postprocess imputed values. The default is to do nothing,
indicated by a vector of empty strings ''
.
A vector of three strings containing the default
imputation methods for numerical columns, factor columns with 2 levels, and
columns with (unordered or ordered) factors with more than two levels,
respectively. If nothing is specified, the following defaults will be used:
pmm
, predictive mean matching (numeric data) logreg
, logistic
regression imputation (binary data, factor with 2 levels) polyreg
,
polytomous regression imputation for unordered categorical data (factor >= 2
levels) polr
, proportional odds model for (ordered, >= 2 levels)
A scalar giving the number of iterations. The default is 5.
A Boolean flag. If TRUE
, diagnostic information
will be appended to the value of the function. If FALSE
, only the
imputed data are saved. The default is TRUE
.
If TRUE
, mice
will print history on console.
Use print=FALSE
for silent computation.
An integer that is used as argument by the set.seed()
for
offsetting the random number generator. Default is to leave the random number
generator alone.
Same as method
argument. Included for
backwards compatibility.
Same as defaultMethod
argument.
Included for backwards compatibility.
A data frame of the same size and type as data
,
without missing data, used to initialize imputations before the start of the
iterative process. The default NULL
implies that starting imputation
are created by a simple random draw from the data. Note that specification of
data.init
will start the m
Gibbs sampling streams from the same
imputations.
Named arguments that are passed down to the univariate imputation functions.
Returns an S3 object of class mids
(multiply imputed data set)
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
The book Flexible Imputation of Missing Data contains a lot of example code.
The mice software was published in the Journal of Statistical Software (Van Buuren and Groothuis-Oudshoorn, 2011). 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.
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. The extended model containing the dummy variables is called the
padded model. Its structure is stored in the list component pad
.
Built-in 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 |
quadratic |
numeric | Imputation of quadratic terms |
ri |
numeric | Random indicator for nonignorable data |
logreg |
binary | Logistic regression |
logreg.boot |
binary | Logistic regression with bootstrap |
polr |
ordered | Proportional odds model |
polyreg |
unordered | Polytomous logistic regression |
lda |
unordered | Linear discriminant analysis |
2l.norm |
numeric | Level-1 normal heteroskedastic |
2l.lmer |
numeric | Level-1 normal homoscedastic, lmer |
2l.pan |
numeric | Level-1 normal homoscedastic, pan |
2lonly.mean |
numeric | Level-2 class mean |
2lonly.norm |
numeric | Level-2 class normal |
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 occurence 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',…{})
.
Passive imputation: mice()
supports a special built-in 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 determinstic
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.
van Buuren, S., Boshuizen, H.C., Knook, D.L. (1999) Multiple imputation of missing blood pressure covariates in survival analysis. Statistics in Medicine, 18, 681--694.
van Buuren, S., Brand, J.P.L., Groothuis-Oudshoorn C.G.M., Rubin, D.B. (2006) Fully conditional specification in multivariate imputation. Journal of Statistical Computation and Simulation, 76, 12, 1049--1064.
van Buuren, S., Groothuis-Oudshoorn, K. (2011). mice
:
Multivariate Imputation by Chained Equations in R
. Journal of
Statistical Software, 45(3), 1--67.
van Buuren, S. (2012). Flexible Imputation of Missing Data. Chapman & Hall/CRC.
Van Buuren, S., Groothuis-Oudshoorn, K. (2011). mice
:
Multivariate Imputation by Chained Equations in R
. Journal of
Statistical Software, 45(3), 1-67.
http://www.jstatsoft.org/v45/i03/
van Buuren, S. (2012). Flexible Imputation of Missing Data. Boca Raton, FL: Chapman & Hall/CRC Press.
Van Buuren, S., Brand, J.P.L., Groothuis-Oudshoorn C.G.M., Rubin, D.B. (2006) Fully conditional specification in multivariate imputation. Journal of Statistical Computation and Simulation, 76, 12, 1049--1064.
Van Buuren, S. (2007) Multiple imputation of discrete and continuous data by fully conditional specification. Statistical Methods in Medical Research, 16, 3, 219--242.
Van Buuren, S., Boshuizen, H.C., Knook, D.L. (1999) Multiple imputation of missing blood pressure covariates in survival analysis. Statistics in Medicine, 18, 681--694.
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.
# NOT RUN {
# do default multiple imputation on a numeric matrix
imp <- mice(nhanes)
imp
# list the actual imputations for BMI
imp$imp$bmi
# first completed data matrix
complete(imp)
# imputation on mixed data with a different method per column
mice(nhanes2, meth=c('sample','pmm','logreg','norm'))
# }
Run the code above in your browser using DataLab