简体   繁体   中英

R random forest using cforest, how to plot tree

I have created random forest model using cforest

library("party")    
crs$rf <- cforest(as.factor(Censor) ~ .,
  data=crs$dataset[crs$sample,c(crs$input, crs$target)],
  controls=cforest_unbiased(ntree=500, mtry=4))
cf <- crs$rf 
tr <- party:::prettytree(cf@ensemble[[1]], names(cf@data@get("input")))  
#tr

plot(new("BinaryTree", tree=tr, data=cf@data, responses=cf@responses))

I get error when plotting tree

Error: no string supplied for 'strwidth/height' unit

Any help how to overcome this error?

Looking at your code, I assume that crs is referring to a data frame. The dollar sign may be a problem (specifically crs$rf). If crs is indeed a data frame, then the $ would tell R to extract items from inside the dataframe by using the usual list indexing device. This may be conflicting with the call to generate the random forest, and be causing the error. You could fix this by starting with:

crs_rf <- cforest(as.factor(Censor) ~ ., .....

Which would create the random forest object. This would replace:

crs$rf <- cforest(as.factor(Censor) ~ ., ......

As a reference, in case this doesn't fix it, I wanted to refer you to a great guide from Stanford that covers random forests . They do have examples showing how the party package is used. In order to make trouble shooting easier, I might recommend pulling apart the call like they do in the guide. For example (taking from the guide that is provided), first, set the controls:

data.controls <- cforest_unbiased(ntree=1000, mtry=3) 

Then make the call:

data.cforest <- cforest(Resp ~ x + y + z…, data = mydata, controls=data.controls)

Then generate the plot once the call works. If you need help plotting trees using cforest(), you might think about looking at this excellent resource (as I believe the root of the problem is with your plotting function call): http://www.r-bloggers.com/a-brief-tour-of-the-trees-and-forests/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM