Skip to content

Commit

Permalink
feat: new new_version param for robyn_performance() to use robyn_marg…
Browse files Browse the repository at this point in the history
…inal()
  • Loading branch information
bl896211_gsk committed Feb 6, 2025
1 parent d15352e commit 504fe7a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Suggests:
rmarkdown
URL: https://github.com/laresbernardo/lares, https://laresbernardo.github.io/lares/
BugReports: https://github.com/laresbernardo/lares/issues
RoxygenNote: 7.3.2
RoxygenNote: 7.3.1
License: AGPL-3
Encoding: UTF-8
LazyData: true
Expand Down
11 changes: 7 additions & 4 deletions R/robyn.R
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,8 @@ plot.robyn_modelselector <- function(x, ...) {
#' @param carryovers Boolean. Add mean percentage of carryover response for
#' date range between \code{start_date} and \code{end_date} on paid channels.
#' Keep in mind organic variables also have carryover but currently not showing.
#' @param new_version Boolean. Use dev version's new function for marginal
#' calculations (if available)?
#' @return data.frame with results on ROAS/CPA, spend, response, contribution
#' per channel, with or without total rows.
#' @examples
Expand All @@ -530,6 +532,7 @@ robyn_performance <- function(
start_date = NULL, end_date = NULL,
solID = NULL, totals = TRUE, non_promo = FALSE,
marginals = FALSE, carryovers = FALSE,
new_version = TRUE,
quiet = FALSE, ...) {
dt_mod <- InputCollect$dt_mod
df <- OutputCollect$mediaVecCollect
Expand Down Expand Up @@ -662,7 +665,7 @@ robyn_performance <- function(
# Add mROAS/mCPA
if (marginals) {
try_require("Robyn")
if (packageVersion("Robyn") >= "3.12.0.9007") {
if (packageVersion("Robyn") >= "3.12.0.9007" && new_version) {
temp <- lapply(InputCollect$all_media, function(x) robyn_marginal(
InputCollect = InputCollect,
OutputCollect = OutputCollect,
Expand Down Expand Up @@ -746,7 +749,8 @@ robyn_marginal <- function(..., marginal_unit = 1) {
args$metric_value <- metric_value + marginal_unit
args$quiet <- TRUE
Response2 <- do.call(robyn_response, args)
ret <- list(Response1 = Response1, Response2 = Response2)
ret <- list(Response1 = Response1, Response2 = Response2, args = args)
ret$args$marginal_unit <- marginal_unit
if (args$InputCollect$dep_var_type == "revenue") {
ret$marginal_metric <- "mROAS"
ret$marginal <- (Response2$sim_mean_response - Response1$sim_mean_response) /
Expand All @@ -756,11 +760,10 @@ robyn_marginal <- function(..., marginal_unit = 1) {
ret$marginal <- (Response2$sim_mean_spend - Response1$sim_mean_spend) /
(Response2$sim_mean_response - Response1$sim_mean_response)
}
ret$marginal_unit <- marginal_unit
cap <- sprintf("\n%s: %s (marginal units: %s)",
ret$marginal_metric,
num_abbr(ret$marginal),
num_abbr(ret$marginal_unit))
num_abbr(marginal_unit))
ret$plot <- ret$Response1$plot + labs(caption = paste0(
ret$Response1$plot$labels$caption, cap
))
Expand Down
6 changes: 6 additions & 0 deletions man/h2o_automl.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions man/robyn_performance.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 504fe7a

Please sign in to comment.