r语言 - 将整理模型进程映射到列表/分组依据或嵌套



我真的很喜欢整洁的模型,但我不清楚如何将该模型工作流程安装在嵌套组之类的东西上。 例如,tidyr 在 mtcars 的圆柱体等东西上勾勒出一个简单的巢,然后将一个独特的线性 reg 模型拟合到每个圆柱体。 我正在尝试基于圆柱体之类的东西拟合数百个独特的模型(可能是随机森林(,但使用整洁的模型工作流程(数据拆分、配方、预测(。

以下是整洁页面上概述的简单嵌套/拟合线性规则:

mtcars_nested <- mtcars %>%
group_by(cyl) %>%
nest()
mtcars_nested <- mtcars_nested %>%
mutate(model = map(data, function(df) lm(mpg ~ wt, data = df)))
mtcars_nested

有没有办法做类似下面的事情,但基于列中的group_by或嵌套属性? 然后,如果可能的话,需要为每个预测和/或准确性组合并存储在一个数据帧中。 我尝试将数据拆分为嵌套数据帧,但没有奏效。我觉得这是一个咕噜咕噜的地图问题,但不清楚它是否是整洁模型已经支持的东西:

library(tidymodels)
library(tidyverse)
#add dataset
mtcars <- mtcars
#create data splits
split <- initial_split(mtcars)
mtcars_train <- training(split)
mtcars_test <- testing(split)
#create recipe
mtcars_recipe <-
recipe(mpg ~., data = mtcars_train) %>%
step_normalize(all_predictors())
#define model
lm_mod <-
linear_reg(mode = "regression") %>%
set_engine("lm")
#create workflow that combines recipe & model
mtcars_workflow <-
workflow() %>%
add_model(lm_mod) %>%
add_recipe(mtcars_recipe)
#fit workflow on train data
mtcars_fit <-
fit(mtcars_workflow, data = mtcars_train)
#predict on test data
predictions <-
predict(mtcars_fit, mtcars_test) 

感谢帮助/建议/指导。

如果你愿意,你绝对可以这样做!我会设置一个函数来做所有整洁的模型拟合和预测你需要的,然后map()你的嵌套数据帧。

首先定义函数外部您喜欢的任何内容,然后创建函数。

library(tidymodels)
#> ── Attaching packages ─────────────────────────────────────────── tidymodels 0.1.1 ──
#> ✓ broom     0.7.0      ✓ recipes   0.1.13
#> ✓ dials     0.0.8      ✓ rsample   0.0.7 
#> ✓ dplyr     1.0.0      ✓ tibble    3.0.3 
#> ✓ ggplot2   3.3.2      ✓ tidyr     1.1.0 
#> ✓ infer     0.5.3      ✓ tune      0.1.1 
#> ✓ modeldata 0.0.2      ✓ workflows 0.1.2 
#> ✓ parsnip   0.1.2      ✓ yardstick 0.0.7 
#> ✓ purrr     0.3.4
#> ── Conflicts ────────────────────────────────────────────── tidymodels_conflicts() ──
#> x purrr::discard() masks scales::discard()
#> x dplyr::filter()  masks stats::filter()
#> x dplyr::lag()     masks stats::lag()
#> x recipes::step()  masks stats::step()
## some example data to use
data("hpc_data")
hpc_data <- hpc_data %>%
select(-protocol, -class)
lm_mod <-
linear_reg(mode = "regression") %>%
set_engine("lm")
wf <-
workflow() %>%
add_model(lm_mod)
## big function of model fitting and predicting
predict_hpc <- function(df) {
split <- initial_split(df)
train_df <- training(split)
test_df <- testing(split)

#create recipe
recipe_train <-
recipe(compounds ~., data = train_df) %>%
step_normalize(all_predictors())

#fit workflow on train data
fit_wf <-
wf %>%
add_recipe(recipe_train) %>%
fit(data = train_df)

#predict on test data
predict(fit_wf, test_df) 

}

现在,您可以嵌套数据,然后使用函数map()这些嵌套的数据帧。最好使用像possibly()这样的咕噜声副词来很好地捕捉失败。

hpc_nested <- hpc_data %>%
group_by(day) %>%
nest()
hpc_nested %>%
mutate(predictions = map(data, possibly(predict_hpc, otherwise = NA)))
#> Timing stopped at: 0.001 0 0.001
#> # A tibble: 7 x 3
#> # Groups:   day [7]
#>   day   data               predictions       
#>   <fct> <list>             <list>            
#> 1 Tue   <tibble [900 × 5]> <tibble [225 × 1]>
#> 2 Thu   <tibble [720 × 5]> <tibble [180 × 1]>
#> 3 Fri   <tibble [923 × 5]> <tibble [230 × 1]>
#> 4 Wed   <tibble [903 × 5]> <tibble [225 × 1]>
#> 5 Mon   <tibble [692 × 5]> <tibble [173 × 1]>
#> 6 Sat   <tibble [32 × 5]>  <lgl [1]>         
#> 7 Sun   <tibble [161 × 5]> <tibble [40 × 1]>

创建于 2020-07-18 由 reprex 软件包 (v0.3.0(

在这种情况下,它周六失败了,可能是因为周六的数据太少了。

最新更新