PLM 软件包的异方差鲁棒标准误差
在使用 Stata 后我正在尝试学习 R,我必须说我喜欢它。但现在我遇到了一些麻烦。我即将使用面板数据进行一些多重回归,因此我使用 plm
包。
现在,我希望在 R 中使用 plm
获得与在执行异方差稳健和实体固定回归时使用 lm
函数和 Stata 相同的结果。
假设我有一个面板数据集,其中包含变量 Y
、ENTITY
、TIME
、V1
。
我在 R 中使用此代码得到了与
lm.model<-lm(Y ~ V1 + factor(ENTITY), data=data)
coeftest(lm.model, vcov.=vcovHC(lm.model, type="HC1))
在 Stata 中执行此回归时
xi: reg Y V1 i.ENTITY, robust
相同的标准错误但是当我使用 plm
包执行此回归时,我得到了其他标准错误
plm.model<-plm(Y ~ V1 , index=C("ENTITY","YEAR"), model="within", effect="individual", data=data)
coeftest(plm.model, vcov.=vcovHC(plm.model, type="HC1))
- 我是否错过了设置某些选项?
plm
模型是否使用其他类型的估计?如果是,如何进行?- 我能否以某种方式使用
plm
获得与 Stata 中使用、强大
相同的标准错误
I am trying to learn R after using Stata and I must say that I love it. But now I am having some trouble. I am about to do some multiple regressions with Panel Data so I am using the plm
package.
Now I want to have the same results with plm
in R as when I use the lm
function and Stata when I perform a heteroscedasticity robust and entity fixed regression.
Let's say that I have a panel dataset with the variables Y
, ENTITY
, TIME
, V1
.
I get the same standard errors in R with this code
lm.model<-lm(Y ~ V1 + factor(ENTITY), data=data)
coeftest(lm.model, vcov.=vcovHC(lm.model, type="HC1))
as when I perform this regression in Stata
xi: reg Y V1 i.ENTITY, robust
But when I perform this regression with the plm
package I get other standard errors
plm.model<-plm(Y ~ V1 , index=C("ENTITY","YEAR"), model="within", effect="individual", data=data)
coeftest(plm.model, vcov.=vcovHC(plm.model, type="HC1))
- Have I missed setting some options?
- Does the
plm
model use some other kind of estimation and if so how? - Can I in some way have the same standard errors with
plm
as in Stata with, robust
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,
plm
软件包不会使用与 Stata 完全相同的面板数据小样本校正。然而,在plm
1.5 版(在 CRAN 上)中,您可以选择模拟 Stata 正在执行的操作。这应该会产生与 Stata 中相同的按组标准错误聚类的结果(但正如评论中提到的,如果没有可重现的示例以及您期望的结果,则很难回答问题)。
有关此问题以及 R 和 Stata 稳健 SE 的一些基准的更多讨论,请参阅 R 中的 Fama-MacBeth 和集群鲁棒性(按公司和时间)标准错误。
另请参阅:
By default the
plm
package does not use the exact same small-sample correction for panel data as Stata. However in version 1.5 ofplm
(on CRAN) you have an option that will emulate what Stata is doing.This should yield the same clustered by group standard-errors as in Stata (but as mentioned in the comments, without a reproducible example and what results you expect it's harder to answer the question).
For more discussion on this and some benchmarks of R and Stata robust SEs see Fama-MacBeth and Cluster-Robust (by Firm and Time) Standard Errors in R.
See also:
您的 Stata 代码是否可能与您使用 plm 所做的不同?
plm
具有“单独”效应的“within”选项意味着以下形式的模型:plm
所做的是降低系数的平均值,使 ci 从方程中消失。这样“bar”后缀意味着每个变量都减去了平均值。平均值是随着时间的推移计算的,这就是为什么效果是针对个人的。您还可以有一个固定的时间效应,这对所有个人来说都是常见的,在这种情况下,效应也将随着时间的推移而变化(尽管在这种情况下无关紧要)。
我不确定 STATA 中的“xi”命令有何作用,但我认为它扩展了交互,对吧?然后在我看来,您正在尝试为每个实体使用一个虚拟变量,正如@richardh 所强调的那样。
为了使您的 Stata 和 plm 代码匹配,您必须使用相同的型号。
您有两种选择:(1) 在 stata 中设置数据并使用带有 fe 修饰符的 xtreg 选项,或者 (2) 使用带有池选项的 plm 以及每个实体一个虚拟对象。
将 Stata 与 R 匹配:
将 plm 与 Stata 匹配:
然后将
vcovHC
与修饰符之一结合使用。请务必查看这篇论文,它对“HC”背后的所有机制进行了很好的回顾" 选项及其影响方差协方差矩阵的方式。希望这有帮助。
Is it possible that your Stata code is different from what you are doing with plm?
plm
's "within" option with "individual" effects means a model of the form:What
plm
does is to demean the coefficients so that ci drops from the equation.Such that the "bar" suffix means that each variable had its mean subtracted. The mean is calculated over time and that is why the effect is for the individual. You could also have a fixed time effect that would be common to all individuals in which case the effect would be through time as well (that is irrelevant in this case though).
I am not sure what the "xi" command does in STATA, but i think it expands an interaction right ? Then it seems to me that you are trying to use a dummy variable per ENTITY as was highlighted by @richardh.
For your Stata and plm codes to match you must be using the same model.
You have two options:(1) you xtset your data in stata and use the xtreg option with the fe modifier or (2) you use plm with the pooling option and one dummy per ENTITY.
Matching Stata to R:
Matching plm to Stata:
Then use
vcovHC
with one of the modifiers. Make sure to check this paper that has a nice review of all the mechanics behind the "HC" options and the way they affect the variance covariance matrix.Hope this helps.