Извлечение бета-значений из обученной модели каретки

#r #machine-learning #logistic-regression #r-caret

Вопрос:

Я пытаюсь извлечь бета-значения из модели, определенной train() с помощью пакета caret.

 cv_model_pls <- train(
  POD1HemoglobinCut ~ ., 
  data = train, 
  method = "pls",
  family = "binomial",
  trControl = trainControl(method = "cv", number = 10),
  preProcess = c("zv", "center", "scale"),
  tuneLength = 6
)
 

Выход есть:

 > cv_model_pls
Partial Least Squares 

9932 samples
   7 predictor
   2 classes: '[0,10)', '[10,Inf)' 

Pre-processing: centered (7), scaled (7) 
Resampling: Cross-Validated (10 fold) 
Summary of sample sizes: 8939, 8939, 8939, 8938, 8940, 8939, ... 
Resampling results across tuning parameters:

  ncomp  Accuracy   Kappa    
  1      0.8569258  0.1994938
  2      0.8698149  0.3215483
  3      0.8707213  0.3303433
  4      0.8710237  0.3335666
  5      0.8710238  0.3341072
  6      0.8708224  0.3330295

Accuracy was used to select the optimal model using the largest value.
The final value used for the model was ncomp = 5.

 

Запуск сводки, чтобы попытаться получить бета-значения, заставляет меня:

 > summary(cv_model_pls)
Data:   X dimension: 9932 7 
    Y dimension: 9932 2
Fit method: oscorespls
Number of components considered: 5
TRAINING: % variance explained
Error in dimnames(tbl) <- list(c("X", yvarnames), paste(1:object$ncomp,  : 
  length of 'dimnames' [1] not equal to array extent
 
  1. Как я могу извлечь бета-значения для оптимизированной модели (или других моделей)?
  2. Как я могу выбрать модель, максимизируя чувствительность (а не точность по умолчанию)?

Ответ №1:

С бета-значениями, я предполагаю, что вы имеете в виду коэффициенты. Функция сводки, вызываемая pls:::summary.mvr из pls , возвращает только объясненные отклонения. Вы можете сделать ?pls:::summary.mvr , чтобы увидеть, что это делает. Это не работает на выходе plsda .

Используя пример набора данных, мы помещаем его с помощью каретки:

 set.seed(111)
df = MASS::Pima.tr

cv_model_pls <- train(type~.,data=df,method="pls",
family="binomial",trControl = trainControl(method = "cv", number = 5),
preProcess = c("center", "scale"),
tuneLength = 6
 )
 

Результаты:

 Partial Least Squares 

200 samples
  7 predictor
  2 classes: 'No', 'Yes' 

Pre-processing: centered (7), scaled (7) 
Resampling: Cross-Validated (5 fold) 
Summary of sample sizes: 159, 161, 159, 161, 160 
Resampling results across tuning parameters:

  ncomp  Accuracy   Kappa    
  1      0.7301063  0.3746033
  2      0.7504909  0.4255505
  3      0.7453627  0.4140426
  4      0.7553690  0.4412532
  5      0.7502408  0.4275158
  6      0.7502408  0.4275158

Accuracy was used to select the optimal model using the largest value.
The final value used for the model was ncomp = 4.
 

вы можете найти коэффициенты в соответствии с окончательной моделью:

 cv_model_pls$finalModel$coefficients
 

в нем будут показаны компоненты до лучших n ПК, поэтому в этом примере выполните:

 cv_model_pls$finalModel$coefficients[,,cv_model_pls$bestTune$ncomp]
                No          Yes
npreg -0.060740474  0.060740474
glu   -0.173639051  0.173639051
bp     0.006635470 -0.006635470
skin  -0.002510842  0.002510842
bmi   -0.065740864  0.065740864
ped   -0.086110972  0.086110972
age   -0.076374824  0.076374824
 

Для чувствительности используйте summaryFunction = twoClassSummary внутри trainControl и установите метрику равной Sens :

 model <- train(type~.,data=df,method="pls",
    family="binomial",
    trControl = trainControl(method = "cv", 
    summaryFunction = twoClassSummary,
    classProbs = TRUE,
    number = 5),
    metric = "Sens",
    preProcess = c("center", "scale"),
    tuneLength = 6
     )

Partial Least Squares 

200 samples
  7 predictor
  2 classes: 'No', 'Yes' 

Pre-processing: centered (7), scaled (7) 
Resampling: Cross-Validated (5 fold) 
Summary of sample sizes: 159, 161, 161, 159, 160 
Resampling results across tuning parameters:

  ncomp  ROC        Sens       Spec     
  1      0.8227357  0.8635328  0.5571429
  2      0.8286638  0.8555556  0.5428571
  3      0.8250728  0.8709402  0.5571429
  4      0.8247738  0.8555556  0.5571429
  5      0.8264237  0.8555556  0.5428571
  6      0.8258946  0.8632479  0.5428571

Sens was used to select the optimal model using the largest value.
The final value used for the model was ncomp = 3.
 

Комментарии:

1. Это такой замечательный ответ. Огромное спасибо. Я надеялся создать уравнение из своей модели в форме, показанной во вставке 1 этой рукописи: bmj.com/content/bmj/351/bmj.h3868.full.pdf . Возможно ли это с вашими коэффициентами, перечисленными в столбце «Да»?

2. это должно быть так

3. Нет никакого перехвата?