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 in-sample, and EBP from posterior draws for Beta in-sample or for held-out periods of areas seen during fitting
  • 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 PyTorch GPU acceleration for the Beta inner solves (gpu="yes")

Installation

pip install stmmlib

This installs NumPy, SciPy and pandas automatically. Nothing else has to be installed by hand.

For the optional GPU backend:

pip install "stmmlib[gpu]"

The gpu extra installs PyTorch; GPU use requires a CUDA-enabled PyTorch build.

Install from source instead:

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

Run the test suite. The tests ship with the source, not with the installed wheel, so this one needs a checkout:

pip install -e ".[dev]"
pytest

Check the installation:

python -c "import stmmlib as stmmlib; print(stmmlib.__version__)"

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(strict=True))  # hides Wald inference when the certificate is not 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 IID 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 IID 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. Nested and interaction models are not supported.
  • Accuracy presets: fast, balanced, medium, full.

Requirements

  • Python 3.10 or newer
  • NumPy 1.24+, SciPy 1.11+, pandas 2.0+
  • PyTorch with CUDA 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.
  • 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.5},
  url     = {https://github.com/ghiffahry/STMMLib},
  license = {MIT}
}

License

MIT. See LICENSE.

Metadata

Release files for stmmlib 0.2.5

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.5
File Size Uploaded Provenance Actions
stmmlib-0.2.5.tar.gz 198.1 kB With provenance Details for stmmlib-0.2.5.tar.gz

Built distribution (wheel)

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

Total release size: 385.0 kB

Release files / stmmlib-0.2.5.tar.gz

Download URL stmmlib-0.2.5.tar.gz
Size 198.1 kB
Tags Source
SHA-256 checksum
How to use checksums
08e8676e338d329ffd9950c5ca33d57087abb3e60f2c78d530e47e2f30e6c017
BLAKE2b-256 checksum
How to use checksums
7003e7a36d7a1695ed46c62969a1dbb84d2eb7cab2e4b963330dc9f78ca44ea8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.

Transparency log

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

Download URL stmmlib-0.2.5-py3-none-any.whl
Size 186.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6a0e7914c3e5510f20f9cd48675522da83565bdf984f5c3342f2a0d4359a9688
BLAKE2b-256 checksum
How to use checksums
d01c120ccfc96a9b8f19749875cc90f49e7852959073db0c64d881c7bebd8deb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.

Transparency log

Security for stmmlib 0.2.5

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for stmmlib-0.2.5.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.

Transparency log

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