[R] How to add decision trees into a list?
Martin Lam
tmlammail at yahoo.com
Sun Aug 14 13:26:35 CEST 2005
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
More information about the R-help
mailing list