0
votes
$\begingroup$

I fitted a RPart model from all of a couple dozen variables. Now, I want to test predictions using just a couple of variables that seem significant to me.

If it was the iris dataset, for example, I would have fit the model to all the characteristics but I wanted to test the prediction just from Petal.width and Sepal.length.

When I try making a new dataframe with only those values to enter as newdata, it complains the dataframe is missing Sepal.width, for example.

What am I doing wrong?

$\endgroup$
1
  • $\begingroup$ I am not sure if this will be considered on topic here (given the software emphasis). However, in order for anyone to help, I think you will need to provide a few lines of code showing what you tried. $\endgroup$
    – GeoMatt22
    Sep 12 '16 at 3:20
0
votes
$\begingroup$

I'm not quite sure why you would want to do that... Fitting a tree with more variables that the ones you are going to use to predict will bias the results: the splittings you are using are suboptimal.

In any case, if you really want to go ahead, you could create the missing columns and fill them with NAs, being careful to change their classes to their corresponding ones in the training dataframe. For example:

mod <- rpart(Species~., iris)
iris2 <- iris[,-3] 
iris2$Petal.Length <- rep(NA, nrow(iris2))
class(iris2$Petal.Length) <- 'numeric'
predictions <- predict(mod, newdata = iris2, na.action = na.pass) 

How rpart handles the NAs (na.action) is explained here: https://stat.ethz.ch/R-manual/R-devel/library/rpart/html/predict.rpart.html

$\endgroup$

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