modelr模型
library(tidyverse)
library(modelr)
data("sim3")
mod1 <- lm(y ~ x1 + x2, data = sim3)
mod2 <- lm(y ~ x1 * x2, data = sim3)
#分布均匀的数值网格,找出其中的唯一值,然后生成所有组合
grid <- sim3 %>%
data_grid(x1, x2) %>%
#两个模型同时生成预测
gather_predictions(mod1, mod2)
ggplot(sim3, aes(x1, y, color = x2)) +
geom_point() +
geom_line(data = grid, aes(y = pred)) +
facet_wrap(~ model)
sim3 <- sim3 %>%
gather_residuals(mod1, mod2)
ggplot(sim3, aes(x1, resid, color = x2)) +
geom_point() +
facet_grid(model ~ x2)
library(modelr)
data("sim3")
mod1 <- lm(y ~ x1 + x2, data = sim3)
mod2 <- lm(y ~ x1 * x2, data = sim3)
#分布均匀的数值网格,找出其中的唯一值,然后生成所有组合
grid <- sim3 %>%
data_grid(x1, x2) %>%
#两个模型同时生成预测
gather_predictions(mod1, mod2)
ggplot(sim3, aes(x1, y, color = x2)) +
geom_point() +
geom_line(data = grid, aes(y = pred)) +
facet_wrap(~ model)
sim3 <- sim3 %>%
gather_residuals(mod1, mod2)
ggplot(sim3, aes(x1, resid, color = x2)) +
geom_point() +
facet_grid(model ~ x2)
评论
发表评论