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

From: Martin Lam <tmlammail_at_yahoo.com>
Date: Sun 14 Aug 2005 - 21:26:35 EST


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?

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 Received on Sun Aug 14 21:34:17 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:39:49 EST