missRanger: Fast Imputation of Missing Values by Chained Random Forests

View source: R/missRanger.R

missRangerR Documentation

Fast Imputation of Missing Values by Chained Random Forests

Description

Uses the "ranger" package (Wright & Ziegler) to do fast missing value imputation by chained random forests, see Stekhoven & Buehlmann and Van Buuren & Groothuis-Oudshoorn. Between the iterative model fitting, it offers the option of predictive mean matching. This firstly avoids imputation with values not present in the original data (like a value 0.3334 in a 0-1 coded variable). Secondly, predictive mean matching tries to raise the variance in the resulting conditional distributions to a realistic level. This allows to do multiple imputation when repeating the call to missRanger().

Usage

missRanger(
  data,
  formula = . ~ .,
  pmm.k = 0L,
  num.trees = 500,
  mtry = NULL,
  min.node.size = NULL,
  min.bucket = NULL,
  max.depth = NULL,
  replace = TRUE,
  sample.fraction = if (replace) 1 else 0.632,
  case.weights = NULL,
  num.threads = NULL,
  save.memory = FALSE,
  maxiter = 10L,
  seed = NULL,
  verbose = 1,
  returnOOB = FALSE,
  data_only = !keep_forests,
  keep_forests = FALSE,
  ...
)

Arguments

data

A data.frame with missing values to impute.

formula

A two-sided formula specifying variables to be imputed (left hand side) and variables used to impute (right hand side). Defaults to . ~ ., i.e., use all variables to impute all variables. For instance, if all variables (with missings) should be imputed by all variables except variable "ID", use . ~ . - ID. Note that a "." is evaluated separately for each side of the formula. Further note that variables with missings must appear in the left hand side if they should be used on the right hand side.

pmm.k

Number of candidate non-missing values to sample from in the predictive mean matching steps. 0 to avoid this step.

num.trees

Number of trees passed to ranger::ranger().

mtry

Number of covariates considered per split. The default NULL equals the rounded down root of the number of features. Can be a function, e.g., function(p) trunc(p/3). Passed to ranger::ranger(). Note that during the first iteration, the number of features is growing. Thus, a fixed value can lead to an error. Using a function like function(p) min(p, 2) will fix such problem.

min.node.size

Minimal node size passed to ranger::ranger(). By default 1 for classification and 5 for regression.

min.bucket

Minimal terminal node size passed to ranger::ranger(). The default NULL means 1.

max.depth

Maximal tree depth passed to ranger::ranger(). NULL means unlimited depth. 1 means single split trees.

replace

Sample with replacement passed to ranger::ranger().

sample.fraction

Fraction of rows per tree passed to ranger::ranger(). The default: use all rows when replace = TRUE and 0.632 otherwise.

case.weights

Optional case weights passed to ranger::ranger().

num.threads

Number of threads passed to ranger::ranger(). The default NULL uses all threads.

save.memory

Slow but memory saving mode of ranger::ranger().

maxiter

Maximum number of iterations.

seed

Integer seed.

verbose

A value in 0, 1, 2 controlling the verbosity.

returnOOB

Should the final average OOB prediction errors be added as data attribute "oob"? Only relevant when data_only = TRUE.

data_only

If TRUE (default), only the imputed data is returned. Otherwise, a "missRanger" object with additional information is returned.

keep_forests

Should the random forests of the last relevant iteration be returned? The default is FALSE. Setting this option will use a lot of memory. Only relevant when data_only = TRUE.

...

Additional arguments passed to ranger::ranger(). Not all make sense.

Details

The iterative chaining stops as soon as maxiter is reached or if the average out-of-bag (OOB) prediction errors stop reducing. In the latter case, except for the first iteration, the second last (= best) imputed data is returned.

OOB prediction errors are quantified as 1 - R^2 for numeric variables, and as classification error otherwise. If a variable has been imputed only univariately, the value is 1.

Value

If data_only = TRUE an imputed data.frame. Otherwise, a "missRanger" object with the following elements:

  • data: The imputed data.

  • data_raw: The original data provided.

  • forests: When keep_forests = TRUE, a list of "ranger" models used to generate the imputed data. NULL otherwise.

  • to_impute: Variables to be imputed (in this order).

  • impute_by: Variables used for imputation.

  • best_iter: Best iteration.

  • pred_errors: Per-iteration OOB prediction errors (1 - R^2 for regression, classification error otherwise).

  • mean_pred_errors: Per-iteration averages of OOB prediction errors.

  • pmm.k: Same as input pmm.k.

References

  1. Wright, M. N. & Ziegler, A. (2016). ranger: A Fast Implementation of Random Forests for High Dimensional Data in C++ and R. Journal of Statistical Software, in press. <arxiv.org/abs/1508.04409>.

  2. Stekhoven, D.J. and Buehlmann, P. (2012). 'MissForest - nonparametric missing value imputation for mixed-type data', Bioinformatics, 28(1) 2012, 112-118. https://doi.org/10.1093/bioinformatics/btr597.

  3. 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/

Examples

iris2 <- generateNA(iris, seed = 1)

imp1 <- missRanger(iris2, pmm.k = 5, num.trees = 50, seed = 1)
head(imp1)

# Extended output
imp2 <- missRanger(iris2, pmm.k = 5, num.trees = 50, data_only = FALSE, seed = 1)
summary(imp2)

all.equal(imp1, imp2$data)

# Formula interface: Univariate imputation of Species and Sepal.Width
imp3 <- missRanger(iris2, Species + Sepal.Width ~ 1)

missRanger documentation built on Sept. 12, 2024, 7:15 a.m.