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 linearmodelsVerified import paths — ran on the pinned version, not inferred.
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.
Review models created with formulas to ensure variable order is as expected. Explicitly define variable order if sensitive.
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.
Update `cov_type='cluster'` to `cov_type='clustered'` when specifying clustered standard errors.
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.
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.
Install the library using pip: `pip install linearmodels`.
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'])`.
Use the `MultiIndex.set_names()` method to rename levels. For example: `df.index = df.index.set_names(['entity_id', 'time_id'])`.
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)`.