[R] predict() an rpart() model: how to ignore missing levels in a factor

jamessc james.faulconbridge at googlemail.com
Thu Nov 18 18:35:41 CET 2010

I am using an algorigm to split my data set into two random sections
repeatedly and constuct a model using rpart() on one, test on the other and
average out the results.

One of my variables is a factor(crop) where each crop type has a code. Some
crop types occur infrequently or singly. when the data set is randomly
split, it may be that the first data set has a crop type which is not
present in the second and so using predict() I get the error:

Error in model.frame.default(Terms, newdata, na.action = na.action, xlev =
attr(object,  : 
  factor 'factor(c2001)' has new level(s) 13, 24, 35

where c2001 is the crop. I would like the predict function to ignore these
records. is there a command which will allow this as part of the predict()
function? With those with a small number of records (eg. 3-4), I would hope
some of the models would have the right balance to allow a prediction to be
View this message in context: http://r.789695.n4.nabble.com/predict-an-rpart-model-how-to-ignore-missing-levels-in-a-factor-tp3049218p3049218.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list