Metadata-Version: 2.4
Name: mixmetapy
Version: 0.1.1
Summary: Multivariate meta-analysis and meta-regression in Python, a port of the R package mixmeta
Author: Craig Parker
License: GPL-3.0-or-later
Project-URL: Homepage, https://github.com/Logic06183/mixmetapy
Project-URL: R package, https://github.com/gasparrini/mixmeta
Keywords: meta-analysis,multivariate meta-analysis,meta-regression,mixmeta,BLUP,epidemiology
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.22
Requires-Dist: scipy>=1.8
Provides-Extra: dev
Requires-Dist: pytest>=7; extra == "dev"
Requires-Dist: ruff==0.16.5; extra == "dev"
Dynamic: license-file

# mixmetapy

[![PyPI](https://img.shields.io/pypi/v/mixmetapy.svg)](https://pypi.org/project/mixmetapy/)
![python](https://img.shields.io/badge/python-3.9%20%7C%203.11%20%7C%203.12%20%7C%203.13-blue)
![licence](https://img.shields.io/badge/licence-GPL--3.0--or--later-green)

Multivariate meta-analysis and meta-regression in Python. A port of the R package [`mixmeta`](https://github.com/gasparrini/mixmeta) by Antonio Gasparrini, checked against it number for number.

Not affiliated with the authors of `mixmeta`. The method is theirs; this makes it usable from Python.

## What it does

Pools multi-parameter estimates across studies when each study reports a vector of correlated estimates rather than one number: spline coefficients of a non-linear exposure-response, several outcomes measured on the same people, a dose-response curve. The model, for a single random level, is

```
y_i ~ N(X_i beta, S_i + Psi)
```

with `y_i` the k estimates from study i, `S_i` their within-study covariance, `X_i` optional study-level predictors, and `Psi` the between-study covariance.

- REML (default), ML or fixed effects
- unstructured, diagonal or identity `Psi`
- meta-regression on study-level predictors
- BLUPs of the study-specific estimates, with prediction intervals
- Cochran's Q and I², overall and per outcome
- predictions at new predictor values, AIC and BIC

It takes plain arrays and nothing else. It does not need to know where the estimates came from.

## Installation

```bash
pip install mixmetapy
```

Needs numpy and scipy.

## Use

```python
import numpy as np
from mixmetapy import mixmeta

# y: (m, k) estimates from m studies; S: (m, k, k) within-study covariances
fit = mixmeta(y, S, method="reml")
fit.coef, fit.vcov, fit.Psi          # pooled estimates and between-study covariance
print(fit.summary())

fit.qtest()                          # Q, df, p-value, I2
fit.blup(se=True)                    # study-specific BLUPs and covariances

# meta-regression on a study-level variable x
mr = mixmeta(y, S, X=np.column_stack([np.ones(len(y)), x]))
mr.predict(np.column_stack([np.ones(3), [10, 15, 20]]), se=True)
```

`S` can also be given as a list of k x k matrices, as `(m, k(k+1)/2)` rows of the lower triangle (R's `vechMat` order), or as `(m, k)` variances when the estimates are uncorrelated.

| R (`mixmeta`) | Python |
|---|---|
| `mixmeta(y ~ 1, S, method="reml")` | `mixmeta(y, S, method="reml")` |
| `mixmeta(y ~ x, S)` | `mixmeta(y, S, X=np.column_stack([np.ones(m), x]))` |
| `mixmeta(..., bscov="diag")` | `mixmeta(..., bscov="diag")` |
| `coef(mm)`, `vcov(mm)`, `mm$Psi` | `mm.coef_vec`, `mm.vcov`, `mm.Psi` |
| `blup(mm, se=TRUE, pi=TRUE, vcov=TRUE)` | `mm.blup(se=True)` |
| `predict(mm, newdata, se=TRUE)` | `mm.predict(Xnew, se=True)` |
| `qtest(mm)`, `summary(mm)$i2stat` | `mm.qtest()` |
| `vechMat`, `xpndMat` | `vech`, `xpnd` |

Formulas are not supported: build `X` yourself, with a column of ones for the intercept.

## Does it give the same answer as R?

Close, and the residual gap is R stopping early rather than a difference in the model.

On the 12-study, 4-outcome validation fixture: coefficients, covariances and `Psi` agree to 1e-5 or better (1e-10 for fixed effects), Q and I² to 1e-9, BLUPs to 1e-4, predictions to 1e-6. Fixed-effects fits, which need no optimiser, agree to machine precision.

Those are the figures for one fixture, and they are not a guarantee for every dataset. On randomly generated designs (varying dimension, studies, and covariance structure) the coefficients stay within about 4e-6, but `Psi` and the BLUPs drift to a few times 1e-5. The reason is that REML and ML likelihoods for these models are often very flat: in every such case checked, the Python fit reached an **equal or higher** log-likelihood than R, so it is R's optimiser stopping first, not a disagreement about the answer. Treat the last digit of `Psi` and the BLUPs as optimiser-dependent in either language; the pooled coefficients and their covariance, which is what most analyses report, are far more stable.

The meta-analysis stages of two published papers by Gasparrini and colleagues are reproduced as well. Detail in [docs/validation.md](docs/validation.md).

## Where it came from

This was the meta-analysis module of [dlnmpy](https://github.com/Logic06183/dlnmpy), the Python port of the R package `dlnm`, until 0.7.0. It was split out in September 2026 so the Python packages follow the R ones: `dlnm` and `mixmeta` are separate on CRAN, and multivariate meta-analysis has users who never fit a distributed lag model. For two-stage DLNM designs, `pip install dlnmpy[twostage]` installs both.

Not yet ported from `mixmeta`: multilevel (nested) random effects, the method of moments and variance components estimators, and missing outcomes within a study (these raise an error; drop the study or the outcome).

## How to cite

Cite the methods papers, and the software as:

> Parker C. mixmetapy: multivariate meta-analysis in Python (version 0.1.1). 2026. https://github.com/Logic06183/mixmetapy

- Gasparrini A, Armstrong B, Kenward MG. Multivariate meta-analysis for non-linear and other multi-parameter associations. *Statistics in Medicine* 2012; 31(29):3821-3839. doi:10.1002/sim.5471
- Sera F, Armstrong B, Blangiardo M, Gasparrini A. An extended mixed-effects framework for meta-analysis. *Statistics in Medicine* 2019; 38(29):5429-5444. doi:10.1002/sim.8362

## Licence

GPL-3.0-or-later, as `mixmeta` (GPL >= 3). It is a derived work of the R source, so it takes the same licence.
