Skip to content

Instantly share code, notes, and snippets.

View alexhallam's full-sized avatar
🚀
Launching Code

Alex Hallam alexhallam

🚀
Launching Code
View GitHub Profile
@alexhallam
alexhallam / tidy_glmnet.R
Created December 26, 2018 01:54
A way to clean a glmnet object
library(tidyverse)
library(tidymodels)
Sacramento %>% str()
y <- Sacramento %>% select(price) %>% as.matrix()
x <- Sacramento %>% as.matrix() %>% scale()
fit <- glmnet::glmnet(x, y, lambda = 4192.847)
str(fit)
tidy_glmnet <- function(fit){
lasso_coeffs <- tibble(index = seq(1:fit$dim[1]) - 1, terms = row.names(fit$beta)) %>%
@alexhallam
alexhallam / rocker_setup
Created December 15, 2018 13:05
Code to setup rocker
sudo docker run -dit --restart unless-stopped -v $PWD/share:/home/rstudio/share -p 8787:8787 -e PASSWORD=*** --name rstudio rocker/rstudio
library(tidyverse)
library(gumballthemes)
dat <- read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2018-09-11/cats_vs_dogs.csv")
dat <- dat %>%
mutate(household_label = ifelse(n_dog_households > n_cat_households, "more dog households", "more cat households"))
p <- ggplot(dat, aes(x = percent_cat_owners, y = percent_dog_owners, color = household_label)) +
geom_point() +
ylab("% Dog Owners") +
ggthemes::geom_tufteboxplot(aes(color = pre_post), size = 2)+
scale_color_viridis(discrete=TRUE, option = "magma", end = .8)
@alexhallam
alexhallam / many_lag_columns.R
Last active June 30, 2018 22:44
Make many lag columns
#https://purrple.cat/blog/2018/03/02/multiple-lags-with-tidy-evaluation/
lags <- function(var, n=10){
var <- enquo(var)
indices <- seq_len(n)
map( indices, ~quo(lag(!!var, !!.x)) ) %>%
set_names(sprintf("lag_%s_%02d", quo_text(var), indices))
}
@alexhallam
alexhallam / pred_work.R
Created June 18, 2018 18:38
Prediction workflow with train:test split
library(purrr)
library(broom)
library(modelr)
#data set up
my_iris <- iris %>%
mutate(train_test = ifelse(rbinom(n=n(), size = 1, prob = .85) == 1,
"train","test"))
#set up model function
@alexhallam
alexhallam / test_train_col.R
Created June 5, 2018 20:35
Test Train Split In dplyr with mutate
# Test/Train split
set.seed(2018) # for reproducability
daily %>%
mutate(test_train = ifelse(rbinom(n=n(), size = 1, prob = .75) == 1,
"train","test")) %>%
count(test_train)
@alexhallam
alexhallam / many_models_and_predictions.R
Created May 30, 2018 12:48
Fit many models and get predictions based off of the training data
library(modelr)
library(tidyverse)
library(gapminder)
# nest data by continent and label test/train data
nested_gap <- gapminder %>%
mutate(test_train = ifelse(year < 1992, "train", "test")) %>%
group_by(continent) %>%
nest()
`%ni%` = Negate(`%in%`)
@alexhallam
alexhallam / marginal_effects_plot.R
Created May 23, 2018 18:48
Brms Marginal Effects plots to ggplot
mp <- plot(brms::marginal_effects(bfit), plot = F)
mp$x_promo_items + theme_ipsum()