我想找到最接近类似于Stata输出的R实现,以便使用具有异方差校正标准误差的最小二乘回归函数.具体来说,我希望更正的标准误差在"摘要"中,而不必为我的第一轮假设检验做额外的计算.我正在寻找一种与Eviews和Stata一样"干净"的解决方案.
到目前为止,使用"lmtest"软件包,我能想到的最好的是:
model <- lm(...)
coeftest(model, vcov = hccm)
Run Code Online (Sandbox Code Playgroud)
这给了我想要的输出,但它似乎没有使用"coeftest"来表达它的目的.我还必须使用不正确的标准错误的摘要来读取R ^ 2和F stat等.我觉得应该存在一个"一线"解决方案来解决动态R的问题.
谢谢
Rei*_*son 39
我认为你coeftest在lmtest包中走在正确的轨道上.看看包含此功能的三明治包装,旨在与您已找到的lmtest包装配合使用.
> # generate linear regression relationship
> # with Homoskedastic variances
> x <- sin(1:100)
> y <- 1 + x + rnorm(100)
> ## model fit and HC3 covariance
> fm <- lm(y ~ x)
> vcovHC(fm)
(Intercept) x
(Intercept) 0.010809366 0.001209603
x 0.001209603 0.018353076
> coeftest(fm, vcov. = vcovHC)
t test of coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.01973 0.10397 9.8081 3.159e-16 ***
x 0.93992 0.13547 6.9381 4.313e-10 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Run Code Online (Sandbox Code Playgroud)
要获得F测试,请查看功能waldtest():
> waldtest(fm, vcov = vcovHC)
Wald test
Model 1: y ~ x
Model 2: y ~ 1
Res.Df Df F Pr(>F)
1 98
2 99 -1 48.137 4.313e-10 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Run Code Online (Sandbox Code Playgroud)
你总是可以做一个简单的功能,如果你想要单行的话,可以将这两个结合起来......
There are lots of examples in the Econometric Computing with HC and HAC Covariance Matrix Estimators vignette that comes with the sandwich package of linking lmtest and sandwich to do what you want.
Edit: A one-liner could be as simple as:
mySummary <- function(model, VCOV) {
print(coeftest(model, vcov. = VCOV))
print(waldtest(model, vcov = VCOV))
}
Run Code Online (Sandbox Code Playgroud)
Which we can use like this (on the examples from above):
> mySummary(fm, vcovHC)
t test of coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.01973 0.10397 9.8081 3.159e-16 ***
x 0.93992 0.13547 6.9381 4.313e-10 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Wald test
Model 1: y ~ x
Model 2: y ~ 1
Res.Df Df F Pr(>F)
1 98
2 99 -1 48.137 4.313e-10 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Run Code Online (Sandbox Code Playgroud)
小智 10
我找到了一个R函数,它可以完全满足您的需求.它为您提供了强大的标准错误,无需进行额外的计算.你summary()在lm.object上运行,如果你设置参数,robust=T它会让你回到类似Stata的异方差性一致的标准错误.
summary(lm.object, robust=T)
Run Code Online (Sandbox Code Playgroud)
你可以找到的功能https://economictheoryblog.com/2016/08/08/robust-standard-errors-in-r/