Skip to content

max578/gretaR

Repository files navigation

gretaR

R-CMD-check pkgdown test-coverage Lifecycle: experimental Codecov test coverage

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.

Installation

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")

Quick Start

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)

Feature Highlights

  • 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 interfacegretaR_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() and sparse_matmul()
  • Native R torch backend – automatic differentiation, GPU acceleration, zero Python dependency
  • Ecosystem integration – posterior draws returned as posterior::draws_array for seamless use with bayesplot, loo, and other tidyverse-compatible tools

Comparison with Other Frameworks

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

Inference Methods

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

Vignettes

  • 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")

Citation

If you use gretaR in your research, please cite:

citation("gretaR")

Contributing

gretaR is currently in active development. Bug reports and feature requests are welcome via GitHub Issues.

Licence

MIT. See LICENSE for details.

About

Bayesian statistical modelling with torch for R

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages