我有一套调查不同种族(黑人、白人和拉丁裔)的抑郁症的数据集。
我想知道基线时的抑郁与所有种族群体的抑郁有什么关系,我做了。
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])
有什么建议吗?
发布于 2018-07-18 17:44:31
将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上的其他帖子。
https://stackoverflow.com/questions/51407989
复制相似问题