## Description

The mice package implements a method to deal with missing data.The package creates multiple imputations (replacement values) formultivariate missing data. The method is based on Fully ConditionalSpecification, where each incomplete variable is imputed by a separatemodel. The MICE algorithm can impute mixes of continuous, binary,unordered categorical and ordered categorical data. In addition, MICEcan impute continuous two-level data, and maintain consistency betweenimputations by means of passive imputation. Many diagnostic plots areimplemented to inspect the quality of the imputations.

Generates Multivariate Imputations by Chained Equations (MICE)

## Usage

`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, ...)`

## Value

Returns an S3 object of class `mids`

(multiply imputed data set)

## Arguments

- data
A data frame or a matrix containing the incomplete data. Missingvalues are coded as

`NA`

.- m
Number of multiple imputations. The default is

`m=5`

.- method
Can be either a single string, or a vector of strings withlength

`length(blocks)`

, specifying the imputation method to beused for each column in data. If specified as a single string, the samemethod will be used for all blocks. The default imputation method (when noargument is specified) depends on the measurement level of the target column,as regulated by the`defaultMethod`

argument. Columns that neednot be imputed have the empty method`""`

. See details.- predictorMatrix
A numeric matrix of

`length(blocks)`

rowsand`ncol(data)`

columns, containing 0/1 data specifyingthe set of predictors to be used for each target column.Each row corresponds to a variable block, i.e., a set of variablesto be imputed. A value of`1`

means that the columnvariable is used as a predictor for the target block (in the rows).By default, the`predictorMatrix`

is a square matrix of`ncol(data)`

rows and columns with all 1's, except for the diagonal.Note: For two-level imputation models (which have`"2l"`

in their names)other codes (e.g,`2`

or`-2`

) are also allowed.- ignore
A logical vector of

`nrow(data)`

elements indicatingwhich rows are ignored when creating the imputation model. The default`NULL`

includes all rows that have an observed value of the variableto imputed. Rows with`ignore`

set to`TRUE`

do not influence theparameters of the imputation model, but are still imputed. We may use the`ignore`

argument to split`data`

into a training set (on which theimputation model is built) and a test set (that does not influence theimputation model estimates).Note: Multivariate imputation methods, like`mice.impute.jomoImpute()`

or`mice.impute.panImpute()`

, do not honour the`ignore`

argument.- where
A data frame or matrix with logicals of the same dimensionsas

`data`

indicating where in the data the imputations should becreated. The default,`where = is.na(data)`

, specifies that themissing data should be imputed. The`where`

argument may be used tooverimpute observed data, or to skip imputations for selected missing values.Note: Imputation methods that generate imptutations outside of`mice`

, like`mice.impute.panImpute()`

may depend on a completepredictor space. In that case, a custom`where`

matrix can not bespecified.- blocks
List of vectors with variable names per block. List elementsmay be named to identify blocks. Variables within a block areimputed by a multivariate imputation method(see

`method`

argument). By default each variable is placedinto its own block, which is effectivelyfully conditional specification (FCS) by univariate models(variable-by-variable imputation). Only variables whose names appear in`blocks`

are imputed. The relevant columns in the`where`

matrix are set to`FALSE`

of variables that are not block members.A variable may appear in multiple blocks. In that case, it iseffectively re-imputed each time that it is visited.- visitSequence
A vector of block names of arbitrary length, specifying thesequence of blocks that are imputed during one iteration of the Gibbssampler. A block is a collection of variables. All variables that aremembers of the same block are imputedwhen the block is visited. A variable that is a member of multiple blocksis re-imputed within the same iteration.The default

`visitSequence = "roman"`

visits the blocks (left to right)in the order in which they appear in`blocks`

.One may also use one of the following keywords:`"arabic"`

(right to left),`"monotone"`

(ordered low to high proportionof missing data) and`"revmonotone"`

(reverse of monotone).*Special case*: If you specify both`visitSequence = "monotone"`

and`maxit = 1`

, then the procedure will edit the`predictorMatrix`

to conform to the monotone pattern. Realize that convergence in oneiteration is only guaranteed if the missing data pattern is actuallymonotone. The procedure does not check this.- formulas
A named list of formula's, or expressions thatcan be converted into formula's by

`as.formula`

. List elementscorrespond to blocks. The block to which the list element applies isidentified by its name, so list names must correspond to block names.The`formulas`

argument is an alternative to the`predictorMatrix`

argument that allows for more flexibility inspecifying imputation models, e.g., for specifying interaction terms.- blots
A named

`list`

of`alist`

's that can be usedto pass down arguments to lower level imputation function. The entriesof element`blots[[blockname]]`

