Adstock with a Weibull kernel, which can place its peak after the period of
spend. See adstock_weights_weibull() for the two parameterisations.
Arguments
- x
Numeric vector of media spend (or impressions, or GRPs) in time order.
xmust already be sorted by time and evenly spaced; the function has no index argument and cannot check this for you. Usestep_adstock()if you want the time index validated.- shape, scale
Weibull shape and scale, both positive.
scaleis in periods.- max_lag
Number of periods the kernel spans. Required and finite: the Weibull kernel has no recursive form.
- type
Either
"cdf"(monotone decay) or"pdf"(permits a delayed peak).- normalise
Should the kernel sum to 1? Defaults to
TRUE. See the Normalisation section.- state
The preceding
max_lag - 1values ofx, oldest first, or0for a cold start. Whenbyis supplied, a named list with one entry per group.adstock_state()produces one of the right shape when called with the samemax_lag; for a finite kernel the state is simply the tail ofx, soutils::tail(x, max_lag - 1)works too.- by
Optional grouping vector, or data frame of grouping vectors, the same length as
x. Adstock is applied independently within each group, which is what geo-level and panel models need. Never rely ondplyr::group_by()for this: grouping metadata does not reliably survive into every context where this function is called.- na_action
What to do about missing values in
x."error"(the default) refuses to guess."zero"treats missing media as no media, which is usually right for spend but is a substantive assumption."keep"letsNApropagate through the filter, which for the recursive form poisons every subsequent value.
Examples
spend <- c(100, 0, 0, 0, 0, 0, 0, 0)
# Delayed peak: the response builds before it decays
round(adstock_weibull(spend, shape = 2, scale = 3, max_lag = 8,
type = "pdf"), 2)
#> [1] 20.27 29.05 25.00 15.31 7.04 2.49 0.69 0.15
# Monotone form
round(adstock_weibull(spend, shape = 2, scale = 3, max_lag = 8,
type = "cdf"), 2)
#> [1] 36.80 32.93 21.11 7.77 1.31 0.08 0.00 0.00