pysemforge
Structural Equation Modeling for Python — closing the R-Python gap for behavioral and econometric modeling.
A Python-native SEM/CFA package inspired by lavaan (syntax) and semTools (multigroup + measurement invariance), with a graphviz-free path diagram backend.
Why pysemforge?
Python has mature estimation backends (e.g. semopy), but still lacks a single package that combines:
| Capability | R (lavaan + semTools) | Python (semopy / pySEM) | pysemforge |
|---|---|---|---|
| lavaan-style syntax | ✅ | partial | ✅ |
| CFA / path / mediation | ✅ | partial | ✅ |
| Multigroup analysis | ✅ native | ❌ not native | ✅ native |
| Measurement invariance (configural → metric → scalar) | ✅ semTools |
❌ missing | ✅ one-liner |
| Partial invariance | ✅ semTools |
❌ | ✅ |
| Ordinal / WLSMV | ✅ | limited | ✅ |
| Robust ML (MLR) + fit-index CIs | ✅ | partial | ✅ |
| Path diagrams (no graphviz, publication-ready) | lavaanPlot |
⚠️ needs graphviz | ✅ matplotlib backend |
The two highlighted rows are the real gaps this package targets — so you stay in Python end-to-end.
Quick Start
import pandas as pd
from pysemforge import Model
# 1) lavaan-compatible syntax
syntax = """
visual =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed =~ x7 + x8 + x9
"""
model = Model(syntax)
result = model.fit(data) # a pandas DataFrame of your indicators
print(result.summary())
# 2) Path diagram (no graphviz needed)
model.plot("sem_path.png", standardized=True)
# 3) Multigroup + measurement invariance (the headline feature)
inv = model.measurement_invariance(data, group_col="gender")
print(inv.summary())
inv.plot("invariance.png")
Install
pip install pysemforge
For the latest development version:
git clone https://github.com/your-username/pysemforge.git
cd pysemforge
pip install -e .
Features
- lavaan-compatible syntax —
=~,~,~~,~1,==, comments, group blocks - CFA / structural regression / mediation as first-class models
- Estimators — ML, MLR (robust SE), WLSMV (polychoric for ordinal data)
- Native multigroup analysis with equality constraints
- Measurement invariance — fully automated
configural → metric → scalarladder with Δχ² / ΔCFI / ΔRMSEA decisions (RsemToolsparity) - Partial invariance — build-up / tear-down of non-invariant parameters
- Fit indices — χ², CFI, TLI, RMSEA (+90% CI), SRMR
- graphviz-free diagrams — publication-quality path plots via matplotlib (no system dependencies)
- pandas / NumPy / SciPy first-class interoperability
Measurement Invariance Example
inv = model.measurement_invariance(data, group_col="group")
# Access each level's fit
inv.levels["configural"]
inv.levels["metric"]
inv.levels["scalar"]
# Pairwise comparisons (Δχ², ΔCFI, decision)
inv.comparisons
The returned object mirrors semTools::measurementInvariance() output, so R users feel at home.
Documentation & Publishing
- Source: https://github.com/your-username/pysemforge
- Docs: https://pysemforge.readthedocs.io
- Issues: https://github.com/your-username/pysemforge/issues
Publish to PyPI (main release target):
pip install build twine
python -m build
twine check dist/*
twine upload --repository testpypi dist/* # test first
twine upload dist/* # then production
Roadmap (post-MVP)
Latent growth curve models · Bayesian estimation · complex survey designs · full lavaanPlot-parity plotting.
License
MIT — see LICENSE.
Metadata
Release files for pysemforge 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pysemforge-0.1.0.tar.gz | 96.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pysemforge-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 126.2 kB
Release files / pysemforge-0.1.0.tar.gz
| Download URL | pysemforge-0.1.0.tar.gz |
|---|---|
| Size | 96.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
91fb71e95b01db55162a2e5a282c548dae3a58d4c27f09561ea8cabfa6fe2e51
|
|
BLAKE2b-256 checksum How to use checksums |
18106a6710a86a8f57cebdf8f48972134edc4074e1d5669fa55635ad5a93c6de
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.8
|
Release files / pysemforge-0.1.0-py3-none-any.whl
| Download URL | pysemforge-0.1.0-py3-none-any.whl |
|---|---|
| Size | 30.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
da5f269935aec636c6b30b8355fb9e5c5d0e180c972b3627c023b4f37679f5f4
|
|
BLAKE2b-256 checksum How to use checksums |
10f4970bd4c273a50cefbda9803316aa89550df119c209b8a16ca608e3aa85da
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.8
|