2

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 和 spread_predictions 采用多个模型。该名称将取自模型名称的参数名称。

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

有没有一些简单的方法可以输入一个列表/大量模型来收集预测?

列表中 10 个模型的示例:

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

如果将模型以其他方式存储而不是列表效果更好,那也很好。

4

2 回答 2

1

有一些解决方法可以解决这个问题。我的方法是: 1. 建立一个具有特定名称的模型列表 2. 使用 modelr::gather_predictions() 的调整版本将列表中的所有模型应用于数据

# prerequisites
library(tidyverse)
set.seed(1363)    

# I'll use generic name 'data' throughout the code, so you can easily try other datasets.
# for this example I'll use your data df
data=df

# data visualization
ggplot(data, aes(x, y)) + 
        geom_point(size=3)

你的样本数据

# build a list of models
models <-vector("list", length = 5)
model_names <- vector("character", length=5)
for (i in 1:5) {
        modelformula <- str_c("y ~ poly(x,", i, ")", sep="")
        models[[i]] <- lm(as.formula(modelformula), data = data)
        model_names[[i]] <- str_c('model', i) # remember we name the models here sequantially
}

# apply names to the models list
names(models) <- model_names

# this is modified verison of modelr::gather_predictions() in order to accept list of models
gather.predictions <- function (data, models, .pred = "pred", .model = "model") 
{
        df <- map2(models, .pred, modelr::add_predictions, data = data)
        names(df) <- names(models)
        bind_rows(df, .id = .model)
}

# the rest is the same as modelr's function...
grids <- gather.predictions(data = data, models = models, .pred = "y")

ggplot(data, aes(x, y)) + 
        geom_point() +
        geom_line(data = grids, colour = "red") +
        facet_wrap(~ model)

应用于样本数据的多项式模型(1:5 阶)示例

旁注:我选择字符串来构建模型有充分的理由......讨论。

于 2018-10-18T08:31:20.317 回答
1
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)))
}
于 2016-12-16T15:33:41.770 回答