问题
I have longitudinal data with level 1 and level 2 variables in R my dataframe (df)
:
ID Year Gender Race MathScore DepressionScore MemoryScore
1 1999 M C 80 15 80
1 2000 M C 81 25 60
1 2001 M C 70 50 75
2 1999 F C 65 15 99
2 2000 F C 70 31 98
2 2001 F C 71 30 99
3 1999 F AA 92 10 90
3 2000 F AA 89 10 91
3 2001 F AA 85 26 80
I've tried these:
summary(fix <- lme(MathScore ~ Gender+Race+DepressionScore+MemoryScore,
random= Year|ID, data=df, na.action="na.omit")
summary(fix2 <- lme(MathScore ~ 1+Gender+Race+DepressionScore+MemoryScore,
random=~1|Year, data=df, na.action=na.omit))
I don't understand how to make DepressionScore
and MemoryScore
vary within Year
while keeping Gender
and Race
constant, particularly in fix2
. Also, I don't know if fix2
is capturing the Year
and ID
variation that is happening in my data. Is there a way to restructure it?
回答1:
In this case Math score varies within ID (year is nested within ID) so ID becomes your grouping variable, you could specify:
summary(fix2 <- lme(MathScore ~ 1+Gender+Race+DepressionScore+MemoryScore,
random=list( ID = ~ 1), data=df, na.action=na.omit))
To get a random intercept model or
summary(fix2 <- lme(MathScore ~ 1+Gender+Race+DepressionScore+MemoryScore,
random=list( ID = ~ Year), data=df, na.action=na.omit))
To get a random slope on year. If you are interested in change in math score over time then you might want to specify a fixed effect for year.
summary(fix2 <- lme(MathScore ~ 1+Gender+Race+DepressionScore+MemoryScore+Year,
random=list( ID = ~ Year), data=df, na.action=na.omit))
Which you can extend with interactions
P.S. : The package you're using is not lme4
but nlme
. Maybe you could change the label of your question to nlme
or lme4-nlme
P.P.S: I.m.o this website provides excellent examples of longitudinal data analyses with lme4 or nlme: http://rpsychologist.com/r-guide-longitudinal-lme-lmer and should be a big help
来源:https://stackoverflow.com/questions/42941089/r-ordering-of-lme-covariates-for-level-1-and-level-2-variables