[R] predict rpart: new data has new level

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Jul 31 07:52:28 CEST 2008

On Thu, 31 Jul 2008, Chua Siang Li wrote:

>   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. "

That's about a level defined in the factor passed to rpart but not present 
in the data, not a level not present in the factor in the original data. 
E.g. if 'fruit' has levels apple, orange, lemon and there were no oranges 
in the training set.  Your situation is that you asked for predictions on 

>   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.

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-help mailing list