Overview
gretaR is a Bayesian statistical modelling package
for R built on the torch backend. It provides an intuitive,
R-native syntax for defining probabilistic models, which are then
compiled to differentiable torch computations and sampled using
Hamiltonian Monte Carlo (HMC) or the No-U-Turn Sampler (NUTS).
Key features:
- Pure R – no Python or reticulate dependency
- Native torch (libtorch) backend – CPU today, GPU/MPS on the roadmap
- Familiar R syntax: standard operators, functions, and assignment
- Output compatible with
posterior,bayesplot, andloo
Installation
# Install from GitHub (development version)
# remotes::install_github("max578/gretaR")
# Ensure torch is installed
torch::install_torch()Example 1: Estimating a Normal Mean
Suppose we observe data from a normal distribution and want to estimate the mean and standard deviation.
library(gretaR)
# Simulate data
set.seed(42)
y_obs <- rnorm(100, mean = 3, sd = 1.5)
# Define priors
mu <- normal(0, 10)
sigma <- half_cauchy(5)
# Wrap observed data
y <- as_data(y_obs)
# Define likelihood
distribution(y) <- normal(mu, sigma)
# Compile model
m <- model(mu, sigma)
print(m)
# Draw posterior samples
fit1 <- mcmc(m, n_samples = 500, warmup = 500, chains = 2)
# Summarise
summary(fit1)
plot(fit1, type = "trace")The two chains mix over the same range of mu and
sigma from the first sampled draw, with no visible trend or
one chain sitting apart from the other – consistent with the R-hat close
to 1 and the effective sample size reported by
summary(fit1).
Example 2: Bayesian Linear Regression
# Simulate data
set.seed(123)
n <- 100
x_obs <- rnorm(n)
y_obs <- 2 + 3 * x_obs + rnorm(n, 0, 0.5)
# Reset model state for new model
reset_gretaR_env()
# Priors
alpha <- normal(0, 10)
beta <- normal(0, 10)
sigma <- half_cauchy(2)
# Data
x <- as_data(x_obs)
y <- as_data(y_obs)
# Linear predictor
mu <- alpha + beta * x
# Likelihood
distribution(y) <- normal(mu, sigma)
# Compile
m <- model(alpha, beta, sigma)
print(m)
plot(fit2, type = "density")The three densities are unimodal, chain-to-chain agreement is close,
and the posterior mass for alpha, beta and
sigma brackets the values used to simulate
y_obs, confirming the model recovers the generating
parameters on this synthetic dataset.
Available Distributions
| Function | Distribution | Support |
|---|---|---|
normal(mean, sd) |
Normal | |
half_normal(sd) |
Half-Normal | |
half_cauchy(scale) |
Half-Cauchy | |
student_t(df, mu, sigma) |
Student-t | |
uniform(lower, upper) |
Uniform | |
exponential(rate) |
Exponential | |
gamma_dist(shape, rate) |
Gamma | |
beta_dist(alpha, beta) |
Beta | |
bernoulli(prob) |
Bernoulli | |
binomial_dist(size, prob) |
Binomial | |
poisson_dist(rate) |
Poisson | |
multivariate_normal(mean, cov) |
Multivariate Normal |
Choosing a Sampler
- NUTS (default): Adaptive, no tuning of leapfrog steps needed. Recommended for most models.
- HMC: Fixed number of leapfrog steps. Useful for debugging or when NUTS tree depth is a concern.
Next Steps
- Explore hierarchical models in
vignette("hierarchical-models", package = "gretaR") - Use
bayesplot::mcmc_trace()andbayesplot::mcmc_dens_overlay()for diagnostics - Compare models with
loo::loo()on the posterior draws
