Python-first access to R's brms with proper parameter names, ArviZ support, and cmdstanr performance
Project description
brmspy
Python-first access to R's brms with proper parameter names, ArviZ support, and cmdstanr performance. The easiest way to run brms models from Python.
This is an early development version of the library, use with caution.
Installation
pip install brmspy
First-time setup (installs brms, cmdstanr, and CmdStan in R):
from brmspy import brms
brms.install_brms() # requires R to be installed already
Prebuilt Runtimes (Optional)
For faster installation (~20-60 seconds vs 20-30 minutes), use prebuilt runtime bundles:
from brmspy import brms
brms.install_brms(use_prebuilt_binaries=True)
Windows RTools
In case you don't have RTools installed, you can use the flag install_rtools = True. This is disabled by default, because the flag runs the full rtools installer and modifies system path. Use with caution!
from brmspy import brms
brms.install_brms(
use_prebuilt_binaries=True,
install_rtools=True # works for both prebuilt and compiled binaries.
)
System Requirements
R >= 4.0
Linux (x86_64):
- glibc >= 2.27 (Ubuntu 18.04+, Debian 10+, RHEL 8+)
- g++ >= 9.0
macOS (Intel & Apple Silicon):
- Xcode Command Line Tools:
xcode-select --install - clang >= 11.0
Windows (x86_64):
- Rtools 4.0+ with MinGW toolchain
- g++ >= 9.0
Download Rtools from: https://cran.r-project.org/bin/windows/Rtools/
Quick Start
from brmspy import brms, prior
import arviz as az
# Load data
epilepsy = brms.get_brms_data("epilepsy")
# Fit model
model = brms.fit(
formula="count ~ zAge + zBase * Trt + (1|patient)",
data=epilepsy,
family="poisson",
priors=[
prior("normal(0, 1)", "b"),
prior("exponential(1)", "sd", group="patient"),
prior("student_t(3, 0, 2.5)", "Intercept")
],
chains=4,
iter=2000
)
# Analyze
az.summary(model.idata)
az.plot_posterior(model.idata)
Key Features
- Proper parameter names: Returns
b_Intercept,b_zAge,sd_patient__Interceptinstead of generic names likeb_dim_0 - arviz integration: Returns
arviz.InferenceDataby default for Python workflow - brms formula syntax: Full support for brms formula interface including random effects
- Dual access: Results include both
.idata(arviz) and.r(brmsfit) attributes - No reimplementation: Delegates all modeling logic to real brms. No Python-side reimplementation, no divergence from native behavior. Opinionated wrappers that rebuild formulas or stancode in Python inevitably drift from brms and accumulate their own bugs.
- Prebuilt Binaries: Fast installation with precompiled runtimes containing cmdstanr and brms (50x faster, 25 seconds on Google Colab)
API Reference
Setup Functions
brms.install_brms()- Install brms, cmdstanr, and CmdStanbrms.get_brms_version()- Get installed brms version
Data Functions
brms.get_brms_data()- Load example datasets from brms
Model Functions
brms.formula()- Define formula with kwargsbrms.fit()- Fit Bayesian regression modelbrms.summary()- Generate summary statistics as DataFramebrms.make_stancode()- Generate Stan code for model
Prediction Functions
brms.posterior_epred()- Expected value predictions (without noise)brms.posterior_predict()- Posterior predictive samples (with noise)brms.posterior_linpred()- Linear predictor valuesbrms.log_lik()- Log-likelihood values
Usage
Basic Model
from brmspy import brms
kidney = brms.get_brms_data("kidney")
model = brms.fit(
formula="time ~ age + disease",
data=kidney,
family="gaussian",
chains=4,
iter=2000
)
With Priors
from brmspy import prior
model = brms.fit(
formula="count ~ zAge + (1|patient)",
data=epilepsy,
family="poisson",
priors=[
prior("normal(0, 0.5)", "b"),
prior("cauchy(0, 1)", "sd")
],
chains=4
)
Model Summary
from brmspy import summary
# Get summary statistics as DataFrame
summary_df = summary(model)
print(summary_df)
Predictions
# Expected value (without noise)
epred = brms.posterior_epred(model, newdata=new_data)
# Posterior predictive (with noise)
ypred = brms.posterior_predict(model, newdata=new_data)
# Linear predictor
linpred = brms.posterior_linpred(model, newdata=new_data)
# Log likelihood
loglik = brms.log_lik(model, newdata=new_data)
Access Both Python and R Objects
model = brms.fit(formula="y ~ x", data=data, chains=4)
# Python workflow with arviz
az.summary(model.idata)
az.plot_trace(model.idata)
# R workflow (if needed)
import rpy2.robjects as ro
ro.r('summary')(model.r)
Sampling Parameters
model = brms.fit(
formula="y ~ x + (1|group)",
data=data,
iter=2000, # Total iterations per chain
warmup=1000, # Warmup iterations
chains=4, # Number of chains
cores=4, # Parallel cores
thin=1, # Thinning
seed=123 # Random seed
)
Requirements
Python: 3.10-3.14
R packages (auto-installed via brms.install_brms()):
- brms >= 2.20.0
- cmdstanr
- posterior
Python dependencies:
- rpy2 >= 3.5.0
- pandas >= 1.3.0
- numpy >= 1.20.0
- arviz (optional, for InferenceData)
Development
git clone https://github.com/kaitumisuuringute-keskus/brmspy.git
cd brmspy
./init-venv.sh
pytest tests/ -v
Architecture
brmspy uses:
- brms::brm() with cmdstanr backend for fitting (ensures proper parameter naming)
- posterior R package for conversion to draws format
- arviz for Python-native analysis and visualization
- rpy2 for Python-R communication
Previous versions used CmdStanPy directly, which resulted in generic parameter names. Current version calls brms directly to preserve brms' parameter renaming logic.
License
Apache License 2.0
Credits
- Original concept: Adam Haber
- Current maintainer: Remi Sebastian Kits
- Built on brms by Paul-Christian Bürkner
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file brmspy-0.1.10.tar.gz.
File metadata
- Download URL: brmspy-0.1.10.tar.gz
- Upload date:
- Size: 72.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
653f3b0fec38ae44f6932484a6d0b138d5abf4997e93a2aeaedd364639fb1a96
|
|
| MD5 |
33805701f98a4fe00e21b027f1d5822f
|
|
| BLAKE2b-256 |
998dd5b8f50b4de75c9252baec74ce1ff3b4526b43fa18e1f030663671ad475d
|
File details
Details for the file brmspy-0.1.10-py3-none-any.whl.
File metadata
- Download URL: brmspy-0.1.10-py3-none-any.whl
- Upload date:
- Size: 64.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9e7de0ee45d7cab8f4f3ec197586d8e7d838e2883401648cbb456cbea9e81a8e
|
|
| MD5 |
0e2a57238a9b343816ed71d573f7de68
|
|
| BLAKE2b-256 |
93bc49a3737ff3f3329208e682b59d9d64d03c63ede3692dacdb128d874b73a9
|