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

David Winsemius dw|n@em|u@ @end|ng |rom comc@@t@net
Sun Jan 19 08:45:45 CET 2025


Sent from my iPhone

> On Jan 19, 2025, at 1:57 PM, David Winsemius <dwinsemius using comcast.net> wrote:
> 
> I don’t understand why you don’t include the full text of the error.
> 
>> David
> Sent from my iPhone
> 
>> On Jan 19, 2025, at 10:00 AM, Sparks, John via R-help <r-help using r-project.org> wrote:
>> 
>> 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")

That last line appears unlikely to be parsed correctly. In R a “=“ sign is interpreted as assignment whereas a “==“ (doubled equals) is a logical operator. Since I’ve only got a iPhone at hand I can’t test. In the future you should include full text of errors, preferably in the context in which they are returned. 

As an experiment I asked ChatGPT your question an it suggested 

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 effect of education differs by gender
test <- linearHypothesis(model, "education:genderMale = 0")

But I have the same concern about that code as I had with whatever your AI produced. 

— 
David

>> 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
>> 
>> 
>> 
>>   [[alternative HTML version deleted]]
>> 
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list