如何使用 Nest 和 mutate 从训练集中创建模型,然后将其应用于 R 中的测试数据(tidymodels)
How to use Nest and mutate to create a model from training set and then apply it on a test data in R (tidymodels)
library(tidymodels)
Train %>% nest(-Groups) %>%
mutate(fit=map(data,~lm(X~Y+Z,x=.)),
augmented = map(fit,augment),
predict = map2(fit,Y,Z)) %>%
unnest(augmented) %>% select(-data)
这与火车数据完美配合。我可以通过使用不同的扫帚功能(如 glance 或 augment)来进行拟合、模型摘要等。每个小组都有自己的模型,就像我想的那样。
挑战在于我想在测试数据上使用这个模型。
看起来很简单,但不知何故解决方案让我望而却步:(
当你像这样适应嵌套数据时,你最终会得到很多模型,而不仅仅是一个,所以你还需要设置自己以预测许多模型 .
library(tidyverse)
library(broom)
data(Orange)
Orange <- as_tibble(Orange)
orange_fit <- Orange %>%
nest(data = c(-Tree)) %>% ## this sets up five separate models
mutate(
fit = map(data, ~ lm(age ~ circumference, data = .x))
)
## the "test data" here is `circumference = c(50, 100, 150)`
orange_fit %>%
select(Tree, fit) %>%
crossing(circumference = c(50, 100, 150)) %>%
mutate(new_data = map(circumference, ~tibble(circumference = .)),
predicted_age = map2_dbl(fit, new_data, predict))
#> # A tibble: 15 x 5
#> Tree fit circumference new_data predicted_age
#> <ord> <list> <dbl> <list> <dbl>
#> 1 3 <lm> 50 <tibble [1 × 1]> 392.
#> 2 3 <lm> 100 <tibble [1 × 1]> 994.
#> 3 3 <lm> 150 <tibble [1 × 1]> 1596.
#> 4 1 <lm> 50 <tibble [1 × 1]> 331.
#> 5 1 <lm> 100 <tibble [1 × 1]> 927.
#> 6 1 <lm> 150 <tibble [1 × 1]> 1523.
#> 7 5 <lm> 50 <tibble [1 × 1]> 385.
#> 8 5 <lm> 100 <tibble [1 × 1]> 824.
#> 9 5 <lm> 150 <tibble [1 × 1]> 1264.
#> 10 2 <lm> 50 <tibble [1 × 1]> 257.
#> 11 2 <lm> 100 <tibble [1 × 1]> 647.
#> 12 2 <lm> 150 <tibble [1 × 1]> 1037.
#> 13 4 <lm> 50 <tibble [1 × 1]> 282.
#> 14 4 <lm> 100 <tibble [1 × 1]> 640.
#> 15 4 <lm> 150 <tibble [1 × 1]> 999.
由 reprex package (v0.3.0)
于 2021-01-25 创建
请注意,最后我们对 each 模型 (5) 的测试集 (3) 中的 each 点进行了预测。
library(tidymodels)
Train %>% nest(-Groups) %>%
mutate(fit=map(data,~lm(X~Y+Z,x=.)),
augmented = map(fit,augment),
predict = map2(fit,Y,Z)) %>%
unnest(augmented) %>% select(-data)
这与火车数据完美配合。我可以通过使用不同的扫帚功能(如 glance 或 augment)来进行拟合、模型摘要等。每个小组都有自己的模型,就像我想的那样。
挑战在于我想在测试数据上使用这个模型。
看起来很简单,但不知何故解决方案让我望而却步:(
当你像这样适应嵌套数据时,你最终会得到很多模型,而不仅仅是一个,所以你还需要设置自己以预测许多模型 .
library(tidyverse)
library(broom)
data(Orange)
Orange <- as_tibble(Orange)
orange_fit <- Orange %>%
nest(data = c(-Tree)) %>% ## this sets up five separate models
mutate(
fit = map(data, ~ lm(age ~ circumference, data = .x))
)
## the "test data" here is `circumference = c(50, 100, 150)`
orange_fit %>%
select(Tree, fit) %>%
crossing(circumference = c(50, 100, 150)) %>%
mutate(new_data = map(circumference, ~tibble(circumference = .)),
predicted_age = map2_dbl(fit, new_data, predict))
#> # A tibble: 15 x 5
#> Tree fit circumference new_data predicted_age
#> <ord> <list> <dbl> <list> <dbl>
#> 1 3 <lm> 50 <tibble [1 × 1]> 392.
#> 2 3 <lm> 100 <tibble [1 × 1]> 994.
#> 3 3 <lm> 150 <tibble [1 × 1]> 1596.
#> 4 1 <lm> 50 <tibble [1 × 1]> 331.
#> 5 1 <lm> 100 <tibble [1 × 1]> 927.
#> 6 1 <lm> 150 <tibble [1 × 1]> 1523.
#> 7 5 <lm> 50 <tibble [1 × 1]> 385.
#> 8 5 <lm> 100 <tibble [1 × 1]> 824.
#> 9 5 <lm> 150 <tibble [1 × 1]> 1264.
#> 10 2 <lm> 50 <tibble [1 × 1]> 257.
#> 11 2 <lm> 100 <tibble [1 × 1]> 647.
#> 12 2 <lm> 150 <tibble [1 × 1]> 1037.
#> 13 4 <lm> 50 <tibble [1 × 1]> 282.
#> 14 4 <lm> 100 <tibble [1 × 1]> 640.
#> 15 4 <lm> 150 <tibble [1 × 1]> 999.
由 reprex package (v0.3.0)
于 2021-01-25 创建请注意,最后我们对 each 模型 (5) 的测试集 (3) 中的 each 点进行了预测。