9

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!

Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
user33125
  • 197
  • 1
  • 3
  • 12
  • From CRAN TaskView "Econometrics": Tests of more general linear hypotheses are implemented in linearHypothesis() and for nonlinear hypotheses in deltaMethod() in package `car`. – jogo Jun 02 '16 at 12:16
  • See the last three lines here: http://stackoverflow.com/questions/37418421/calculate-and-compare-coefficient-estimates-from-a-regression-interaction-for-ea/37419103#37419103 – coffeinjunky Jun 02 '16 at 12:38
  • 1
    By the way, according to StataCorp LP, Stata is a name, not an abbreviation. Hence, Stata prefers to be called `Stata` instead of `STATA`. – coffeinjunky Jun 02 '16 at 12:40

1 Answers1

9

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
coffeinjunky
  • 11,254
  • 39
  • 57
  • Hi coffeinjunky! Your answer is not 100% the answer to my question, because I want to know how to compare multiple coefficients of 2 groups with each other. However, your suggestion helped me to find the answer to my question. So thank you very much. For me in particular: when running the model, I have to use `summary mod` to see how R names the interactions. Then I can do the thing that you specify `linearHypothesis(mod, c("Group0:t1=Group1:t1", "Group0:t2=Group1:t2"))` – user33125 Jun 02 '16 at 15:13
  • Perfect! Thank you very much for your help! – user33125 Jun 02 '16 at 15:33
  • Could you describe some of the linear algebra that let's us do `anova(mod, rmod`? – vsocrates Dec 15 '20 at 19:09