R回归分析:分析特定种族的数据

R regression analysis: analyzing data for a certain ethnicity

我有一个数据集可以调查不同种族(黑人、白人和拉丁裔)个体的抑郁症。

我想知道基线时的抑郁症与 post 时所有种族的抑郁症有何关系,我做到了

lm(depression_base ~ depression_post, data=Data

现在,我想按种族查看关系。我的数据集中的种族编码为 0 = White1 = Black2 = 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 上的其他帖子。