R code to test the difference between coefficients of regressors from one regression

后端 未结 2 947
别那么骄傲
别那么骄傲 2021-02-06 14:40

I want to test whether coefficients in one linear regression are different from each other or whether at least one of them is significantly different from one certain value, say

2条回答
  •  粉色の甜心
    2021-02-06 15:17

    The car package has a simple function to do that.

    First, fit your model:

    model <- lm(Sepal.Length ~ Sepal.Width + Petal.Length, data = iris)
    

    Than you may test different linear hypothesis using the linearHypothesis function, for instance:

    library(car)
    
    # tests if the coefficient of Sepal.Width = Petal.Length
    linearHypothesis(model, "Sepal.Width = Petal.Length")
    Linear hypothesis test
    
    Hypothesis:
    Sepal.Width - Petal.Length = 0
    
    Model 1: restricted model
    Model 2: Sepal.Length ~ Sepal.Width + Petal.Length
    
      Res.Df    RSS Df Sum of Sq      F  Pr(>F)  
    1    148 16.744                              
    2    147 16.329  1    0.4157 3.7423 0.05497 .
    ---
    Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
    
    # Other examples:
    # tests if the coefficient of Sepal.Width = 0
    linearHypothesis(model, "Sepal.Width = 0")
    
    # tests if the coefficient of Sepal.Width = 0.5
    linearHypothesis(model, "Sepal.Width = 0.5")
    
    # tests if both are equal to zero
    linearHypothesis(model, c("Sepal.Width = 0", "Petal.Length = 0"))
    

提交回复
热议问题