5151# ' # "reach 1+" for gamma lower bound and "reach 10+" for gamma upper bound
5252# ' facebook_I_gammas <- c(
5353# ' curve_out[["curve_collect"]][["reach 1+"]][["hill"]][["gamma_best"]],
54- # ' curve_out[["curve_collect"]][["reach 10+"]][["hill"]][["gamma_best"]])
54+ # ' curve_out[["curve_collect"]][["reach 10+"]][["hill"]][["gamma_best"]]
55+ # ' )
5556# ' print(facebook_I_gammas)
5657# ' }
5758# ' @return List. Class: \code{curve_out}. Contains the results of all trials
@@ -94,7 +95,8 @@ robyn_calibrate <- function(
9495 burn_in_rel ,
9596 sim_n ,
9697 hp_interval ,
97- quiet )
98+ quiet
99+ )
98100 }
99101
100102 df_curve_plot <- bind_rows(lapply(curve_collect , function (x ) x $ df_out ))
@@ -108,12 +110,14 @@ robyn_calibrate <- function(
108110 x = " cumulative spend" ,
109111 y = " cumulative reach"
110112 ) +
111- # theme_lares(background = "white")+
112- # scale_alpha_discrete(range = c(1, 0.2))
113- scale_colour_discrete(h = c(120 ,260 ))
113+ # theme_lares(background = "white")+
114+ # scale_alpha_discrete(range = c(1, 0.2))
115+ scale_colour_discrete(h = c(120 , 260 ))
114116
115- return (list (curve_collect = curve_collect ,
116- plot_reach_freq = p_rnf ))
117+ return (list (
118+ curve_collect = curve_collect ,
119+ plot_reach_freq = p_rnf
120+ ))
117121 } else {
118122 curve_collect <- robyn_calibrate_single_dim(
119123 df_curve ,
@@ -127,7 +131,8 @@ robyn_calibrate <- function(
127131 burn_in_rel ,
128132 sim_n ,
129133 hp_interval ,
130- quiet )
134+ quiet
135+ )
131136 return (list (curve_collect = curve_collect ))
132137 }
133138}
@@ -314,7 +319,8 @@ robyn_calibrate_single_dim <- function(
314319
315320 # # get calibration range for hyparameters
316321 p_alpha <- data.frame (alpha = alpha_collect_converged ) %> %
317- ggplot(aes(x = alpha )) + geom_density(fill = " grey99" , color = " grey" )
322+ ggplot(aes(x = alpha )) +
323+ geom_density(fill = " grey99" , color = " grey" )
318324 alpha_den <- .den_interval(p_alpha , hp_interval , best_alpha )
319325
320326 p_gamma <- data.frame (gamma = gamma_collect_converged ) %> % ggplot(aes(x = gamma )) +
@@ -330,11 +336,12 @@ robyn_calibrate_single_dim <- function(
330336 # qt_coef_out <- .qti(x = coef_collect_converged, interval = hp_interval)
331337
332338 # # plotting & prompting
333- # coef_response <- max(response_cum_sot) / max(response_sot_scaled)
339+ # coef_response <- max(response_cum_sot) / max(response_sot_scaled)
334340 df_sot_plot <- data.frame (
335341 spend = spend_cum_sot ,
336342 response = response_cum_sot ,
337- response_pred = best_pred_response )
343+ response_pred = best_pred_response
344+ )
338345 temp_spend <- seq(0 , max(spend_cum_sot ), length.out = sim_n )
339346 temp_sat <- best_coef * saturation_hill(x = total_cum_spend , alpha = best_alpha , gamma = best_gamma , x_marginal = temp_spend )[[" x_saturated" ]]
340347 df_pred_sim_plot <- data.frame (spend = temp_spend , response = temp_sat )
@@ -368,7 +375,7 @@ robyn_calibrate_single_dim <- function(
368375 aes(
369376 x = .data $ sim_spend , y = .data $ sim_saturation ,
370377 color = .data $ sim
371- ), size = 2 , alpha = 0.2
378+ ), linewidth = 2 , alpha = 0.2
372379 ) +
373380 scale_colour_grey() +
374381 geom_point(
@@ -389,14 +396,14 @@ robyn_calibrate_single_dim <- function(
389396 iterations = unlist(mapply(function (x ) seq(x ), max_iters_vec , SIMPLIFY = FALSE )),
390397 trials = as.character(unlist(
391398 mapply(function (x , y ) rep(x , y ),
392- x = 1 : max_trials , y = max_iters_vec
399+ x = 1 : max_trials , y = max_iters_vec
393400 )
394401 ))
395402 )
396403 p_mse <- df_mse %> %
397404 mutate(trials = factor (.data $ trials , levels = seq(max_trials ))) %> %
398405 ggplot(aes(x = .data $ iterations , y = .data $ mse )) +
399- geom_line(size = 0.2 ) +
406+ geom_line(linewidth = 0.2 ) +
400407 facet_grid(.data $ trials ~ . ) +
401408 labs(
402409 title = paste0(
@@ -415,8 +422,10 @@ robyn_calibrate_single_dim <- function(
415422 p_alpha <- p_alpha +
416423 labs(
417424 title = paste0(" Alpha (Hill) density after " , round(burn_in_rel * 100 ), " % burn-in" ),
418- subtitle = paste0(round(hp_interval * 100 ), " % center density: " , round(alpha_den $ interval [1 ], 4 ), " -" , round(alpha_den $ interval [2 ], 4 ),
419- " \n Best alpha: " , round(best_alpha ,4 ))
425+ subtitle = paste0(
426+ round(hp_interval * 100 ), " % center density: " , round(alpha_den $ interval [1 ], 4 ), " -" , round(alpha_den $ interval [2 ], 4 ),
427+ " \n Best alpha: " , round(best_alpha , 4 )
428+ )
420429 ) +
421430 theme_lares(... ) +
422431 scale_y_abbr()
@@ -425,8 +434,10 @@ robyn_calibrate_single_dim <- function(
425434 p_gamma <- p_gamma +
426435 labs(
427436 title = paste0(" Gamma (Hill) density after " , round(burn_in_rel * 100 ), " % burn-in" ),
428- subtitle = paste0(round(hp_interval * 100 ), " % center density: " , round(gamma_den $ interval [1 ], 4 ), " -" , round(gamma_den $ interval [2 ], 4 ),
429- " \n Best gamma: " , round(best_gamma ,4 ))
437+ subtitle = paste0(
438+ round(hp_interval * 100 ), " % center density: " , round(gamma_den $ interval [1 ], 4 ), " -" , round(gamma_den $ interval [2 ], 4 ),
439+ " \n Best gamma: " , round(best_gamma , 4 )
440+ )
430441 ) +
431442 theme_lares(... ) +
432443 scale_y_abbr()
@@ -458,13 +469,15 @@ robyn_calibrate_single_dim <- function(
458469 }
459470
460471 curve_out <- list (
461- hill = list (alpha_range = c(alpha_den $ interval ),
462- alpha_best = best_alpha ,
463- gamma_range = c(gamma_den $ interval ),
464- gamma_best = best_gamma ,
465- coef_range = c(coef_den $ interval ),
466- coef_best = best_coef ,
467- inflexion_max = total_cum_spend ),
472+ hill = list (
473+ alpha_range = c(alpha_den $ interval ),
474+ alpha_best = best_alpha ,
475+ gamma_range = c(gamma_den $ interval ),
476+ gamma_best = best_gamma ,
477+ coef_range = c(coef_den $ interval ),
478+ coef_best = best_coef ,
479+ inflexion_max = total_cum_spend
480+ ),
468481 plot = p_lines / p_mse / (p_alpha + p_gamma ) +
469482 plot_annotation(
470483 theme = theme_lares(background = " white" , ... )
@@ -482,13 +495,15 @@ robyn_calibrate_single_dim <- function(
482495 get_den <- ggplot_build(plot_object )$ data [[1 ]]
483496 # mode_loc <- which.max(get_den$y)
484497 mode_loc <- which.min(abs(get_den $ x - best_val ))
485- mode_wing <- sum(get_den $ y ) * hp_interval / 2
498+ mode_wing <- sum(get_den $ y ) * hp_interval / 2
486499 int_left <- mode_loc - which.min(abs(cumsum(get_den $ y [mode_loc : 1 ]) - mode_wing )) + 1
487500 int_left <- ifelse(is.na(int_left ) | int_left < 1 , 1 , int_left )
488- int_right <- mode_loc + which.min(abs(cumsum(get_den $ y [(mode_loc + 1 ): length(get_den $ y )]) - mode_wing ))
489- int_right <- ifelse(length(int_right ) == 0 , length(get_den $ y ), int_right )
490- return (list (interval = c(get_den $ x [int_left ], get_den $ x [int_right ]),
491- mode = get_den $ x [mode_loc ]))
501+ int_right <- mode_loc + which.min(abs(cumsum(get_den $ y [(mode_loc + 1 ): length(get_den $ y )]) - mode_wing ))
502+ int_right <- ifelse(length(int_right ) == 0 , length(get_den $ y ), int_right )
503+ return (list (
504+ interval = c(get_den $ x [int_left ], get_den $ x [int_right ]),
505+ mode = get_den $ x [mode_loc ]
506+ ))
492507}
493508
494509
0 commit comments