# Outer is entity, inner is time
entity = list(map(chr,range(65,91)))
time = list(pd.date_range('1-1-2014',freq='A', periods=4))
index = pd.MultiIndex.from_product([entity, time])
df = pd.DataFrame(np.random.randn(26*4, 2),index=index, columns=['y','x'])
from linearmodels.panel import PanelOLS
mod = PanelOLS(df.y, df.x, entity_effects=True)
res = mod.fit(cov_type='clustered', cluster_entity=True)
print(res)
这将产生以下输出:
PanelOLS Estimation Summary
================================================================================
Dep. Variable: y R-squared: 0.0029
Estimator: PanelOLS R-squared (Between): -0.0109
No. Observations: 104 R-squared (Within): 0.0029
Date: Thu, Jun 29 2017 R-squared (Overall): -0.0007
Time: 23:52:28 Log-likelihood -125.69
Cov. Estimator: Clustered
F-statistic: 0.2256
Entities: 26 P-value 0.6362
Avg Obs: 4.0000 Distribution: F(1,77)
Min Obs: 4.0000
Max Obs: 4.0000 F-statistic (robust): 0.1784
P-value 0.6739
Time periods: 4 Distribution: F(1,77)
Avg Obs: 26.000
Min Obs: 26.000
Max Obs: 26.000
Parameter Estimates
==============================================================================
Parameter Std. Err. T-stat P-value Lower CI Upper CI
------------------------------------------------------------------------------
x 0.0573 0.1356 0.4224 0.6739 -0.2127 0.3273
==============================================================================
F-test for Poolability: 1.0903
P-value: 0.3739
Distribution: F(25,77)
Included effects: Entity
它还有一个类似于statsmodels的公式接口
mod = PanelOLS.from_formula('y ~ x + EntityEffects', df)
>>> from pandas.stats.plm import PanelOLS
>>> df
y x
date id
2012-01-01 1 0.1 0.2
2 0.3 0.5
3 0.4 0.8
4 0.0 0.2
2012-02-01 1 0.2 0.7
2 0.4 0.5
3 0.2 0.3
4 0.1 0.1
2012-03-01 1 0.6 0.9
2 0.7 0.5
3 0.9 0.6
4 0.4 0.5
注意,数据帧必须具有多索引集;panelOLS根据索引确定time和entity效果:
>>> reg = PanelOLS(y=df['y'],x=df[['x']],time_effects=True)
>>> reg
-------------------------Summary of Regression Analysis-------------------------
Formula: Y ~ <x>
Number of Observations: 12
Number of Degrees of Freedom: 4
R-squared: 0.2729
Adj R-squared: 0.0002
Rmse: 0.1588
F-stat (1, 8): 1.0007, p-value: 0.3464
Degrees of Freedom: model 3, resid 8
-----------------------Summary of Estimated Coefficients------------------------
Variable Coef Std Err t-stat p-value CI 2.5% CI 97.5%
--------------------------------------------------------------------------------
x 0.3694 0.2132 1.73 0.1214 -0.0485 0.7872
---------------------------------End of Summary---------------------------------
有一个名为
linearmodels
(https://pypi.org/project/linearmodels/)的包,它具有相当完整的固定效果和随机效果实现,包括集群标准错误。它不使用高维OLS来消除影响,因此可以用于大型数据集。这将产生以下输出:
它还有一个类似于statsmodels的公式接口
如评论中所述,PanelOLS从Pandas 0.20.0版本中删除。所以你真的有三个选择:
如果您使用Python 3,您可以使用最近的答案中指定的
linearmodels
:https://stackoverflow.com/a/44836199/3435183只需在} 。如果固定效果的数量很大,可能不可行。
statsmodels
规范中指定各种虚拟对象,例如使用^{或者做一些基于分组的贬低,然后使用
statsmodels
(如果你估计了很多固定效果,这将起作用)。下面是一个你可以做的单程固定效果的简单版本:如果使用较旧版本的
Pandas
,您可以执行以下操作:使用pandas'
PanelOLS
(在plm模块中)的时间固定效果的示例。注意,PanelOLS
的导入:注意,数据帧必须具有多索引集;
panelOLS
根据索引确定time
和entity
效果:文档字符串:
这是另一个函数(比如
fama_macbeth
),我认为计划是将这个函数移到statsmodels
。相关问题 更多 >
编程相关推荐