@@ -132,12 +132,12 @@ robyn_response <- function(InputCollect = NULL,
132132 if (is.null(dt_hyppar )) dt_hyppar <- OutputCollect $ resultHypParam
133133 if (is.null(dt_coef )) dt_coef <- OutputCollect $ xDecompAgg
134134 } else {
135- # Get pre-filled values
136- if (is.null(dt_hyppar )) dt_hyppar <- OutputCollect $ resultHypParam
137- if (is.null(dt_coef )) dt_coef <- OutputCollect $ xDecompAgg
138- if (any(is.null(dt_hyppar ), is.null(dt_coef ), is.null(InputCollect ), is.null(OutputCollect ))) {
139- stop(" When 'json_file' is not provided, 'InputCollect' & 'OutputCollect' must be provided" )
140- }
135+ # Get pre-filled values
136+ if (is.null(dt_hyppar )) dt_hyppar <- OutputCollect $ resultHypParam
137+ if (is.null(dt_coef )) dt_coef <- OutputCollect $ xDecompAgg
138+ if (any(is.null(dt_hyppar ), is.null(dt_coef ), is.null(InputCollect ), is.null(OutputCollect ))) {
139+ stop(" When 'json_file' is not provided, 'InputCollect' & 'OutputCollect' must be provided" )
140+ }
141141 }
142142
143143 if (" selectID" %in% names(OutputCollect )) {
@@ -202,23 +202,26 @@ robyn_response <- function(InputCollect = NULL,
202202 hist_transform <- transform_decomp(
203203 all_values = all_values ,
204204 adstock , theta , shape , scale , alpha , gamma ,
205- window_loc , coeff , metric_loc = ds_list $ metric_loc )
205+ window_loc , coeff , metric_loc = ds_list $ metric_loc
206+ )
206207 dt_line <- data.frame (
207208 metric = hist_transform $ input_total [window_loc ],
208209 response = hist_transform $ response_total ,
209- channel = metric_name_updated )
210+ channel = metric_name_updated
211+ )
210212 dt_point <- data.frame (
211213 mean_input_immediate = hist_transform $ mean_input_immediate ,
212214 mean_input_carryover = hist_transform $ mean_input_carryover ,
213215 mean_input_total = hist_transform $ mean_input_immediate + hist_transform $ mean_input_carryover ,
214216 mean_response_immediate = hist_transform $ mean_response_total - hist_transform $ mean_response_carryover ,
215217 mean_response_carryover = hist_transform $ mean_response_carryover ,
216218 mean_response_total = hist_transform $ mean_response_total
217- )
219+ )
218220 if (! is.null(date_range )) {
219221 dt_point_sim <- data.frame (
220222 input = hist_transform $ sim_mean_spend + hist_transform $ sim_mean_carryover ,
221- output = hist_transform $ sim_mean_response )
223+ output = hist_transform $ sim_mean_response
224+ )
222225 }
223226
224227 # # Simulated transformation
@@ -227,10 +230,12 @@ robyn_response <- function(InputCollect = NULL,
227230 all_values = all_values_updated ,
228231 adstock , theta , shape , scale , alpha , gamma ,
229232 window_loc , coeff , metric_loc = ds_list $ metric_loc ,
230- calibrate_inflexion = hist_transform $ inflexion )
233+ calibrate_inflexion = hist_transform $ inflexion
234+ )
231235 dt_point_sim <- data.frame (
232236 input = hist_transform_sim $ sim_mean_spend + hist_transform_sim $ sim_mean_carryover ,
233- output = hist_transform_sim $ sim_mean_response )
237+ output = hist_transform_sim $ sim_mean_response
238+ )
234239 }
235240
236241 # # Plot optimal response
@@ -239,17 +244,20 @@ robyn_response <- function(InputCollect = NULL,
239244 geom_point(
240245 data = dt_point ,
241246 aes(x = .data $ mean_input_total , y = .data $ mean_response_total ),
242- size = 3 , color = " grey" ) +
247+ size = 3 , color = " grey"
248+ ) +
243249 labs(
244250 title = paste(
245251 " Saturation curve of" , metric_type $ metric_type ,
246252 " media:" , metric_type $ metric_name_updated
247253 ),
248- subtitle = sprintf(paste(
249- " Response: %s @ mean input %s" ,
250- " Response: %s @ mean input carryover %s" ,
251- " Response: %s @ mean input immediate %s" ,
252- sep = " \n " ),
254+ subtitle = sprintf(
255+ paste(
256+ " Response: %s @ mean input %s" ,
257+ " Response: %s @ mean input carryover %s" ,
258+ " Response: %s @ mean input immediate %s" ,
259+ sep = " \n "
260+ ),
253261 num_abbr(dt_point $ mean_response_total ),
254262 num_abbr(dt_point $ mean_input_total ),
255263 num_abbr(dt_point $ mean_response_carryover ),
@@ -294,6 +302,7 @@ robyn_response <- function(InputCollect = NULL,
294302 mean_input_carryover = hist_transform $ mean_input_carryover ,
295303 mean_response_total = hist_transform $ mean_response_total ,
296304 mean_response_carryover = hist_transform $ mean_response_carryover ,
305+ mean_response = hist_transform $ mean_response ,
297306 sim_mean_spend = sim_mean_spend ,
298307 sim_mean_carryover = sim_mean_carryover ,
299308 sim_mean_response = sim_mean_response ,
@@ -327,7 +336,7 @@ which_usecase <- function(metric_value, date_range) {
327336}
328337
329338transform_decomp <- function (all_values , adstock , theta , shape , scale , alpha , gamma ,
330- window_loc , coeff , metric_loc , calibrate_inflexion = NULL ) {
339+ window_loc , coeff , metric_loc , calibrate_inflexion = NULL ) {
331340 # # adstock
332341 x_list <- transform_adstock(x = all_values , adstock , theta , shape , scale )
333342 input_total <- x_list $ x_decayed
@@ -348,6 +357,7 @@ transform_decomp <- function(all_values, adstock, theta, shape, scale, alpha, ga
348357 # # simulate mean response of all_values periods
349358 mean_input_immediate <- mean(input_immediate [window_loc ])
350359 mean_input_carryover <- mean(input_carryover_rw )
360+ mean_response <- mean(saturated_total $ x_saturated [window_loc ] * coeff )
351361 mean_response_total <- fx_objective(
352362 x = mean_input_immediate ,
353363 coeff = coeff ,
@@ -392,6 +402,7 @@ transform_decomp <- function(all_values, adstock, theta, shape, scale, alpha, ga
392402 mean_input_immediate = mean_input_immediate ,
393403 mean_input_carryover = mean_input_carryover ,
394404 mean_response_total = mean_response_total ,
405+ mean_response = mean_response ,
395406 mean_response_carryover = mean_response_carryover ,
396407 sim_mean_spend = sim_mean_spend ,
397408 sim_mean_carryover = sim_mean_carryover ,
0 commit comments