Last active
April 9, 2021 08:46
-
-
Save honake/9483221459bd733836c0c21852a3a4d4 to your computer and use it in GitHub Desktop.
Calculate the optimal Ad Stock in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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