Skip to main content

stmmlib

Spatio-temporal mixed models for areal panel data, with Gaussian and Beta families behind one API.

Overview

stmmlib fits area-level panel models that combine spatial random effects (IID, SAR, nested, SEBLUP Rao-Yu), temporal random effects (IID, AR(1)), and optional area-time interactions. Two likelihood families are available, so the same model structure can be fitted to continuous responses or to proportions.

Developed as a reference implementation for a study of Indonesian district poverty data. It is a general tool for area-level panel data, does not depend on that dataset, and is published as a software reference.

Features

  • 14 model structures (M1 to M14) plus a Beta regression baseline
  • Gaussian family: ML and REML by marginal likelihood
  • Beta family: Laplace-approximated ML with convergence certificates (gradient norm, Hessian positive definiteness, iteration count)
  • Prediction: EBLUP for Gaussian, EBP from posterior draws for Beta, both in-sample and for same-area holdout
  • Configurable column names, so data does not need renaming
  • Fixed-effect inference in both families: Wald tests and restricted-refit likelihood-ratio tests (inference="lrt", lrt_terms=[...])
  • Optional GPU backend through CuPy or PyTorch

Installation

pip install stmmlib

For the optional GPU backend:

pip install "stmmlib[gpu]"

Install from source instead:

git clone https://github.com/ghiffahry/STMMLib.git
cd STMMLib
pip install -e .

Run the test suite:

pip install -e ".[dev]"
pytest

Quick start

Runs end to end on synthetic data, nothing external needed:

import numpy as np
import pandas as pd
from stmmlib.stmm import stmm

m, T = 12, 6
rng = np.random.default_rng(7)
area = np.repeat(np.arange(m), T)
time = np.tile(np.arange(T), m)

df = pd.DataFrame({
    "y": np.clip(rng.beta(6, 4, m * T), 1e-4, 1 - 1e-4),   # in (0, 1), as required
    "area": area,
    "time": time,
    "x1": rng.normal(size=m * T),
    "x2": rng.normal(size=m * T),
})

fit = stmm(data=df, model="m1_iid_iid", family="beta",
           method="Laplace", gpu="no", n_starts=1)
print(fit.summary())             # reads the certificate, refuses to call an invalid fit inference-grade
print(fit.certificate["valid"])  # True/False: may the SEs be quoted?

Gaussian family, same call surface:

df_gauss = df.assign(y=df["x1"] + rng.normal(0, 1, len(df)))
W = np.ones((m, m)); np.fill_diagonal(W, 0); W /= W.sum(1, keepdims=True)

fit = stmm(data=df_gauss, model="m3_sar_iid", family="gaussian",
           W=W, method="REML", area_col="area", time_col="time", gpu="no")
fit["beta_hat"]; fit["se_beta"]; fit["blup"]["v_hat"]

Fixed-effect inference in both families, Wald or likelihood-ratio test, and the requirement that the LRT compares ML likelihoods, are documented in docs/API.md.

Models

ID Area effect Time effect Interaction
m1_iid_iid IID IID none
m2_iid_ar1 IID AR(1) none
m3_sar_iid SAR IID none
m4_sar_ar1 SAR AR(1) none
m5_seblup_rao_yu_it SEBLUP Rao-Yu AR(1) none
m6_nested_iid_iid nested IID IID none
m7_nested_ar1 nested SAR AR(1) none
m8_iid_iid_int IID IID IID
m9_sar_iid_int SAR IID spatial
m10_iid_ar1_int IID AR(1) temporal
m11_sar_ar1_int SAR AR(1) spatio-temporal
m12_seblup_rao_yu_it_int SEBLUP Rao-Yu AR(1) spatio-temporal
m13_nested_iid_iid_int nested IID IID temporal
m14_nested_ar1_int nested SAR AR(1) IID
beta_regression none none none

Models ending in _int add an interaction term between area and time effects. Models with SAR or SEBLUP area effects require W.

Estimation and prediction

  • Gaussian: method="ML" or method="REML". The fit is returned as a dict with beta_hat, se_beta, log_lik, AIC, BIC, and blup (v_hat, u_hat, se_v).
  • Beta: method="Laplace". The fit is an object with summary(), certificate, beta_original, se_original, ebp(), and ebp_newdata().
  • Fixed effects in the Beta family are standardised internally. Use beta_original and se_original for reporting, not the standardised values.
  • ebp_newdata() predicts for held-out periods of areas seen during fitting. Areas never seen during fitting are rejected.
  • Accuracy presets: fast, balanced, medium, full.

Requirements

  • Python 3.10 or newer
  • NumPy 1.24+, SciPy 1.11+, pandas 2.0+
  • CuPy and PyTorch only for the gpu extra

Documentation

docs/API.md holds a compact API reference covering fitting, diagnostics, LRT, and prediction intervals.

Limitations

  • EBP here means E[mu | y], computed from posterior draws. It is not the Molina-Rao EBP, and its mean squared error is not implemented.
  • Spatial block cross-validation supports fixed effects only.
  • The Gaussian path is CPU only. Run it with gpu="no".
  • Gaussian fits return a dict, Beta fits return an object. The two return types are not unified yet.
  • Nested models with interactions are dense and slow. Raise max_iter for them.
  • simulate_beta supports the flat models M1-M5 and the interaction models M8-M12. Nested models M6/M7/M13/M14 raise ValueError.
  • beta_criteria (cAIC, conditional R2) returns NaN for nested models; their province/district/cell precision builder is not implemented.
  • When the automatic prior refit is used, the reported logLik, AIC, and BIC are the penalized objective. The fit's optim record (fit._opt["optim"]) flags this with penalty_auto and reports the unpenalized value at the same estimate in penalty_refit_msg.

Citation

@software{ghiffahry_stmmlib,
  author  = {Ghiffary, Ghardapaty},
  title   = {STMMLib: spatio-temporal mixed models for areal panel data},
  year    = {2026},
  version = {0.2.0},
  url     = {https://github.com/ghiffahry/STMMLib},
  license = {MIT}
}

License

MIT. See LICENSE.

Metadata

Release files for stmmlib 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stmmlib 0.2.0
File Size Uploaded Provenance Actions
stmmlib-0.2.0.tar.gz 173.2 kB Without provenance Details for stmmlib-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for stmmlib 0.2.0
File Interpreter ABI Platform Actions
stmmlib-0.2.0-py3-none-any.whl Python 3 none any Details for stmmlib-0.2.0-py3-none-any.whl

Total release size: 340.9 kB

Release files / stmmlib-0.2.0.tar.gz

Download URL stmmlib-0.2.0.tar.gz
Size 173.2 kB
Tags Source
SHA-256 checksum
How to use checksums
50254e95666177459d081731774e6ca9636373310e8c22fe7efe2b9afe786864
BLAKE2b-256 checksum
How to use checksums
ddca3912a94c849e74fdce86f132af4de03365ef888123c2193d77a72b271b1d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / stmmlib-0.2.0-py3-none-any.whl

Download URL stmmlib-0.2.0-py3-none-any.whl
Size 167.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ee849ef1db7aa066bde7bdae65b5f09c464cfea633d0add5c8fcf5d9858acc8a
BLAKE2b-256 checksum
How to use checksums
1357fff0d46e56a13303e0b5d3d046eefb4a312150b5478b9e8be97d7a6922cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for stmmlib 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page