are passed down to the functioncalled for block`blockname`

.- post
A vector of strings with length

`ncol(data)`

specifyingexpressions as strings. Each string is parsed andexecuted within the`sampler()`

function to post-processimputed values during the iterations.The default is a vector of empty strings, indicating no post-processing.Multivariate (block) imputation methods ignore the`post`

parameter.- defaultMethod
A vector of length 4 containing the defaultimputation methods for 1) numeric data, 2) factor data with 2 levels, 3)factor data with > 2 unordered levels, and 4) factor data with > 2ordered levels. By default, the method uses

`pmm`

, predictive mean matching (numeric data)`logreg`

, logisticregression imputation (binary data, factor with 2 levels)`polyreg`

,polytomous regression imputation for unordered categorical data (factor > 2levels)`polr`

, proportional odds model for (ordered, > 2 levels).- maxit
A scalar giving the number of iterations. The default is 5.

- printFlag
If

`TRUE`

,`mice`

will print history on console.Use`print=FALSE`

for silent computation.- seed
An integer that is used as argument by the

`set.seed()`

foroffsetting the random number generator. Default is to leave the random numbergenerator alone.- data.init
A data frame of the same size and type as

`data`

,without missing data, used to initialize imputations before the start of theiterative process. The default`NULL`

implies that starting imputationare created by a simple random draw from the data. Note that specification of`data.init`

will start all`m`

Gibbs sampling streams from the sameimputation.- ...
Named arguments that are passed down to the univariate imputationfunctions.

## Functions

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 |

## Vignettes

There is a detailed series ofsix online vignettes that walk you through solving realistic inferenceproblems 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.

## Methodology

The mice software was published in the Journal of Statistical Software (Van Buuren and Groothuis-Oudshoorn, 2011). tools:::Rd_expr_doi("10.18637/jss.v045.i03")The first application of the methodconcerned 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.

## Enhanced linear algebra

Updating the BLAS can improve speed of R, sometime considerably. The detailsdepend on the operating system. See the discussion in the"R Installation and Administration" guide for further information.

## Author

Stef van Buuren stef.vanbuuren@tno.nl, KarinGroothuis-Oudshoorn c.g.m.oudshoorn@utwente.nl, 2000-2010, withcontributions of Alexander Robitzsch, Gerko Vink, Shahab Jolani,Roel de Jong, Jason Turner, Lisa Doove,John Fox, Frank E. Harrell, and Peter Malewski.

## Details

The mice package contains functions to

Inspect the missing data pattern

Impute the missing data

*m*times, resulting in*m*completed data setsDiagnose 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 Gibbssampling. Missing data can occur anywhere in the data. The algorithm imputesan incomplete column (the target column) by generating 'plausible' syntheticvalues given other columns in the data. Each incomplete column must act as atarget column, and has its own specific set of predictors. The default set ofpredictors for a given target consists of all other columns in the data. Forpredictors that are incomplete themselves, the most recently generatedimputations are used to complete the predictors prior to imputation of thetarget column.

A separate univariate imputation model can be specified for each column. Thedefault imputation method depends on the measurement level of the targetcolumn. In addition to these, several other methods are provided. You canalso write their own imputation functions, and call these from within thealgorithm.

The data may contain categorical variables that are used in a regressions onother variables. The algorithm creates dummy variables for the categories ofthese variables, and imputes these from the corresponding categoricalvariable.

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 |

