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 / ggpairs_like_pairs.R
Created March 7, 2018 21:43
ggpairs like pairs
GGally::ggpairs(upper = list(continuous = "points"),
lower = list(continuous = "cor"))
@alexhallam
alexhallam / media_mix_modeling_exGAUS.R
Created March 20, 2018 10:34
using the exgaus distribution to model response time
# https://link.springer.com/article/10.1057/jma.2014.3#Tab2
# using the exgaus distribution to model response time
library(tidyverse)
data.frame(x=c(0, 20)) %>%
ggplot(aes(x)) +
stat_function(fun=function(x) dexGAUS(x, mu=1, sigma=2, nu=3),
linetype = 1) +
stat_function(fun=function(x) dexGAUS(x, mu=4, sigma=0.04, nu=4),
linetype = 2) +
@alexhallam
alexhallam / my_example.R
Last active April 23, 2018 00:42
Debug some stan code
# load libraries
library(purrr)
library(tidyverse)
# positive_round function
positive_round <- function(...) round(pmax(..., 0), 0)
# adstock function
adstock<-function(x,rate=0){
return(as.numeric(stats::filter(x=x,filter=rate,method="recursive")))
@alexhallam
alexhallam / working_adstock_finder.Rmd
Last active April 23, 2018 14:08
Working adstock Finder
```{r}
# load libraries
library(purrr)
library(tidyverse)
# positive_round function
positive_round <- function(...) round(pmax(..., 0), 0)
# adstock function
adstock<-function(x,rate=0){
@alexhallam
alexhallam / week_year.R
Created April 23, 2018 17:13
week year to date in r
mutate(date = as.Date(paste(fiscal_year, fiscal_week_number, 1, sep="-"), "%Y-%U-%u")) %>%
@alexhallam
alexhallam / grouped_complete_case.R
Created May 2, 2018 19:27
grouped_complete_case
trans %>%
clean_names() %>%
group_by(rest_number) %>%
filter(!any(!complete.cases(comp_trans_ty_ty)))
@alexhallam
alexhallam / roll_avg.R
Created May 18, 2018 16:13
rolling averages
tq_mutate(select = usd,mutate_fun = rollapply,FUN = mean,width = 13,
col_rename = "roll13") %>%
tq_mutate(select = usd,mutate_fun = rollapply,FUN = mean,width = 2,
col_rename = "roll2")
@alexhallam
alexhallam / marketing_mix.stan
Created May 22, 2018 11:47
Bayesian Methods for Media Mix Modeling with Carryover and Shape Effects 14th April 2017
functions {
// the Hill function
real Hill(real t, real ec, real slope) {
return 1 / (1 + (t / ec)^(-slope));
}
// the adstock transformation with a vector of weights
real Adstock(row_vector t, row_vector weights) {
return dot_product(t, weights) / sum(weights);
}
}
@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()
`%ni%` = Negate(`%in%`)