[R] predict.tree

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed May 9 20:06:15 CEST 2007


The idea is that you use

treemod<-tree(y~x1+x2, data = old)
predict(treemod, new, type = "class")

where new is a data frame containing the same column names as old (except 
perhaps 'y').

This applies to all model fitting functions, not just tree and rpart.

On Wed, 9 May 2007, ande8047 at umn.edu wrote:

> I have a classification tree model similar to the following (slightly
> simplified here):
>
>> treemod<-tree(y~x)
>
> where y is a factor and x is a matrix of numeric predictors. They have
> dimensions:
>
>> length(y)
> [1] 1163
>> dim(x)
> [1] 1163   75
>
> I’ve evaluated the tree model and am happy with the fit. I also have a
> matrix of cases that I want to use the tree model to classify. Call it
> newx:
>
>> dim(newx)
> [1] 68842    75
>
> The column names of newx match the column names of x. It seems that
> prediction should be straightforward. To classify the first 10 values of
> newx, for example, I think I should use:
>
>> predict(treemod, newx[1:10,], type = "class")
>
> However, this returns a vector of the predicted classes of the training
> data x, rather than the predicted classes of the new data. The returned
> vector has length 1163, not length 10. This occurs regardless of the number
> of rows in newx. It gives this warning message:
>
> 'newdata' had 10 rows but variable(s) found have 1163 rows
>
> I must be misunderstanding the way I should format the newdata I pass to
> predict. I’ve tried the rpart package as well, but have a similar problem.
> What am I missing?
>
> Thanks in advance,
>
> Ryan Anderson
> Graduate Student
> Dept. of Forest Resources
> University of Minnesota
> ande8047 at umn.edu
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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