我正在尝试从两个插入符号模型输出创建敏感性和特异性的汇总表,并且在我的 for 循环中,当从包含值的名为 models 的列表中查找值时,它会抛出下标越界错误?这是一个完全可重现的示例。 Dplyr 的版本是 0.7.0。谢谢。
heart <- read.table("http://www-stat.stanford.edu/~tibs/ElemStatLearn/datasets/SAheart.data",
sep=",",head=T,row.names=1)
library(car)
heart$chd <- factor(car::recode(heart$chd, "c(1)='Yes';
else='No'"), levels = c('Yes', 'No'))
library(caret)
set.seed(42)
index <- createDataPartition(heart$chd, p = 0.7, list = FALSE)
train_data <- heart[index, ]
test_data <- heart[-index, ]
set.seed(42)
model_rf <- caret::train(chd ~ .,
data = train_data,
method = "rf",
trControl = trainControl(method = "repeatedcv",
number = 10,
repeats = 10,
verboseIter = FALSE))
final <- data.frame(actual = test_data$chd,
predict(model_rf, newdata = test_data, type = "prob"))
final$predict <- factor(ifelse(final$Yes > 0.5, "Yes", "No"), levels = c('Yes', 'No'))
cm_original <- confusionMatrix(final$predict, test_data$chd)
# down sampling
ctrl <- trainControl(method = "repeatedcv",
number = 10,
repeats = 10,
verboseIter = FALSE,
sampling = "down")
set.seed(42)
model_rf_under <- caret::train(chd ~ .,
data = train_data,
method = "rf",
trControl = ctrl)
final_under <- data.frame(actual = test_data$chd,
predict(model_rf_under, newdata = test_data, type = "prob"))
final_under$predict <- factor(ifelse(final_under$Yes > 0.5, "Yes", "No"), levels = c('Yes', 'No'))
cm_down <- confusionMatrix(final_under$predict, test_data$chd)
models <- list(original = model_rf,
under = model_rf_under
)
library(dplyr)
comparison <- data.frame(model = names(models),
Sensitivity = rep(NA, length(models)),
Specificity = rep(NA, length(models))
)
for (name in names(models)) {
model <- get(paste0("cm_", name))
comparison[comparison$model == name, ] <- filter(comparison, model ==
name) %>% mutate( Sensitivity = model[['byClass']][['Sensitivity']],
Specificity = model[['byClass']][['Specificity']] )
}
comparison