Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 0, this seems quite intuitive to do in Stata. For example

webuse iris reg iris seplen sepwid petlen seplen==sepwid==petlen seplen==sepwid==petlen==0

I wonder how I can do this if I want to test this in R?

like image 469
johnsonzhj Avatar asked Jun 12 '16 13:06

johnsonzhj


People also ask

How do you find the difference between two regression coefficients?

Observation: You can use a slightly more complicated trick to test whether two regression coefficients are equal. To test whether b2 is significantly different from b1 in y = b2x + b1z + b0, you need to rewrite the regression equation as y = B2(x+z) + B1(x-z) + b0.

How do you know if two regression lines are significantly different?

Use analysis of covariance (ancova) when you want to compare two or more regression lines to each other; ancova will tell you whether the regression lines are different from each other in either slope or intercept.

Which test is used for regression coefficients?

The most common hypothesis test in econometrics is the t-test of the null hypothesis that a coefficient equals zero. To perform this test,we calculate a t-statistic by dividing the estimated coefficient by its standard error. The resulting ratio tells us how many standard-error units the coefficient is away from zero.

Can you compare regressions?

You can graph the regression lines to visually compare the slope coefficients and constants. However, you should also statistically test the differences. Hypothesis testing helps separate the true differences from the random differences caused by sampling error so you can have more confidence in your findings.


1 Answers

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"))
like image 122
Carlos Cinelli Avatar answered Oct 17 '22 08:10

Carlos Cinelli