Re: [R] How to add decision trees into a list?

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Sun 14 Aug 2005 - 22:23:42 EST

You have not created a list to add trees (not 'decision trees' from rpart, BTW) to. You need something like

collection <- vector("list", 5)
collection[[1]] <- fit
...

Reading ?c should illuminate your mistake: note that 'fit' is a list (and so already a vector).

On Sun, 14 Aug 2005, Martin Lam wrote:

> Hi,
>
> I am somewhat new to R so this question may be
> foolish, but is it possible to add decision trees into
> a list, array or vector in R?
>
> I am trying to build a collection (ensemble) of
> decision trees. Every time a new instance arrive I
> need to get the prediction of each decision tree. I
> have tried to add a decision tree into a variable but
> without luck. Is a special package needed perhaps?

No, just reading the basic documentation.

> This is what I tried:
>
> # load the rpart package
> library("rpart")
>
> # 6*3 (18) samples are left out for prediction
> sub <- c(sample(1:50, 45), sample(51:100, 45),
> sample(101:150, 45))
>
> # train the tree with the subset
> fit <- rpart(Species ~ ., data=iris, subset=sub)
>
> # make a nice test table
> table(predict(fit, iris[-sub,], type="class"),
> iris[-sub, "Species"])
>
> # add fit to the collection
> collection <- fit
>
> # for the illustrative purpose, I add fit again to the
> # collection
> collection <- c(collection, fit)
>
> # get the first tree in the collection???
> tree1 <- collection[1]
>
> # test the tree
> table(predict(tree1, iris[-sub,], type="class"),
> iris[-sub, "Species"])
>
> # error message from R
> Error in predict(tree1, iris[-sub, ], type = "class")
> : no applicable method for "predict"
>
>
> The problem is that I am not able to add the decision
> trees into a variable/list. Somehow the function 'c'
> isn't 'copying' it right.
>
> Any help is appreciated.
>
> Thanks in advance,
>
> M. Lam
>
> ______________________________________________
> R-help@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
>

-- 
Brian D. Ripley,                  ripley@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

______________________________________________
R-help@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
Received on Sun Aug 14 22:31:16 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 15:19:24 EST