r - 如何将大量模型传递给gather_predictions

标签 r tidyverse

modelr 包中,函数 gather_predictions 可用于将多个模型的预测添加到数据框中,但是我不确定如何在中指定这些模型函数调用。帮助文档给出了以下示例:

df <- tibble::data_frame(
  x = sort(runif(100)),
  y = 5 * x + 0.5 * x ^ 2 + 3 + rnorm(length(x))
)

m1 <- lm(y ~ x, data = df)
grid <- data.frame(x = seq(0, 1, length = 10))
grid %>% add_predictions(m1)

m2 <- lm(y ~ poly(x, 2), data = df)
grid %>% spread_predictions(m1, m2)
grid %>% gather_predictions(m1, m2)

这里在函数调用中特别提到了模型。如果我们有一些想要预测的模型,那么这种方法就很好用,但如果我们有大量或未知数量的模型怎么办?在这种情况下,手动指定模型实际上不再可行。

帮助文档表述参数段的方式似乎表明您需要将每个模型添加为单独的参数。

gather_predictions and spread_predictions take multiple models. The name will be taken from either the argument name of the name of the model.

例如,将模型列表输入到 Gather_predictions 中是行不通的。

是否有一些简单的方法可以将列表/大量模型输入到gather_predictions?

列表中 10 个模型的示例:

modelslist <- list()
for (N in 1:10) {
  modelslist[[N]] <- lm(y ~ poly(x, N), data = df)
}

如果以列表以外的其他方式存储模型效果更好,那也没关系。

最佳答案

m <- grid %>% gather_predictions(lm(y ~ poly(x, 1), data = df))
for (N in 2:10) {
  m <- rbind(m, grid %>% gather_predictions(lm(y ~ poly(x, N), data = df)))
}

关于r - 如何将大量模型传递给gather_predictions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41187242/

相关文章:

r - 在 R 中将 cbind() 与矩阵一起使用时如何保留列标题

regex - 将字符串拆分为列

r - 查找R中字符列的差异

r - dplyr::summarize 按字母顺序排列,但我需要原始顺序

r - 列出所有 1 个恰好相差 1 位的数字

r - 取消列出嵌套列表的倒数第二个列表

r - 如何将 data.table 转换为矩阵?

r - 因子到因子的条件重新编码

r - 将值排列在特定组内

r - R : how to scrape tables after specific Title 中的 Tabulizer 包