threshold_perf()
可以采用一組類別概率預測,並確定不同概率閾值和任何現有組的性能特征。
用法
threshold_perf(.data, ...)
# S3 method for data.frame
threshold_perf(
.data,
truth,
estimate,
thresholds = NULL,
metrics = NULL,
na_rm = TRUE,
event_level = "first",
...
)
參數
- .data
-
一個小標題,可能已分組。
- ...
-
目前未使用。
- truth
-
真實 two-class 結果的列標識符(即一個因子)。這應該是一個不帶引號的列名。
- estimate
-
預測類概率的列標識符(數字)。這應該是一個不帶引號的列名。
- thresholds
-
概率閾值的數值向量。如果未指定,則使用 0.5 到 1.0 之間的一係列值。注意:如果使用此參數,則必須命名它。
- metrics
-
NULL
或yardstick::metric_set()
以及要計算的性能指標列表。這些指標都應該麵向硬類預測(例如yardstick::sensitivity()
、yardstick::accuracy()
、yardstick::recall()
等),而不是類概率。NULL
時使用一組默認指標(請參閱下麵的詳細信息)。 - na_rm
-
單一邏輯:是否應該刪除丟失的數據?
- event_level
-
單個字符串。
"first"
或"second"
指定將truth
的哪個級別視為"event"。
細節
請注意,全局選項yardstick.event_first
將用於確定哪個級別是感興趣的事件。有關更多詳細信息,請參閱 yardstick::sens()
的相關級別部分。
默認計算指標為:
-
distance = (1 - sens) ^ 2 + (1 - spec) ^ 2
如果傳遞的自定義指標不計算敏感性和特異性,則不會計算距離指標。
例子
library(dplyr)
data("segment_logistic")
# Set the threshold to 0.6
# > 0.6 = good
# < 0.6 = poor
threshold_perf(segment_logistic, Class, .pred_good, thresholds = 0.6)
#> # A tibble: 3 × 4
#> .threshold .metric .estimator .estimate
#> <dbl> <chr> <chr> <dbl>
#> 1 0.6 sensitivity binary 0.639
#> 2 0.6 specificity binary 0.869
#> 3 0.6 j_index binary 0.508
# Set the threshold to multiple values
thresholds <- seq(0.5, 0.9, by = 0.1)
segment_logistic %>%
threshold_perf(Class, .pred_good, thresholds)
#> # A tibble: 15 × 4
#> .threshold .metric .estimator .estimate
#> <dbl> <chr> <chr> <dbl>
#> 1 0.5 sensitivity binary 0.714
#> 2 0.6 sensitivity binary 0.639
#> 3 0.7 sensitivity binary 0.561
#> 4 0.8 sensitivity binary 0.451
#> 5 0.9 sensitivity binary 0.249
#> 6 0.5 specificity binary 0.825
#> 7 0.6 specificity binary 0.869
#> 8 0.7 specificity binary 0.911
#> 9 0.8 specificity binary 0.937
#> 10 0.9 specificity binary 0.977
#> 11 0.5 j_index binary 0.539
#> 12 0.6 j_index binary 0.508
#> 13 0.7 j_index binary 0.472
#> 14 0.8 j_index binary 0.388
#> 15 0.9 j_index binary 0.226
# ---------------------------------------------------------------------------
# It works with grouped data frames as well
# Let's mock some resampled data
resamples <- 5
mock_resamples <- resamples %>%
replicate(
expr = sample_n(segment_logistic, 100, replace = TRUE),
simplify = FALSE
) %>%
bind_rows(.id = "resample")
resampled_threshold_perf <- mock_resamples %>%
group_by(resample) %>%
threshold_perf(Class, .pred_good, thresholds)
resampled_threshold_perf
#> # A tibble: 75 × 5
#> resample .threshold .metric .estimator .estimate
#> <chr> <dbl> <chr> <chr> <dbl>
#> 1 1 0.5 sensitivity binary 0.676
#> 2 1 0.6 sensitivity binary 0.595
#> 3 1 0.7 sensitivity binary 0.568
#> 4 1 0.8 sensitivity binary 0.405
#> 5 1 0.9 sensitivity binary 0.297
#> 6 2 0.5 sensitivity binary 0.794
#> 7 2 0.6 sensitivity binary 0.676
#> 8 2 0.7 sensitivity binary 0.618
#> 9 2 0.8 sensitivity binary 0.5
#> 10 2 0.9 sensitivity binary 0.235
#> # ℹ 65 more rows
# Average over the resamples
resampled_threshold_perf %>%
group_by(.metric, .threshold) %>%
summarise(.estimate = mean(.estimate))
#> `summarise()` has grouped output by '.metric'. You can override using the
#> `.groups` argument.
#> # A tibble: 15 × 3
#> # Groups: .metric [3]
#> .metric .threshold .estimate
#> <chr> <dbl> <dbl>
#> 1 j_index 0.5 0.500
#> 2 j_index 0.6 0.480
#> 3 j_index 0.7 0.475
#> 4 j_index 0.8 0.377
#> 5 j_index 0.9 0.218
#> 6 sensitivity 0.5 0.697
#> 7 sensitivity 0.6 0.620
#> 8 sensitivity 0.7 0.555
#> 9 sensitivity 0.8 0.427
#> 10 sensitivity 0.9 0.240
#> 11 specificity 0.5 0.803
#> 12 specificity 0.6 0.860
#> 13 specificity 0.7 0.920
#> 14 specificity 0.8 0.950
#> 15 specificity 0.9 0.978
相關用法
- R probably append_class_pred 添加 class_pred 列
- R probably cal_plot_logistic 通過邏輯回歸繪製概率校準圖
- R probably class_pred 創建類別預測對象
- R probably cal_plot_breaks 通過分箱繪製概率校準圖
- R probably cal_estimate_multinomial 使用多項校準模型來計算新的概率
- R probably cal_validate_logistic 使用和不使用邏輯校準來測量性能
- R probably cal_validate_isotonic_boot 使用和不使用袋裝等滲回歸校準來測量性能
- R probably cal_estimate_beta 使用 Beta 校準模型來計算新概率
- R probably cal_plot_regression 回歸校準圖
- R probably as_class_pred 強製轉換為 class_pred 對象
- R probably levels.class_pred 提取class_pred級別
- R probably cal_estimate_isotonic 使用等滲回歸模型來校準模型預測。
- R probably locate-equivocal 找到模棱兩可的值
- R probably cal_estimate_logistic 使用邏輯回歸模型來校準概率
- R probably int_conformal_quantile 通過保形推理和分位數回歸預測區間
- R probably cal_validate_multinomial 使用和不使用多項式校準來測量性能
- R probably make_class_pred 根據類概率創建 class_pred 向量
- R probably cal_apply 對一組現有預測應用校準
- R probably reportable_rate 計算報告率
- R probably cal_validate_linear 使用和不使用線性回歸校準來測量性能
- R probably cal_estimate_isotonic_boot 使用引導等滲回歸模型來校準概率
- R probably cal_plot_windowed 通過移動窗口繪製概率校準圖
- R probably int_conformal_split 通過分割共形推理預測區間
- R probably is_class_pred 測試對象是否繼承自class_pred
- R probably cal_validate_isotonic 使用和不使用等滲回歸校準來測量性能
注:本文由純淨天空篩選整理自Max Kuhn等大神的英文原創作品 Generate performance metrics across probability thresholds。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。