2
votes

Using R, I want to statistically compare two coefficients from the same regression. In the Stata software, there is the test B1 = B2. What is the equivalent in R? I check several posts, but no one answered this issue.

https://stats.stackexchange.com/questions/33013/what-test-can-i-use-to-compare-slopes-from-two-or-more-regression-models

SPSS: Comparing regression coefficient from multiple models

Comparing regression models in R

Here are some simulated data.

library('MASS')

mu <- c(0,0,0)

Sigma <- matrix(.5, nrow=3, ncol=3) + diag(3)*0.3

MyData <- mvrnorm(n=10000, mu=mu, Sigma=Sigma) %>% 
  as.data.frame()

names(MyData) = c('v1', 'v2', 'y')

MyModel = lm(y ~ v1 * v2, data = MyData)

summary(MyModel)

I want to compare the estimate of V1 to the one of V2. So that if V1 and V2 are manipulated, I would like to tell something like "the influence of V1 on Y, is significantly higher than the influence of V2 on Y"

1
note: was cross posted here stats.stackexchange.com/q/441024/17060 - Sam Mason
You can use linearHypothesis from the car package, linearHypothesis(MyModel, "v1 = v2"). See here: stats.stackexchange.com/questions/228351/… - caldwellst
Please add it as an answer so I can accept it - Rtist
Do you know if it works also for logistic regression? - Rtist

1 Answers

1
votes

You can try multcomp , so if you look at the coefficients of your model:

coefficients(MyModel)
(Intercept)           v1           v2        v1:v2 
 0.006961219  0.373547048  0.394760005 -0.012167754 

You want to find the difference between the 2nd and 3rd term, so your contrast matrix is:

# yes it looks a bit weird at first
ctr = rbind("v1-v2"=c(0,1,-1,0))

And we can apply this using glht:

summary(glht(MyModel,ctr))

     Simultaneous Tests for General Linear Hypotheses

Fit: lm(formula = y ~ v1 * v2, data = MyData)

Linear Hypotheses:
           Estimate Std. Error t value Pr(>|t|)
v1-v2 == 0 -0.02121    0.01640  -1.294    0.196
(Adjusted p values reported -- single-step method)

This works for most general linear models. In your summary function, you get the the significance of each term based on the effect / standard error. The glht function does something similar. One exception for logistic regression I can think of, is when you have complete separation