`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 | Level-1 normal heteroscedastic |

`2l.lmer` | numeric | Level-1 normal homoscedastic, lmer |

`2l.pan` | numeric | Level-1 normal homoscedastic, pan |

`2l.bin` | binary | Level-1 logistic, glmer |

`2lonly.mean` | numeric | Level-2 class mean |

`2lonly.norm` | numeric | Level-2 class normal |

`2lonly.pmm` | any | Level-2 class predictive mean matching |

These corresponding functions are coded in the `mice`

library undernames `mice.impute.method`

, where `method`

is a string with thename of the univariate imputation method name, for example `norm`

. The`method`

argument specifies the methods to be used. For the `j`

'thcolumn, `mice()`

calls the first occurrence of`paste('mice.impute.', method[j], sep = '')`

in the search path. Themechanism 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 bysetting its entry to the empty method: `""`

. For complete columns withoutmissing data `mice`

will automatically set the empty method. Setting the empty method does not produce imputations for the column, so any missingcells remain `NA`

. If column A contains `NA`

's and is used aspredictor in the imputation model for column B, then `mice`

produces noimputations for the rows in B where A is missing. The imputed datafor B may thus contain `NA`

's. The remedy is to remove column A fromthe imputation model for the other columns in the data. This can be doneby setting the entire column for variable A in the `predictorMatrix`

equal to zero.

*Passive imputation:* `mice()`

supports a special built-in method,called passive imputation. This method can be used to ensure that a datatransform always depends on the most recently generated imputations. In somecases, an imputation model may need transformed data in addition to theoriginal data (e.g. log, quadratic, recodes, interaction, sum scores, and soon).

Passive imputation maintains consistency among different transformations ofthe same data. Passive imputation is invoked if `~`

is specified as thefirst 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 deterministicdependencies among the columns. For example, suppose that the missing entriesin variables `data$height`

and `data$weight`

are imputed. The bodymass index (BMI) can be calculated within `mice`

by specifying thestring `'~I(weight/height^2)'`

as the univariate imputation method forthe target column `data$bmi`

. Note that the `~`

mechanism worksonly on those entries which have missing values in the target column. Youshould make sure that the combined observed and imputed parts of the targetcolumn make sense. An easy way to create consistency is by coding all entriesin the target as `NA`

, but for large data sets, this could beinefficient. Note that you may also need to adapt the default`predictorMatrix`

to evade linear dependencies among the predictors thatcould cause errors like `Error in solve.default()`

or `Error:system is exactly singular`

. Though not strictly needed, it is often usefulto specify `visitSequence`

such that the column that is imputed by the`~`

mechanism is visited each time after one of its predictors wasvisited. In that way, deterministic relation between columns will always besynchronized.

#'A new argument `ls.meth`

can be parsed to the lower level`.norm.draw`

to specify the method for generating the least squaresestimates and any subsequently derived estimates. Argument `ls.meth`

takes one of three inputs: `"qr"`

for QR-decomposition, `"svd"`

forsingular 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 overthe corresponding row in the `predictMatrix`

argument. Thisprecedence is, however, restricted to the subset of variablesspecified in the terms of the block formula. Anyvariables not specified by `formulas`

are imputedaccording to the `predictMatrix`

specification. Variables withnon-zero `type`

values in the `predictMatrix`

willbe added as main effects to the `formulas`

, which willact as supplementary covariates in the imputation model. It is possibleto turn off this behavior by specifying theargument `auxiliary = FALSE`

.

## References

van Buuren, S., Boshuizen, H.C., Knook, D.L. (1999) Multipleimputation 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 ofStatistical Computation and Simulation*, **76**, 12, 1049--1064.

van Buuren, S., Groothuis-Oudshoorn, K. (2011). `mice`

:Multivariate Imputation by Chained Equations in `R`

. *Journal ofStatistical Software*, **45**(3), 1--67. tools:::Rd_expr_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., Groothuis-Oudshoorn, K. (2011). `mice`

:Multivariate Imputation by Chained Equations in `R`

. *Journal ofStatistical Software*, **45**(3), 1-67.tools:::Rd_expr_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., Groothuis-Oudshoorn C.G.M., Rubin, D.B. (2006)Fully conditional specification in multivariate imputation. *Journal ofStatistical Computation and Simulation*, **76**, 12, 1049--1064.

Van Buuren, S. (2007) Multiple imputation of discrete and continuous data byfully conditional specification. *Statistical Methods in MedicalResearch*, **16**, 3, 219--242.

Van Buuren, S., Boshuizen, H.C., Knook, D.L. (1999) Multiple imputation ofmissing blood pressure covariates in survival analysis. *Statistics inMedicine*, **18**, 681--694.

Brand, J.P.L. (1999) *Development, implementation and evaluation ofmultiple imputation strategies for the statistical analysis of incompletedata sets.* Dissertation. Rotterdam: Erasmus University.

## See Also

`mice`

, `with.mids`

,`pool`

, `complete`

, `ampute`

`mids`

, `with.mids`

,`set.seed`

, `complete`

## Examples

`# do default multiple imputation on a numeric matriximp <- mice(nhanes)imp# list the actual imputations for BMIimp$imp$bmi# first completed data matrixcomplete(imp)# imputation on mixed data with a different method per columnmice(nhanes2, meth = c("sample", "pmm", "logreg", "norm"))if (FALSE) {# example where we fit the imputation model on the train data# and apply the model to impute the test dataset.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 datasetimp <- mice(nhanes2, m = 2, ignore = ignore, print = FALSE, seed = 22112)imp.test1 <- filter(imp, ignore)imp.test1$datacomplete(imp.test1, 1)complete(imp.test1, 2)# scenario 2: train and test in separate datasetstraindata <- 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)}`

Run the code above in your browser using DataLab