55
66stat_brms_model <- function (mapping = NULL , data = NULL ,
77 fit = NULL , ss = NULL , CI = 0.95 ,
8+ hierarchy_value = NULL ,
89 inherit.aes = TRUE , ... ) {
910 # These would normally be arguments to a stat layer but they should not be changed
1011 geom <- " ribbon"
@@ -29,12 +30,19 @@ stat_brms_model <- function(mapping = NULL, data = NULL,
2930 c(((1 - i ) / 2 ), (i + (1 - i ) / 2 ))
3031 )
3132 })
33+ # get hierarchy value if NULL
34+ if (is.null(hierarchy_value ) && ! is.null(parsed_form $ hierarchical_predictor )) {
35+ hierarchy_value <- mean(fit $ data [[parsed_form $ hierarchical_predictor ]])
36+ }
3237 # make layer for each of the intervals
3338 layers <- lapply(formatted_prob_list , function (prob_pair ) {
3439 lyr <- ggplot2 :: layer(
3540 stat = stat , data = data , mapping = mapping , geom = geom ,
3641 position = position , show.legend = show.legend , inherit.aes = inherit.aes ,
37- params = list (na.rm = na.rm , fit = fit , parsed_form = parsed_form , probs = prob_pair , ... )
42+ params = list (
43+ na.rm = na.rm , fit = fit , parsed_form = parsed_form ,
44+ probs = prob_pair , hierarchy_value = hierarchy_value , ...
45+ )
3846 )
3947 return (lyr )
4048 })
@@ -52,7 +60,7 @@ stat_brms_model <- function(mapping = NULL, data = NULL,
5260
5361statBrmsMod <- ggplot2 :: ggproto(" StatBrm" , Stat ,
5462 # `specify that there will be extra params`
55- extra_params = c(" na.rm" , " fit" , " parsed_form" , " probs" ),
63+ extra_params = c(" na.rm" , " fit" , " parsed_form" , " probs" , " hierarchy_value " ),
5664 # `data setup function`
5765 setup_data = function (data , params ) {
5866 # ' possible that ss is not a pcvrss object for compatibility with other brms models
@@ -122,14 +130,17 @@ statBrmsMod <- ggplot2::ggproto("StatBrm", Stat,
122130 # ' the model and ss objects.
123131 compute_group = function (data , scales ,
124132 fit = NULL , parsed_form = NULL , probs = NULL ,
133+ hierarchy_value = NULL ,
125134 ... ) {
126135 yvar <- parsed_form $ y
127136 xvar <- parsed_form $ x
128137 group <- parsed_form $ group
138+ hierarchy <- parsed_form $ hierarchical_predictor
129139 # make data to use drawing posterior predictions
130140 nd <- data [, c(" x" , " MOD_GROUP" , " PANEL" )]
131141 nd <- nd [! duplicated(nd ), ]
132142 colnames(nd ) <- c(xvar , group , " PANEL" )
143+ nd [[hierarchy %|| % " none" ]] <- hierarchy_value
133144 # make predictions
134145 mod_data <- cbind(nd , predict(fit , newdata = nd , probs = probs ))
135146 # lengthen predictions as in brmPlot
0 commit comments