[R] predict rpart: new data has new level

From: Chua Siang Li <siang.li.chua_at_acceval-intl.com>
Date: Thu, 31 Jul 2008 11:37:16 +0800

   Hi. I uses rpart to build a regression tree. Y is continuous. Now, I try    to predict on a new set of data. In the new set of data, one of my x (call    Incoterm, a factor) has a new level.
   I wonder why the error below appears as the guide says "For factor    predictors, if an observation contains a level not used to grow the tree, it    is left at the deepest possible node and frame$yval at the node is the
   prediction. "
   Many thanks.
> mod <- rpart(y~., data=data.frame(y=y,x=x), method="anova",

   +                       cp=0.05, minsplit=100, minbucket=50, maxdepth=5)

> predictLost <- predict(mod, newdata=data.frame(y=yLost, x=xLost),
   type="vector")
   Error in model.frame.default(Terms, newdata, na.action = act, xlev =    attr(object, :
     factor 'x.Incoterm' has new level(s) MTD

   Chua Siang Li
   Consultant - Operations Research
   Acceval Pte Ltd
   Tel: 6297 8740
   Email: siang.li.chua_at_acceval-intl.com    Website: www.acceval-intl.com
   This message and any attachments (the "message"...{{dropped:13}}

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 31 Jul 2008 - 03:39:39 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 31 Jul 2008 - 06:33:12 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive