當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


R probably cal_estimate_multinomial 使用多項校準模型來計算新的概率

使用多項校準模型來計算新的概率

用法

cal_estimate_multinomial(
  .data,
  truth = NULL,
  estimate = dplyr::starts_with(".pred_"),
  smooth = TRUE,
  parameters = NULL,
  ...
)

# S3 method for data.frame
cal_estimate_multinomial(
  .data,
  truth = NULL,
  estimate = dplyr::starts_with(".pred_"),
  smooth = TRUE,
  parameters = NULL,
  ...,
  .by = NULL
)

# S3 method for tune_results
cal_estimate_multinomial(
  .data,
  truth = NULL,
  estimate = dplyr::starts_with(".pred_"),
  smooth = TRUE,
  parameters = NULL,
  ...
)

# S3 method for grouped_df
cal_estimate_multinomial(
  .data,
  truth = NULL,
  estimate = NULL,
  smooth = TRUE,
  parameters = NULL,
  ...
)

參數

.data

未分組的 data.frame 對象或 tune_results 對象,包含預測和概率列。

truth

真實類別結果的列標識符(即一個因子)。這應該是一個不帶引號的列名。

estimate

列標識符向量,或 dplyr 選擇器函數之一,用於選擇哪些變量包含類概率。它默認為 tidymodels 使用的前綴 ( .pred_ )。標識符的順序將被視為與 truth 變量的級別順序相同。

smooth

適用於物流模型。當 TRUE 時,它在邏輯樣條曲線之間切換;當 FALSE 時,它在簡單邏輯回歸之間切換。

parameters

(可選)可選的調整參數值小標題,可用於在處理之前過濾預測值。僅適用於tune_results 對象。

...

傳遞給用於計算新概率的模型或例程的附加參數。

.by

分組變量的列標識符。這應該是一個不帶引號的列名稱,用於選擇用於分組的定性變量。默認為 NULL 。當.by = NULL時,不會進行分組。

細節

smooth = FALSE時,使用nnet::multinom()函數來估計模型,否則使用mgcv::gam()

也可以看看

https://www.tidymodels.org/learn/models/calibration/, cal_validate_multinomial()

例子

library(modeldata)
library(parsnip)
library(dplyr)

f <-
  list(
    ~ -0.5 + 0.6 * abs(A),
    ~ ifelse(A > 0 & B > 0, 1.0 + 0.2 * A / B, -2),
    ~ -0.6 * A + 0.50 * B - A * B
  )

set.seed(1)
tr_dat  <- sim_multinomial(500, eqn_1 = f[[1]], eqn_2 = f[[2]], eqn_3 = f[[3]])
cal_dat <- sim_multinomial(500, eqn_1 = f[[1]], eqn_2 = f[[2]], eqn_3 = f[[3]])
te_dat  <- sim_multinomial(500, eqn_1 = f[[1]], eqn_2 = f[[2]], eqn_3 = f[[3]])

set.seed(2)
rf_fit <-
  rand_forest() %>%
  set_mode("classification") %>%
  set_engine("randomForest") %>%
  fit(class ~ ., data = tr_dat)

cal_pred <-
  predict(rf_fit, cal_dat, type = "prob") %>%
  bind_cols(cal_dat)
te_pred <-
  predict(rf_fit, te_dat, type = "prob") %>%
  bind_cols(te_dat)

cal_plot_windowed(cal_pred, truth = class, window_size = 0.1, step_size = 0.03)


smoothed_mn <- cal_estimate_multinomial(cal_pred, truth = class)

new_test_pred <- cal_apply(te_pred, smoothed_mn)

cal_plot_windowed(new_test_pred, truth = class, window_size = 0.1, step_size = 0.03)

相關用法


注:本文由純淨天空篩選整理自Max Kuhn等大神的英文原創作品 Uses a Multinomial calibration model to calculate new probabilities。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。