Skip to content

Instantly share code, notes, and snippets.

@samcarlos
Created March 1, 2018 04:58
Show Gist options
  • Save samcarlos/3f309ba7d4e178c857b62c9a2e52473e to your computer and use it in GitHub Desktop.
Save samcarlos/3f309ba7d4e178c857b62c9a2e52473e to your computer and use it in GitHub Desktop.
library(dplyr)
library(genlasso)
d <- data.frame(x = sample(-seq(-20,20,2), 1e5, replace = TRUE)) %>%
mutate(event_rate = abs(round((x)/2)/10),
y = rbinom(n(), 1, event_rate),
x_fctr = as.factor(x))
y <- d$y
X <- model.matrix(y ~ x_fctr - 1, d)
edges <- c(1, 2)
for (i in 2:20) {
edges <- c(edges, i, i+1)
}
gr <- graph(edges = edges, directed = FALSE)
fl <- fusedlasso(y, X, graph = gr, verbose = TRUE)
plot(fl, numbers = TRUE, vlines = FALSE)
library(reshape)
melt_beta = melt(fl$beta)
colnames(melt_beta) = c('beta_index', 'lambda', 'beta_coefficient')
library(ggplot2)
ggplot(melt_beta, aes(x = beta_index, y = beta_coefficient, group = as.factor(lambda), colour = as.factor(lambda))) + geom_line()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment