我有一个.dta文件的贷款数据在10年的过程中,并希望运行一个Fama-麦克白回归数据,以估计风险溢价的贷款回报。
要快速了解Fama-Macbeth回归是什么,以下是旧的堆叠溢流柱的摘录
Fama Macbeth回归是指对面板数据进行回归的过程(其中有N个不同的个人,每个人对应于多个时期T,例如天、月、年)。所以总共有。注意,如果面板数据不平衡,就可以了。Fama Macbeth回归是首先对每个周期进行交叉回归,即在给定周期t内将N个个体聚在一起,并对t=1、...T进行这一回归。然后,我们得到了每个自变量的时间序列系数。然后利用系数的时间序列进行假设检验。通常我们取平均值作为每个自变量的最终系数。我们用t-stats来检验意义。
可以使用asreg命令在Stata中完成此过程。在将数据声明为面板后在数据上运行它将给我们提供如下信息:
. sort FacilityID yyyymm
. xtset FacilityID yyyymm
Panel variable: FacilityID (unbalanced)
Time variable: yyyymm, 199908 to 200911, but with gaps
Delta: 1 unit
. asreg ExcessRet1 Mom STM, fmb newey(3)
Fama-MacBeth Two-Step procedure (Newey SE) Number of obs = 58608
(Newey-West adj. Std. Err. using lags(3)) Num. time periods = 124
F( 2, 121) = 4.69
Prob > F = 0.0109
avg. R-squared = 0.1284
Adj. R-squared = 0.1245
------------------------------------------------------------------------------
| Newey-FMB
ExcessRet1 | Coefficient std. err. t P>|t| [95% conf. interval]
-------------+----------------------------------------------------------------
Mom | 5.44422 1.793302 3.04 0.003 1.893906 8.994534
STM | .8705018 2.164802 0.40 0.688 -3.415295 5.156298
cons | -.0756198 .1027633 -0.74 0.463 -.2790669 .1278273
------------------------------------------------------------------------------但是,使用来自线性模型包(Documental这里)的FamaMacbeth类在python中运行相同的过程会给出不同的结果。
数据文件是使用pd.read_stata()导入到python中的。将数据声明为面板后,运行回归会得到非常不同的结果:
import linearmodels.panel.model.FamaMacbeth as lm_fm
factors = ["Mom", "STM"]
df = df.set_index(["FacilityID", "yyyymm"])
formula = "ExcessRet1 ~ 1 + " + " + ".join(factors)
reg = lm_fm.from_formula(formula, data=tempdf)
res = reg.fit(cov_type='kernel', kernel="newey-west")
print(res)版画
Parameter Estimates
==============================================================================
Parameter Std. Err. T-stat P-value Lower CI Upper CI
------------------------------------------------------------------------------
Intercept -0.2338 0.1223 -1.9107 0.0561 -0.4736 0.0060
Mom 5.8947 2.0287 2.9057 0.0037 1.9184 9.8709
STM 3.9649 3.1279 1.2676 0.2050 -2.1659 10.096
==============================================================================两个回归的结果与其他解决方案(例如前面提到的旧的堆栈溢出帖子中发布的解决方案)和我在GitHub上发现的回归的实现都有显着性差异,所有这些结果都与线性模型尝试的结果相同。
是什么导致了这一结果的差异?这是程序上的改变吗?如何更改代码才能从Stata实现中获得结果?
发布于 2022-07-27 08:58:46
你可能做错了什么,我不能在我的最后复制。我将创建一些模拟数据,并发布来自我的asreg程序和来自finance_byu库的fama_macbeth的结果。
来自finance_byu库的结果
n_firms = 1.0e2
n_periods = 1.0e2
def firm(fid):
>>> f = np.random.random((int(n_periods),4))
>>> f = pd.DataFrame(f)
>>> f['period'] = f.index
>>> f['firmid'] = fid
>>> return f
>>> df = [firm(i) for i in range(int(n_firms))]
>>> df = pd.concat(df).rename(columns={0:'ret',1:'exmkt',2:'smb',3:'hml'})
>>> df.head()
ret exmkt smb hml period firmid
0 0.607847 0.264077 0.158241 0.025651 0 0
1 0.140113 0.215597 0.262877 0.953297 1 0
2 0.504742 0.531757 0.812430 0.937104 2 0
3 0.709870 0.299985 0.080907 0.624482 3 0
4 0.682049 0.455993 0.230743 0.368847 4 0
result = fama_macbeth(df,'period','ret',['exmkt','smb','hml'],intercept=True)
fm_summary(result)
mean std_error tstat
intercept 0.483657 0.009682 49.956515
exmkt 0.017926 0.009364 1.914239
smb -0.001474 0.010007 -0.147283
hml 0.001873 0.010330 0.181276asreg的结果
/* Save the dataframe as a Stata data file */
df.to_stata("example.dta")
use "example.dta"
tsset firmid period
asreg ret exmkt smb hml, fmb
Fama-MacBeth (1973) Two-Step procedure Number of obs = 10000
Num. time periods = 100
F( 3, 96) = 1.23
Prob > F = 0.3016
avg. R-squared = 0.0293
Adj. R-squared = -0.0010
------------------------------------------------------------------------------
| Fama-MacBeth
ret | Coefficient std. err. t P>|t| [95% conf. interval]
-------------+----------------------------------------------------------------
exmkt | .0179255 .0093643 1.91 0.059 -.0006625 .0365136
smb | -.0014739 .010007 -0.15 0.883 -.0213375 .0183898
hml | .0018726 .0103299 0.18 0.857 -.0186322 .0223773
cons | .483657 .0096816 49.96 0.000 .4644393 .5028748
------------------------------------------------------------------------------在asreg和finance_byu库的输出中,回归系数和误差是一致的。
下面是官方页面,在这里可以找到更多的asreg示例和用法。
https://stackoverflow.com/questions/73100204
复制相似问题