Skip to content

Instantly share code, notes, and snippets.

@honake
Last active April 9, 2021 08:46
Show Gist options
  • Save honake/9483221459bd733836c0c21852a3a4d4 to your computer and use it in GitHub Desktop.
Save honake/9483221459bd733836c0c21852a3a4d4 to your computer and use it in GitHub Desktop.
Calculate the optimal Ad Stock in R
optimizeAdStock <- function(grp,sales){
rmax <- 0
corrmax <- -1
rates <- seq(0.00, 1.00, by = 0.01)
corrs <- vector()
for(r in rates){
adstock <- stats::filter(grp, filter=r, method = "recursive")
corr <- cor(adstock,sales)
if(corr>corrmax){
corrmax <- corr
rmax <- r
}
corrs <- append(corrs, corr)
}
plot(rates,corrs)
sprintf("忘却率が%fのとき相関係数は最大値%f",rmax,corrmax)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment