適用於多種型號的裝袋函數通用套件。
用法
bagger(x, ...)
# S3 method for default
bagger(x, ...)
# S3 method for data.frame
bagger(
x,
y,
weights = NULL,
base_model = "CART",
times = 11L,
control = control_bag(),
cost = NULL,
...
)
# S3 method for matrix
bagger(
x,
y,
weights = NULL,
base_model = "CART",
times = 11L,
control = control_bag(),
cost = NULL,
...
)
# S3 method for formula
bagger(
formula,
data,
weights = NULL,
base_model = "CART",
times = 11L,
control = control_bag(),
cost = NULL,
...
)
# S3 method for recipe
bagger(
x,
data,
base_model = "CART",
times = 11L,
control = control_bag(),
cost = NULL,
...
)
參數
- x
-
DataFrame 、矩陣或配方(取決於所使用的方法)。
- ...
-
傳遞給基本模型函數的可選參數。
- y
-
結果的數字或因子向量。分類結果(即類別)應表示為因子,而不是整數。
- weights
-
非負案例權重的數值向量。在引導重采樣期間不使用這些值。
- base_model
-
正在裝袋的模型的單個字符值。可能的值為"CART"、"MARS"、"nnet" 和"C5.0"(僅限分類)。
- times
-
引導樣本/集合成員的最大數量大於 1 的單個整數(某些模型擬合可能會失敗)。
- control
-
control_bag()
生成的選項列表。 - cost
-
非負標度(對於兩類問題)或成本矩陣。
- formula
-
"formula" 類的對象(或可以強製為該類的對象):要擬合的模型的符號說明。請注意,此包不支持多變量結果,並且如果某些預測變量是因子,則除非通過基礎模型函數,否則不會創建虛擬變量。
- data
-
包含公式或配方中使用的變量的 DataFrame 。
細節
bagger()
適合單獨的模型來引導樣本。每個模型對象的預測函數被編碼在 R 表達式中,並且原始模型對象被丟棄。進行預測時,每個預測公式都會根據新數據進行評估並使用平均值進行聚合。
變量重要性分數是使用每個包中的實現來計算的。根據請求,結果位於標題中,列名稱為term
(預測變量)、value
(重要性得分)和used
(變量在預測方程中的次數百分比)。
這些模型可以使用並行擬合未來包。啟用並行性,使用future::plan()
聲明的函數如何計算應該是分布式的。請注意,這幾乎肯定會增加適合模型所需的內存要求。
對於神經網絡,變量重要性是使用 Gevrey 等人 (2003) 中說明的 Garson 方法計算的
例子
library(recipes)
#> Loading required package: dplyr
#>
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#>
#> filter, lag
#> The following objects are masked from ‘package:base’:
#>
#> intersect, setdiff, setequal, union
#>
#> Attaching package: ‘recipes’
#> The following object is masked from ‘package:stats’:
#>
#> step
library(dplyr)
data(biomass, package = "modeldata")
biomass_tr <-
biomass %>%
dplyr::filter(dataset == "Training") %>%
dplyr::select(-dataset, -sample)
biomass_te <-
biomass %>%
dplyr::filter(dataset == "Testing") %>%
dplyr::select(-dataset, -sample)
# ------------------------------------------------------------------------------
ctrl <- control_bag(var_imp = TRUE)
# ------------------------------------------------------------------------------
# `times` is low to make the examples run faster
set.seed(7687)
mars_bag <- bagger(x = biomass_tr[, -6], y = biomass_tr$HHV,
base_model = "MARS", times = 5, control = ctrl)
mars_bag
#> Bagged MARS (regression with 5 members)
#>
#> Variable importance scores include:
#>
#> # A tibble: 5 × 4
#> term value std.error used
#> <chr> <dbl> <dbl> <int>
#> 1 carbon 100 0 5
#> 2 hydrogen 18.1 2.66 5
#> 3 oxygen 11.6 2.53 4
#> 4 sulfur 1.56 0 1
#> 5 nitrogen 1.09 0.757 2
#>
var_imp(mars_bag)
#> # A tibble: 5 × 4
#> term value std.error used
#> <chr> <dbl> <dbl> <int>
#> 1 carbon 100 0 5
#> 2 hydrogen 18.1 2.66 5
#> 3 oxygen 11.6 2.53 4
#> 4 sulfur 1.56 0 1
#> 5 nitrogen 1.09 0.757 2
set.seed(7687)
cart_bag <- bagger(x = biomass_tr[, -6], y = biomass_tr$HHV,
base_model = "CART", times = 5, control = ctrl)
cart_bag
#> Bagged CART (regression with 5 members)
#>
#> Variable importance scores include:
#>
#> # A tibble: 5 × 4
#> term value std.error used
#> <chr> <dbl> <dbl> <int>
#> 1 carbon 5716. 226. 5
#> 2 oxygen 3190. 110. 5
#> 3 hydrogen 2297. 283. 5
#> 4 sulfur 464. 63.2 5
#> 5 nitrogen 268. 12.3 5
#>
# ------------------------------------------------------------------------------
# Other interfaces
# Recipes can be used
biomass_rec <-
recipe(HHV ~ ., data = biomass_tr) %>%
step_pca(all_predictors())
set.seed(7687)
cart_pca_bag <- bagger(biomass_rec, data = biomass_tr, base_model = "CART",
times = 5, control = ctrl)
cart_pca_bag
#> Bagged CART (regression with 5 members)
#>
#> Variable importance scores include:
#>
#> # A tibble: 5 × 4
#> term value std.error used
#> <chr> <dbl> <dbl> <int>
#> 1 PC2 4500. 245. 5
#> 2 PC1 3559. 156. 5
#> 3 PC3 1107. 210. 5
#> 4 PC5 648. 137. 5
#> 5 PC4 468. 71.6 5
#>
# Using formulas
mars_bag <- bagger(HHV ~ ., data = biomass_tr, base_model = "MARS", times = 5,
control = ctrl)
mars_bag
#> Bagged MARS (regression with 5 members)
#>
#> Variable importance scores include:
#>
#> # A tibble: 5 × 4
#> term value std.error used
#> <chr> <dbl> <dbl> <int>
#> 1 carbon 100 0 5
#> 2 oxygen 22.4 3.09 5
#> 3 hydrogen 17.2 1.45 5
#> 4 sulfur 2.22 2.10 2
#> 5 nitrogen 1.39 0 1
#>
相關用法
- R baguette control_bag 控製裝袋過程
- R baguette predict.bagger 來自 bagged 模型的預測
- R baguette class_cost 少數群體的成本參數
- R file.path 構造文件路徑
- R grep 模式匹配和替換
- R getwd 獲取或設置工作目錄
- R vector 向量 - 創建、強製等
- R lapply 對列表或向量應用函數
- R dump R 對象的文本表示
- R Sys.getenv 獲取環境變量
- R rank 樣本排名
- R getDLLRegisteredRoutines DLL 中 C/Fortran 例程的反射信息
- R pushBack 將文本推回連接
- R strsplit 分割字符向量的元素
- R seq.Date 生成規則的日期序列
- R invisible 將打印模式更改為不可見
- R noquote “無引號”字符串打印類
- R warning 警告信息
- R rapply 遞歸地將函數應用於列表
- R basename 操作文件路徑
- R with 評估數據環境中的表達式
- R formals 訪問和操縱形式參數
- R icuSetCollate 按 ICU 設置整理
- R search 給出 R 對象的搜索路徑
- R Defunct 將對象標記為已失效
注:本文由純淨天空篩選整理自Max Kuhn等大神的英文原創作品 Bagging functions。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。