Bayesian statistical modelling in R, powered by torch – no Python required.
gretaR is a probabilistic programming package that lets you define Bayesian models interactively using native R syntax, then compile them to torch tensors for GPU-accelerated inference via HMC, NUTS, ADVI, MAP, and Laplace approximation.
gretaR will be available from CRAN after acceptance. In the meantime,
install binaries from R-Universe (built within ~1 hour of every commit
to main):
options(repos = c(
max578 = "https://max578.r-universe.dev",
CRAN = "https://cloud.r-project.org"
))
install.packages("gretaR")Or build from source with vignettes:
# install.packages("remotes")
remotes::install_github("max578/gretaR", build_vignettes = TRUE)After installing the package, set up the torch backend (one-time download of LibTorch on first use):
torch::install_torch()Browse the bundled vignettes:
browseVignettes("gretaR")Bayesian linear regression in 15 lines:
library(gretaR)
# Observed data
x <- as_data(mtcars$wt)
y <- as_data(mtcars$mpg)
# Priors
alpha <- normal(0, 10)
beta <- normal(0, 10)
sigma <- half_cauchy(5)
# Likelihood
mu <- alpha + beta * x
distribution(y) <- normal(mu, sigma)
# Compile and sample
m <- model(alpha, beta, sigma)
draws <- mcmc(m, n_samples = 1000, chains = 4)
summary(draws)- 18 distributions – Normal, HalfNormal, HalfCauchy, StudentT, Uniform, Bernoulli, Binomial, Poisson, Gamma, Beta, Exponential, MultivariateNormal, Dirichlet, NegativeBinomial, LKJ, LogNormal, Cauchy, Wishart
- 5 inference methods – HMC, NUTS, ADVI (mean-field and full-rank), MAP, Laplace approximation
- Formula interface –
gretaR_glm()for specifying GLMs with standard R formula syntax (Gaussian, Binomial, Poisson families) - Hierarchical models – nested and crossed random effects via the core DSL
- Sparse matrix support – efficient large design matrices via
as_data_sparse()andsparse_matmul() - Native R torch backend – automatic differentiation, GPU acceleration, zero Python dependency
- Ecosystem integration – posterior draws returned as
posterior::draws_arrayfor seamless use with bayesplot, loo, and other tidyverse-compatible tools
| Feature | gretaR | greta | brms | RStan |
|---|---|---|---|---|
| Backend | torch (R) | TensorFlow (Python) | Stan (C++) | Stan (C++) |
| Python dependency | None | Required | None | None |
| Installation | Simple | Fragile (TF/Python) | Moderate | Moderate (C++ toolchain) |
| Syntax style | Interactive R DSL | Interactive R DSL | Formula | Stan language |
| GPU support | Yes (CUDA/MPS) | Yes (CUDA) | No | Limited (OpenCL) |
| Automatic differentiation | torch autograd | TF autograd | Stan autodiff | Stan autodiff |
| HMC/NUTS | Yes | Yes | Yes (via Stan) | Yes |
| Variational inference | ADVI (MF + FR) | No | No | Yes |
| Formula interface | gretaR_glm() |
No | brm() |
No |
| Maintenance status | Active | Maintenance-only | Active | Active |
| Method | Function | Use Case |
|---|---|---|
| Hamiltonian Monte Carlo | hmc() / mcmc(sampler = "hmc") |
Full posterior, simpler models |
| No-U-Turn Sampler | nuts() / mcmc(sampler = "nuts") |
Full posterior, general purpose (default) |
| ADVI (mean-field) | variational(method = "meanfield") |
Fast approximate posterior |
| ADVI (full-rank) | variational(method = "fullrank") |
Approximate posterior with correlations |
| MAP estimation | opt() |
Point estimate (mode of posterior) |
| Laplace approximation | laplace() |
Gaussian approximation around MAP |
- Getting Started – core DSL walkthrough with a complete linear regression example
- Hierarchical Models – random effects, partial pooling, and nested structures
- GLM Models – formula interface for Gaussian, logistic, and Poisson regression
After installation, browse vignettes with:
browseVignettes("gretaR")If you use gretaR in your research, please cite:
citation("gretaR")gretaR is currently in active development. Bug reports and feature requests are welcome via GitHub Issues.
MIT. See LICENSE for details.