简体   繁体   English

R使用cforest的随机森林,如何绘制树

[英]R random forest using cforest, how to plot tree

I have created random forest model using cforest我使用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错误:没有为“strwidth/height”单位提供字符串

Any help how to overcome this error?任何帮助如何克服这个错误?

Looking at your code, I assume that crs is referring to a data frame.查看您的代码,我假设 crs 指的是数据框。 The dollar sign may be a problem (specifically crs$rf).美元符号可能是个问题(特别是 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.如果 crs 确实是一个数据框,那么 $ 将告诉 R 通过使用通常的列表索引设备从数据框内提取项目。 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/如果您需要帮助使用 cforest() 绘制树,您可能会考虑查看这个优秀的资源(因为我相信问题的根源在于您的绘图函数调用): http ://www.r-bloggers.com/a -简要游览树木和森林/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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