将TidyModels进程映射到列表/分组依据或嵌套 [英] Map Tidymodels process to a list/group by or nest
本文介绍了将TidyModels进程映射到列表/分组依据或嵌套的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我真的很喜欢整洁模型,但我不清楚如何才能将该模型工作流适合嵌套的GROUP BY之类的东西。例如,tidyr在mtars的圆柱体之类的东西上勾勒出一个简单的巢,然后为每个圆柱体拟合一个独特的线性reg模型。我正在尝试基于类似圆柱体的东西来适应数百个独特的模型(可能是一个随机森林),但使用的是tidyModels工作流(数据拆分、配方、预测)。
以下是tidyr页面上作为一个简单的Nest/Fit线性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或NEST属性执行类似下面的操作?然后,如果可能,预测和/或精度将需要为每个预测组合并存储在一个数据帧中。我尝试将数据拆分为嵌套的数据帧,但不起作用。我觉得这是一个模糊的地图问题,但不清楚它是否是一些整洁的模型已经支持的东西:
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()
这样的purrr副词来很好地捕捉失败。
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]>
由reprex package(v0.3.0)于2020-07-18创建
在这种情况下,星期六失败了,可能是因为星期六的数据太少了。
这篇关于将TidyModels进程映射到列表/分组依据或嵌套的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文