step_ratio()
創建配方步驟的規範,該步驟將從選定的數值變量創建一個或多個比率。
用法
step_ratio(
recipe,
...,
role = "predictor",
trained = FALSE,
denom = denom_vars(),
naming = function(numer, denom) {
make.names(paste(numer, denom, sep = "_o_"))
},
columns = NULL,
keep_original_cols = TRUE,
skip = FALSE,
id = rand_id("ratio")
)
denom_vars(...)
參數
- recipe
-
一個菜譜對象。該步驟將添加到此配方的操作序列中。
- ...
-
一個或多個選擇器函數,用於選擇將在比率分子中使用哪些變量。與
denom_vars
一起使用時,點表示分母中使用了哪些變量。有關更多詳細信息,請參閱selections()
。 - role
-
對於此步驟創建的模型項,應為其分配什麽分析角色?默認情況下,此步驟根據原始變量創建的新列將用作模型中的預測變量。
- trained
-
指示預處理數量是否已估計的邏輯。
- denom
-
調用
denom_vars
來指定分母中使用哪些變量,分母可以包括由逗號或不同選擇器分隔的特定變量名稱(請參閱selections()
)。如果某一列同時包含在兩個列表中作為分子和分母,則將從列表中刪除該列。 - naming
-
定義新比率列的命名約定的函數。
- columns
-
所選變量名稱的字符串。該字段是一個占位符,一旦使用
prep()
就會被填充。 - keep_original_cols
-
將原始變量保留在輸出中的邏輯。默認為
TRUE
。 - skip
-
一個合乎邏輯的。當
bake()
烘焙食譜時是否應該跳過此步驟?雖然所有操作都是在prep()
運行時烘焙的,但某些操作可能無法對新數據進行(例如處理結果變量)。使用skip = TRUE
時應小心,因為它可能會影響後續操作的計算。 - id
-
該步驟特有的字符串,用於標識它。
整理
當您 tidy()
此步驟時,將返回包含列 terms
(選定的選擇器或變量)和 denom
的 tibble。
也可以看看
其他多元變換步驟:step_classdist_shrunken()
, step_classdist()
, step_depth()
, step_geodist()
, step_ica()
, step_isomap()
, step_kpca_poly()
, step_kpca_rbf()
, step_kpca()
, step_mutate_at()
, step_nnmf_sparse()
, step_nnmf()
, step_pca()
, step_pls()
, step_spatialsign()
例子
library(recipes)
data(biomass, package = "modeldata")
biomass$total <- apply(biomass[, 3:7], 1, sum)
biomass_tr <- biomass[biomass$dataset == "Training", ]
biomass_te <- biomass[biomass$dataset == "Testing", ]
rec <- recipe(HHV ~ carbon + hydrogen + oxygen + nitrogen +
sulfur + total,
data = biomass_tr
)
ratio_recipe <- rec %>%
# all predictors over total
step_ratio(all_numeric_predictors(), denom = denom_vars(total),
keep_original_cols = FALSE)
ratio_recipe <- prep(ratio_recipe, training = biomass_tr)
ratio_data <- bake(ratio_recipe, biomass_te)
ratio_data
#> # A tibble: 80 × 6
#> HHV carbon_o_total hydrogen_o_total oxygen_o_total nitrogen_o_total
#> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 18.3 0.465 0.0568 0.473 0.00301
#> 2 17.6 0.432 0.055 0.481 0.0285
#> 3 17.2 0.427 0.055 0.491 0.024
#> 4 18.9 0.504 0.0662 0.405 0.0195
#> 5 20.5 0.497 0.0645 0.436 0.00204
#> 6 18.5 0.479 0.0595 0.451 0.00758
#> 7 15.1 0.389 0.0523 0.541 0.0119
#> 8 16.2 0.515 0.0570 0.414 0.0116
#> 9 11.1 0.419 0.0631 0.446 0.00201
#> 10 10.8 0.456 0.0619 0.389 0.0760
#> # ℹ 70 more rows
#> # ℹ 1 more variable: sulfur_o_total <dbl>
相關用法
- R recipes step_range 將數值數據縮放到特定範圍
- R recipes step_relu 應用(平滑)修正線性變換
- R recipes step_regex 檢測正則表達式
- R recipes step_rename 使用 dplyr 按名稱重命名變量
- R recipes step_rename_at 使用 dplyr 重命名多個列
- R recipes step_rm 通用可變過濾器
- R recipes step_relevel 將因子重新調整至所需水平
- R recipes step_unknown 將缺失的類別分配給“未知”
- R recipes step_poly_bernstein 廣義伯恩斯坦多項式基
- R recipes step_impute_knn 通過 k 最近鄰進行插補
- R recipes step_impute_mean 使用平均值估算數值數據
- R recipes step_inverse 逆變換
- R recipes step_pls 偏最小二乘特征提取
- R recipes step_geodist 兩個地點之間的距離
- R recipes step_nzv 近零方差濾波器
- R recipes step_nnmf 非負矩陣分解信號提取
- R recipes step_normalize 中心和比例數值數據
- R recipes step_depth 數據深度
- R recipes step_other 折疊一些分類級別
- R recipes step_harmonic 添加正弦和餘弦項以進行諧波分析
- R recipes step_corr 高相關濾波器
- R recipes step_novel 新因子水平的簡單賦值
- R recipes step_select 使用 dplyr 選擇變量
- R recipes step_spline_b 基礎樣條
- R recipes step_window 移動窗口函數
注:本文由純淨天空篩選整理自Max Kuhn等大神的英文原創作品 Ratio Variable Creation。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。