插入符号';s train()&;重采样()逆转GLM的敏感性/特异性

插入符号';s train()&;重采样()逆转GLM的敏感性/特异性,r,r-caret,glm,resampling,R,R Caret,Glm,Resampling,glm()函数的文档说明了与因子响应变量相关的 第一级表示失败和所有其他成功 我假设在使用方法='glm'时,插入符号的train()函数在引擎盖下调用glm(),因此同样适用 因此,为了生成与其他模型一致的可解释模型(,即系数对应于成功事件),我必须遵循此约定 问题是,即使glm()和caret的train()函数将第二级因素视为成功,caret的重采样函数(和$resample变量)仍然将第一级因素视为成功/正,因此,敏感性和特异性与我想使用重采样()与其他模型进行比较时的情况正好相反。 例

glm()
函数的文档说明了与因子响应变量相关的

第一级表示失败和所有其他成功

我假设在使用
方法='glm'
时,插入符号的
train()
函数在引擎盖下调用
glm()
,因此同样适用

因此,为了生成与其他模型一致的可解释模型(,即系数对应于
成功
事件
),我必须遵循此约定

问题是,即使glm()和caret的
train()
函数将第二级因素视为成功,caret的
重采样
函数(和
$resample
变量)仍然将第一级因素视为
成功
/
,因此,
敏感性
特异性
与我想使用
重采样()
与其他模型进行比较时的情况正好相反。

例如:

install.packages('ISLR')
library('ISLR')
summary(Default)
levels(Default$default) # 'yes' is second level on factor
glm_model <- glm(default ~ ., family = "binomial", data = Default)
summary(glm_model)

train_control <- trainControl(
    summaryFunction = twoClassSummary,
    classProbs = TRUE,
    method = 'repeatedcv',
    number = 5,
    repeats = 5,
    verboseIter = FALSE,
    savePredictions = TRUE)
set.seed(123)
caret_model <- train(default ~ ., data = Default, method = 'glm', metric='ROC', preProc=c('nzv', 'center', 'scale', 'knnImpute'), trControl = train_control)
summary(caret_model)
caret_model # shows Sens of ~0.99 and Spec of ~0.32
caret_model$resample # shows same, but for each fold/repeat; by now, resamples are already the opposite of what they should be, which will propagate to resamples() method, no way to specify positive/success class in train()?

confusionMatrix(data = predict(caret_model, Default), reference = Default$default, positive = 'Yes') # once I set 'Yes' as positive class, the true sensitivity and specificity are calculated, but no way to do this for resamples()?
install.packages('ISLR'))
库('ISLR')
摘要(默认)
级别(默认值$Default)#“是”是系数的第二级

glm_模型以下将反转灵敏度:

temp <- Default
temp$default <- fct_relevel(temp$default, "Yes")
levels(temp$default)
levels(Default$default)

caret_model <- train(relevel(default, ref = "Yes") ~ ., data = temp, method = 'glm', metric='ROC', preProc=c('nzv', 'center', 'scale', 'knnImpute'), trControl = train_control)
summary(caret_model)
caret_model 
temp