Skip to content

Commit

Permalink
recode: use (new) Robyn function instead for imm vs carr
Browse files Browse the repository at this point in the history
  • Loading branch information
laresbernardo committed Jul 18, 2024
1 parent e16e758 commit 000e9b3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Imports:
rpart.plot,
rvest,
stringr,
tidyr,
tidyr (>= 1.0.0),
yaml
Suggests:
beepr,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,8 @@ importFrom(stringr,str_split)
importFrom(stringr,str_to_title)
importFrom(stringr,word)
importFrom(tidyr,gather)
importFrom(tidyr,pivot_longer)
importFrom(tidyr,pivot_wider)
importFrom(tidyr,spread)
importFrom(tidyr,unite)
importFrom(utils,URLencode)
Expand Down
4 changes: 2 additions & 2 deletions R/lares.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
#' reorder start kmeans var xtabs as.formula prcomp p.adjust pt model.matrix qt cor.test
#' @importFrom stringr fixed str_count str_length str_pad str_replace_all str_split
#' str_to_title word
#' @importFrom tidyr gather spread unite
#' @importFrom tidyr gather pivot_longer pivot_wider spread unite
#' @importFrom utils head tail packageVersion URLencode capture.output data download.file
#' globalVariables installed.packages write.table install.packages remove.packages object.size
#' type.convert flush.console read.table modifyList write.csv combn browseURL type.convert
Expand Down Expand Up @@ -126,7 +126,7 @@ recommended <- list(
quantmod = c("getDividends", "getSymbols", "getQuote"),
rdrop2 = c("drop_auth", "drop_dir", "drop_download", "drop_search", "drop_upload"),
rgdal = c("readOGR", "project"),
Robyn = "robyn_allocator",
Robyn = c("robyn_allocator", "robyn_immcarr"),
RPostgreSQL = "PostgreSQL",
Rtsne = "Rtsne",
rtweet = c("create_token", "search_tweets"),
Expand Down
34 changes: 16 additions & 18 deletions R/robyn.R
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,11 @@ plot.robyn_modelselector <- function(x, ...) {
#' (promotional which is paid and organic channels, baseline, grand total).
#' @param marginals Boolean. Include mROAS or mCPA marginal performance metric
#' as an additional column called "marginal". Calculations are based on
#' mean spend and mean carryover values between \code{start_date} and code{end_date}.
#' mean spend and mean response with mean carryover results,
#' between \code{start_date} and code{end_date}.
#' @param carryovers Boolean. Add mean percentage of carryover response for
#' date range between \code{start_date} and code{end_date}.
#' 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.
#' @return data.frame with results on ROAS/CPA, spend, response, contribution
#' per channel, with or without total rows.
#' @examples
Expand Down Expand Up @@ -568,18 +570,15 @@ robyn_performance <- function(
if (totals) ret <- rbind(ret, totals_df, totals_base, grand_total)
ret <- left_join(ret, mktg_contr2, "channel")

# Build auxiliary object with allocator metrics
if (marginals || carryovers) {
ba_temp <- suppressWarnings(robyn_allocator(
# Add mROAS/mCPA
if (marginals) {
try_require("Robyn")
ba_temp <- robyn_allocator(
InputCollect = InputCollect,
OutputCollect = OutputCollect,
date_range = c(as.Date(start_date), as.Date(end_date)),
export = FALSE, quiet = TRUE
))
}

# Add mROAS/mCPA
if (marginals) {
)
marginal <- ba_temp$dt_optimOut %>%
select(c("channels", "initResponseMargUnit")) %>%
rename(
Expand All @@ -591,15 +590,14 @@ robyn_performance <- function(
}
# Add carryover response percentage
if (carryovers) {
curve_points <- ba_temp$mainPoints %>%
filter(.data$type %in% c("Initial", "Carryover")) %>%
select(c("channel", "type", "mean_spend", "spend_point", "response_point")) %>%
arrange(.data$channel, .data$type)
a <- filter(curve_points, .data$type == "Carryover")
b <- filter(curve_points, .data$type == "Initial")
try_require("Robyn")
carrov <- robyn_immcarr(
InputCollect, OutputCollect, solID = solID,
start_date = start_date, end_date = end_date, ...) %>%
filter(.data$type == "Carryover")
mean_carryovers <- data.frame(
channel = a$channel,
carryover = signif(a$response_point / b$response_point, 4)
channel = carrov$rn,
carryover = signif(carrov$carryover_pct, 4)
)
ret <- left_join(ret, mean_carryovers, "channel") %>%
dplyr::relocate("carryover", .after = "response")
Expand Down
6 changes: 4 additions & 2 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 000e9b3

Please sign in to comment.