# Repeated measures in R with both independent and dependent variables changing

I searched many posts but could not find a solution to my particular problem.

I would like to implement in R a way to measure the association between a dependent variable Y and an independent variable X, and estimate if a treatment influencing both X and Y has an effect when comparing pre- and post-treatment X and Y (in a treated and control group) -- while accounting for covariates.

In other words I have:

• Dependent (continous) variable Y measured at PRE and POST
• Independent (continous) variable X measured at PRE and POST
• Covariates C1, C2, C3 (categorical and continuous) fixed (e.g. age, gender, occupation)
• Categorical variable TREATMENT (yes or no, i.e. treated and controls)
• Complete data for all subjects S at TIME = PRE and POST

And would ideally answer all the questions:

• what is the association between X and Y?
• does the treatment affect Y?
• does the treatment affect X?
• does the treatment affect the relationship between X and Y?
• what is the association between the value of X at PRE-treatment and the change in Y (i.e. what is the effect of the baseline value of X on treatment efficacy)?

I thought maybe using a mixed-effect model but I am really unsure about to formulate it:

library(lme4)
mle.model <- lmer(Y ~ X + TIME + TREATMENT + X*TREATMENT + (1 | S) + C1 + C2 + C3, data = my.data)


Then i guess the answers to the questions (in the same order) would be:

• look at beta coefficient of X
• look at beta coefficient of TREATMENT
• ?
• look at beta coefficient of X*TREATMENT
• ?

I would also think the model should include TIME*TREATMENT but i am not sure how this would help answer any of the questions above

UPDATE This post seems to describe a very similar situation, although with several time points (defined as "sessions" there). So after the great answer by @matt-barstead I am not sure if I should go with a level 1 / level 2 approach (and how to implement it) or choose between the two models below (and which one to choose and why):

mle.model.1 <- lmer(Y ~ X*TIME*TREATMENT + (1|S), data)
mle.model.2 <- lmer(Y ~ X*TREATMENT + (1|S) + (1|TIME), data)


1) The conditional association between X and Y would be expressed in the coefficient estimated for X in your model above.

2) The TREATMENT effect on Y is misleading if you look at coefficient for the TREATMENT variable in your model. You likely only care about the change in Y as a function of treatment which would require a TIME*TREATMENT term in your model.

3) Your model cannot address in its current form whether TREATMENT is related to X. That would require a separate model with X positioned as the dependent variable.

4) This seems like something of an odd question to me, unless again you are incorporating time. In that case, you would likely need something like a X*TIME*TREATMENT term in your model (incidentally all lower order interactions would need to be included if you are going to add this three-way interaction term). This term would address whether the slope of X varies as a function of time point and treatment; though I suspect the more interesting question may be something like whether X alters the effect of treatment over time.

5) You may want to consider exploring this question of baseline X values in a separate model: lmer(Y~X.t1+TIME*X.t1+TREATMENT*X.t1+TIME*TREATMENT*X.t1.... If you had more time points it may have been possible to separate baseline X scores in your model as a separate level-2 predictor of level-1 coefficients.

In general, I think it may be useful to break up your variables into level-1 and level-2 measures. Writing out the two sets of equations can help you think through the predictions you want to make and how to set up your models. Using this conceptualization your level 1 variables are your Y, X, and TIME (each is nested within subject). TREATMENT & C1-C3 are level two predictors (in this case between subjects variables). There are a number of resources out that describe how to use and setup mixed models for longitudinal research. For R users a particularly helpful resource is Long (2012). I tend to prefer the Raudenbush & Bryk notation for creating my initial models.

Hope that helps.

• Thanks a lot Matt. From what I understand, I need to either add TIME*TREATMENT, if I avoid question 4, or add X*TIME*TREATMENT and all the lower term interactions otherwise. In the latter case my model would be lmer(Y ~ X * TIME * TREATMENT + (1 | S) + C1 + C2 + C3, data = my.data). For the reference, I read that most longitudinal approaches were not optimal for situations with only two time points. Would you still recommend Long2012? Jul 14, 2017 at 11:54
• The real issue with two time points is that you cannot estimate random slopes in the model for your level 1 predictors. In your case, you are essentially running a souped up repeated measures ANOVA of sorts (that is being a bit glib, but it is conceptually on track). The Long book is a general reference for using R to analyze longitudinal data and is written with multiple time points in mind. It may not be integral, but it could be useful to you. I'll leave it to you to decide. Jul 14, 2017 at 14:30