knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(glmbayes)
Gamma regression models are used when the response variable is strictly positive and right‑skewed.
Typical applications include:
Gamma regression is a standard GLM for positive, right‑skewed responses [@NelderWedderburn1972; @McCullagh1989; @Agresti2015].
In classical statistics, these models are fit using
glm(..., family = Gamma(link = ...)).
In glmbayes, the Bayesian analogue is
glmb(..., family = Gamma(link = ...), pfamily = dNormal(...))
or, when dispersion is unknown and modeled,
glmb(..., family = Gamma(link = ...), pfamily = dNormal_Gamma(...))
or related pfamilies.
This chapter:
Gamma regression models strictly positive, right-skewed responses.
In a Gamma GLM with fixed dispersion, the response satisfies
[ Y_i > 0, \qquad Y_i \sim \text{Gamma}(\text{mean}=\mu_i,\ \text{dispersion}=\phi), ]
where the variance is
[ \mathrm{Var}(Y_i) = \phi\,\mu_i^2. ]
The mean is linked to a linear predictor through
[ \eta_i = x_i^\top \beta, \qquad \mu_i = g^{-1}(\eta_i). ]
In this chapter we focus exclusively on the log link, which is the standard choice for Gamma GLMs and the only link supported by glmb() when dispersion is fixed.
Let (w_i) denote observation weights (e.g., claim counts or exposures).
Under the log link,
[ \mu_i = e^{\eta_i}, ]
and the Gamma log-likelihood (up to constants) is
[ \ell(\beta) = \sum_{i=1}^n w_i\left[ -\frac{1}{\phi}\,\eta_i - \frac{1}{\phi}\,y_i e^{-\eta_i} \right]. ]
Terms not involving (\beta) have been absorbed into the constant.
This form is used by both glm() and the Bayesian functions glmb() and rglmb() when dispersion is fixed.
The Gamma distribution belongs to the exponential family [@McCullagh1989; @Agresti2015] with canonical parameter
[ \theta_i = -\frac{1}{\mu_i}, ]
and cumulant function
[ b(\theta_i) = -\log(-\theta_i). ]
The variance is
[ \mathrm{Var}(Y_i) = \phi\,\mu_i^2. ]
Under the log link, the canonical parameter is a monotone transformation of the linear predictor, and the resulting log-likelihood is log-concave in (\eta_i).
This property is essential for stable envelope construction and accept–reject sampling in glmbayes.
The log link is defined by
[ g(\mu_i) = \log(\mu_i), \qquad \mu_i = e^{\eta_i}. ]
It is the preferred link for Gamma GLMs because:
glmb() when dispersion is fixed.Other links (inverse, identity) do not preserve log-concavity and are therefore not used in this chapter.
Models with estimated dispersion or non-log links are covered in Chapter 15.
With the log link, the weighted log-likelihood is
[ \ell(\beta) = \sum_{i=1}^n w_i\left[ -\frac{1}{\phi}\,\eta_i - \frac{1}{\phi}\,y_i e^{-\eta_i} \right]. ]
A multivariate normal prior is specified as
[ \log p(\beta) = -\tfrac{1}{2}(\beta - \mu_0)^\top \Sigma_0^{-1}(\beta - \mu_0) + \text{const}. ]
Combining likelihood and prior yields the log-posterior:
[ \log p(\beta \mid y) = \sum_{i=1}^n w_i\left[ -\frac{1}{\phi}\,\eta_i - \frac{1}{\phi}\,y_i e^{-\eta_i} \right] - \tfrac{1}{2}(\beta - \mu_0)^\top \Sigma_0^{-1}(\beta - \mu_0) + \text{const}. ]
Because both terms are concave in (\beta), the posterior is log-concave, enabling efficient iid sampling via the envelope-based accept–reject sampler implemented in glmbayes.
The link function relates the mean (\mu) to the linear predictor (\eta = X\beta):
[ g(\mu) = \eta. ]
The Gamma family in base R supports:
| Link | Formula | Notes | |-----------|----------------------|-----------------------------------------| | inverse | (\eta = 1/\mu) | canonical link | | identity | (\eta = \mu) | must ensure (\mu > 0) | | log | (\eta = \log(\mu)) | most common; ensures (\mu > 0) |
In practice, the log link is usually preferred because:
At present (due to the need for log-concavity), only the log link is implemented in the glmb function.
We now consider a Gamma regression example based on the carinsca dataset.
The goal is to model average claim cost as a function of rating variables Merit and Class, using claim counts as weights.
We begin by preparing the data and setting appropriate factor levels and contrasts.
data(carinsca) carinsca$Merit <- ordered(carinsca$Merit) carinsca$Class <- factor(carinsca$Class) oldopt <- options(contrasts = c("contr.treatment", "contr.treatment")) Claims <- carinsca$Claims Insured <- carinsca$Insured Merit <- carinsca$Merit Class <- carinsca$Class Cost <- carinsca$Cost
The response Cost/Claims represents the average claim cost (severity) per claim.
The weights Claims reflect the number of claims on which each average is based, improving efficiency and aligning with standard GLM practice for rate or average models.
We fit a classical Gamma GLM with a log link. As glm() only crudely estimates the dispersion, we use MASS::gamma.dispersion() [@VenablesRipley2002] and pass the estimate to summary().
out <- glm(Cost/Claims ~ Merit + Class, family = Gamma(link = "log"), weights = Claims, x = TRUE) ## Estimate the dispersion using MLE disp <- gamma.dispersion(out) summary(out,dispersion=disp)
The summary shows:
Under the log link, exp((\beta_{j})) can be interpreted as a multiplicative factor on the expected cost ratio for a one‑unit change in the corresponding covariate (or a change in factor level).
To fit the Bayesian analogue, we proceed in two steps:
The gamma.dispersion() helper estimates the dispersion parameter from a fitted classical Gamma GLM:
## better than the crude estimate from the summary function disp <- gamma.dispersion(out) disp
This value will be treated as known in our Bayesian model, so that the Bayesian and classical fits are directly comparable in terms of how they treat phi.
Next we use Prior_Setup() to construct a Zellner‑type g‑prior [@GriffinBrown2010] for the coefficients, aligned with the Gamma log‑link model and weighting structure:
ps <- Prior_Setup(Cost/Claims ~ Merit + Class, family = Gamma(link = "log"), weights = Claims) mu <- ps$mu V <- ps$Sigma
The output from ps (if printed) will include:
With the default pwt = 0.01, the prior is weakly informative, and the posterior estimates typically remain close to the MLEs.
We now call glmb(), specifying the same model and likelihood as in the classical fit, but adding a dNormal prior family that fixes dispersion at the value estimated above:
out_glmb <- glmb(Cost/Claims ~ Merit + Class, family = Gamma(link = "log"), pfamily = dNormal(mu = mu, Sigma = V, dispersion = disp), weights = Claims)
The arguments mirror the glm() call, with pfamily providing the prior:
Bayesian summary:
summary(out_glmb) options(oldopt)
The Bayesian summary will report:
Because dispersion is fixed at the same value in both models, differences between out and out_glmb reflect primarily the influence of the prior on (\beta), not differences in (\phi).
Under the log link, coefficient interpretations are multiplicative:
With a weak prior, posterior means in out_glmb will be close to the MLEs in out, but with slightly more regularization and smaller posterior standard deviations.
By fixing dispersion at disp from the classical model, the Bayesian and classical fits share the same assumed level of variability.
This allows:
If desired, a more advanced model can estimate dispersion by switching to a pfamily such as dNormal_Gamma, but that is covered in more detail in the dispersion‑focused vignettes.
The Bayesian summary of out_glmb reports:
These can be compared to AIC and deviance from the classical model, especially when considering alternative specifications (for example, reduced models, different covariate sets, or alternative link functions).
Gamma regression provides a flexible framework for modeling positive, skewed response variables [@McCullagh1989; @Gelman2013].
In this chapter, we have:
In later vignettes, we extend these ideas to:
[@JohnsonLauEtAl2022] does not include a dedicated chapter on Gamma regression. Chapter 12.7 notes that stan_glm() can use family = Gamma, but the book's worked positive-response examples (e.g. cherry_blossom_sample finishing times) are analyzed with Gaussian models in Chapters 15 and 17.
In this package:
| Topic | Where to read |
|-------|----------------|
| Gamma regression (Gamma family, log link, carinsca) | Main body of this chapter |
| Scalar Gamma–Gamma conjugate rate | Chapter 02-S05 (cherry_blossom_sample with dGamma(Inv_Dispersion = FALSE)) |
| Unknown Gamma dispersion | Chapter 15 (dNormal_Gamma, glmbdisp) |
If you are following Bayes Rules! sequentially, complete Chapter 12 (Poisson) and the Chapter 10, Appendix A example before Gamma regression here; use Chapter 02-S05 when you need conjugate intuition for a Gamma-distributed rate with known shape.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.