Testing the equality of multiple coefficients in R

前端 未结 1 1517
心在旅途
心在旅途 2021-01-07 01:54

I have the following model:

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

Easil

1条回答
  •  囚心锁ツ
    2021-01-07 02:55

    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
    

    0 讨论(0)
提交回复
热议问题