[英]How to fix the Absorbing Effect Error when trying to include fixed effects in the PanelOLS function in Python's linearmodels?
[英]Is there a way to derive the intercept of the firm fixed effect from the Python PanelOLS model?
我正在使用 Python statsmodel package 估計面板數據的固定效應。
首先,分析中使用的數據包括隨着時間的推移與幾家公司觀察到的 X 和 Y。 下面是一些來自實際數據的例子,但最初,有一個大約 5,000 家公司一年數據的平衡面板。
| date | firm | X1 | X2 | X3 | Y |
|:---------- |:----:|:--:|:--:|:--:|--:|
| 2021-01-01 | A | 1 | 4 | 1 | 10|
| 2021-01-02 | A | 2 | 7 | 0 | 21|
| 2021-01-03 | A | 4 | 3 | 1 | 12|
| 2021-01-01 | B | 2 | 1 | 0 | 4 |
| 2021-01-02 | B | 3 | 7 | 1 | 9 |
| 2021-01-03 | B | 7 | 1 | 1 | 4 |
用下面的代碼分析控制公司效果的固定效果model時,結果很好推導出來,沒有任何問題。
mod = PanelOLS.from_formula('Y ~ X1 + X2 + X3 + EntityEffects',
data=df.set_index(['firm', 'date']))
result = mod.fit(cov_type='clustered', cluster_entity=True)
result.summary
[輸出]
但是,問題是截距項的效果並沒有打印在結果值上,所以我想想辦法解決這個問題。
是否可以強制截取項為 output?
從git中不是很清楚,但看起來它存儲在result.estimated_effects
下。 您還應該提到它來自linearmodels
,而不是statsmodels
。
from linearmodels import PanelOLS
import pandas as pd
df = pd.DataFrame({'date':['2021-01-01','2021-01-02','2021-01-03','
2021-01-01','2021-01-02','2021-01-03'],
'firm':['A','A','A','B','B','B'],
'X1':[1,2,4,2,3,7],'X2':[4,7,3,1,7,1],
'X3':[1,0,1,0,1,1],'Y':[10,21,12,4,9,4]})
df['date'] = pd.to_datetime(df['date'])
mod = PanelOLS.from_formula('Y ~ X1 + X2 + X3 + EntityEffects',
data=df.set_index(['firm', 'date']))
result = mod.fit(cov_type='clustered', cluster_entity=True)
estimated_effects
firm date
A 2021-01-01 8.179545
2021-01-02 8.179545
2021-01-03 8.179545
B 2021-01-01 0.258438
2021-01-02 0.258438
2021-01-03 0.258438
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.