Testing the equality of multiple coefficients in R

user33125 picture user33125 · Jun 2, 2016 · Viewed 10.7k times · Source

I have the following model:

y = b1_group1*X1 + b1_group2*X1 + b2_group1*X2 + b2_group2*X2 + ... +
    b10_group1*X10 + b10_group2*X10

Easily made in R as follows:

OLS <- lm(Y ~ t1:Group + t2:Group + t3:Group + t4:Group + t5:Group + t6:Group +
          t7:Group + t8:Group + t9:Group + t10:Group,weights = weight, Alldata)

In STATA, I can now do the following test:

test (b1_group1=b1_group2) (b2_group1=b2_group2) (b3_group1=b3_group2)
  • b1_group1 - b1_group2 = 0
  • b2_group1 - b2_group2 = 0
  • b3_group1 - b3_group2 = 0

Which tells me whether the group of coefficents from X1, X2 and X3 are jointly different between Group 1 and Group 2 by means of an F test.

Can somebody please tell how how to do this in R? Thanks!

Answer

coffeinjunky picture coffeinjunky · Jun 2, 2016

Look at this example:

library(car)

mod <- lm(mpg ~ disp + hp + drat*wt, mtcars)
linearHypothesis(mod, c("disp = hp", "disp = drat", "disp = drat:wt" ))
Linear hypothesis test

Hypothesis:
disp - hp = 0
disp - drat = 0
disp - drat:wt = 0

Model 1: restricted model
Model 2: mpg ~ disp + hp + drat * wt

  Res.Df    RSS Df Sum of Sq      F  Pr(>F)  
1     29 211.80                              
2     26 164.67  3    47.129 2.4804 0.08337 .
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

See ?linearHypothesis for a variety of other ways to specify the test.

Alternative:

The above shows you a quick and easy way to carry out hypothesis tests. Users with a solid understanding of the algebra of hypothesis tests may find the following approach more convenient, at least for simple versions of the test. Let's say we want to test whether or not the coefficients on cyl and carb are identical.

mod <- lm(mpg ~ disp + hp + cyl + carb, mtcars)

The following tests are equivalent:

Test one:

linearHypothesis(mod, c("cyl = carb" ))

Linear hypothesis test
Hypothesis:
cyl - carb = 0
Model 1: restricted model
Model 2: mpg ~ disp + hp + cyl + carb
  Res.Df    RSS Df Sum of Sq      F Pr(>F)
1     28 238.83                           
2     27 238.71  1   0.12128 0.0137 0.9076

Test two:

rmod<- lm(mpg ~ disp + hp + I(cyl + carb), mtcars)
anova(mod, rmod)

Analysis of Variance Table
Model 1: mpg ~ disp + hp + cyl + carb
Model 2: mpg ~ disp + hp + I(cyl + carb)
  Res.Df    RSS Df Sum of Sq      F Pr(>F)
1     27 238.71                           
2     28 238.83 -1  -0.12128 0.0137 0.9076