2

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?

rcs
  • 67,191
  • 22
  • 172
  • 153
user3056186
  • 839
  • 1
  • 11
  • 16
  • please post a reproducible example so we can see what you're trying to do (https://stackoverflow.com/questions/5963269) – rcs Apr 11 '14 at 07:10
  • One hack given in the last answer of this question: http://stackoverflow.com/questions/19924402/cforest-prints-empty-tree – Divi Jan 28 '16 at 00:03

1 Answers1

0

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/

Nathaniel Payne
  • 2,749
  • 1
  • 28
  • 32
  • I tried both suggested solutions but still have same error, here is new code data.controls <- cforest_unbiased(ntree=1000, mtry=3) crs_rf <- cforest(as.factor(Censor) ~ ., data=crs$dataset[crs$sample,c(crs$input, crs$target)], controls=data.controls) cf <- crs_rf tr <- party:::prettytree(cf@ensemble[[1]], names(cf@data@get("input"))) #pt plot(new("BinaryTree", tree=tr, data=cf@data, responses=cf@responses)) – user3056186 Apr 11 '14 at 05:52
  • Let's forget about the plotting for a moment. Can you put in the calls 1 at a time into R, run them, and show the output in your message. Also, what about changing as.factor(Censor) to as.factor(crs_rf$Censor) – Nathaniel Payne Apr 11 '14 at 05:54
  • Show the output of this call first: data.controls <- cforest_unbiased(ntree=1000, mtry=3) – Nathaniel Payne Apr 11 '14 at 05:55
  • Then, show the output of this call after: crs_rf <- cforest(as.factor(Censor) ~ ., data=crs$dataset[crs$sample,c(crs$input, crs$target)], controls=data.controls) – Nathaniel Payne Apr 11 '14 at 05:55
  • Also, if you type crs, what is the output? I am concerned about this line: data=crs$dataset[crs$sample,c(crs$input, crs$target)]. Check if you get an error when you run this. – Nathaniel Payne Apr 11 '14 at 05:57
  • partial output of data.controls ## An object of class "ForestControl" ## Slot "ntree": ## [1] 1000 ## ## Slot "replace": ## [1] FALSE ## ## Slot "fraction": ## [1] 0.632 ## ## Slot "trace": ## [1] FALSE ## – user3056186 Apr 11 '14 at 06:09
  • and output of cforest i.e.crs_rf ## ## Random Forest using Conditional Inference Trees ## ## Number of trees: 500 ## ## Response: as.factor(Censor) ## Inputs: ip1,ip2 ## Number of observations: 2431 – user3056186 Apr 11 '14 at 06:10
  • also when i print "tr" it shows the tree in text format – user3056186 Apr 11 '14 at 06:13
  • So, I am confused on the error then. You have a tree that has been generated, which means all the original calls are correct. The problem then is the plot part of the last function (if I understand, to generate the tree). Do you get the error when you type this command: plot(new("BinaryTree", tree=tr, data=cf@data, responses=cf@responses)) – Nathaniel Payne Apr 11 '14 at 06:19
  • So, at least the suggestion of breaking the call out, which was made above, helped :-) Can you take out the new() wrapper – Nathaniel Payne Apr 11 '14 at 06:23
  • I think the plot call format is off. I am looking here: http://cran.r-project.org/web/packages/party/party.pdf – Nathaniel Payne Apr 11 '14 at 06:24
  • what if you just do plot(crs_rf) or plot(tr) – Nathaniel Payne Apr 11 '14 at 06:25
  • plot(crs_rf) gives error Error: cannot coerce type 'S4' to vector of type 'double'. plot(tr) gives error ## Error: 'x' is a list, but does not have components 'x' and 'y' – user3056186 Apr 11 '14 at 06:30
  • i am using cforest not party package, – user3056186 Apr 11 '14 at 06:32
  • I see. But this call in your code is calling the party package: tr <- party:::prettytree(cf@ensemble[[1]], names(cf@data@get("input"))). – Nathaniel Payne Apr 11 '14 at 06:37
  • Perhaps have a look at the introduction to the party package: " Based on conditional inference trees, cforest() provides an implementation of Breiman's random forests. The function mob() implements an algorithm for recursive partitioning based on parametric models (e.g. linear models, GLMs or survival regression) employing parameter instability tests for split selection. Extensible functionality for visualizing tree-structured regression models is available" – Nathaniel Payne Apr 11 '14 at 06:38