mjskay / tidybayes

Bayesian analysis + tidy data + geoms (R package)
http://mjskay.github.io/tidybayes
GNU General Public License v3.0
718 stars 59 forks source link

stat_intervalh and multimodal distributions? #206

Closed fkgruber closed 4 years ago

fkgruber commented 4 years ago

Hi I was trying use the stat_intervalh to get credible intervals of a multimodal posterior distribution but the interval are continuous. Is there any way to make it show split intervals? For example: `

generate some data

n = 1000 bb = rbinom(n, 1, 0.3) val = rnorm(n, ifelse(bb == 1, 0.3, 1.3),0.1) val2 = rnorm(n, ifelse(bb == 1, 0.8, 2),0.1)

tidybayes::hdi(val) tidybayes::hdi(val2)

plot

toplot=rbind( data.frame(Experiment = "X1", Out = val), data.frame(Experiment = "X2", Out = val2) )

toplot %>% ggplot(aes(y = Experiment, x = Out)) + geom_halfeyeh(show.legend = F, position = position_nudge(y = 0.1), .width = c( 0.8, 0.95), trim = TRUE) + stat_intervalh(.width = c(0.8, 0.95)) `

I get: image

The 80% HDI is clearly multimodal: tidybayes::hdi(filter(toplot, Experiment == "X1")$Out, 0.8)

` [,1] [,2] [1,] 0.1761321 0.4239348 [2,] 1.0772329 1.5245651

`

mjskay commented 4 years ago

Yeah, the default summary function is median_qi (median and quantile interval). The point_interval argument to the geoms/stats in tidybayes can be passed any function in the point_interval family to change this. So values like median_hdi, mean_hdi, or mode_hdi depending on what you want the point summary to be.

fkgruber commented 4 years ago

Perfect thanks

I tried toplot %>% ggplot(aes(y = Experiment, x = Out)) + geom_halfeyeh(show.legend = F, position = position_nudge(y = 0.1), .width = c( 0.8, 0.95), trim = TRUE) + stat_intervalh(.width = c(0.8, 0.95), point_interval=median_hdi)

I got image

thanks FKG