應用模型堆棧來創建不同類型的預測。
用法
# S3 method for model_stack
predict(object, new_data, type = NULL, members = FALSE, opts = list(), ...)
參數
- object
-
從
fit_members()
輸出的具有擬合成員的模型堆棧。 - new_data
-
矩形數據對象,例如 DataFrame 。
- type
-
返回預測值的格式 — "numeric"、"class" 或 "prob" 之一。當為NULL時,
predict()
將根據模型的模式選擇合適的值。 - members
-
邏輯性強。是否另外返回每個集合成員的預測。
- opts
-
傳遞給每個成員的 parsnip::predict.model_fit 的基礎預測函數的可選參數列表。
- ...
-
附加參數。目前被忽略。
示例數據
該軟件包提供了一些重采樣對象和數據集,用於源自對 1212 個red-eyed 樹蛙胚胎的研究的示例和小插圖!
如果 Red-eyed 樹蛙 (RETF) 胚胎檢測到潛在的捕食者威脅,它們的孵化時間可能會比正常情況下的 7 天更早。研究人員想要確定這些樹蛙胚胎如何以及何時能夠檢測到來自環境的刺激。為此,他們通過用鈍探針搖動胚胎,對不同發育階段的胚胎進行"predator stimulus"測試。盡管一些胚胎事先接受了慶大黴素處理,慶大黴素是一種可以消除側線(感覺器官)的化合物。研究員朱莉·榮格(Julie Jung)和她的團隊發現,這些因子決定了胚胎是否過早孵化!
請注意,stacks 包中包含的數據不一定是完整數據集的代表性或無偏差子集,並且僅用於演示目的。
reg_folds
和 class_folds
是來自 rsample
的 rset
交叉驗證對象,分別將訓練數據分為回歸模型對象和分類模型對象。 tree_frogs_reg_test
和tree_frogs_class_test
是類似的測試集。
reg_res_lr
、reg_res_svm
和 reg_res_sp
分別包含線性回歸、支持向量機和樣條模型的回歸調整結果,擬合 latency
(即胚胎響應抖動需要多長時間孵化)在 tree_frogs
數據中,使用大多數其他變量作為預測變量。請注意,這些模型背後的數據經過過濾,僅包含來自響應刺激而孵化的胚胎的數據。
class_res_rf
和 class_res_nn
分別包含隨機森林和神經網絡分類模型的多類分類調整結果,使用大多數其他變量作為預測變量在數據中擬合 reflex
(耳朵函數的度量)。
log_res_rf
和 log_res_nn
分別包含隨機森林和神經網絡分類模型的二元分類調整結果,使用大多數其他變量擬合 hatched
(無論胚胎是否響應刺激而孵化)預測因子。
請參閱?example_data
了解有關這些對象的更多信息,並瀏覽生成它們的源代碼。
例子
# see the "Example Data" section above for
# clarification on the data and tuning results
# objects used in these examples!
data(tree_frogs_reg_test)
data(tree_frogs_class_test)
# build and fit a regression model stack
reg_st <-
stacks() %>%
add_candidates(reg_res_lr) %>%
add_candidates(reg_res_sp) %>%
blend_predictions() %>%
fit_members()
reg_st
#> ── A stacked ensemble model ─────────────────────────────────────
#>
#> Out of 11 possible candidate members, the ensemble retained 4.
#> Penalty: 1e-06.
#> Mixture: 1.
#>
#> The 4 highest weighted members are:
#> # A tibble: 4 × 3
#> member type weight
#> <chr> <chr> <dbl>
#> 1 reg_res_sp_03_1 linear_reg 0.485
#> 2 reg_res_sp_10_1 linear_reg 0.247
#> 3 reg_res_lr_1_1 linear_reg 0.129
#> 4 reg_res_sp_05_1 linear_reg 0.0666
# predict on the tree frogs testing data
predict(reg_st, tree_frogs_reg_test)
#> # A tibble: 143 × 1
#> .pred
#> <dbl>
#> 1 119.
#> 2 81.4
#> 3 102.
#> 4 35.5
#> 5 119.
#> 6 50.5
#> 7 122.
#> 8 82.7
#> 9 50.2
#> 10 75.7
#> # ℹ 133 more rows
# include the predictions from the members
predict(reg_st, tree_frogs_reg_test, members = TRUE)
#> # A tibble: 143 × 5
#> .pred reg_res_lr_1_1 reg_res_sp_10_1 reg_res_sp_05_1 reg_res_sp_03_1
#> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 119. 138. 125. 121. 114.
#> 2 81.4 82.4 84.8 81.8 77.1
#> 3 102. 116. 111. 112. 93.3
#> 4 35.5 35.8 29.7 32.5 29.6
#> 5 119. 111. 115. 115. 127.
#> 6 50.5 38.8 37.4 36.2 55.3
#> 7 122. 123. 103. 104. 137.
#> 8 82.7 82.3 78.6 82.0 82.8
#> 9 50.2 38.7 37.3 36.2 54.8
#> 10 75.7 78.8 75.3 76.9 71.8
#> # ℹ 133 more rows
# build and fit a classification model stack
class_st <-
stacks() %>%
add_candidates(class_res_nn) %>%
add_candidates(class_res_rf) %>%
blend_predictions() %>%
fit_members()
#> Warning: Predictions from 1 candidate were identical to those from existing
#> candidates and were removed from the data stack.
class_st
#> ── A stacked ensemble model ─────────────────────────────────────
#>
#> Out of 21 possible candidate members, the ensemble retained 5.
#> Penalty: 0.1.
#> Mixture: 1.
#> Across the 3 classes, there are an average of 2.5 coefficients per class.
#>
#> The 5 highest weighted member classes are:
#> # A tibble: 5 × 4
#> member type weight class
#> <chr> <chr> <dbl> <fct>
#> 1 .pred_full_class_res_nn_1_1 mlp 12.0 full
#> 2 .pred_mid_class_res_rf_1_06 rand_forest 0.670 mid
#> 3 .pred_full_class_res_rf_1_05 rand_forest 0.101 full
#> 4 .pred_full_class_res_rf_1_07 rand_forest 0.00457 full
#> 5 .pred_full_class_res_rf_1_01 rand_forest 0.00219 full
# predict reflex, first as a class, then as
# class probabilities
predict(class_st, tree_frogs_class_test)
#> # A tibble: 303 × 1
#> .pred_class
#> <fct>
#> 1 full
#> 2 mid
#> 3 mid
#> 4 mid
#> 5 full
#> 6 full
#> 7 full
#> 8 full
#> 9 full
#> 10 full
#> # ℹ 293 more rows
predict(class_st, tree_frogs_class_test, type = "prob")
#> # A tibble: 303 × 3
#> .pred_full .pred_low .pred_mid
#> <dbl> <dbl> <dbl>
#> 1 0.909 0.0540 0.0371
#> 2 0.0992 0.435 0.466
#> 3 0.0956 0.418 0.486
#> 4 0.104 0.432 0.464
#> 5 0.908 0.0542 0.0373
#> 6 0.909 0.0540 0.0372
#> 7 0.909 0.0540 0.0372
#> 8 0.909 0.0540 0.0371
#> 9 0.909 0.0540 0.0371
#> 10 0.909 0.0540 0.0371
#> # ℹ 293 more rows
# returning the member predictions as well
predict(
class_st,
tree_frogs_class_test,
type = "prob",
members = TRUE
)
#> # A tibble: 303 × 18
#> .pred_full .pred_low .pred_mid .pred_low_class_res_rf_1_06
#> <dbl> <dbl> <dbl> <dbl>
#> 1 0.909 0.0540 0.0371 0
#> 2 0.0992 0.435 0.466 0.337
#> 3 0.0956 0.418 0.486 0.216
#> 4 0.104 0.432 0.464 0.334
#> 5 0.908 0.0542 0.0373 0
#> 6 0.909 0.0540 0.0372 0
#> 7 0.909 0.0540 0.0372 0
#> 8 0.909 0.0540 0.0371 0
#> 9 0.909 0.0540 0.0371 0
#> 10 0.909 0.0540 0.0371 0
#> # ℹ 293 more rows
#> # ℹ 14 more variables: .pred_low_class_res_nn_1_1 <dbl>,
#> # .pred_low_class_res_rf_1_05 <dbl>, .pred_low_class_res_rf_1_01 <dbl>,
#> # .pred_low_class_res_rf_1_07 <dbl>, .pred_mid_class_res_rf_1_06 <dbl>,
#> # .pred_mid_class_res_nn_1_1 <dbl>, .pred_mid_class_res_rf_1_05 <dbl>,
#> # .pred_mid_class_res_rf_1_01 <dbl>, .pred_mid_class_res_rf_1_07 <dbl>,
#> # .pred_full_class_res_rf_1_06 <dbl>, …
相關用法
- R stacks axe_model_stack 砍掉 model_stack。
- R stacks add_candidates 將模型定義添加到數據堆棧
- R stacks fit_members 擬合具有非零堆疊係數的模型堆疊成員
- R stacks blend_predictions 從數據堆棧中確定堆棧係數
- R stacks collect_parameters 收集候選參數和疊加係數
- R stlmethods STL 對象的方法
- R medpolish 矩陣的中值波蘭(穩健雙向分解)
- R naprint 調整缺失值
- R summary.nls 總結非線性最小二乘模型擬合
- R summary.manova 多元方差分析的匯總方法
- R formula 模型公式
- R nls.control 控製 nls 中的迭代
- R aggregate 計算數據子集的匯總統計
- R deriv 簡單表達式的符號和算法導數
- R kruskal.test Kruskal-Wallis 秩和檢驗
- R quade.test 四方測試
- R decompose 移動平均線的經典季節性分解
- R profile-methods stats4 包中的函數配置文件方法
- R plot.stepfun 繪製階躍函數
- R alias 查找模型中的別名(依賴項)
- R qqnorm 分位數-分位數圖
- R update-methods stats4包中函數更新的方法
- R eff.aovlist 多層方差分析的計算效率
- R pairwise.t.test 成對 t 檢驗
- R loglin 擬合對數線性模型
注:本文由純淨天空篩選整理自Max Kuhn等大神的英文原創作品 Predicting with a model stack。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。