library(tidyverse)
library(brms)
library(posterior)
library(bayesplot)
library(ggdist)
library(patchwork)
color_scheme_set(scheme = "red")
theme_set(theme_ggdist())
<- function(post) {
prior_pred_lines map(.x = seq_len(nrow(post)),
.f = function(ii, post){
<- post$b_Intercept[ii]
p_Intercept <- post$shape[ii]
p_shape tibble(ii = ii,
x = seq(0, 30, length.out = 100),
y = dgamma(x, shape = exp(p_Intercept), rate = 1 / p_shape))
post = post) |>
}, list_rbind()
}
Gamma
Resources
- https://discourse.mc-stan.org/t/output-for-family-in-brms/9379
- https://discourse.mc-stan.org/t/decoding-smooth-fits-in-brms/34473
- https://stats.stackexchange.com/questions/96972/how-to-interpret-parameters-in-glm-with-family-gamma
Gamma distribution
Shape (\(\theta\)) and Scale (\(k\))
\[PDF(x)=\frac{1}{\Gamma(k)~\theta^k} x^{k - 1}~e^{-x/\theta}\]
Shape (\(\alpha = \theta\)) and Rate (\(\beta\))
\[PDF(x) = \frac{\beta^\alpha}{\Gamma(\alpha)} x^{\alpha - 1}~e^{-\beta x}\]
Inverse scale parameter = rate
\[\beta = \frac{1}{\theta} = \frac{1}{\alpha}\]
\[\bar{x} = \mu = \frac{\alpha}{\beta}\]
Data
set.seed(3475934)
<- 4
x_mean <- 12
alpha <- alpha / x_mean) (beta
[1] 3
<- 1e4
n
<- tibble(x = rgamma(n, shape = alpha, rate = beta)) GD