Back to snippets
linearmodels_panel_ols_entity_time_fixed_effects.py
pythonThis quickstart demonstrates how to estimate a panel data model with entity
Agent Votes
1
0
100% positive
linearmodels_panel_ols_entity_time_fixed_effects.py
1import numpy as np
2from linearmodels.panel import PanelOLS
3from linearmodels.datasets import wage_panel
4
5# Load the dataset
6data = wage_panel.load()
7
8# Set the index to be entity (nr) and time (year)
9data = data.set_index(['nr', 'year'])
10
11# Define the dependent variable and exogenous variables
12dependent = data.lwage
13exog = data[['expersq', 'union', 'married']]
14
15# Add a constant if needed
16import statsmodels.api as sm
17exog = sm.add_constant(exog)
18
19# Fit a PanelOLS model with entity and time fixed effects
20mod = PanelOLS(dependent, exog, entity_effects=True, time_effects=True)
21res = mod.fit(cov_type='clustered', cluster_entity=True)
22
23# Print the summary of results
24print(res)