Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R package "tree": how to control the maximum tree depth?

The R package "tree" restricts the maximum tree depth to 31. If the function tree is applied to a large dataset, this limit is easily reached:

> library("tree")
> library("ElemStatLearn")
> data <- list(image=as.matrix(zip.train[,-1]), digit=as.factor(zip.train[,1]))
> t <- tree(digit~image, data, split="gini")
Error in tree(digit ~ image, data, split = "gini") : 
  maximum depth reached
Calls: source -> withVisible -> eval -> eval -> tree -> .C

Is there a way to tell tree to just stop growing the tree when the maximum tree depth is reached, rather than aborting with an error? (In other words: is there an equivalent for the maxdepth parameter of rpart.control?)

like image 973
jochen Avatar asked Oct 19 '22 06:10

jochen


1 Answers

I agree with @jochen's suggestion that the rpart package seems to be more polished than the tree package. However, there are some little things that the tree package seems to be doing better. For example, it's much easier to draw decision boundaries for a tree object than it is for an rpart object (especially using ggplot). Regarding Vincent's question, I had some limited success controlling the depth of a tree tree by using the tree.control(min.cut=) option as in the code below. By changing the minimum nuber of elements in a terminal node you may be able to exercise limited control over depth.

library("ElemStatLearn")
 library("tree")
 data <- list(image=as.matrix(zip.train[,-1]), digit=as.factor(zip.train[,1]))
 t <- tree(digit~image, data, split="gini", control=tree.control(1866496, mincut = 1000))
 library(maptree)
 draw.tree(t)
like image 119
user3204008 Avatar answered Oct 22 '22 00:10

user3204008