本文介绍了如何使用聚类协方差矩阵对回归系数进行线性假设检验?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对在R中进行线性回归后计算系数的线性组合的估计值和标准误差感兴趣.例如,假设我有回归并检验:

I am interested in calculating estimates and standard errors for linear combinations of coefficients after a linear regression in R. For example, suppose I have the regression and test:

data(mtcars)
library(multcomp)
lm1 <- lm(mpg ~ cyl + hp, data = mtcars)
summary(glht(lm1, linfct = 'cyl + hp = 0'))

这将估计cylhp上的系数之和的值,并基于lm产生的协方差矩阵提供标准误差.

This will estimate the value of the sum of the coefficients on cyl and hp, and provide the standard error based on the covariance matrix produced by lm.

但是,假设我想将我的标准错误集中在第三个变量上:

But, suppose I want to cluster my standard errors, on a third variable:

data(mtcars)
library(multcomp)
library(lmtest)
library(multiwayvcov)
lm1 <- lm(mpg ~ cyl + hp, data = mtcars)
vcv <- cluster.vcov(lm1, cluster = mtcars$am)
ct1 <- coeftest(lm1,vcov. = vcv)

ct1包含我通过am进行聚类的SE.但是,如果我尝试在glht中使用ct1对象,则会显示一条错误消息

ct1 contains the SEs for my clustering by am. However, if I try to use the ct1 object in glht, you get an error saying

关于如何使用聚类方差协方差矩阵进行线性假设的任何建议?

Any advice on how to do the linear hypothesis with the clustered variance covariance matrix?

谢谢!

推荐答案

glht(ct1, linfct = 'cyl + hp = 0')将不起作用,因为ct1不是glht对象,并且不能通过as.glht强制这样做.我不知道是否有一个软件包或一个现有的函数来执行此操作,但是要自己完成这项工作并不困难.下面的小功能可以做到这一点:

glht(ct1, linfct = 'cyl + hp = 0') won't work, because ct1 is not a glht object and can not be coerced to such via as.glht. I don't know whether there is a package or an existing function to do this, but this is not a difficult job to work out ourselves. The following small function does it:

LinearCombTest <- function (lmObject, vars, .vcov = NULL) {
  ## if `.vcov` missing, use the one returned by `lm`
  if (is.null(.vcov)) .vcov <- vcov(lmObject)
  ## estimated coefficients
  beta <- coef(lmObject)
  ## sum of `vars`
  sumvars <- sum(beta[vars])
  ## get standard errors for sum of `vars`
  se <- sum(.vcov[vars, vars]) ^ 0.5
  ## perform t-test on `sumvars`
  tscore <- sumvars / se
  pvalue <- 2 * pt(abs(tscore), lmObject$df.residual, lower.tail = FALSE)
  ## return a matrix
  matrix(c(sumvars, se, tscore, pvalue), nrow = 1L,
         dimnames = list(paste0(paste0(vars, collapse = " + "), " = 0"),
                         c("Estimate", "Std. Error", "t value", "Pr(>|t|)")))
  }

让我们进行测试:

data(mtcars)
lm1 <- lm(mpg ~ cyl + hp, data = mtcars)
library(multiwayvcov)
vcv <- cluster.vcov(lm1, cluster = mtcars$am)

如果在LinearCombTest中未指定.vcov,则与multcomp::glht相同:

If we leave .vcov unspecified in LinearCombTest, it is as same as multcomp::glht:

LinearCombTest(lm1, c("cyl","hp"))

#              Estimate Std. Error   t value     Pr(>|t|)
#cyl + hp = 0 -2.283815  0.5634632 -4.053175 0.0003462092

library(multcomp)
summary(glht(lm1, linfct = 'cyl + hp = 0'))

#Linear Hypotheses:
#              Estimate Std. Error t value Pr(>|t|)
#cyl + hp == 0  -2.2838     0.5635  -4.053 0.000346 ***

如果我们提供协方差,它将满足您的要求:

If we provide a covariance, it does what you want:

LinearCombTest(lm1, c("cyl","hp"), vcv)

#              Estimate Std. Error  t value    Pr(>|t|)
#cyl + hp = 0 -2.283815  0.7594086 -3.00736 0.005399071


备注

LinearCombTest在,在这里我们可以测试组合系数为alpha的任何组合:

LinearCombTest is upgraded at Get p-value for group mean difference without refitting linear model with a new reference level, where we can test any combination with combination coefficients alpha:

alpha[1] * vars[1] + alpha[2] * vars[2] + ... + alpha[k] * vars[k]

不仅仅是总和

vars[1] + vars[2] + ... + vars[k]

这篇关于如何使用聚类协方差矩阵对回归系数进行线性假设检验?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-10 00:08