R Ordering of LME covariates for level 1 and level 2 variables?

半世苍凉 提交于 2019-12-25 09:00:33

问题


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 nlmeor 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!