gretaR supports generalised linear models (GLMs) through two interfaces:
gretaR_glm): quick
specification for standard modelsFor maximum flexibility, use the gretaR DSL directly.
library(gretaR)
set.seed(123)
n <- 100
x_obs <- rnorm(n)
y_obs <- 2 + 3 * x_obs + rnorm(n, 0, 0.5)
reset_gretaR_env()
alpha <- normal(0, 10)
beta <- normal(0, 10)
sigma <- half_cauchy(2)
x <- as_data(x_obs)
y <- as_data(y_obs)
mu <- alpha + beta * x
distribution(y) <- normal(mu, sigma)
m <- model(alpha, beta, sigma)
print(m)gretaR offers five inference methods:
| Method | Function | Speed | Accuracy | Best for |
|---|---|---|---|---|
| NUTS | mcmc(sampler="nuts") |
Slow | Asymptotically exact | Final analysis |
| HMC | mcmc(sampler="hmc") |
Slow | Asymptotically exact | Debugging |
| ADVI | variational() |
Fast | Approximate | Quick checks, large data |
| MAP | opt() |
Very fast | Point estimate | Model verification |
| Laplace | laplace() |
Very fast | Gaussian approximation | Quick uncertainty around a mode |
# MAP (seconds)
map_fit <- opt(m)
coef(map_fit)
# Laplace approximation around the MAP (seconds)
laplace_fit <- laplace(m)
# Variational inference (seconds)
vi_fit <- variational(m, method = "meanfield")
# NUTS (seconds on this small model)
mcmc_fit <- mcmc(m, n_samples = 500, warmup = 500, chains = 2)NUTS gives the reference posterior here: MAP and the Laplace approximation report a point estimate and a local Gaussian curvature around it, and ADVI reports a factorised approximate posterior, so the three faster methods are used to sanity-check a model quickly before committing to the full NUTS run.