@@ -99,7 +99,7 @@ plot_er.ersim_med_qi <- function(
9999 list (
100100 add_boxplot = FALSE , boxplot_height = 0.15 ,
101101 show_boxplot_y_title = TRUE , var_group = NULL ,
102- n_bins = 4 , qi_width = 0.95
102+ n_bins = 4 , bin_breaks = NULL , qi_width = 0.95
103103 ),
104104 options_orig_data
105105 )
@@ -306,7 +306,6 @@ plot_er.ersim_med_qi <- function(
306306 return (caption )
307307}
308308
309-
310309.plot_er_binary <- function (
311310 gg , x , origdata , show_orig_data , options_orig_data ) {
312311 var_resp <- extract_var_resp(x )
@@ -328,17 +327,22 @@ plot_er.ersim_med_qi <- function(
328327
329328 var_group <- options_orig_data $ var_group
330329 n_bins <- options_orig_data $ n_bins
330+ bin_breaks <- options_orig_data $ bin_breaks
331331 qi_width <- options_orig_data $ qi_width
332332
333333 # Convert the response variable to a numeric index
334334 origdata $ .resp_num <- as.numeric(factor (origdata [[var_resp ]])) - 1
335335
336336 # binned probability ------------------------------------------------------
337- breaks <-
338- stats :: quantile(origdata [[var_exposure ]], probs = seq(0 , 1 , 1 / n_bins ))
337+ if (is.null(bin_breaks )) {
338+ bin_breaks <- stats :: quantile(
339+ origdata [[var_exposure ]],
340+ probs = seq(0 , 1 , 1 / n_bins )
341+ )
342+ }
339343
340344 # Show error when the breaks are not unique
341- if (length(unique(breaks )) != length(breaks )) {
345+ if (length(unique(bin_breaks )) != length(bin_breaks )) {
342346 stop(
343347 " The breaks for the binned probability are not unique, " ,
344348 " possibly due to too few unique values in the exposure variable.\n " ,
@@ -348,20 +352,34 @@ plot_er.ersim_med_qi <- function(
348352
349353 gg <- gg +
350354 ggplot2 :: geom_vline(
351- xintercept = breaks , linetype = " dashed" ,
355+ xintercept = bin_breaks ,
356+ linetype = " dashed" ,
352357 alpha = 0.3
353358 ) +
354359 xgxr :: xgx_stat_ci(
355360 data = origdata ,
356- ggplot2 :: aes(x = .data [[var_exposure ]], y = .data [[" .resp_num" ]]),
357- bins = n_bins , conf_level = qi_width , distribution = " binomial" ,
358- geom = c(" point" ), shape = 0 , size = 4
361+ ggplot2 :: aes(
362+ x = .data [[var_exposure ]],
363+ y = .data [[" .resp_num" ]]
364+ ),
365+ breaks = bin_breaks ,
366+ conf_level = qi_width ,
367+ distribution = " binomial" ,
368+ geom = c(" point" ),
369+ shape = 0 ,
370+ size = 4
359371 ) +
360372 xgxr :: xgx_stat_ci(
361373 data = origdata ,
362- ggplot2 :: aes(x = .data [[var_exposure ]], y = .data [[" .resp_num" ]]),
363- bins = n_bins , conf_level = qi_width , distribution = " binomial" ,
364- geom = c(" errorbar" ), linewidth = 0.5
374+ ggplot2 :: aes(
375+ x = .data [[var_exposure ]],
376+ y = .data [[" .resp_num" ]]
377+ ),
378+ breaks = bin_breaks ,
379+ conf_level = qi_width ,
380+ distribution = " binomial" ,
381+ geom = c(" errorbar" ),
382+ linewidth = 0.5
365383 )
366384
367385
@@ -550,6 +568,8 @@ plot_er.ermod <- function(
550568# ' and colored by this column. Default is `NULL`.
551569# ' @param n_bins Number of bins to use for observed probability
552570# ' summary. Only relevant for binary models. Default is `4`.
571+ # ' @param bin_breaks Manually specify bin breaks for binary models. If specified
572+ # ' this overrides `n_bins`.
553573# ' @param qi_width_obs Confidence level for the observed probability
554574# ' summary. Default is `0.95`.
555575# ' @param show_coef_exp Logical, whether to show the credible interval
@@ -610,11 +630,17 @@ plot_er.ermod <- function(
610630# ' }
611631# '
612632plot_er_gof <- function (
613- x , add_boxplot = ! is.null(var_group ), boxplot_height = 0.15 ,
633+ x , add_boxplot = ! is.null(var_group ),
634+ boxplot_height = 0.15 ,
614635 show_boxplot_y_title = FALSE ,
615- var_group = NULL , n_bins = 4 , qi_width_obs = 0.95 ,
636+ var_group = NULL ,
637+ n_bins = 4 ,
638+ bin_breaks = NULL ,
639+ qi_width_obs = 0.95 ,
616640 show_coef_exp = FALSE ,
617- coef_pos_x = NULL , coef_pos_y = NULL , coef_size = 4 ,
641+ coef_pos_x = NULL ,
642+ coef_pos_y = NULL ,
643+ coef_size = 4 ,
618644 qi_width_coef = 0.95 ,
619645 qi_width_sim = 0.95 ,
620646 show_caption = TRUE ) {
@@ -624,17 +650,23 @@ plot_er_gof <- function(
624650 show_coef_exp = show_coef_exp ,
625651 show_caption = show_caption ,
626652 options_orig_data = list (
627- add_boxplot = add_boxplot , boxplot_height = boxplot_height ,
653+ add_boxplot = add_boxplot ,
654+ boxplot_height = boxplot_height ,
628655 show_boxplot_y_title = show_boxplot_y_title ,
629656 var_group = var_group ,
630- n_bins = n_bins , qi_width = qi_width_obs
657+ n_bins = n_bins ,
658+ bin_breaks = bin_breaks ,
659+ qi_width = qi_width_obs
631660 ),
632661 options_coef_exp = list (
633- qi_width = qi_width_coef , pos_x = coef_pos_x , pos_y = coef_pos_y ,
662+ qi_width = qi_width_coef ,
663+ pos_x = coef_pos_x ,
664+ pos_y = coef_pos_y ,
634665 size = coef_size
635666 ),
636667 options_caption = list (
637- orig_data_summary = TRUE , coef_exp = show_coef_exp
668+ orig_data_summary = TRUE ,
669+ coef_exp = show_coef_exp
638670 ),
639671 qi_width_sim = qi_width_sim
640672 )
0 commit comments