Metadata-Version: 2.4
Name: spmixw
Version: 0.2.3
Summary: Bayesian Spatial Panel Data Models with Convex Combinations of Weight Matrices
Author-email: Mustapha Wasseja Mohammed <muswaseja@gmail.com>
Maintainer-email: Mustapha Wasseja Mohammed <muswaseja@gmail.com>
License-Expression: GPL-3.0-or-later
Project-URL: Homepage, https://github.com/Mustapha-Wasseja/spmixw-py
Project-URL: Repository, https://github.com/Mustapha-Wasseja/spmixw-py
Project-URL: Issues, https://github.com/Mustapha-Wasseja/spmixw-py/issues
Project-URL: R Package on CRAN, https://cran.r-project.org/package=spmixW
Project-URL: R Package on GitHub, https://github.com/Mustapha-Wasseja/spmixW
Keywords: spatial econometrics,panel data,bayesian,MCMC,spatial autoregressive,convex combinations
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
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: Topic :: Scientific/Engineering
Classifier: Topic :: Scientific/Engineering :: Mathematics
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.21
Requires-Dist: scipy>=1.7
Requires-Dist: pandas>=1.3
Provides-Extra: full
Requires-Dist: matplotlib>=3.5; extra == "full"
Requires-Dist: arviz>=0.12; extra == "full"
Requires-Dist: libpysal>=4.6; extra == "full"
Requires-Dist: formulaic>=0.6; extra == "full"
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: pytest-cov>=4.0; extra == "dev"
Requires-Dist: black>=23.0; extra == "dev"
Requires-Dist: ruff>=0.1; extra == "dev"
Requires-Dist: matplotlib>=3.5; extra == "dev"
Dynamic: license-file

# spmixw

Bayesian Spatial Panel Data Models with Convex Combinations of Weight Matrices.

Python port of the R package
[spmixW](https://cran.r-project.org/package=spmixW), implementing Bayesian Markov
chain Monte Carlo (MCMC) estimation of spatial panel data models including
Spatial Autoregressive (SAR), Spatial Durbin Model (SDM), Spatial Error
Model (SEM), Spatial Durbin Error Model (SDEM), and Spatial Lag of X (SLX)
specifications with fixed effects.

## Status

**v0.2.1 (current)** — feature-complete:

- Standard panel models: OLS, SAR, SDM, SEM, SDEM, SLX with fixed effects
  and optional heteroscedastic errors
- Convex combinations of multiple weight matrices (Debarsy and LeSage, 2021)
- Bayesian Model Averaging (BMA) over subsets of weight matrices
- Formula interface (`spmodel`), `simulate_panel`, and `compare_models`
- Tidy output via `.tidy()` and `.glance()` methods

## Installation

```bash
pip install spmixw
```

## Quick start

```python
import numpy as np
from spmixw import simulate_panel, spmodel, make_knw

# Generate weight matrix from random coordinates
rng = np.random.default_rng(42)
coords = rng.uniform(size=(80, 2))
W = make_knw(coords, k=5)

# Simulate from a SAR DGP
panel = simulate_panel(
    N=80, T=10, W=W, rho=0.5,
    beta=[1.0, -0.5], seed=42,
)

# Estimate using formula interface
res = spmodel(
    "y ~ x1 + x2",
    data=panel, W=W, model="sar",
    id="region", time="year",
    effects="twoway",
    ndraw=8000, nomit=2000, seed=42,
)

print(res)
print(res.tidy())
print(res.glance())
```

## Models supported

| Model | Function | Spatial parameter | Formula interface |
|---|---|---|---|
| OLS | `ols_panel()` | none | `model="ols"` |
| SAR | `sar_panel()` | ρ (lag) | `model="sar"` |
| SDM | `sdm_panel()` | ρ (lag) | `model="sdm"` |
| SEM | `sem_panel()` | λ (error) | `model="sem"` |
| SDEM | `sdem_panel()` | λ (error) | `model="sdem"` |
| SLX | `slx_panel()` | none | `model="slx"` |

All models support fixed-effects specifications (`"none"`, `"region"`,
`"time"`, `"twoway"`) and optional heteroscedastic errors via the
Geweke (1993) Student-t mixture.

## Model comparison

```python
from spmixw import compare_models

comp = compare_models(
    "y ~ x1 + x2",
    data=panel, W=W,
    id="region", time="year", effects="twoway",
)
print(comp)
#   model  log_marginal  probability
# 0   SLX    -3587.68      0.0000
# 1   SDM    -3503.57      0.9817
# 2  SDEM    -3528.06      0.0183
```

## License

GPL-3.0-or-later

## References

- Debarsy, N. and LeSage, J.P. (2021). "Bayesian model averaging for spatial
  autoregressive models based on convex combinations of different types of
  connectivity matrices." *Journal of Business & Economic Statistics*,
  40(2), 547-558. <doi:10.1080/07350015.2020.1840993>
- LeSage, J.P. and Pace, R.K. (2009). *Introduction to Spatial Econometrics*.
  Taylor & Francis/CRC Press.
- LeSage, J.P. (2020). "Fast MCMC estimation of multiple W-matrix spatial
  regression models and Metropolis-Hastings Monte Carlo log-marginal
  likelihoods." *Journal of Geographical Systems*, 22(1), 47-75.
- Geweke, J. (1993). "Bayesian Treatment of the Independent Student-t
  Linear Model." *Journal of Applied Econometrics*, 8(S1), S19-S40.
