问题
I have a data set that investigate depression among individuals with different ethnicities (Black, White, and Latina).
I want to know how depression at baseline relates to depression at post with all ethnic groups, I did
lm(depression_base ~ depression_post, data=Data
Now, I want to look at the relationship by ethnicity. Ethnicity in my dataset is coded as 0 = White
, 1 = Black
, and 2 = Latina
. I am thinking that I need to use the ifelse
function, but I cannot seem to get it to work. Here is what I tried for White
:
lm(depression_base[ifelse, ethnicity == 0],
depression_post[ifelse, ethnicity == 0])
Any suggestions?
回答1:
Code your ethnicity
as factors with correct levels, then do a single regression:
## recode your 0, 1, 2 from numeric to factor
Data$ethnicity <- factor(Data$ethnicity)
fit <- lm(depression_base ~ depression_post * ethnicity, data = Data)
A single model allows you decent test for variability between groups.
You might be confused about the meaning of the coefficients. If so, have a look at this or other posts on CrossValidated.
来源:https://stackoverflow.com/questions/51407989/r-regression-analysis-analyzing-data-for-a-certain-ethnicity