vcovHC 和置信区间

发布于 2024-09-25 11:07:30 字数 44 浏览 0 评论 0原文

拟合模型后是否可以使用 vcovHC(来自三明治包)获得的稳健 vcov?

Is it possible to have confint use the robust vcov obtained by vcovHC (from the sandwich package) after fitting a model?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

决绝 2024-10-02 11:07:30

不,您不能直接将函数confint 与强大的vcov 一起使用。但手动完成此操作非常简单。

x <- sin(1:100)
y <- 1 + x + rnorm(100)
## model fit and HC3 covariance
fm <- lm(y ~ x)
Cov <- vcovHC(fm)

tt <-qt(c(0.025,0.975),summary(fm)$df[2])
se <- sqrt(diag(Cov))
ci <-coef(fm) + se %o% tt

否则,您可以根据自己的需要调整 confint.default() 函数:

confint.robust <- function (object, parm, level = 0.95, ...)
{
    cf <- coef(object)
    pnames <- names(cf)
    if (missing(parm))
        parm <- pnames
    else if (is.numeric(parm))
        parm <- pnames[parm]
    a <- (1 - level)/2
    a <- c(a, 1 - a)
    pct <- stats:::format.perc(a, 3)
    fac <- qnorm(a)
    ci <- array(NA, dim = c(length(parm), 2L), dimnames = list(parm,
        pct))
    ses <- sqrt(diag(sandwich::vcovHC(object)))[parm]
    ci[] <- cf[parm] + ses %o% fac
    ci
}

正如布兰登已经建议的那样,如果您在 stats.stackexchange.com 上询问这些问题,您将有更多机会获得快速答案

Nope, you cannot use the function confint directly with the robust vcov. But it's pretty straight-forward to do this by hand.

x <- sin(1:100)
y <- 1 + x + rnorm(100)
## model fit and HC3 covariance
fm <- lm(y ~ x)
Cov <- vcovHC(fm)

tt <-qt(c(0.025,0.975),summary(fm)$df[2])
se <- sqrt(diag(Cov))
ci <-coef(fm) + se %o% tt

Otherwise, your can adapt the confint.default() function to your own needs :

confint.robust <- function (object, parm, level = 0.95, ...)
{
    cf <- coef(object)
    pnames <- names(cf)
    if (missing(parm))
        parm <- pnames
    else if (is.numeric(parm))
        parm <- pnames[parm]
    a <- (1 - level)/2
    a <- c(a, 1 - a)
    pct <- stats:::format.perc(a, 3)
    fac <- qnorm(a)
    ci <- array(NA, dim = c(length(parm), 2L), dimnames = list(parm,
        pct))
    ses <- sqrt(diag(sandwich::vcovHC(object)))[parm]
    ci[] <- cf[parm] + ses %o% fac
    ci
}

As Brandon already suggested, you'd get more chance of a quick answer if you ask these things at stats.stackexchange.com

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文