具有交互项的联合显着性检验:包含错误的 coefficient/variable 个名称

Joint Significance Test with Interaction Terms: contains bad coefficient/variable names

我正在尝试 运行 R 中的联合显着性检验:

library(car)
data("mtcars")
mylm <- lm(mpg ~ qsec + gear + am + am:qsec + am:hp, data=mtcars)

linearHypothesis(mylm, c("am + am:qsec + am:hp"))

但我总是以这个错误结束:

Error in constants(lhs, cnames_symb) : 
  The hypothesis "am + am:qsec + am:hp" is not well formed: contains bad coefficient/variable names.

我想测试的是

am + am:qsec + am:hp = 0

我在文档中找到了如何测试所有交互项:

linearHypothesis(mylm, matchCoefs(mylm, ":"), verbose=TRUE)

但我想同时测试交互项和级别项。这可能吗?

只需注意

mylm$coefficients
# (Intercept)        qsec        gear          am     qsec:am       am:hp 
# -12.2376256   0.8891289   4.1170265 -19.4050359   1.5298394  -0.0316123 

qsec:am 而不是 am:qsec。那么

linearHypothesis(mylm, c("am + qsec:am + am:hp"))

确实有效,但这种排序并不明显。例如,

lm(mpg ~ am:qsec + am:hp, data = mtcars)$coef
# (Intercept)     am:qsec       am:hp 
#  17.1256930   0.7542508  -0.0456892