Patrick Glettig
Patrick Glettig

Reputation: 601

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

I am trying to run a joint significance test in 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"))

But I always end up with this error:

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

What I am trying to test is whether

am + am:qsec + am:hp = 0

I have found in the documentation how to test for all interaction terms:

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

But I want to test interaction terms and level terms together. Is this possible?

Upvotes: 3

Views: 4400

Answers (1)

Julius Vainora
Julius Vainora

Reputation: 48211

Simply notice that

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

has qsec:am rather than am:qsec. Then

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

does work, but this kind of ordering isn't something obvious. For instance,

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

Upvotes: 5

Related Questions