Skip to content

Commit

Permalink
recode: return immediate for lagged adstock #706
Browse files Browse the repository at this point in the history
  • Loading branch information
gufengzhou committed May 9, 2023
1 parent ac1d864 commit 102ca54
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
5 changes: 1 addition & 4 deletions R/R/calibration.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,9 @@ robyn_calibrate <- function(calibration_input,
scale <- hypParamSam[paste0(get_channels[l_chn], "_scales")][[1]][[1]]
}
x_list <- transform_adstock(m, adstock, theta = theta, shape = shape, scale = scale)
m_imme <- x_list$x
if (adstock == "weibull_pdf") {m_imme <- x_list$x_imme} else {m_imme <- m}

This comment has been minimized.

Copy link
@laresbernardo

laresbernardo May 9, 2023

Collaborator

A bit cleaner: m_imme <- if (adstock == "weibull_pdf") x_list$x_imme else m @gufengzhou

m_total <- x_list$x_decayed
m_caov <- m_total - m_imme
# Adapt for weibull_pdf with lags
m_imme[m_caov < 0] <- 0
m_caov[m_caov < 0] <- m_total[m_caov < 0]

## 2. Saturation
m_caov_calib <- m_caov[calib_pos]
Expand Down
15 changes: 7 additions & 8 deletions R/R/transformation.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,16 @@ adstock_weibull <- function(x, shape, scale, windlen = length(x), type = "cdf")
thetaVecCumLag <- lag(thetaVecCum, x_pos - 1, default = 0)
x.prod <- x.vec * thetaVecCumLag
return(x.prod)
}, x_val = x, x_pos = x_bin[seq_along(x)])
}, x_val = x, x_pos = seq_along(x))
x_imme <- mapply(function(pos) {x_decayed[pos, pos]}, pos = seq_along(x), SIMPLIFY = TRUE)
x_decayed <- rowSums(x_decayed)[seq_along(x)]
}
} else {
x_decayed <- x
x_decayed <- x_imme <- x
thetaVecCum <- 1
}
inflation_total <- sum(x_decayed) / sum(x)
return(list(x = x, x_decayed = x_decayed, thetaVecCum = thetaVecCum, inflation_total = inflation_total))
return(list(x = x, x_decayed = x_decayed, thetaVecCum = thetaVecCum, inflation_total = inflation_total, x_imme = x_imme))
}

#' @rdname adstocks
Expand Down Expand Up @@ -379,12 +380,11 @@ run_transformations <- function(InputCollect, hypParamSam, adstock) {
scale <- hypParamSam[paste0(all_media[v], "_scales")][[1]][[1]]
}
x_list <- transform_adstock(m, adstock, theta = theta, shape = shape, scale = scale)
if (adstock == "weibull_pdf") {m_imme <- x_list$x_imme} else {m_imme <- m}
m_adstocked <- x_list$x_decayed
mediaAdstocked[[v]] <- m_adstocked
m_carryover <- m_adstocked - m
m[m_carryover < 0] <- m_adstocked[m_carryover < 0] # adapt for weibull_pdf with lags
m_carryover[m_carryover < 0] <- 0 # adapt for weibull_pdf with lags
mediaImmediate[[v]] <- m
m_carryover <- m_adstocked - m_imme
mediaImmediate[[v]] <- m_imme
mediaCarryover[[v]] <- m_carryover
mediaVecCum[[v]] <- x_list$thetaVecCum

Expand All @@ -394,7 +394,6 @@ run_transformations <- function(InputCollect, hypParamSam, adstock) {
m_adstockedRollWind <- m_adstocked[rollingWindowStartWhich:rollingWindowEndWhich]
m_carryoverRollWind <- m_carryover[rollingWindowStartWhich:rollingWindowEndWhich]


alpha <- hypParamSam[paste0(all_media[v], "_alphas")][[1]][[1]]
gamma <- hypParamSam[paste0(all_media[v], "_gammas")][[1]][[1]]
mediaSaturated[[v]] <- saturation_hill(
Expand Down

0 comments on commit 102ca54

Please sign in to comment.