Skip to contents

Samples the posterior of a GLM with nuts-rs, the Rust core of nutpie. The design is R's model.matrix(), as for glm_fit(). Coefficients have normal priors with mean 0: standard deviation intercept_sd for the intercept and prior_sd for the rest (on the link scale; standardize covariates). For the Gaussian, gamma and inverse Gaussian the dispersion is sampled with a half-normal prior of scale dispersion_scale, unless dispersion fixes it. Chains run in parallel, start near the maximum-likelihood fit, and replay exactly from seed. Posterior means and standard deviations match exact grid integration (validation/scripts/bayes_glm_grid.py).

Usage

bayes_glm_fit(
  formula,
  data,
  family = "poisson",
  link = NULL,
  offset = NULL,
  weights = NULL,
  prior_sd = 2.5,
  intercept_sd = 10,
  dispersion = NULL,
  dispersion_scale = 10,
  chains = 4,
  tune = 1000,
  draws = 1000,
  seed = 0,
  target_accept = 0.8,
  max_depth = 10,
  theta = NULL,
  power = NULL,
  link_power = NULL
)

Arguments

formula

A model formula; offset(...) terms are honoured.

data

A data frame.

family

"gaussian", "poisson", "gamma", "inverse_gaussian", "binomial" (response a proportion, weights the trials), "negative_binomial" (needs theta) or "tweedie" (needs power).

"identity", "log", "logit", "probit", "cloglog", "inverse", "inverse_squared" or "power" (needs link_power); NULL for the log link for the gamma, inverse Gaussian and Tweedie and the canonical link otherwise. R's own glm() defaults to the canonical link (Gamma() is the inverse link), so give link when porting a model from it.

offset

Optional offset added to any offset() terms.

weights

Optional prior weights.

prior_sd, intercept_sd

Prior standard deviations of the slopes and the intercept.

dispersion

A fixed dispersion, or NULL for the family's default.

dispersion_scale

Scale of the half-normal prior on a sampled dispersion.

chains, tune, draws

Chains, warm-up draws and kept draws per chain.

seed

Seed, a whole number.

target_accept

Target acceptance rate for step-size adaptation.

max_depth

Largest tree depth.

theta

Negative binomial theta (variance mu + mu^2 / theta).

power

Tweedie power in (1, 2).

Exponent of the power link.

Value

A bayes_glm_model with properties coefficients (posterior means), summary (a data frame of mean, sd, quantiles, R-hat and ESS per parameter), draws (a matrix, one row per draw), dispersion_draws and divergences. Use stats::coef(), stats::predict(), predict_distribution() and bayes_loo().

Examples

d <- data.frame(claims = rep(c(1, 2, 3, 5), 10), x = rep(c(-1.5, -0.5, 0.5, 1.5), 10))
m <- bayes_glm_fit(claims ~ x, d, family = "poisson", chains = 2, tune = 300, draws = 300)
m@summary
#>     parameter      mean        sd       q05       q50       q95     rhat
#> 1 (Intercept) 0.8480391 0.1091623 0.6753386 0.8519540 1.0276557 1.008628
#> 2           x 0.5079711 0.1001039 0.3503982 0.5108863 0.6782719 1.004548
#>   ess_bulk ess_tail
#> 1 362.3648 460.7138
#> 2 470.7439 367.1854