linearmodels

7.0 · active · verified Wed Apr 15

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.

Warnings

Install

Imports

Quickstart

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)

view raw JSON →