gpt4 book ai didi

r - 具有异方差校正的标准误差的回归

转载 作者:行者123 更新时间:2023-12-03 09:06:26 27 4
gpt4 key购买 nike

我想找到最类似于Stata输出的R实现,以最小二乘回归函数与异方差校正的标准误差拟合。具体来说,我希望校正后的标准误差在“摘要”中,而不必为我的第一轮假设检验做额外的计算。我正在寻找一种像Eviews和Stata一样“干净”的解决方案。

到目前为止,使用“lmtest”软件包,我能想到的最好的方法是:

model <- lm(...)
coeftest(model, vcov = hccm)

这给了我想要的输出,但是它似乎并没有将“coeftest”用于其声明的目的。我还必须使用带有不正确标准错误的摘要来读取R ^ 2和F stat等。我认为,鉴于动态R的大小,应该针对该问题存在“一行”解决方案。

谢谢

最佳答案

我认为您在lmtest软件包中使用coeftest处于正确的轨道。看一看sandwich package,它包含此功能,旨在与您已经找到的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

要获得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

如果您需要单线,您总是可以制作一个简单的函数来将这两者结合起来...

Econometric Computing with HC and HAC Covariance Matrix Estimators小插图中有很多示例,它们附带了将lmtest和三明治链接到所需的三明治程序包。

编辑:单线可能很简单:
mySummary <- function(model, VCOV) {
print(coeftest(model, vcov. = VCOV))
print(waldtest(model, vcov = VCOV))
}

我们可以这样使用(在上面的示例中):
> 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

关于r - 具有异方差校正的标准误差的回归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4385436/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com