Skip to main content

PyMR: Mendelian Randomization in Python

Tests codecov PyPI Documentation

A modern, test-driven Python package for Mendelian Randomization (MR) analysis.

Features

  • Multiple MR methods: IVW, weighted median, MR-Egger, mode-based
  • Bayesian MR: Full posterior inference, Bayes factors, model comparison
  • Sensitivity analyses: Heterogeneity tests, MR-PRESSO, leave-one-out
  • Data harmonization: Automatic allele alignment and strand flipping
  • GWAS integration: Load from Pan-UKB, IEU OpenGWAS, or custom files
  • Visualization: Forest plots, scatter plots, funnel plots, posterior distributions
  • No external dependencies: Pure Python (no PLINK, PyMC, or Stan required)

Installation

pip install pymr

Quick Start

Frequentist MR

from pymr import MR, load_gwas

# Load GWAS summary statistics
exposure = load_gwas("bmi_gwas.tsv.gz")
outcome = load_gwas("diabetes_gwas.tsv.gz")

# Run MR analysis
mr = MR(exposure, outcome)
results = mr.run()

print(results)
#              method      beta        se        OR      pval  nsnp
# 0               IVW  0.924740  0.030497  2.521214  5.83e-202   192
# 1   Weighted Median  1.039266  0.021565  2.827140  0.00e+00    192
# 2          MR-Egger  0.911587  0.075401  2.488269  1.19e-33    192

Bayesian MR

from pymr import BayesianMR

# Run Bayesian MR with full posterior inference
bmr = BayesianMR(harmonized_data, prior_mean=0, prior_sd=1)
bmr.sample(n_samples=10000, n_chains=4, warmup=1000)

# Get posterior summary
summary = bmr.summary()
print(f"Effect: {summary['mean']:.3f} [{summary['ci_lower']:.3f}, {summary['ci_upper']:.3f}]")
print(f"Bayes Factor: {bmr.bayes_factor(null_value=0):.2f}")

# Visualize posterior
bmr.plot_posterior()

See docs/bayesian_mr.md for comprehensive Bayesian MR documentation.

Development

# Clone repository
git clone https://github.com/maxghenis/pymr.git
cd pymr

# Install in development mode
pip install -e ".[dev,docs]"

# Run tests
pytest

# Build documentation
jupyter-book build docs

Citation

If you use PyMR in your research, please cite:

@software{pymr2025,
  author = {Ghenis, Max},
  title = {PyMR: Mendelian Randomization in Python},
  year = {2025},
  url = {https://github.com/maxghenis/pymr}
}

License

MIT License - see LICENSE for details.

Download files

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

Source Distribution

pymr_genetics-0.1.0.tar.gz (13.1 MB view details)

Uploaded Source

Built Distribution

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

pymr_genetics-0.1.0-py3-none-any.whl (41.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pymr_genetics-0.1.0.tar.gz
  • Upload date:
  • Size: 13.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for pymr_genetics-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2f15884e7276bab28c4f078b7fdaa670cdf250d9f3b5d8af6cc1aa8d0c0f6603
MD5 da75fab172a65a799112d17b8a4e33e8
BLAKE2b-256 635cd6667ad07e1c8725e03dec69fb6e95106dc14a4a1287015911fef2b302c5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pymr_genetics-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 41.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for pymr_genetics-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5e942517d438ff6e03d0f38ae66f22c81e33dfb95ca9dfad70403e84dad02906
MD5 616ace88b82c08d65b79fd64118a9780
BLAKE2b-256 da00269b1ec7605cc63be1692d2d9e5c56e4dce82d52c5c48d45dc4ed3714541

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

Supported by

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