Skip to content

Instantly share code, notes, and snippets.

@timcdlucas
Created April 19, 2017 13:48
Show Gist options
  • Save timcdlucas/08356a0351df890eb6a0f143c17d9172 to your computer and use it in GitHub Desktop.
Save timcdlucas/08356a0351df890eb6a0f143c17d9172 to your computer and use it in GitHub Desktop.
gams with glmnet
library(dplyr)
library(glmnet)
library(splines)
library(tidyr)
library(ggplot2)
library(magrittr)
d <- data_frame(cov1 = rnorm(100),
cov2 = rnorm(100),
y = cov1 ^ 2 + rnorm(100, sd = 0.3))
spline <- bs(d$cov1)
d <- cbind(d, spline)
ggplot(d %>% gather('var', 'value', -y), aes(value, y)) +
geom_point() +
geom_smooth() +
facet_wrap(~ var, scale = 'free_x')
model.lm <- lm(y ~ `1` + `2` + `3`, data = d)
gampreds <- data_frame(x = seq(-4, 4, length.out = 1000),
y = predict(model.lm, newdata = predict(spline, seq(-4, 4, length.out = 1000))))
ggplot(gampreds, aes(x, y)) +
geom_line() +
geom_point(data = d, aes(cov1, y))
model <- glmnet(x = as.matrix(d %>% select(-y, -cov1)), y = d$y)
coef(model, s = 0.01)
elasticnetpreds <- cbind(cov1 = seq(-4, 4, length.out = 1000),
predict(model,
newx = cbind(cov2 = 0, predict(spline, seq(-4, 4, length.out = 1000))),
s = c(0.01, 0.1)) %>% set_colnames(c('pred_w_lambda1', 'pred_w_lambda2'))
) %>% data.frame
ggplot(gampreds, aes(x, y)) +
geom_line(colour = 'red') +
geom_point(data = d, aes(cov1, y)) +
geom_line(data = elasticnetpreds, aes(cov1, pred_w_lambda2), colour = 'steelblue') +
geom_line(data = elasticnetpreds, aes(cov1, pred_w_lambda1), colour = 'darkblue')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment