量化邏輯斯迴歸的擬合

R 迴歸入門

Richie Cotton

Data Evangelist at DataCamp

四種結果

實際為 false 實際為 true
預測為 false 正確 偽陰性
預測為 true 偽陽性 正確
R 迴歸入門

混淆矩陣:各結果的筆數

mdl_recency <- glm(has_churned ~ time_since_last_purchase, data = churn, family = "binomial")
actual_response <- churn$has_churned
predicted_response <- round(fitted(mdl_recency))
outcomes <- table(predicted_response, actual_response)
                  actual_response
predicted_response   0   1
                 0 141 111
                 1  59  89
R 迴歸入門

視覺化混淆矩陣:鑲嵌圖

library(ggplot2)
library(yardstick)
confusion <- conf_mat(outcomes)
                  actual_response
predicted_response   0   1
                 0 141 111
                 1  59  89
autoplot(confusion)

針對「流失」與「最近一次購買」模型結果的鑲嵌圖。真流失與真未流失各有 200 筆觀測,因此兩欄寬度相同。

R 迴歸入門

效能指標

summary(confusion, event_level = "second")
# A tibble: 13 x 3
   .metric              .estimator .estimate
   <chr>                <chr>          <dbl>
 1 accuracy             binary         0.575
 2 kap                  binary         0.150
 3 sens                 binary         0.445
 4 spec                 binary         0.705
 5 ppv                  binary         0.601
 6 npv                  binary         0.560
 7 mcc                  binary         0.155
 8 j_index              binary         0.150
 9 bal_accuracy         binary         0.575
10 detection_prevalence binary         0.37 
11 precision            binary         0.601
12 recall               binary         0.445
13 f_meas               binary         0.511
R 迴歸入門

Accuracy(準確率)

summary(confusion) %>% 
  slice(1)
# A tibble: 3 x 3
  .metric  .estimator .estimate
  <chr>    <chr>          <dbl>
1 accuracy binary         0.575

「Accuracy」是正確預測所占比例。

$$ accuracy = \frac{TN + TP}{TN + FN + FP + TP} $$

confusion
                  actual_response
predicted_response   0   1
                 0 141 111
                 1  59  89
(141 + 89) / (141 + 111 + 59 + 89)
0.575
R 迴歸入門

Sensitivity(靈敏度)

summary(confusion) %>% 
  slice(3)
# A tibble: 1 x 3
  .metric .estimator .estimate
  <chr>   <chr>          <dbl>
1 sens    binary         0.445

「Sensitivity」是真陽性的比例。

$$ sensitivity = \frac{TP}{FN + TP} $$

confusion
                  actual_response
predicted_response   0   1
                 0 141 111
                 1  59  89
89 / (111 + 89)
0.445
R 迴歸入門

Specificity(特異度)

summary(confusion) %>% 
  slice(4)
# A tibble: 1 x 3
  .metric .estimator .estimate
  <chr>   <chr>          <dbl>
1 spec    binary         0.705

「Specificity」是真陰性的比例。

$$ specificity = \frac{TN}{TN + FP} $$

confusion
                  actual_response
predicted_response   0   1
                 0 141 111
                 1  59  89
141 / (141 + 59)
0.705
R 迴歸入門

一起來練習吧!

R 迴歸入門

Preparing Video For Download...