glht(ct1, linfct = 'cyl + hp = 0')
將不起作用,因爲ct1
不是glht
對象,不能通過as.glht
強制轉移。我不知道是否有一個包或一個現有的功能來做到這一點,但這不是一個困難的工作,自己解決。下面的小功能做的:
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)
如果我們把.vcov
中未指定LinearCombTest
,它是作爲同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 ***
如果我們提供一個協變量,它做你想要的:
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
在Get p-value for group mean difference without refitting linear model with a new reference level,在這裏我們可以測試任何組合與組合係數alpha
升級:
alpha[1] * vars[1] + alpha[2] * vars[2] + ... + alpha[k] * vars[k]
而不僅僅是總和
vars[1] + vars[2] + ... + vars[k]
檢查[這個](http://rforpublichealth.blogspot.com/2014/10/easy-clustered-standard-errors-in-r.html)。關於如何將羣集SE納入線性假設檢驗的很多信息。看起來與@Zheyuan提供的基本相同。 – paqmo
'summary(glht(ct1,linfct ='cyl + hp = 0'))' –