[R] Rpart -- using predict() when missing data is present?

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Oct 8 20:07:32 CEST 2005


On Sat, 8 Oct 2005, Ajay Narottam Shah wrote:

> I am doing
>
>> library(rpart)
>> m <- rpart("y ~ x", D[insample,])
>> D[outsample,]
>            y           x
> 8  0.78391922 0.579025591
> 9  0.06629211          NA
> 10         NA 0.001593063
>>   p <- predict(m, newdata=D[9,])
> Error in model.frame(formula, rownames, variables, varnames, extras, extranames,  :
> 	invalid result from na.action
>
> How do I persuade him to give me NA since x is NA?

I think the point is to do something sensible!  One x prediction problems 
are not what rpart is designed to do, and the default na.action (na.rpart) 
fails in that case.  (The author forgot drop=F.)

> I looked at ?predict.rpart but didn't find any mention about NAs.

How about ?rpart ?  That does.

> (In this problem, I can easily do it manually, but this is a part of
> something bigger where I want him to be able to gracefully handle
> prediction requests involving NA).

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