【问题标题】:Coding Multiple Models in a Function in R Tidyverse在 R Tidyverse 的函数中编码多个模型
【发布时间】:2019-06-26 04:09:19
【问题描述】:

我正在尝试使用一些公式来拟合几个机器学习模型,并将它们作为 list_column 对象存储在 tibble 中。

我试图修改“R for Datascience”(第 25 章:许多模型)一书中引用的代码,但它只给了我最后的输出。请参阅下面的代码以获取更多详细信息。我们以 gapminder 包中的 gapminder 数据集为例。

lab_formula <- as.formula("pop ~ lifeExp ")

temp_formula <- as.formula("gdppercap ~ year")

formula_list <- list(lab_formula,temp_formula)
library(gapminder)

by_country <- gapminder %>% 
  dplyr :: group_by(country, continent) %>% 
  nest()

country_model <- function(df) {
for (i in formula_list) {
  lm(formula=formula[i], data = df)
  randomForest(formula=formula[i], data = df)
  gbm(formula=formula[i], data = df, n.minobsinnode = 2)
}
}

by_country <- by_country %>% 
  mutate(model = map(data, country_model))

by_country
# A tibble: 142 x 4
   country     continent data              model    
   <fct>       <fct>     <list>            <list>   
 1 Afghanistan Asia      <tibble [12 x 4]> <S3: gbm>
 2 Albania     Europe    <tibble [12 x 4]> <S3: gbm>
 3 Algeria     Africa    <tibble [12 x 4]> <S3: gbm>
 4 Angola      Africa    <tibble [12 x 4]> <S3: gbm>
 5 Argentina   Americas  <tibble [12 x 4]> <S3: gbm>
 6 Australia   Oceania   <tibble [12 x 4]> <S3: gbm>
 7 Austria     Europe    <tibble [12 x 4]> <S3: gbm>
 8 Bahrain     Asia      <tibble [12 x 4]> <S3: gbm>
 9 Bangladesh  Asia      <tibble [12 x 4]> <S3: gbm>
10 Belgium     Europe    <tibble [12 x 4]> <S3: gbm>
# ... with 132 more rows

There is no error code but it does not achieve my objective of training the 3 machine learning models (LM, RF, GBM) with the different variables.

【问题讨论】:

  • 您的函数country_model 不输出任何内容。您应该创建一个对象并将其传递给return。此外,您没有在创建模型时将模型存储在对象中,那么您会输出什么?您的模型可能确实运行了,但您没有得到任何输出,因为您没有将它们分配给一个对象,然后从函数中返回它。
  • 嗨 Dij,变量 'by_country' 将输出我的模型结果。请查看已编辑的问题以使其更清楚。
  • 我认为您正在覆盖函数调用中的每个模型,以便最终得到最后一个。

标签: r function tidyverse


【解决方案1】:

您需要考虑如何存储结果。这是一种方法。首先创建要应用的公式列表

library(randomForest)
library(gbm)
library(tidyverse)

lab_formula <- as.formula("pop ~ lifeExp ")
temp_formula <- as.formula("gdpPercap ~ year")
formula_list <- list(lab_formula,temp_formula)

创建一个函数,该函数返回一次仅应用于一个公式的模型列表。

country_model <- function(df, formula_list, index) {
    list(lm(formula = formula_list[[index]] , data = df), 
         randomForest(formula=formula_list[[index]], data = df),
         gbm(formula=formula_list[[index]], data = df, n.minobsinnode = 2))
}

现在将其应用于每个data,传递formula_list 和列表中要应用于数据的公式编号,

df1 <- by_country %>% 
  mutate(model1 = map(data, ~country_model(., formula_list, 1)), 
         model2 = map(data, ~country_model(., formula_list, 2)))
df1

# A tibble: 142 x 5
#   country     continent data              model1     model2    
#   <fct>       <fct>     <list>            <list>     <list>    
# 1 Afghanistan Asia      <tibble [12 × 4]> <list [3]> <list [3]>
# 2 Albania     Europe    <tibble [12 × 4]> <list [3]> <list [3]>
# 3 Algeria     Africa    <tibble [12 × 4]> <list [3]> <list [3]>
# 4 Angola      Africa    <tibble [12 × 4]> <list [3]> <list [3]>
# 5 Argentina   Americas  <tibble [12 × 4]> <list [3]> <list [3]>
# 6 Australia   Oceania   <tibble [12 × 4]> <list [3]> <list [3]>
# 7 Austria     Europe    <tibble [12 × 4]> <list [3]> <list [3]>
# 8 Bahrain     Asia      <tibble [12 × 4]> <list [3]> <list [3]>
# 9 Bangladesh  Asia      <tibble [12 × 4]> <list [3]> <list [3]>
#10 Belgium     Europe    <tibble [12 × 4]> <list [3]> <list [3]>
# … with 132 more rows

现在model1 中的每一行都有一个使用公式formula_list[[1]] 的三个模型的列表,同样对于model2,你有使用公式formula_list[[2]] 的模型。


要使用这些模型进行预测,我们可能需要以不同的方式对待 randomForest 模型,因为它需要 n.trees 参数,并且当我们从函数返回这些模型时,我们知道它是列表中的第三个模型,我们可以根据指数。

df1 %>%
   mutate(pred= map2(data,model1, function(x, y) 
     map(seq_along(y), function(i) 
        if (i == 3) predict(y[[i]], n.trees = y[[i]]$n.trees)
        else as.numeric(predict(y[[i]])))))

【讨论】:

  • 我有一个附加问题,希望你们能提供帮助!我们是否可以使用“invoke_map”函数应用于上述机器学习模型?基本上,models_tbl_fit &lt;- models_tbl %&gt;% mutate(fit = invoke_map(f, params)) models_tbl_fit 请参阅下面的链接了解更多详情:cran.rstudio.com/web/packages/sweep/vignettes/…
  • @MatthewLoh 我之前没有使用过invoke_map,但是阅读了它说它一直是retired 的文档,并且没有在积极开发中,并且有更好的替代品。
  • 再次感谢您!最后最后一个问题!进行预测的代码如下:by_country &lt;- by_country %&gt;% mutate(pred=map2(data,model1,add_predictions)) mutate_impl(.data, dots) 中的错误:评估错误:缺少参数“n.trees”,没有默认值。但我得到了一个错误,因为随机森林预测需要一个额外的参数。我如何将其插入>
  • @MatthewLoh add_predictions 有 n.trees 参数吗?如果不是,那么您可能需要单独使用 stats::predict 来接受该参数的 randomForest。 predict(model, n.trees = model$n.trees)
  • @MatthewLoh 更新了答案,现在应该可以解决了。
猜你喜欢
  • 2023-04-06
  • 2021-05-30
  • 1970-01-01
  • 2022-07-14
  • 2022-11-02
  • 1970-01-01
  • 2020-09-22
  • 1970-01-01
  • 2017-12-22
相关资源
最近更新 更多