首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对` `bwplot()`中的`aret`模型进行排序

对` `bwplot()`中的`aret`模型进行排序
EN

Stack Overflow用户
提问于 2020-08-23 01:23:25
回答 2查看 296关注 0票数 1

我正在绘制用caret训练的年度模型重采样的精确度得分的框图。这些模型按它们所指的年份命名: 2000、2001、2002、...、2010。我希望模型出现在框图中,并根据年份升序排列,即模型的名称。

基于以下代码的重采样摘要

代码语言:javascript
复制
fit.year.res <- resamples(fit.year)
summary(fit.year.res)

看起来像这样:

但是,盒子图中的不同年度模型没有排序:

代码语言:javascript
复制
scales <- list(x=list(relation="free"), y=list(relation="free"))
bwplot(fit.year.res, scales=scales)

我已经尝试过将重采样fit.year.res$models的models元素转换为来自字符的因子,但这并没有什么不同。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-08-23 02:56:07

我不知道使用插入符号包中的bwplot方法有什么简单的解决方案。也许有一个,但我的网格技能是缺乏的。我建议使用ggplot2手动绘制箱形图。这样你就可以更好地控制最终的绘图了。

由于您没有发布包含数据的示例,因此我将使用?caret:::bwplot.resamples中的一个示例

代码语言:javascript
复制
library(caret)
library(party)
library(RWeka)

load(url("http://topepo.github.io/caret/exampleModels.RData"))

resamps <- resamples(list(CART = rpartFit,
                          CondInfTree = ctreeFit,
                          MARS = earthFit))

bwplot(resamps,
       metric = "RMSE")

产生:

要使用ggplot手动绘制绘图,您需要进行一些数据操作:

代码语言:javascript
复制
library(tidyverse)
resamps$values %>% #extract the values
  select(1, ends_with("RMSE")) %>% #select the first column and all columns with a name ending with "RMSE"
  gather(model, RMSE, -1) %>% #convert to long table
  mutate(model = sub("~RMSE", "", model)) %>% #leave just the model names
  ggplot()+ #call ggplot
  geom_boxplot(aes(x = RMSE, y = model)) -> p1 #and plot the box plot

p1

要在y轴上设置特定顺序,请执行以下操作:

代码语言:javascript
复制
p1 +
  scale_y_discrete(limits = c("MARS", "CART", "CondInfTree"))

如果您更喜欢晶格

代码语言:javascript
复制
library(lattice)

resamps$values %>%
  select(1, ends_with("RMSE")) %>%
  gather(model, RMSE, -1) %>%
  mutate(model = sub("~RMSE", "", model)) %>%
  {bwplot(model ~ RMSE, data = .)}

要更改顺序,请更改模型的级别(此方法也适用于ggplot2):

代码语言:javascript
复制
resamps$values %>%
  select(1, ends_with("RMSE")) %>%
  gather(model, RMSE, -1) %>%
  mutate(model = sub("~RMSE", "", model),
         model = factor(model, levels = c("MARS", "CART", "CondInfTree"))) %>%
    {bwplot(model ~ RMSE, data = .)}

票数 2
EN

Stack Overflow用户

发布于 2020-08-23 03:07:58

函数bwplot.resamples用于生成此图,如果您查看underlying code,就会根据变量在感兴趣的度量下的平均性能进行因子分解。

下面我有执行因式分解的相关代码:

代码语言:javascript
复制
bwplot.resamples <- function (x, data = NULL, models = x$models, metric = x$metric, ...)
{
....
  avPerf <- ddply(subset(plotData, Metric == metric[1]),
                  .(Model),
                  function(x) c(Median = median(x$value, na.rm = TRUE)))
  avPerf <- avPerf[order(avPerf$Median),]

    ......
}

我想你需要做的是手动绘制图:

代码语言:javascript
复制
data(BloodBrain)
gbmFit <- train(bbbDescr[,-3], logBBB,"gbm",tuneLength=6,
            trControl = trainControl(method = "cv"),verbose=FALSE)
     
glmnetFit <- train(bbbDescr[,-3], logBBB,"glmnet",tuneLength=6,
            trControl = trainControl(method = "cv"))

rfFit <- train(bbbDescr[,-3], logBBB,"rf",tuneLength=6,
            trControl = trainControl(method = "cv"))

knnFit <- train(bbbDescr[,-3], logBBB,"knn",tuneLength=6,
            trControl = trainControl(method = "cv"))

resamps <- resamples(list(gbm = gbmFit,glmnet=glmnetFit,knn=knnFit,rf=rfFit))

如果绘制,可以看到它们是根据中线(实心点)进行排序的:

代码语言:javascript
复制
bwplot(resamps,metric="MAE")

您可以访问$values下的值,并创建一个函数来绘制它,如下所示:

代码语言:javascript
复制
plotMet = function(obj,metric,var_order){

mat = obj$values
mat = mat[,grep(metric,colnames(mat))]
colnames(mat) = gsub("[~][^ ]*","",colnames(mat))
boxplot(mat[,var_order],horizontal=TRUE,las=2,xlab=metric)

}

plotMet(resamps,"MAE",c("rf","knn","gbm","glmnet"))

用数字来命名你的模型也不是一个好主意。试试model_2000、model_2001等

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63539057

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档