I am looking for some help with my analysis of longitudinal data with time-varying covariates. I am planning to use R and the lme4 package. However, I am happy to use Stata also. I am interested in looking at the relationship between cognition and taking ACE inhibitors in longitudinal data
The example dataset is below:
df = data.frame(cognition = rnorm(200)+2,
wave = rep(c("W1", "W2", "W3", "W4"), each = 50) ,
hypertension = c(rep(c( "Y","N", "N", "N", "Y", "N", "N", "N", "N", "N"), 5),
rep(c( "Y","Y", "N", "N", "Y", "N", "N", "N", "N", "N"), 5) ,
rep(c( "Y","Y", "N", "N", "Y", "N", "Y", "Y", "N", "N"), 5) ,
rep(c( "Y","Y", "N", "N", "Y", "N", "Y", "Y", "Y", "N"), 5) )
,
diabetes = c(rep(c( "N","N", "N", "N", "Y", "N", "N", "N", "Y", "N"), 5),
rep(c( "Y","N", "N", "N", "Y", "N", "N", "N", "Y", "N"), 5) ,
rep(c( "Y","N", "N", "N", "Y", "N", "Y", "Y", "Y", "N"), 5) ,
rep(c( "Y","Y", "N", "N", "Y", "N", "Y", "Y", "Y", "Y"), 5) )
,
Smoking = c(rep(c( "Current","Never", "Never", "Former", "Current", "Former", "Former", "Former", "Never", "Current"), 5),
rep(c( "Current","Never", "Never", "Former", "Current", "Former", "Former", "Former", "Never", "Former"), 5) ,
rep(c( "Former","Never", "Never", "Former", "Current", "Current", "Former", "Former", "Never", "Former"), 5) ,
rep(c( "Former","Never", "Never", "Current", "Current", "Current", "Former", "Former", "Never", "Former"), 5) )
,
TakingACEinh = c(rep(c( "Y","N", "N", "N", "N", "N", "N", "N", "N", "N"), 5),
rep(c( "Y","Y", "N", "N", "N", "N", "N", "N", "N", "N"), 5) ,
rep(c( "Y","Y", "N", "N", "Y", "N", "Y", "Y", "N", "N"), 5) ,
rep(c( "Y","N", "N", "N", "Y", "N", "Y", "Y", "Y", "N"), 5) ) ,
id = rep(1:50, 4)
)
Hypertension is the diagnosis of hypertension at each wave (timepoint) - once a person has been diagnosed they cannot go back to being non-hypertensive, the same is true for the variable diabetes. However, there are variables such as smoking that can differ and change over the different waves. Also Taking ACE inhibitors: someone can take this drug in one wave but then in others, they might not. How do I model these variables in my mixed effect model?
I was thinking of two approaches: 1) Keep the data as is and use lme4 but still not sure which is the correct model
library(lme4)
lmer(cognition ~ factor(wave) + hypertension + Smoking + diabetes + TakingACEinh + (1|id), data = df)
lmer(cognition ~ factor(wave) + hypertension + Smoking + diabetes + TakingACEinh + (1|id) + (1+TakingACEinh|Wave), data = df)
2) Recode the variable hypertension to indicate if a person is 0 non hypertensive, 1 = newly hypertensive, 2 = previous and currently hypertensive and perform the models again using the code above
If anyone has any suggestions on how to model and analyse this type of data please let me know and thanks for your help.