问题描述
我正在尝试比较具有相同变量但针对不同子组运行的两个线性回归的系数.我想检查模型 1 中的系数是否等于模型 2 中的系数.我需要知道每个系数.
I am trying to compare the coefficients of two linear regressions with the same variables, but run for different subgroups. I want to check if the coefficients in my model 1 are equal to my coefficients in my model 2. I need to know for each coefficient.
我的可重复数据:
Data <- data.frame(
gender = sample (c("men", "women"), 2000, replace = TRUE),
var1 = sample (c("value1", "value2"), 2000, replace = TRUE),
var2 = sample (c("valueA", "valueB"), 2000, replace = TRUE),
y = sample(0:10, 2000, replace = TRUE)
)
我运行了两个回归:
men <- subset(Data, gender =="men")
women <- subset(Data, gender =="women")
lm.men <- lm(y~var1+var2, data = men)
summary(lm.men)
lm.women <- lm(y~var1+var2, data = women)
summary(lm.women)
基本上,我想测试:
- coefficient
var1
inlm.men
=lm.women
中的系数 - coefficient
var2
inlm.men
=lm.women
中的系数
var1
var2
- coefficient
var1
inlm.men
= coefficientvar1
inlm.women
- coefficient
var2
inlm.men
= coefficientvar2
inlm.women
我无法使用 anova()
函数,因为我的两个样本不同.我想我应该应用 F 检验,但我找不到此测试的函数.
I can't use the anova()
function, because my two samples are different. I think I should apply an F-test but I can't find a function for this test.
有人知道如何解决我的问题吗?
Does anyone know how to solve my problem ?
推荐答案
正如@Barker 在评论中指出的,这个问题的统计部分是 已在 CrossValidated 上回答;我将在这里添加一些 R 编码细节.
As @Barker points out in comments, the statistical part of this question is already answered on CrossValidated; I'll add some R-coding details here.
为了回答这些问题(var1
和 var2
对男性和女性的影响是否有显着差异?)交互并测试交互项.
In order to answer these questions ("do the effects of var1
and var2
differ significantly between men and women?), fit a model with variable-by-gender interactions and test the interaction terms.
Data <- data.frame(
gender = sample (c("men", "women"), 2000, replace = TRUE),
var1 = sample (c("value1", "value2"), 2000, replace = TRUE),
var2 = sample (c("valueA", "valueB"), 2000, replace = TRUE),
y = sample(0:10, 2000, replace = TRUE)
)
mm <- lm(y~(var1+var2)*gender,Data)
以下是互动条款:
interax <- c("var1value2:genderwomen","var2valueB:genderwomen")
printCoefmat(coef(summary(mm))[interax,])
## Estimate Std. Error t value Pr(>|t|)
## var1value2:genderwomen 0.20144 0.28241 0.7133 0.4758
## var2valueB:genderwomen -0.15423 0.28266 -0.5456 0.5854
这篇关于比较不同回归的系数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!