[R] Test For Difference of Betas By Group in car

John Fox j|ox @end|ng |rom mcm@@ter@c@
Sun Jan 19 16:48:57 CET 2025


Dear John,

Here's the summary for your model:

----- snip -----

 > summary(model)

Call:
lm(formula = income ~ education * gender, data = data)

Residuals:
          1          2          3          4          5          6
-4.286e+00 -5.000e+00  6.429e+00 -1.080e-14 -2.143e+00  5.000e+00

Coefficients:
                      Estimate Std. Error t value Pr(>|t|)
(Intercept)          -16.6667    22.0389  -0.756    0.528
education              4.1667     1.5430   2.700    0.114
genderMale            -4.7619    34.1067  -0.140    0.902
education:genderMale   0.4762     2.3328   0.204    0.857

Residual standard error: 7.559 on 2 degrees of freedom
Multiple R-squared:  0.8823,	Adjusted R-squared:  0.7057
F-statistic: 4.997 on 3 and 2 DF,  p-value: 0.1713

----- snip -----

The factor gender, with two levels ("Female" and "Male") produces one 
dummy regressor, genderMale, coded 1 for "Male" and 0 for "Female". The 
difference in education slopes between the genders is then captured by 
the education:genderMale interaction coefficient, so the t-test for the 
difference is directly in the summary output.

You can also get essentially the same test from 
car::linearyHypothesis(), as David Winsemius suggested to you:

 > linearHypothesis(model, "education:genderMale = 0")

Linear hypothesis test:
education:genderMale = 0

Model 1: restricted model
Model 2: income ~ education * gender

   Res.Df    RSS Df Sum of Sq      F Pr(>F)
1      3 116.67
2      2 114.29  1     2.381 0.0417 0.8571

Your attempt to use linearHypothesis() failed because there is no 
genderFemale coefficients in the model. Apparently, the AI you consulted 
wasn't very I.

Frankly, all this is very basic information about how linear (and 
similar) statistical models are implemented in R, covered in the book 
associated with car package and many other places.

I hope this helps,
  John
-- 
John Fox, Professor Emeritus
McMaster University
Hamilton, Ontario, Canada
web: https://www.john-fox.ca/
--

On 2025-01-18 9:59 p.m., Sparks, John wrote:
> 	
> Caution: External email.
> 
> 
> Hello R-Helpers,
> 
> I was looking into how to test whether the beta coefficient from a 
> regression would be the same for two different groups contained in the 
> dataset for the regression.
> 
> When I put that question into google, AI returned a very nice looking 
> answer (and a couple of variations on it).
> 
> library(car)
> data <- data.frame(income = c(30, 45, 50, 25, 60, 55),
>                     education = c(12, 16, 14, 10, 18, 16),
>                     gender = c("Male", "Female", "Male", "Female", 
> "Male", "Female"))
> model <- lm(income ~ education * gender, data = data)
> # Test if the beta for "education" is significantly different between 
> genders
> test <- linearHypothesis(model, "genderMale - genderFemale = 0")
> print(test)
> 
> This, however, produces an error that I can't find a way to resolve.
> 
> Can this test actually be done in this manner, or is this a case of AI 
> run amok.
> 
> Guidance would be appreciated.
> --John Sparks
> 
>



More information about the R-help mailing list