我有一个数据集,用于调查不同种族(黑人,白人和拉丁裔)个体中的抑郁症。
我想知道所有种族的基线抑郁症与岗后抑郁症之间的关系,
lm(depression_base ~ depression_post, data=Data
现在,我想按族裔来看这种关系。我的数据集中的种族编码为
0 = White
,1 = Black
和2 = Latina
。我以为我需要使用ifelse
函数,但似乎无法正常工作。这是我尝试White
的方法:lm(depression_base[ifelse, ethnicity == 0],
depression_post[ifelse, ethnicity == 0])
有什么建议么?
最佳答案
将ethnicity
编码为具有正确水平的因子,然后进行一次回归:
## recode your 0, 1, 2 from numeric to factor
Data$ethnicity <- factor(Data$ethnicity)
fit <- lm(depression_base ~ depression_post * ethnicity, data = Data)
单个模型使您可以对组之间的变异性进行适当的测试。
您可能对系数的含义感到困惑。如果是这样,请查看this或CrossValidated上的其他帖子。