我正在绘制用caret训练的年度模型重采样的精确度得分的框图。这些模型按它们所指的年份命名: 2000、2001、2002、...、2010。我希望模型出现在框图中,并根据年份升序排列,即模型的名称。
基于以下代码的重采样摘要
fit.year.res <- resamples(fit.year)
summary(fit.year.res)看起来像这样:

但是,盒子图中的不同年度模型没有排序:
scales <- list(x=list(relation="free"), y=list(relation="free"))
bwplot(fit.year.res, scales=scales)

我已经尝试过将重采样fit.year.res$models的models元素转换为来自字符的因子,但这并没有什么不同。
发布于 2020-08-23 02:56:07
我不知道使用插入符号包中的bwplot方法有什么简单的解决方案。也许有一个,但我的网格技能是缺乏的。我建议使用ggplot2手动绘制箱形图。这样你就可以更好地控制最终的绘图了。
由于您没有发布包含数据的示例,因此我将使用?caret:::bwplot.resamples中的一个示例
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手动绘制绘图,您需要进行一些数据操作:
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轴上设置特定顺序,请执行以下操作:
p1 +
scale_y_discrete(limits = c("MARS", "CART", "CondInfTree"))

如果您更喜欢晶格
library(lattice)
resamps$values %>%
select(1, ends_with("RMSE")) %>%
gather(model, RMSE, -1) %>%
mutate(model = sub("~RMSE", "", model)) %>%
{bwplot(model ~ RMSE, data = .)}

要更改顺序,请更改模型的级别(此方法也适用于ggplot2):
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 = .)}

发布于 2020-08-23 03:07:58
函数bwplot.resamples用于生成此图,如果您查看underlying code,就会根据变量在感兴趣的度量下的平均性能进行因子分解。
下面我有执行因式分解的相关代码:
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),]
......
}我想你需要做的是手动绘制图:
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))如果绘制,可以看到它们是根据中线(实心点)进行排序的:
bwplot(resamps,metric="MAE")

您可以访问$values下的值,并创建一个函数来绘制它,如下所示:
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等
https://stackoverflow.com/questions/63539057
复制相似问题