# Repeated Measures with LMER: Include time as random effect?

I have a dataset of measurements of 29 patients across three time-points. The scenario can be easily simulated using:

 set.seed(123)

dat<-data.frame(ID=rep(seq(1:100),each=3),
TimePoints=rep(c("00h","06h","24h"),100),
Var=rnorm(n=300))

dat$$ID<-as.character(dat$$ID)


Now, I could use a repeated measures one-way ANOVA to compare differences among time-points. However, if I want to use linear mixed models, should my model be:

ml1<-lmer(Var ~ TimePoints + (1|ID),data=dat)


vs

ml2<-lmer(Var ~ TimePoints + (TimePoints|ID),data=dat)


I think it should be the latter to include the fact that each patient has its own slope.

What do you think?

Thanks

The correct answer is that your model should be the one that best captures the data generating mechanism used in your data simulation.

However, your data simulation is just giving you some quick numbers you can use to fit your two models - the way you have set it up is such that it does NOT actually simulate data from either of those models. You can search this forum for the correct way to simulate data from either of your two models - or just use Google.

In practice, we almost never know the data generating mechanism responsible for generating our data so the best we can do is approximate it with our modelling.

Getting back to how you fitted your two models, there are some things you must pay attention to before you even formulate your lmer models.

First, you should convert your ID variable to a factor:

dat$$ID <- factor(dat$$ID)


This will force R to treat ID as a grouping variable (i.e., a categorical variable) for modelling purposes.

Second, you should decide how you want to model the effect of time for each of your patients. Should the effect of time be linear or potentially nonlinear?

For a potentially nonlinear time effect within subject, you need to convert your TimePoints variable to a factor:

dat$$TimePoints <- factor(dat$$TimePoints, levels = c("00h", "06h", "24h"))


If you do this, R will treat the first listed level (or category) of TimePoints, "00h", as the reference against which the other two will be compared. (A day should have 24 hours, so you want to be careful with how you define your time points.)

Because "00h" was listed first and is now the reference level, R will code the potentially nonlinear effect of time using two dummy variables: TimePoints06h and TimePoints24h. The dummy variable TimePoints06h is set to 1 for all records in your data where TimePoints is equal to "06h" and 0 else. The dummy variable TimePoints24h is set to 1 for all records in your data where TimePoints is equal to "24h" and 0 else. You will see these variables reported in your model summaries!

Because you now have 2 dummy variables, you will have 2 slopes to worry about in your model. So your second model will have two random slopes, one for each of the two dummy variables!

For a linear effect of TimePoints, your TimePoints variable will take on numeric values like 0, 6, 24.

Generally, you would use TimePoints as a factor if interested in time point to time point comparisons. If only interested in describing the overall shape of the time effect (e.g., linear), then TimePoints would be used as a numeric variable.

After fitting your two models, you could compare them against each other (e.g., via the AIC criterion) to see which one is preferred for your data (e.g., model with lowest AIC value is preferred).