library(tidyverse)
library(modelr)
library(splines)
m_ns <- lm(mpg ~ ns(wt, df = 2), data = mtcars)
m_bs <- lm(mpg ~ bs(wt, df = 4), data = mtcars)
pred <- mtcars %>%
data_grid(wt = seq(0, 7, len = 256)) %>%
gather_predictions(m_ns, m_bs)
#> Warning in bs(wt, degree = 3L, knots = c(`50%` = 3.325), Boundary.knots =
#> c(1.513, : some 'x' values beyond boundary knots may cause ill-conditioned
#> bases
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
geom_line(data = pred, aes(y = pred, color = model))Created on 2018-11-22 by the reprex package (v0.2.0.9000).
