我使用gather_predictions()向我的数据框添加了几个预测。稍后,也许在做了一些可视化之后,我想添加一个新模型的预测。我似乎无法弄清楚如何做到这一点。
我尝试使用add_predictions()和gather_predictions(),但是当我只想添加其他行时,他们添加了全新的列。
library(tidyverse)
library(modelr)
#The 3 original models
mdisp = lm(mpg ~ disp, mtcars)
mcyl = lm(mpg ~ cyl, mtcars)
mhp = lm(mpg ~ hp, mtcars)
#I added them to the data frame.
mtcars_pred <- mtcars %>%
gather_predictions(mdisp, mcyl, mhp)
#New model I want to add.
m_all <- lm(mpg ~ hp + cyl + disp, mtcars)
似乎有两种选择。
gather_predictions()
is used at the endlibrary(tidyverse)
library(modelr)
#The 3 original models
mdisp <- lm(mpg ~ disp, mtcars)
mcyl <- lm(mpg ~ cyl, mtcars)
mhp <- lm(mpg ~ hp, mtcars)
# New model
m_all <- lm(mpg ~ hp + cyl + disp, mtcars)
# Gather predictions for all four models at the same time
mtcars_pred <- mtcars %>%
gather_predictions(mdisp, mcyl, mhp, m_all)
bind_rows()
plus another call to gather_predictions()
library(tidyverse)
library(modelr)
#The 3 original models
mdisp <- lm(mpg ~ disp, mtcars)
mcyl <- lm(mpg ~ cyl, mtcars)
mhp <- lm(mpg ~ hp, mtcars)
# Get predictions from the first three models
mtcars_pred <- mtcars %>%
gather_predictions(mdisp, mcyl, mhp)
# New model
m_all <- lm(mpg ~ hp + cyl + disp, mtcars)
# Get the new model's predictions and append them
mtcars_pred <- bind_rows(mtcars_pred,
gather_predictions(data = mtcars,
m_all))