R中随机森林地块的图例

 为爱进地狱天堂_954 发布于 2023-02-13 14:05

我使用randomForest函数在R中创建了一个随机森林预测模型:

model = randomForest(classification ~., data=train, ntree=100, proximity=T)

接下来,我绘制模型以查看模型的整体错误:

plot(model, log="y")

这给了我以下情节: 在此输入图像描述

我的问题是如何在这上面添加一个图例,以便我可以看到哪个颜色对应于用于分类的因子中的每个值?因子变量是data$classification.我无法弄清楚要做到这一点的legend()调用.

1 个回答
  • 图S3方法图用于matplot绘制随机森林模型.您应该手动添加图例.这应该是一个好的开始:

    library(randomForest)
    model = randomForest(Species ~., data=iris, ntree=100, proximity=T)
    layout(matrix(c(1,2),nrow=1),
           width=c(4,1)) 
    par(mar=c(5,4,4,0)) #No margin on the right side
    plot(model, log="y")
    par(mar=c(5,0,4,2)) #No margin on the left side
    plot(c(0,1),type="n", axes=F, xlab="", ylab="")
    legend("top", colnames(model$err.rate),col=1:4,cex=0.8,fill=1:4)
    

    在此输入图像描述

    2023-02-13 14:08 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有