Skip to content

Instantly share code, notes, and snippets.

@mikmart
Created November 22, 2018 13:19
Show Gist options
  • Select an option

  • Save mikmart/30131d26de39b8ff9412435d382de118 to your computer and use it in GitHub Desktop.

Select an option

Save mikmart/30131d26de39b8ff9412435d382de118 to your computer and use it in GitHub Desktop.
Natural vs. B-splines
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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment