bashtage/linearmodels
bashtage/linearmodels has 1,080 GitHub stars and 216 forks. GitHot measured 0 stars in 24 hours, +2 in 7 days, and +2 in 15 days. Last checked 2026-11-24 05:46 UTC. Last push 2026-11-11.
实测星标增长
- 24小时
- 0
- 7天
- +2
- 15天
- +2
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
关于
提供 statsmodels 缺少的其他线性模型,包括工具变量模型和面板数据模型。
开发工具README.md
原文Linear Models
| Metric | |
|---|---|
| Latest Release | |
| Continuous Integration | |
| Coverage | |
| Code Quality | |
| Citation |
Linear (regression) models for Python. Extends statsmodels with Panel regression, instrumental variable estimators, system estimators and models for estimating asset prices:
Panel models:
- Fixed effects (maximum two-way)
- Random effects
- First difference regression
- Between estimator for panel data
- Pooled regression for panel data
- Fama-MacBeth estimation of panel models
- Specification tests: the Hausman test of random effects against fixed effects and the Pesaran CD test of cross-sectional dependence
High-dimensional Regression:
- Absorbing Least Squares
Instrumental Variable estimators
- Two-stage Least Squares
- Limited Information Maximum Likelihood
- k-class Estimators
- Generalized Method of Moments, also with continuously updating
- Jackknife IV (JIVE)
- Specification tests: the Cragg-Donald and Kleibergen-Paap rank tests of the first stage (underidentification)
Factor Asset Pricing Models:
- 2- and 3-step estimation
- Time-series estimation
- GMM estimation
System Regression:
- Seemingly Unrelated Regression (SUR/SURE)
- Three-Stage Least Squares (3SLS)
- Generalized Method of Moments (GMM) System Estimation
Designed to work equally well with NumPy, Pandas or xarray data.
Panel models
Like statsmodels to include, supports formulas for specifying models. For example, the classic Grunfeld regression can be specified
import numpy as np
from statsmodels.datasets import grunfeld
data = grunfeld.load_pandas().data
data.year = data.year.astype(np.int64)
# MultiIndex, entity - time
data = data.set_index(['firm','year'])
from linearmodels import PanelOLS
mod = PanelOLS(data.invest, data[['value','capital']], entity_effects=True)
res = mod.fit(cov_type='clustered', cluster_entity=True)
Models can also be specified using the formula interface.
from linearmodels import PanelOLS
mod = PanelOLS.from_formula('invest ~ value + capital + EntityEffects', data)
res = mod.fit(cov_type='clustered', cluster_entity=True)
The formula interface for PanelOLS supports the special values
EntityEffects and TimeEffects which add entity (fixed) and time
effects, respectively.
Formula support comes from the formulaic package which is a replacement for patsy.
Instrumental Variable Models
IV regression models can be similarly specified.
import numpy as np
from linearmodels.iv import IV2SLS
from linearmodels.datasets import mroz
data = mroz.load()
mod = IV2SLS.from_formula('np.log(wage) ~ 1 + exper + exper ** 2 + [educ ~ motheduc + fatheduc]', data)
The expressions in the [ ] indicate endogenous regressors (before ~)
and the instruments.
Installation
The latest release can be installed using pip
pip install linearmodels
Binary wheels are available for Windows, macOS and Linux. Without a wheel, pip builds linearmodels from source, which requires a C compiler.
The main branch can be installed by cloning the repo and building it with pip. The build uses meson-python, and pip installs the other build requirements (Cython, NumPy and setuptools_scm) automatically.
git clone https://github.com/bashtage/linearmodels
cd linearmodels
pip install .
To work on linearmodels, install the requirements and then build an editable install
python -m pip install -r requirements.txt -r requirements-dev.txt
python -m pip install -e . --no-build-isolation
Running the tests
The tests are installed with linearmodels. They need pytest 9 and xarray, and pytest-xdist to run in parallel.
python -c "import linearmodels; linearmodels.test(['--skip-slow', '-n', 'auto'])"
--skip-slow skips the slow tests and --skip-examples the example notebooks.
pytest loads every plugin that is installed in the environment, and the test
configuration of linearmodels is strict, so that, for example, a marker that is
not registered is an error. A plugin that uses such a marker makes tests fail
with a message such as 'gpu' not found in markers configuration option. To run
the tests without the other plugins, turn off their automatic loading and load
pytest-xdist explicitly. This works with python -m pytest too.
PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 python -c "import linearmodels; linearmodels.test(['-p', 'xdist', '-n', 'auto'])"
When the tests run in several processes with pytest-xdist, limit the BLAS libraries to one thread in each process. Otherwise every process starts as many threads as there are cores, and excessive context switching makes the tests run very slow. This problem is particularly acute on machines with high CPU counts.
export MKL_NUM_THREADS=1
export OPENBLAS_NUM_THREADS=1
export OMP_NUM_THREADS=1
or, in PowerShell,
$env:MKL_NUM_THREADS = 1
$env:OPENBLAS_NUM_THREADS = 1
$env:OMP_NUM_THREADS = 1
Documentation
Stable Documentation is built by GitHub Actions when a release is published. Development Documentation is built by the same workflow on every push to main.
Plan and status
- Linear Instrumental variable estimation - complete
- Linear Panel model estimation - complete
- Fama-MacBeth regression - complete
- Linear Factor Asset Pricing - complete
- System regression - complete
- Linear IV Panel model estimation - not started
- Dynamic Panel model estimation - not started
Requirements
linearmodels 7.x supports Python 3.10 and NumPy 1.x (NumPy < 2). linearmodels 8.0 will raise the minimum supported versions of Python, NumPy and the other dependencies.
Running
- Python 3.10+
- NumPy (1.23.5+)
- SciPy (1.8+)
- pandas (1.4+)
- statsmodels (0.14.6+)
- formulaic (1.2.1+)
- pyhdfe (0.2.0+)
- mypy_extensions (0.4+)
- packaging
- xarray (0.16+, optional)
Building from source
- A C compiler
- Cython (3.1.0+)
- meson-python
- NumPy (2.0+)
- setuptools_scm (9.2+)
Testing
- pytest (9.0+)
- xarray (0.16+)
- pytest-xdist (optional, to run the tests in parallel)
Documentation
- sphinx
- sphinx-immaterial
- nbsphinx
- nbconvert
- nbformat
- ipython
- jupyter
探索相关项目
加载中…