通过或嵌套将 Tidymodels 进程映射到 list/group
Map Tidymodels process to a list/group by or nest
我真的很喜欢 tidymodels,但我不清楚我如何才能将该模型工作流适合嵌套分组依据之类的东西。例如,tidyr 在 mtcars 的圆柱体之类的东西上勾勒出一个简单的嵌套,然后为每个圆柱体拟合一个独特的线性调节模型。我正在尝试基于圆柱体之类的东西来拟合数百个独特的模型(可能是随机森林),但使用的是 tidymodels 工作流程(数据拆分、配方、预测)。
这是 tidyr 页面上概述的简单 nest/fit 线性规则:
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 或嵌套属性?然后,如果可能的话,需要将预测 and/or 的准确性组合起来并存储在一个数据帧中。我尝试将数据拆分为嵌套数据框,但没有成功。我觉得这是一个 purrr 地图问题,但不清楚 tidymodels 是否已经支持:
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)
欣赏help/advice/direction。
只要你愿意,绝对可以!我会设置一个函数来完成所有 tidymodels 拟合和预测你需要的,然后 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,但我不清楚我如何才能将该模型工作流适合嵌套分组依据之类的东西。例如,tidyr 在 mtcars 的圆柱体之类的东西上勾勒出一个简单的嵌套,然后为每个圆柱体拟合一个独特的线性调节模型。我正在尝试基于圆柱体之类的东西来拟合数百个独特的模型(可能是随机森林),但使用的是 tidymodels 工作流程(数据拆分、配方、预测)。
这是 tidyr 页面上概述的简单 nest/fit 线性规则:
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 或嵌套属性?然后,如果可能的话,需要将预测 and/or 的准确性组合起来并存储在一个数据帧中。我尝试将数据拆分为嵌套数据框,但没有成功。我觉得这是一个 purrr 地图问题,但不清楚 tidymodels 是否已经支持:
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)
欣赏help/advice/direction。
只要你愿意,绝对可以!我会设置一个函数来完成所有 tidymodels 拟合和预测你需要的,然后 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 创建在本例中,它在星期六失败了,可能是因为星期六开始的数据太少了。