# What is the proper way to use rfImpute? (Imputation by Random Forest in R)

Is it right to use rfImpute to impute missing feature values on the whole data set and then use other regression/classification techniques on the new data set created? Or, is an rfImpute model intended to be fitted on a subset of the data and then that fitted model is somehow used to fill in missing values in the rest of the data?

To be clear, I only bring up these questions because rfImpute seems to require arguments for both X (features) and y (target variable). In addition, y cannot have any missing values. Does this mean that y gets used for the imputation of the features? Wouldn't this be harmful later when trying to fit models to the new imputed data set? Obviously the y values we are trying to predict in the future won't be known, so how could rfImpute fit into a machine learning pipeline?

Thank you!

• Are you trying to get 1 imputed dataset, or multiple imputed datasets (mice)? – RNB Aug 2 '16 at 6:34
• In either case, when trying to predict on new data in the future rfImpute would not work since those y values would be missing. It looks like there is a workaround: stackoverflow.com/questions/20537186/… However, I am still worried about the target variable being used to impute missing training data. – Doug7 Aug 2 '16 at 7:48

I'm not entirely sure if this is an answer to your question, but maybe you'll find it useful.

Maybe the author of the randomForest package would disagree with me, but I feel like the rfImpute() function is mostly used or called upon other imputation packages in their algorithms to impute many variables. If you only have one variable with missing data, then using this function as a stand alone may work. However, I think it is the case for most people that they have many variables with missing data in a datset that they'd like to impute. Enter the packages missForest and mice.

If you use the R package missForest, you can impute your entire dataset (many variables of different types may be missing) with one command missForest(). If I recall correctly, this function draws on the rfImpute() function from the randomForest package. For some reason (maybe others can elaborate), when you use the missForest() function, the other variables that are used to predict a single variable can also have missingness. So I think using this function and package are a nice idea if you are hoping to only get one dataset out, after all variables have been imputed.

The downside to using missForest() is that you only get one dataset, which does not allow you to take into account the uncertainty of your estimates (in your follow-on analytical models). So your analytical models will have incorrect confidence intervals if you just base the analysis on that one imputed dataset. If that doesn't matter to you, then I highly recommend this package and function, because it is very easy to use and specify your imputation model.

However, if you do need to get appropriate confidence intervals and pooled estimates in your analytical models, then you should probably use multivariate imputation by chained equations (MICE) approaches to imputation. For this, you can use the mice package. There is recent functionality within this package that allows you to specify which variables you'd like to impute with a random forest algorithm, and which you would like to use the usual methods (e.g. pmm). When specifying your imputation model with the mice() function, under methods you would do something like meth <- c("rfcat", "rfcont").

missForest has a nice vignette you can look up in R.

Here is a nice resource for how to set up your imputation models using mice:

http://www.stefvanbuuren.nl/publications/MICE%20in%20R%20-%20Draft.pdf

• Thank you for the useful information. missForest is well featured and works great. By the way, it is possible to get multiple datasets out of it. All you need to do is run it multiple times with different random seeds. – Doug7 Aug 5 '16 at 4:29
• Good tip. I suppose if you impute, say 50, datasets that way, then you could theoretically do a bit of manual manipulation to get those datasets into a mids object so that you can use mice post-impuation pooling functions for your analytical models. – RNB Aug 5 '16 at 10:02
• Thanks for pointing out missForest (I hadn't seen it before), but it doesn't use rfImpute. rfImpute will populate the missing values with the vector median (if continuous) or mode (if factor). missForest seems to actually use a random forest algorithm in order to impute the values. I think that's what OP was looking for anyway, but I wanted to clarify. – EconomySizeAl May 8 '18 at 15:02