Skip to main content

Joint REML estimation for linear mixed models with crossed random intercepts using sparse design matrices

Project description

interlace

interlace

Documentation

Pure-Python profiled REML estimation for linear mixed models with crossed random intercepts, validated to match R's lme4::lmer().

Designed as a drop-in replacement for statsmodels.MixedLM in diagnostics pipelines that require crossed grouping factors (e.g. (1|worker) + (1|company)), which statsmodels does not support.

Scope: interlace fits models with random intercepts only — it does not support random slopes, generalised outcomes (GLMM), or nested random effects with / syntax. For those cases, use R's lme4 directly or a Python GLMM library.

Installation

pip install interlace-lme

Requires Python ≥ 3.13.

Quick start

import pandas as pd
from interlace import fit

result = fit(
    formula="score ~ hours_studied + prior_gpa",
    data=df,
    groups=["student_id", "school_id"],   # crossed random intercepts
)

print(result.fe_params)          # fixed-effect coefficients
print(result.variance_components) # per-factor variance components
print(result.scale)              # residual variance σ²

groups accepts a single string (one random intercept) or a list (crossed intercepts). The first entry is the primary grouping factor.

Usage

Fitting

from interlace import fit

result = fit(formula, data, groups, method="REML")

Returns a CrossedLMEResult with the following attributes:

Attribute Description
fe_params Fixed-effect coefficients (Series)
fe_bse Standard errors
fe_pvalues Wald p-values
fe_conf_int 95% confidence intervals
random_effects Dict of BLUPs per grouping factor
variance_components Dict of variance estimates per grouping factor
scale Residual variance σ²
fittedvalues Conditional fitted values (Xβ + Zû)
resid Conditional residuals
llf, aic, bic Log-likelihood and information criteria

Prediction

# In-sample (uses BLUPs)
result.predict()

# New data (unseen group levels shrink to zero)
result.predict(newdata=df_new)

# Fixed effects only
result.predict(newdata=df_new, include_re=False)

Residuals

from interlace import hlm_resid

resid_df = hlm_resid(result, type="conditional")  # or "marginal"
# Returns DataFrame with .resid, .fitted, and original data columns

Leverage

from interlace import leverage

lev = leverage(result)  # array of hat-matrix diagonal values

Influence diagnostics

from interlace import hlm_influence, cooks_distance, mdffits, n_influential, tau_gap

infl = hlm_influence(result, level=1)   # Cook's D, MDFFITS, COVTRACE, COVRATIO, RVC per obs

# Scalar summaries
n = n_influential(result)   # count of high-influence observations
gap = tau_gap(result)        # gap statistic between influential and non-influential groups

Combined augment

from interlace import hlm_augment

aug = hlm_augment(result)
# DataFrame: original data + conditional residuals + influence statistics

Plotting

from interlace import plot_resid, plot_influence, dotplot_diag

plot_resid(resid_df, type="resid_vs_fitted")  # or "qq"
plot_influence(infl_df, measure="cooks_d")
dotplot_diag(infl_df, variable="cooks_d", cutoff="internal")

All plots return plotnine.ggplot objects.

statsmodels compatibility

CrossedLMEResult exposes the same interface as statsmodels.MixedLMResults so it can be used as a drop-in in downstream code that accesses fe_params, resid, scale, fittedvalues, random_effects, predict(), and model.exog / model.groups / model.data.frame.

hlm_resid, hlm_influence, and hlm_augment all accept either a CrossedLMEResult or a statsmodels MixedLMResults object.

Parity with lme4

Results are validated against R's lme4::lmer() to the following tolerances:

Metric Tolerance
Fixed effects abs diff < 1e-4
Variance components rel diff < 5%
BLUP correlation > 0.99
Conditional residual correlation > 0.999

Contributing

Bug reports, documentation fixes, and new features are welcome — see CONTRIBUTING.md for how to get started. To open an issue or ask a question, use the GitHub issue tracker.

Attribution

  • lme4 — the reference implementation for mixed-effects models in R; interlace targets parity with lme4::lmer() and uses its output as the validation benchmark.
  • HLMdiag — the R package whose diagnostics API (hlm_resid, hlm_influence, hlm_augment, dotplot_diag) interlace replicates in Python.

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

interlace_lme-0.2.3.tar.gz (6.8 MB view details)

Uploaded Source

Built Distribution

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

interlace_lme-0.2.3-py3-none-any.whl (36.9 kB view details)

Uploaded Python 3

File details

Details for the file interlace_lme-0.2.3.tar.gz.

File metadata

  • Download URL: interlace_lme-0.2.3.tar.gz
  • Upload date:
  • Size: 6.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for interlace_lme-0.2.3.tar.gz
Algorithm Hash digest
SHA256 f4525199a74b9a672b024bd597e604634d139f0fcc08635d6d19ba542dda130a
MD5 d9958ff363e8485e533f698d6d5c5274
BLAKE2b-256 9c35d57961e9c067d2833b669041bed0f4f2a183d4847186ac47ac41128ce2f6

See more details on using hashes here.

Provenance

The following attestation bundles were made for interlace_lme-0.2.3.tar.gz:

Publisher: publish.yml on heliopais/interlace

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file interlace_lme-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: interlace_lme-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 36.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for interlace_lme-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f9d70d5188899ea2c9d530e93af9f1a31314e32bf7d33eaac67cc5c7e2d5b72a
MD5 0dda184556c6e53ffb65a3daf3fc2f7e
BLAKE2b-256 d9d00ea1776c331a67043ee62df6be1dfc51f4d303ad760e170d10ca968655e1

See more details on using hashes here.

Provenance

The following attestation bundles were made for interlace_lme-0.2.3-py3-none-any.whl:

Publisher: publish.yml on heliopais/interlace

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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