为什么 plot 使用 MLeval 包会给出错误的拟合分数?
Why does plot gives wrong score of fits using MLeval package?
我想使用 MLeval
包绘制几个拟合的 AUC-ROC。问题在于颜色和线条并不代表正确的 AUR-ROC 分数。正如您在下图中看到的那样,它表示红线 (lda) 的 AUC-ROC 为 0.84,但您也可以在图中看到红线实际上应该具有最低的 AUC-ROC 分数。如果你用 3 个模型检查这个 (https://www.r-bloggers.com/2019/11/how-to-easily-make-a-roc-curve-in-r/) post,它实际上做对了。有谁知道为什么会这样?
这是我的情节:
这里是一个数据样本(因为太大了):
structure(list(Survived = structure(c(1L, 2L, 2L, 2L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L,
1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L,
1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L,
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L), .Label = c("No",
"Yes"), class = "factor"), Pclass = c(0.826912816524371, -1.56522783127827,
0.826912816524371, -1.56522783127827, 0.826912816524371, 0.826912816524371,
-1.56522783127827, 0.826912816524371, 0.826912816524371, -0.369157507376952,
0.826912816524371, -1.56522783127827, 0.826912816524371, 0.826912816524371,
0.826912816524371, -0.369157507376952, 0.826912816524371, -0.369157507376952,
0.826912816524371, 0.826912816524371, -0.369157507376952, -0.369157507376952,
0.826912816524371, -1.56522783127827, 0.826912816524371, 0.826912816524371,
0.826912816524371, -1.56522783127827, 0.826912816524371, 0.826912816524371,
-1.56522783127827, -1.56522783127827, 0.826912816524371, -0.369157507376952,
-1.56522783127827, -1.56522783127827, 0.826912816524371, 0.826912816524371,
0.826912816524371, 0.826912816524371, 0.826912816524371, -0.369157507376952,
0.826912816524371, -0.369157507376952, 0.826912816524371, 0.826912816524371,
0.826912816524371, 0.826912816524371, 0.826912816524371, 0.826912816524371,
0.826912816524371, 0.826912816524371, -1.56522783127827, -0.369157507376952,
-1.56522783127827, -1.56522783127827, -0.369157507376952, 0.826912816524371,
-0.369157507376952, 0.826912816524371, 0.826912816524371, -1.56522783127827,
-1.56522783127827, 0.826912816524371, -1.56522783127827, 0.826912816524371,
-0.369157507376952, 0.826912816524371, 0.826912816524371, 0.826912816524371,
-0.369157507376952, 0.826912816524371, -0.369157507376952, 0.826912816524371,
0.826912816524371, 0.826912816524371, 0.826912816524371, 0.826912816524371,
-0.369157507376952, 0.826912816524371, 0.826912816524371, 0.826912816524371,
0.826912816524371, -1.56522783127827, -0.369157507376952, 0.826912816524371,
0.826912816524371, 0.826912816524371, -1.56522783127827, 0.826912816524371,
0.826912816524371, 0.826912816524371, -1.56522783127827, 0.826912816524371,
0.826912816524371, 0.826912816524371, -1.56522783127827, -1.56522783127827,
-0.369157507376952, -0.369157507376952), Sex = c("male", "female",
"female", "female", "male", "male", "male", "male", "female",
"female", "female", "female", "male", "male", "female", "female",
"male", "male", "female", "female", "male", "male", "female",
"male", "female", "female", "male", "male", "female", "male",
"male", "female", "female", "male", "male", "male", "male", "male",
"female", "female", "female", "female", "male", "female", "female",
"male", "male", "female", "male", "female", "male", "male", "female",
"female", "male", "male", "female", "male", "female", "male",
"male", "female", "male", "male", "male", "male", "female", "male",
"female", "male", "male", "female", "male", "male", "male", "male",
"male", "male", "male", "female", "male", "male", "female", "male",
"female", "female", "male", "male", "female", "male", "male",
"male", "male", "male", "male", "male", "male", "male", "female",
"male"), Age = c(-0.530005098333073, 0.571430411821611, -0.254646220794402,
0.364911253667608, 0.364911253667608, -0.32348594017907, 1.67286592197629,
-1.90679948602643, -0.185806501409734, -1.08072285341041, -1.76912004725709,
1.94822479951497, -0.667684537102408, 0.640270131206279, -1.08072285341041,
1.74170564136096, -1.90679948602643, 0.0344806006212027, 0.0895523761289369,
0.055132516436603, 0.364911253667608, 0.29607153428294, -1.01188313402575,
-0.116966782025066, -1.49376116971842, 0.571430411821611, 0.055132516436603,
-0.736524256487076, -0.309717996302136, -0.392325659563737, 0.709109850590947,
-0.130734725902, -0.0205911748865315, 2.49894255459231, -0.116966782025066,
0.846789289360282, -0.571308929963873, -0.598844817717741, -0.805363975871744,
-1.08072285341041, 0.709109850590947, -0.185806501409734, -0.392325659563737,
-1.83795976664176, -0.736524256487076, 0.50947466437541, -0.034359118763465,
-0.0205911748865315, -0.32348594017907, -0.805363975871744, -1.56260088910309,
-0.598844817717741, 1.32866732505296, -0.0481270626403986, 2.43010283520764,
1.35620321280682, -0.598844817717741, -0.0825469223327325, -1.70028032787242,
-1.28724201156442, -0.530005098333073, 0.571430411821611, 1.05330844751429,
-1.76912004725709, 0.839905317421815, -0.530005098333073, -0.0481270626403986,
-0.736524256487076, -0.874203695256412, -0.254646220794402, 0.158392095513605,
-0.943043414641079, -0.598844817717741, -0.254646220794402, 0.158392095513605,
-0.32348594017907, -0.392325659563737, 0.50947466437541, -1.98734195770649,
0.0207126567442691, -0.530005098333073, -0.0481270626403986,
-0.36478977180987, -0.116966782025066, -0.874203695256412, 0.227231814898272,
-0.943043414641079, 0.50947466437541, -0.461165378948405, -0.392325659563737,
-0.0481270626403986, -0.667684537102408, 1.12214816689895, -0.254646220794402,
2.01706451889963, 0.50947466437541, 2.84314115151565, -0.461165378948405,
0.29607153428294, 0.29607153428294), SibSp = c(0.432550428041802,
0.432550428041802, -0.474278822276423, 0.432550428041802, -0.474278822276423,
-0.474278822276423, -0.474278822276423, 2.24620892867825, -0.474278822276423,
0.432550428041802, 0.432550428041802, -0.474278822276423, -0.474278822276423,
0.432550428041802, -0.474278822276423, -0.474278822276423, 3.15303817899648,
-0.474278822276423, 0.432550428041802, -0.474278822276423, -0.474278822276423,
-0.474278822276423, -0.474278822276423, -0.474278822276423, 2.24620892867825,
0.432550428041802, -0.474278822276423, 2.24620892867825, -0.474278822276423,
-0.474278822276423, -0.474278822276423, 0.432550428041802, -0.474278822276423,
-0.474278822276423, 0.432550428041802, 0.432550428041802, -0.474278822276423,
-0.474278822276423, 1.33937967836003, 0.432550428041802, 0.432550428041802,
0.432550428041802, -0.474278822276423, 0.432550428041802, -0.474278822276423,
-0.474278822276423, 0.432550428041802, -0.474278822276423, 1.33937967836003,
0.432550428041802, 3.15303817899648, -0.474278822276423, 0.432550428041802,
0.432550428041802, -0.474278822276423, -0.474278822276423, -0.474278822276423,
-0.474278822276423, 0.432550428041802, 4.05986742931471, -0.474278822276423,
-0.474278822276423, 0.432550428041802, 2.24620892867825, -0.474278822276423,
0.432550428041802, -0.474278822276423, -0.474278822276423, 3.15303817899648,
1.33937967836003, -0.474278822276423, 4.05986742931471, -0.474278822276423,
0.432550428041802, -0.474278822276423, -0.474278822276423, -0.474278822276423,
-0.474278822276423, -0.474278822276423, -0.474278822276423, -0.474278822276423,
-0.474278822276423, -0.474278822276423, -0.474278822276423, -0.474278822276423,
2.24620892867825, 0.432550428041802, -0.474278822276423, 2.24620892867825,
-0.474278822276423, -0.474278822276423, -0.474278822276423, 0.432550428041802,
0.432550428041802, -0.474278822276423, -0.474278822276423, -0.474278822276423,
-0.474278822276423, -0.474278822276423, 0.432550428041802), Parch = c(-0.47340772456839,
-0.47340772456839, -0.47340772456839, -0.47340772456839, -0.47340772456839,
-0.47340772456839, -0.47340772456839, 0.767198988932891, 2.00780570243417,
-0.47340772456839, 0.767198988932891, -0.47340772456839, -0.47340772456839,
5.72962584293801, -0.47340772456839, -0.47340772456839, 0.767198988932891,
-0.47340772456839, -0.47340772456839, -0.47340772456839, -0.47340772456839,
-0.47340772456839, -0.47340772456839, -0.47340772456839, 0.767198988932891,
5.72962584293801, -0.47340772456839, 2.00780570243417, -0.47340772456839,
-0.47340772456839, -0.47340772456839, -0.47340772456839, -0.47340772456839,
-0.47340772456839, -0.47340772456839, -0.47340772456839, -0.47340772456839,
-0.47340772456839, -0.47340772456839, -0.47340772456839, -0.47340772456839,
-0.47340772456839, -0.47340772456839, 2.00780570243417, -0.47340772456839,
-0.47340772456839, -0.47340772456839, -0.47340772456839, -0.47340772456839,
-0.47340772456839, 0.767198988932891, -0.47340772456839, -0.47340772456839,
-0.47340772456839, 0.767198988932891, -0.47340772456839, -0.47340772456839,
-0.47340772456839, 2.00780570243417, 2.00780570243417, -0.47340772456839,
-0.47340772456839, -0.47340772456839, 2.00780570243417, -0.47340772456839,
0.767198988932891, -0.47340772456839, -0.47340772456839, 2.00780570243417,
-0.47340772456839, -0.47340772456839, 2.00780570243417, -0.47340772456839,
-0.47340772456839, -0.47340772456839, -0.47340772456839, -0.47340772456839,
-0.47340772456839, 2.00780570243417, -0.47340772456839, -0.47340772456839,
-0.47340772456839, -0.47340772456839, -0.47340772456839, -0.47340772456839,
-0.47340772456839, 3.24841241593545, -0.47340772456839, 2.00780570243417,
-0.47340772456839, -0.47340772456839, -0.47340772456839, -0.47340772456839,
2.00780570243417, -0.47340772456839, -0.47340772456839, -0.47340772456839,
0.767198988932891, 0.767198988932891, -0.47340772456839), Fare = c(-0.502163136515605,
0.786403617834538, -0.488579851581261, 0.420494069765409, -0.486064428445271,
-0.477848050313875, 0.395591380719111, -0.223957337675147, -0.424017995203696,
-0.0429313901012591, -0.311997147434786, -0.113781804318799,
-0.486064428445271, -0.0186988097783889, -0.490004587245486,
-0.326083516996328, -0.0619640877174114, -0.38645367226008, -0.285836746820493,
-0.502666221142803, -0.124849666117153, -0.38645367226008, -0.4864829948551,
0.0663224922180623, -0.223957337675147, -0.0164349289559982,
-0.502666221142803, 4.64439259971928, -0.489501502618288, -0.489167454425828,
-0.090221345057865, 2.30043680338673, -0.492101443971647, -0.436762134979874,
1.00549697297924, 0.3983583461687, -0.502581702925434, -0.486064428445271,
-0.285836746820493, -0.421836620260165, -0.457388604694989, -0.225466591556741,
-0.489167454425828, 0.18865657484453, -0.489501502618288, -0.486064428445271,
-0.336145209540286, -0.492101443971647, -0.211798788405027, -0.289861423838076,
0.150589167273717, -0.491095274717251, 0.895993560684828, -0.124849666117153,
0.599173630638046, 0.0663224922180623, -0.436762134979874, -0.502581702925434,
-0.0896337422132979, 0.295729082220321, -0.502581702925434, 0.961813128630388,
1.0317418918109, -0.0866152344501103, -0.090221345057865, -0.341260574029635,
-0.436762134979874, -0.48388506584025, -0.488579851581261, -0.473738855078922,
-0.436762134979874, 0.295729082220321, 0.831011125558925, -0.35719024566523,
0.488829060846959, -0.494113782480438, -0.489167454425828, -0.486064428445271,
-0.064479510853401, -0.397018449431237, -0.46694721261175, -0.456885520067791,
-0.49134681703085, 0.299753759237905, -0.436762134979874, -0.329102024759515,
0.0436836839941552, -0.486064428445271, 4.64439259971928, -0.486064428445271,
-0.486064428445271, -0.490004587245486, 0.582990404350342, -0.234019030219105,
-0.502163136515605, -0.486064428445271, 0.0493021331107018, 0.626925791012793,
-0.185219821380906, -0.124849666117153), Embarked = c("S", "C",
"S", "S", "S", "Q", "S", "S", "S", "C", "S", "S", "S", "S", "S",
"S", "Q", "S", "S", "C", "S", "S", "Q", "S", "S", "S", "C", "S",
"Q", "S", "C", "C", "Q", "S", "C", "S", "C", "S", "S", "C", "S",
"S", "C", "C", "Q", "S", "Q", "Q", "C", "S", "S", "S", "C", "S",
"C", "S", "S", "C", "S", "S", "C", "", "S", "S", "C", "C", "S",
"S", "S", "S", "S", "S", "S", "C", "S", "S", "S", "S", "S", "S",
"S", "S", "Q", "S", "S", "S", "S", "S", "S", "S", "S", "S", "S",
"S", "S", "S", "C", "C", "S", "S")), row.names = c(NA, 100L), class = "data.frame")
这是我用来拟合和绘图的代码:
library(MLeval)
library(caret)
# Run algorithms using 5-fold cross validation
control <- trainControl(method="cv",
number=5,
repeats = 5,
savePredictions = "final",
search = "grid",
classProbs = TRUE)
metric <- "Accuracy"
# a) linear algorithms
set.seed(7)
fit.lda <- train(Survived~., data=trainset, method="lda", metric=metric, trControl=control, allowParallel = TRUE)
# b) nonlinear algorithms
# CART
set.seed(7)
fit.cart <- train(Survived~., data=trainset, method="rpart", metric=metric, trControl=control, tuneLength = 1)
# glm
set.seed(7)
fit.glm <- train(Survived~., data=trainset, method="glmnet", metric=metric, trControl=control, verbose = FALSE)
# c) advanced algorithms
# SVM
set.seed(7)
fit.svm <- train(Survived~., data=trainset, method="svmRadial", metric=metric, trControl=control, verbose = FALSE, tuneLength = 2)
# Random Forest
set.seed(7)
fit.rf <- train(Survived~., data=trainset, method="ranger", metric=metric, trControl=control, verbose = FALSE, tuneLength = 3, importance = "impurity")
# xgbDART
set.seed(7)
fit.xgbDART <- train(Survived~., data = trainset, method = "xgbTree", metric = metric, trControl = control, verbose = FALSE, tuneLength = 7, nthread = 1, verbosity = 0)
# plot
plot <- evalm(list(fit.lda, fit.cart, fit.svm, fit.rf, fit.xgbDART, fit.glm),
gnames = c("lda", "cart", "svm", "rf", "xgbTree", "glm"),
rlinethick=0.8, fsize=8, plots='r')
模型似乎是按方法名称的字母顺序排序的(从 CART 是 AUC 最小的模型并以红色显示的事实来看)。
因此您可以在列表中按名称对模型进行排序。
PS:tidyverse 已经进化; tidymodels 是训练和评估模型的好工具。
models <- list(
lda = fit.lda,
cart = fit.cart,
svm = fit.svm,
rf = fit.rf,
xgbTree = fit.xgbDART,
glm = fit.glm
)
models <- models[order(names(models))]
evalm(
models,
gnames = names(models),
rlinethick = 0.8, fsize = 8, plots = "r"
)
#> cart AUC-ROC = 0.68
#> glm AUC-ROC = 0.79
#> lda AUC-ROC = 0.84
#> rf AUC-ROC = 0.84
#> svm AUC-ROC = 0.77
#> xgbTree AUC-ROC = 0.89
由 reprex package (v2.0.1)
于 2022-03-23 创建
我想使用 MLeval
包绘制几个拟合的 AUC-ROC。问题在于颜色和线条并不代表正确的 AUR-ROC 分数。正如您在下图中看到的那样,它表示红线 (lda) 的 AUC-ROC 为 0.84,但您也可以在图中看到红线实际上应该具有最低的 AUC-ROC 分数。如果你用 3 个模型检查这个 (https://www.r-bloggers.com/2019/11/how-to-easily-make-a-roc-curve-in-r/) post,它实际上做对了。有谁知道为什么会这样?
这是我的情节:
这里是一个数据样本(因为太大了):
structure(list(Survived = structure(c(1L, 2L, 2L, 2L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L,
1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L,
1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L,
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L), .Label = c("No",
"Yes"), class = "factor"), Pclass = c(0.826912816524371, -1.56522783127827,
0.826912816524371, -1.56522783127827, 0.826912816524371, 0.826912816524371,
-1.56522783127827, 0.826912816524371, 0.826912816524371, -0.369157507376952,
0.826912816524371, -1.56522783127827, 0.826912816524371, 0.826912816524371,
0.826912816524371, -0.369157507376952, 0.826912816524371, -0.369157507376952,
0.826912816524371, 0.826912816524371, -0.369157507376952, -0.369157507376952,
0.826912816524371, -1.56522783127827, 0.826912816524371, 0.826912816524371,
0.826912816524371, -1.56522783127827, 0.826912816524371, 0.826912816524371,
-1.56522783127827, -1.56522783127827, 0.826912816524371, -0.369157507376952,
-1.56522783127827, -1.56522783127827, 0.826912816524371, 0.826912816524371,
0.826912816524371, 0.826912816524371, 0.826912816524371, -0.369157507376952,
0.826912816524371, -0.369157507376952, 0.826912816524371, 0.826912816524371,
0.826912816524371, 0.826912816524371, 0.826912816524371, 0.826912816524371,
0.826912816524371, 0.826912816524371, -1.56522783127827, -0.369157507376952,
-1.56522783127827, -1.56522783127827, -0.369157507376952, 0.826912816524371,
-0.369157507376952, 0.826912816524371, 0.826912816524371, -1.56522783127827,
-1.56522783127827, 0.826912816524371, -1.56522783127827, 0.826912816524371,
-0.369157507376952, 0.826912816524371, 0.826912816524371, 0.826912816524371,
-0.369157507376952, 0.826912816524371, -0.369157507376952, 0.826912816524371,
0.826912816524371, 0.826912816524371, 0.826912816524371, 0.826912816524371,
-0.369157507376952, 0.826912816524371, 0.826912816524371, 0.826912816524371,
0.826912816524371, -1.56522783127827, -0.369157507376952, 0.826912816524371,
0.826912816524371, 0.826912816524371, -1.56522783127827, 0.826912816524371,
0.826912816524371, 0.826912816524371, -1.56522783127827, 0.826912816524371,
0.826912816524371, 0.826912816524371, -1.56522783127827, -1.56522783127827,
-0.369157507376952, -0.369157507376952), Sex = c("male", "female",
"female", "female", "male", "male", "male", "male", "female",
"female", "female", "female", "male", "male", "female", "female",
"male", "male", "female", "female", "male", "male", "female",
"male", "female", "female", "male", "male", "female", "male",
"male", "female", "female", "male", "male", "male", "male", "male",
"female", "female", "female", "female", "male", "female", "female",
"male", "male", "female", "male", "female", "male", "male", "female",
"female", "male", "male", "female", "male", "female", "male",
"male", "female", "male", "male", "male", "male", "female", "male",
"female", "male", "male", "female", "male", "male", "male", "male",
"male", "male", "male", "female", "male", "male", "female", "male",
"female", "female", "male", "male", "female", "male", "male",
"male", "male", "male", "male", "male", "male", "male", "female",
"male"), Age = c(-0.530005098333073, 0.571430411821611, -0.254646220794402,
0.364911253667608, 0.364911253667608, -0.32348594017907, 1.67286592197629,
-1.90679948602643, -0.185806501409734, -1.08072285341041, -1.76912004725709,
1.94822479951497, -0.667684537102408, 0.640270131206279, -1.08072285341041,
1.74170564136096, -1.90679948602643, 0.0344806006212027, 0.0895523761289369,
0.055132516436603, 0.364911253667608, 0.29607153428294, -1.01188313402575,
-0.116966782025066, -1.49376116971842, 0.571430411821611, 0.055132516436603,
-0.736524256487076, -0.309717996302136, -0.392325659563737, 0.709109850590947,
-0.130734725902, -0.0205911748865315, 2.49894255459231, -0.116966782025066,
0.846789289360282, -0.571308929963873, -0.598844817717741, -0.805363975871744,
-1.08072285341041, 0.709109850590947, -0.185806501409734, -0.392325659563737,
-1.83795976664176, -0.736524256487076, 0.50947466437541, -0.034359118763465,
-0.0205911748865315, -0.32348594017907, -0.805363975871744, -1.56260088910309,
-0.598844817717741, 1.32866732505296, -0.0481270626403986, 2.43010283520764,
1.35620321280682, -0.598844817717741, -0.0825469223327325, -1.70028032787242,
-1.28724201156442, -0.530005098333073, 0.571430411821611, 1.05330844751429,
-1.76912004725709, 0.839905317421815, -0.530005098333073, -0.0481270626403986,
-0.736524256487076, -0.874203695256412, -0.254646220794402, 0.158392095513605,
-0.943043414641079, -0.598844817717741, -0.254646220794402, 0.158392095513605,
-0.32348594017907, -0.392325659563737, 0.50947466437541, -1.98734195770649,
0.0207126567442691, -0.530005098333073, -0.0481270626403986,
-0.36478977180987, -0.116966782025066, -0.874203695256412, 0.227231814898272,
-0.943043414641079, 0.50947466437541, -0.461165378948405, -0.392325659563737,
-0.0481270626403986, -0.667684537102408, 1.12214816689895, -0.254646220794402,
2.01706451889963, 0.50947466437541, 2.84314115151565, -0.461165378948405,
0.29607153428294, 0.29607153428294), SibSp = c(0.432550428041802,
0.432550428041802, -0.474278822276423, 0.432550428041802, -0.474278822276423,
-0.474278822276423, -0.474278822276423, 2.24620892867825, -0.474278822276423,
0.432550428041802, 0.432550428041802, -0.474278822276423, -0.474278822276423,
0.432550428041802, -0.474278822276423, -0.474278822276423, 3.15303817899648,
-0.474278822276423, 0.432550428041802, -0.474278822276423, -0.474278822276423,
-0.474278822276423, -0.474278822276423, -0.474278822276423, 2.24620892867825,
0.432550428041802, -0.474278822276423, 2.24620892867825, -0.474278822276423,
-0.474278822276423, -0.474278822276423, 0.432550428041802, -0.474278822276423,
-0.474278822276423, 0.432550428041802, 0.432550428041802, -0.474278822276423,
-0.474278822276423, 1.33937967836003, 0.432550428041802, 0.432550428041802,
0.432550428041802, -0.474278822276423, 0.432550428041802, -0.474278822276423,
-0.474278822276423, 0.432550428041802, -0.474278822276423, 1.33937967836003,
0.432550428041802, 3.15303817899648, -0.474278822276423, 0.432550428041802,
0.432550428041802, -0.474278822276423, -0.474278822276423, -0.474278822276423,
-0.474278822276423, 0.432550428041802, 4.05986742931471, -0.474278822276423,
-0.474278822276423, 0.432550428041802, 2.24620892867825, -0.474278822276423,
0.432550428041802, -0.474278822276423, -0.474278822276423, 3.15303817899648,
1.33937967836003, -0.474278822276423, 4.05986742931471, -0.474278822276423,
0.432550428041802, -0.474278822276423, -0.474278822276423, -0.474278822276423,
-0.474278822276423, -0.474278822276423, -0.474278822276423, -0.474278822276423,
-0.474278822276423, -0.474278822276423, -0.474278822276423, -0.474278822276423,
2.24620892867825, 0.432550428041802, -0.474278822276423, 2.24620892867825,
-0.474278822276423, -0.474278822276423, -0.474278822276423, 0.432550428041802,
0.432550428041802, -0.474278822276423, -0.474278822276423, -0.474278822276423,
-0.474278822276423, -0.474278822276423, 0.432550428041802), Parch = c(-0.47340772456839,
-0.47340772456839, -0.47340772456839, -0.47340772456839, -0.47340772456839,
-0.47340772456839, -0.47340772456839, 0.767198988932891, 2.00780570243417,
-0.47340772456839, 0.767198988932891, -0.47340772456839, -0.47340772456839,
5.72962584293801, -0.47340772456839, -0.47340772456839, 0.767198988932891,
-0.47340772456839, -0.47340772456839, -0.47340772456839, -0.47340772456839,
-0.47340772456839, -0.47340772456839, -0.47340772456839, 0.767198988932891,
5.72962584293801, -0.47340772456839, 2.00780570243417, -0.47340772456839,
-0.47340772456839, -0.47340772456839, -0.47340772456839, -0.47340772456839,
-0.47340772456839, -0.47340772456839, -0.47340772456839, -0.47340772456839,
-0.47340772456839, -0.47340772456839, -0.47340772456839, -0.47340772456839,
-0.47340772456839, -0.47340772456839, 2.00780570243417, -0.47340772456839,
-0.47340772456839, -0.47340772456839, -0.47340772456839, -0.47340772456839,
-0.47340772456839, 0.767198988932891, -0.47340772456839, -0.47340772456839,
-0.47340772456839, 0.767198988932891, -0.47340772456839, -0.47340772456839,
-0.47340772456839, 2.00780570243417, 2.00780570243417, -0.47340772456839,
-0.47340772456839, -0.47340772456839, 2.00780570243417, -0.47340772456839,
0.767198988932891, -0.47340772456839, -0.47340772456839, 2.00780570243417,
-0.47340772456839, -0.47340772456839, 2.00780570243417, -0.47340772456839,
-0.47340772456839, -0.47340772456839, -0.47340772456839, -0.47340772456839,
-0.47340772456839, 2.00780570243417, -0.47340772456839, -0.47340772456839,
-0.47340772456839, -0.47340772456839, -0.47340772456839, -0.47340772456839,
-0.47340772456839, 3.24841241593545, -0.47340772456839, 2.00780570243417,
-0.47340772456839, -0.47340772456839, -0.47340772456839, -0.47340772456839,
2.00780570243417, -0.47340772456839, -0.47340772456839, -0.47340772456839,
0.767198988932891, 0.767198988932891, -0.47340772456839), Fare = c(-0.502163136515605,
0.786403617834538, -0.488579851581261, 0.420494069765409, -0.486064428445271,
-0.477848050313875, 0.395591380719111, -0.223957337675147, -0.424017995203696,
-0.0429313901012591, -0.311997147434786, -0.113781804318799,
-0.486064428445271, -0.0186988097783889, -0.490004587245486,
-0.326083516996328, -0.0619640877174114, -0.38645367226008, -0.285836746820493,
-0.502666221142803, -0.124849666117153, -0.38645367226008, -0.4864829948551,
0.0663224922180623, -0.223957337675147, -0.0164349289559982,
-0.502666221142803, 4.64439259971928, -0.489501502618288, -0.489167454425828,
-0.090221345057865, 2.30043680338673, -0.492101443971647, -0.436762134979874,
1.00549697297924, 0.3983583461687, -0.502581702925434, -0.486064428445271,
-0.285836746820493, -0.421836620260165, -0.457388604694989, -0.225466591556741,
-0.489167454425828, 0.18865657484453, -0.489501502618288, -0.486064428445271,
-0.336145209540286, -0.492101443971647, -0.211798788405027, -0.289861423838076,
0.150589167273717, -0.491095274717251, 0.895993560684828, -0.124849666117153,
0.599173630638046, 0.0663224922180623, -0.436762134979874, -0.502581702925434,
-0.0896337422132979, 0.295729082220321, -0.502581702925434, 0.961813128630388,
1.0317418918109, -0.0866152344501103, -0.090221345057865, -0.341260574029635,
-0.436762134979874, -0.48388506584025, -0.488579851581261, -0.473738855078922,
-0.436762134979874, 0.295729082220321, 0.831011125558925, -0.35719024566523,
0.488829060846959, -0.494113782480438, -0.489167454425828, -0.486064428445271,
-0.064479510853401, -0.397018449431237, -0.46694721261175, -0.456885520067791,
-0.49134681703085, 0.299753759237905, -0.436762134979874, -0.329102024759515,
0.0436836839941552, -0.486064428445271, 4.64439259971928, -0.486064428445271,
-0.486064428445271, -0.490004587245486, 0.582990404350342, -0.234019030219105,
-0.502163136515605, -0.486064428445271, 0.0493021331107018, 0.626925791012793,
-0.185219821380906, -0.124849666117153), Embarked = c("S", "C",
"S", "S", "S", "Q", "S", "S", "S", "C", "S", "S", "S", "S", "S",
"S", "Q", "S", "S", "C", "S", "S", "Q", "S", "S", "S", "C", "S",
"Q", "S", "C", "C", "Q", "S", "C", "S", "C", "S", "S", "C", "S",
"S", "C", "C", "Q", "S", "Q", "Q", "C", "S", "S", "S", "C", "S",
"C", "S", "S", "C", "S", "S", "C", "", "S", "S", "C", "C", "S",
"S", "S", "S", "S", "S", "S", "C", "S", "S", "S", "S", "S", "S",
"S", "S", "Q", "S", "S", "S", "S", "S", "S", "S", "S", "S", "S",
"S", "S", "S", "C", "C", "S", "S")), row.names = c(NA, 100L), class = "data.frame")
这是我用来拟合和绘图的代码:
library(MLeval)
library(caret)
# Run algorithms using 5-fold cross validation
control <- trainControl(method="cv",
number=5,
repeats = 5,
savePredictions = "final",
search = "grid",
classProbs = TRUE)
metric <- "Accuracy"
# a) linear algorithms
set.seed(7)
fit.lda <- train(Survived~., data=trainset, method="lda", metric=metric, trControl=control, allowParallel = TRUE)
# b) nonlinear algorithms
# CART
set.seed(7)
fit.cart <- train(Survived~., data=trainset, method="rpart", metric=metric, trControl=control, tuneLength = 1)
# glm
set.seed(7)
fit.glm <- train(Survived~., data=trainset, method="glmnet", metric=metric, trControl=control, verbose = FALSE)
# c) advanced algorithms
# SVM
set.seed(7)
fit.svm <- train(Survived~., data=trainset, method="svmRadial", metric=metric, trControl=control, verbose = FALSE, tuneLength = 2)
# Random Forest
set.seed(7)
fit.rf <- train(Survived~., data=trainset, method="ranger", metric=metric, trControl=control, verbose = FALSE, tuneLength = 3, importance = "impurity")
# xgbDART
set.seed(7)
fit.xgbDART <- train(Survived~., data = trainset, method = "xgbTree", metric = metric, trControl = control, verbose = FALSE, tuneLength = 7, nthread = 1, verbosity = 0)
# plot
plot <- evalm(list(fit.lda, fit.cart, fit.svm, fit.rf, fit.xgbDART, fit.glm),
gnames = c("lda", "cart", "svm", "rf", "xgbTree", "glm"),
rlinethick=0.8, fsize=8, plots='r')
模型似乎是按方法名称的字母顺序排序的(从 CART 是 AUC 最小的模型并以红色显示的事实来看)。
因此您可以在列表中按名称对模型进行排序。
PS:tidyverse 已经进化; tidymodels 是训练和评估模型的好工具。
models <- list(
lda = fit.lda,
cart = fit.cart,
svm = fit.svm,
rf = fit.rf,
xgbTree = fit.xgbDART,
glm = fit.glm
)
models <- models[order(names(models))]
evalm(
models,
gnames = names(models),
rlinethick = 0.8, fsize = 8, plots = "r"
)
#> cart AUC-ROC = 0.68
#> glm AUC-ROC = 0.79
#> lda AUC-ROC = 0.84
#> rf AUC-ROC = 0.84
#> svm AUC-ROC = 0.77
#> xgbTree AUC-ROC = 0.89
由 reprex package (v2.0.1)
于 2022-03-23 创建