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

2

6

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?

Feverish answered 12/6, 2016 at 13:5 Comment(2)
You mention that you want to test whether these differences are significant. That assumes you need a statistical test? A likelihood-ratio test is commonly used to compare the fit of different models. 'lmtest' package in r does that.Soninlaw
can you give me a demo to show this?Feverish
S
8

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"))
Spay answered 12/6, 2016 at 14:42 Comment(2)
Does the package have a function, like linearHypothesis, if the model is a regression logistic?Grider
@Grider I'm a little late to the show but the linearHypothesis() function does seem to handle objects of class glm (as well as a ton of other classes)Estis
D
0

You can compare the coefficients list from each respective model (say mod1 and mod2), as in:

diff=merge(mod1$coefficients, mod2$coefficients, by=0, all=TRUE)
diff[is.na(diff)]=0
diff$error=abs(diff$x-diff$y)
diff[order(diff$error, decreasing=TRUE),]

This produces a data frame sorted by the absolute value of the difference in coefficients, i.e.:

    Row.names            x            y        error
1 (Intercept) -0.264189182 -0.060450853 2.037383e-01
6          id  0.003402056  0.000000000 3.402056e-03
3           b -0.001804978 -0.003357193 1.552215e-03
2           a -0.049900767 -0.049417150 4.836163e-04
4           c  0.013749907  0.013819799 6.989203e-05
5           d -0.004097366 -0.004110830 1.346320e-05

If the slopes are not what you are after, you can access the other coefficients using the coef() function:

coef(summary(model))

To get Pr(>|z|), for example, use:

coef(summary(model))[,"Pr(>|z|)"]
Daunt answered 12/6, 2016 at 13:46 Comment(2)
Thanks for your effort, the answer you gave seems to test whether the same variable has the same slope across models. What I want to get is whether the different variables say a and b has the same coefficient or not in one model, and what the p value is.Feverish
OK. I'm not familiar with stata, so I didn't have a good example to go on. You can access other coefficient parameters from a glm model using coef(), as in my edited answer. But it looks like Carlos's answer may be more what you're looking for.Daunt

© 2022 - 2024 — McMap. All rights reserved.