Registry / data / linearmodels

linearmodels

JSON →
library7.0pypypi✓ verified 85d ago

linearmodels is a Python library that extends `statsmodels` with advanced econometric models, including Panel data models (Fixed Effects, Random Effects), Instrumental Variable (IV) estimators (2SLS, GMM), Factor Asset Pricing models, and System Regression models (SUR, 3SLS). It is currently at version 7.0 and sees active development with several major/minor releases per year.

pip install linearmodels
INSTALL
IMPORT
SIG · LINEARMODELS
L
linearmodels
datapythonv7.0
Install
15.4s avg
Import
4164ms
Disk
375MB
Pass rate
9/ 10
Env Coverage9 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v7.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✓ —
✓ 14.68s
py 3.11
✓ —
✓ 14.23s
py 3.12
✓ —
✓ 14.68s
py 3.13
✓ —
✓ 14.83s
py 3.9
✕ build_error
✓ 18.75s
375MB installed
● package 375MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

PanelOLS
from linearmodels import PanelOLS
IV2SLS
from linearmodels.iv import IV2SLS
SUR
from linearmodels.system import SUR
PooledOLS
from linearmodels.panel import PooledOLS
RandomEffects
from linearmodels.panel import RandomEffects

This quickstart demonstrates how to load panel data, structure it for `linearmodels` using a pandas MultiIndex, and fit a basic PanelOLS model with entity fixed effects and clustered standard errors.

import numpy as np import pandas as pd from linearmodels.datasets import grunfeld from linearmodels.panel import PanelOLS data = grunfeld.load_pandas().data data.year = data.year.astype(np.int64) # Create a MultiIndex (entity - time) for panel data data = data.set_index(['firm', 'year']) # Define dependent and independent variables dep = data.invest exog = data[['value', 'capital']] # Initialize and fit the PanelOLS model with entity effects mod = PanelOLS(dep, exog, entity_effects=True) res = mod.fit(cov_type='clustered', cluster_entity=True) print(res)
Debug
Known issues
breakingStarting with version 5.0, the variable ordering in formulas processed by `from_formula` is preserved as it appears in the formula, rather than being sorted alphabetically. This can lead to changes in coefficient ordering or model interpretation if relying on the previous sorting behavior.
fix
Review models created with formulas to ensure variable order is as expected. Explicitly define variable order if sensitive.
affects: 5.0.0 and later (including 7.0)
breakingVersion 5.0 and 7.0 significantly increased the minimum required versions for Python, NumPy, SciPy, pandas, statsmodels, and formulaic. Older environments may not be compatible.
fix
Upgrade your environment to meet the new minimum requirements: Python >=3.10, NumPy >=1.22, SciPy >=1.8, pandas >=1.4, statsmodels >=0.13, formulaic >=1.0.
affects: 5.0.0, 7.0.0 and later
gotchaThe name for clustered covariance was corrected from 'cluster' to 'clustered' in version 7.0. Using the old name will likely result in an error.
fix
Update `cov_type='cluster'` to `cov_type='clustered'` when specifying clustered standard errors.
affects: 7.0.0 and later
gotchaThe library transitioned to `formulaic` as the preferred formula parser. While `patsy` might have been implicitly used or supported in older versions, `formulaic` is now the standard for R-style formula parsing.
fix
Ensure `formulaic` is installed and that formulas conform to its expected syntax. Be aware that `linearmodels.future.ordering` is a no-op in 5.0+ and has no effect on variable ordering.
affects: 4.27.0 and later
gotchaEstimating models with rank-deficient regressors can lead to unreliable estimates. `linearmodels` includes a rank check by default, but it can be skipped using `rank_check=False`.
fix
Always ensure your regressor matrix is full rank, especially when using `rank_check=False`. Carefully inspect model output and diagnostics for signs of multicollinearity or rank deficiency.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'linearmodels'
The `linearmodels` library has not been installed in the Python environment being used.
fix
Install the library using pip: `pip install linearmodels`.
ValueError: DataFrame input must have a MultiIndex with 2 levels.
When using `linearmodels.panel` estimators like `PanelOLS`, the input DataFrame must have a `pandas.MultiIndex` with exactly two levels, typically representing the entity (individual) and time dimensions. This error occurs if the DataFrame index is not set correctly or is not a two-level MultiIndex.
fix
Ensure your DataFrame has a MultiIndex with two levels, setting the entity and time identifiers as the index. For example: `df = df.set_index(['entity_id', 'time_id'])`.
Cannot set name on a level of a MultiIndex. Use 'MultiIndex.set_names' instead.
This error arises when attempting to directly assign names to levels of a MultiIndex (e.g., `df.index.levels[0].name = 'entity'`) which is an immutable operation.
fix
Use the `MultiIndex.set_names()` method to rename levels. For example: `df.index = df.index.set_names(['entity_id', 'time_id'])`.
AbsorbingEffectError: The model cannot be estimated. The included effects have fully absorbed one or more of the variables.
This error occurs in panel models, especially with fixed effects, when one or more explanatory variables are perfectly collinear with the fixed effects or other variables, meaning they are constant within an entity or over time when an effect is present. This leads to perfect multicollinearity, making it impossible to estimate the coefficient for the absorbed variable.
fix
Identify and remove the absorbed (perfectly collinear) variables from your exogenous regressors. The error message often suggests setting `drop_absorbed=True` in the model's `fit()` method to automatically handle this, e.g., `model.fit(drop_absorbed=True)`.
Upgrade
Version history
7.0latest on PyPI · released Oct 21, 2025
Audit
Dependencies
pythonrequiredRequired runtime environment
numpyrequiredNumerical operations
scipyrequiredScientific computing
pandasrequiredData manipulation and structures
statsmodelsrequiredCore statistical models and formulas
formulaicrequiredR-style formula parsing
pyhdfeoptionalHigh-dimensional fixed effects
mypy-extensionsoptionalType checking extensions
Agent activity
5 hits · last 30 days
node
4
Amazon
1
Resources
linearmodels — pip install linearmodels · libregistry