Skip to main content

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.

Python 3.10+ License: Apache 2.0 Documentation Coverage Tests

Overview

This is an early development version of the library, use with caution.

brmspy provides Python access to brms (Bayesian Regression Models using Stan) with proper parameter naming and seamless arviz integration. Uses brms with cmdstanr backend. Arviz is required at the moment, numpy-only mode is coming.

Installation

pip install brmspy

First-time setup (installs brms, cmdstanr, and CmdStan in R):

from brmspy import brms
brms.install_brms()

Quick Start

from brmspy import brms
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",
    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__Intercept instead of generic names like b_dim_0
  • arviz integration: Returns arviz.InferenceData by 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

API Reference

Setup Functions

  • brms.install_brms() - Install brms, cmdstanr, and CmdStan
  • brms.get_brms_version() - Get installed brms version

Data Functions

  • brms.get_brms_data() - Load example datasets from brms

Model Functions

  • brms.fit() - Fit Bayesian regression model
  • brms.summary() - Generate summary statistics as DataFrame
  • brms.get_stan_code() - 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 values
  • brms.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

model = brms.fit(
    formula="count ~ zAge + (1|patient)",
    data=epilepsy,
    family="poisson",
    priors=[
        ("normal(0, 0.5)", "b"),
        ("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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

brmspy-0.1.4.tar.gz (27.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

brmspy-0.1.4-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

Details for the file brmspy-0.1.4.tar.gz.

File metadata

  • Download URL: brmspy-0.1.4.tar.gz
  • Upload date:
  • Size: 27.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for brmspy-0.1.4.tar.gz
Algorithm Hash digest
SHA256 f560bd938f22ee26406e3baf069f4058e0dc40b336092d45af5ba7c4d20ac8ac
MD5 ca162e7d1bda1dd94606ba02485ceff7
BLAKE2b-256 4fc303e8b94dd831091c3b4dca501dabeedb9cd90c2b8b339c9141dc7a4b4ea4

See more details on using hashes here.

File details

Details for the file brmspy-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: brmspy-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 18.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for brmspy-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 46a098bbeafd1a49f4c6096786f50cc493aec73ed19230c06ce265b8833e6e45
MD5 5f79ad3a60f2da982a22aa2c4e8574f5
BLAKE2b-256 2c6a5553abe705ddb403405552462a892357cc5c924533466904875843aa8c6c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page