Skip to main content

Pythonic interface to the brms R package using CmdStanPy

Project description

brmspy

Pythonic interface to R's brms for Bayesian regression modeling

Python 3.8+ License: Apache 2.0

brmspy brings the power of brms (Bayesian Regression Models using Stan) to Python, providing proper parameter names and seamless integration with the Python Bayesian ecosystem.

Quick Start

pip install brmspy
import brmspy

# One-time setup: install brms and CmdStan
brmspy.install_brms()

# Load example data
epilepsy = brmspy.get_brms_data("epilepsy")

# Fit model - returns arviz InferenceData by default
model = brmspy.fit(
    formula="count ~ zAge + zBase * Trt + (1|patient)",
    data=epilepsy,
    family="poisson",
    chains=4,
    iter=2000
)
idata = model.idata

# Analyze with arviz
import arviz as az
az.plot_posterior(idata)
az.summary(idata)

Key Features

  • Proper Parameter Names: Returns b_Intercept, b_zAge, sd_patient__Intercept (not b_dim_0, sd_1_dim_0)
  • Pythonic by Default: Returns arviz.InferenceData for seamless Python integration
  • Flexible: Optional R brmsfit return for full brms functionality
  • Formula Syntax: Use brms' intuitive formula interface
  • Modern Stack: Python 3.8-3.14, brms + cmdstanr backend

Installation

# Install from PyPI
pip install brmspy

# Install with optional dependencies
pip install brmspy[viz]    # includes arviz, matplotlib
pip install brmspy[all]    # includes all optional dependencies

# First-time setup (installs brms and CmdStan in R)
python -c "import brmspy; brmspy.install_brms()"

Note: The package is imported as brmspy but installed as brmspy from PyPI.

Usage

Basic Model

import brmspy
import arviz as az

# Load data
kidney = brmspy.get_brms_data("kidney")

# Fit Gaussian model
idata = brmspy.fit(
    formula="time ~ age + disease",
    data=kidney,
    family="gaussian",
    return_type="idata"
)

# View summary
az.summary(idata)

With Priors

idata = brmspy.fit(
    formula="count ~ zAge + (1|patient)",
    data=epilepsy,
    family="poisson",
    priors=[
        ("normal(0, 0.5)", "b"),
        ("cauchy(0, 1)", "sd")
    ],
    return_type="idata"
)

Return Type Options

# Default: arviz InferenceData (Pythonic)
idata = brmspy.fit(..., return_type="idata")
az.plot_posterior(idata)

# R brmsfit object (for advanced R users)
fit = brmspy.fit(..., return_type="brmsfit")
import rpy2.robjects as ro
ro.globalenv['fit'] = fit
ro.r('summary(fit)')

# Both formats
result = brmspy.fit(..., return_type="both")
az.plot_posterior(result.idata)  # Python
# result.brmsfit for R methods

Sampling Parameters

model = brmspy.fit(
    formula="y ~ x",
    data=data,
    iter=2000,      # Total iterations per chain
    warmup=1000,    # Warmup iterations
    chains=4,       # Number of chains
    cores=4,        # Parallel cores
    seed=123        # Reproducibility
)

Requirements

Python: 3.8+

R Packages (auto-installed):

  • brms ≥ 2.20.0
  • cmdstanr
  • posterior

Python Dependencies:

  • rpy2 ≥ 3.5.0
  • pandas ≥ 1.3.0
  • numpy ≥ 1.20.0
  • arviz (optional, for InferenceData conversion)

Development

# Clone repository
git clone https://github.com/kaitumisuuringute-keskus/brmspy.git
cd brmspy

# Setup environment (requires Python 3.8+)
./init-venv.sh

# Run tests
pytest tests/ -v

# Run tests with coverage
pytest tests/ --cov=brmspy --cov-report=html

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.0.tar.gz (23.2 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.0-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: brmspy-0.1.0.tar.gz
  • Upload date:
  • Size: 23.2 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.0.tar.gz
Algorithm Hash digest
SHA256 fe848f15546aaabcec65ba9469b331a83b50e69709a8958ea8f28f1bff4923d3
MD5 974e3d0a8b6cd9f6c1c61d15a5a4d795
BLAKE2b-256 b3fa440fb60063728e03b10be163cbd19083cabeae7f8116afcc672c539061ba

See more details on using hashes here.

File details

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

File metadata

  • Download URL: brmspy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.7 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b7da32c4ab344fffc6acec04d9b8805b43b7ef372d987d18144abe3dc045a13c
MD5 5d8e362db2ab9b8969a1874c6b44f4dc
BLAKE2b-256 c27f05c609a01845d0ed185ea96ab507f5959768fe5d5c3cf181afbdb8468110

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