I have a set of x, y data I'm using to build a random forest. The x data is a vector of values that includes some NAs. So I use rfImpute to handle the missing data and create a random forest. Now I have a new unseen observation x (with an NA) and I want to predict y. How do I impute the missing value so that I may use the random forest that I have already grown? The rfImpute function seems to require x and y. I only have x for prediction purposes.

My question is similar (but different) to this question. And for example, I can use the same iris dataset. If I've correctly interpreted the code in the answer to the question I reference, the code iris.na[148, , drop=FALSE] in the statement iris.na2 = rbind(iris.imputed, iris.na[148, , drop=FALSE]) represents the new data which includes the Species (the Y value). In my problem I would not know the Species—I want to use the random forest to predict that. I would have the 4 independent variables, but some might be NA for a given row. To continue the analogy, imagine I have 3 of the 4 variables (one is missing). I want to impute that value. Then I want to predict the species which I do not know.

In response to gung's comment that I should add an illustration, let me put it in terms of the iris data set. Imagine I have the following data on a flower. I know it's Sepal.Length, Sepal.Width, Petal.Length, but not the Petal.Width. I'd like to impute the Petal.Width and then use those 4 values within a RF model to predict the Species.

  • $\begingroup$ Are you saying you have nothing? You start with only x & y to build a model, & now you want to predict y for an observation for which you have neither x nor y. Is that correct? $\endgroup$ – gung - Reinstate Monica Sep 22 '15 at 18:18
  • $\begingroup$ gung, I have something. I have a new x observation but no y. I want to predict the y based on the new x. But the x vector has some NA values which I want to impute. Thanks. $\endgroup$ – rmacey Sep 23 '15 at 19:21
  • $\begingroup$ I see, so x is a set of predictor variables where you have some but not others. Is that right? You may want to update your Q (which will bump it on the main page), & you may want to include a small illustrative example. One note of caution: this Q looks on the border of 'how do I use R?' &/or 'what code will help me do this?', which will be considered to program-y & could be closed. You should make sure to be clear that this is a question about RF, not how to do RF w/ R. $\endgroup$ – gung - Reinstate Monica Sep 23 '15 at 19:45
  • $\begingroup$ LOL...when I put it on plain stack exchange, they suggested I move it here. This is really about rfImpute or something similar. $\endgroup$ – rmacey Sep 24 '15 at 21:36
  • $\begingroup$ By "plain stack exchange", do you mean Stack Overflow? If your question is how to use the code, it goes there; if it's how to understand the underlying statistics, it goes here. (Of course, there are also Q's that are off topic both places.) I don't think your Q is necessarily off topic here. I'm just informing you to be careful how you word it, so it's clear that it's a statistical question, not a coding question. $\endgroup$ – gung - Reinstate Monica Sep 24 '15 at 21:41

I think you need an unsupervised imputing method. That is one which do not use the target values for imputation. If you only have few prediction feature vectors, it may be difficult to uncover a data structure. Instead you could mix your predictions with already imputed training feature vectors and use this structure to impute once again. Notice this procedure may violate assumptions of independence, therefore wrap the entire procedure in an outer cross-validation to check for serious overfitting.

I just learned about missForest from a comment to this question. missForest seems to do the trick. I simulated your problem on the iris data. (without outer cross-validation)

n.train = 100
train.index = sample(nrow(iris),n.train)
feature.train = as.matrix(iris[ train.index,1:4])
feature.test  = as.matrix(iris[-train.index,1:4])

#simulate 40 NAs in train
n.NAs = 40
NA.index = sample(length(feature.train),n.NAs)
NA.feature.train = feature.train; NA.feature.train[NA.index] = NA

#imputing 40 NAs unsupervised
imp.feature.train = missForest(NA.feature.train)$ximp
#check how well imputation went, seems promsing for this data set
plot(    feature.train[NA.index],xlab="true value",
     imp.feature.train[NA.index],ylab="imp  value",)

#simulate random NAs in feature test
feature.test[sample(length(feature.test),20)] = NA

#mix feature.test with imp.feature.train
nrow.test = nrow(feature.test)
mix.feature = rbind(feature.test,imp.feature.train)
imp.feature.test = missForest(mix.feature)$ximp[1:nrow.test,]

#train RF and predict
rf = randomForest(imp.feature.train,iris$Species[train.index])
pred.test = predict(rf,imp.feature.test)
table(pred.test, iris$Species[-train.index])

pred.test    setosa versicolor virginica
  setosa         12          0         0
  versicolor      0         20         2
  virginica       0          1        15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.