Hi, could someone please help me plot this graph for a beta_binomial model with dpar = mu, phi?
I have the dataframe ready, just not sure how to specify xdist, as I cannot find beta_binomial in the package ‘distributional’.
You can use distributional::dist_wrap()
to specify custom distributions. You need the d
/ p
/ q
/ r
prefixed functions that define the beta-binomial distribution.
If you don’t want to write these functions, you can usually find them for most distributions in a package somewhere. For example, {TailRank} defines dbb
, pbb
, qbb
, and rbb
.
To use dist_wrap()
, we specify the suffix of these four functions (“bb”) and the package name (“TailRank”). Note that {TailRank} needs to be installed via BioConductor.
TailRank uses the more typical alpha
/ beta
parameterization of the Beta distribution (it calls them u
and v
) instead of mu
/ phi
, so we also have to translate mu
/ phi
into u
and v
:
library(distributional)
dist_beta_binomial = function(size, mu, phi) {
dist_wrap(
"bb",
N = size, u = mu * phi, v = (1 - mu) * phi,
package = asNamespace("TailRank")
)
}
You could also manually define the d
/ p
/ q
/ r
functions with a common suffix and then use that with dist_wrap()
(then the package
argument is not needed).
I’ll make a fake dataset with size
, mu
, and phi
so we can demo the distribution type:
set.seed(1234)
df = data.frame(
size = 30,
mu = rbeta(100, 90, 110),
phi = rgamma(100,300,10)
)
Then plot:
library(ggplot2)
library(ggdist)
df |>
ggplot() +
stat_slab(
aes(xdist = dist_beta_binomial(size, mu, phi)),
fill = NA, color = "gray65", alpha = 0.1
) +
theme_ggdist()
🤯