2
$\begingroup$

I am looking for a way to perform linear regression in R. So far I have done a lot of regression analyses, but never with repeated measures.

My dataset has the following variables:

  • DV - the dependent variable - a continuous variable
  • IV1 - first independent variable (gender) - dichotomous variable
  • IV2 - second independent variable - continuous variable
  • MEAS_POINT - dichotomous variable indicating the measurement point at which the data was collected (1 or 2)
  • S.ID - subject ID

DV and IV2 were collected at two timepoints as indicated by MEAS_POINT but IV1 (gender) was obviously measured only once.

The data is in long format.

I have no idea on how to set up a model and I don't even know if the data needs to be in long or wide format.

Also, I have no clue on how to set up an interaction term.

$\endgroup$
1
  • 1
    $\begingroup$ What are your hypotheses? $\endgroup$ Commented Apr 29, 2019 at 13:06

1 Answer 1

5
$\begingroup$

One option for analysing your long format data would be to use linear mixed effects modelling via the lme() function in the nlme package in R or via the lmer() function in the lme4 package.

lme() in nlme

For example, a so-called random intercept linear mixed effects model for your data could be set up as follows:

library(nlme)

M1.lme <- lme(DV ~ IV1*IV2, random = ~ 1|S.ID, data = DATASET, method = "REML")

A linear mixed effects model with random intercepts and random slopes for IV2 can be specified as:

M2.lme <- lme(DV ~ IV1*IV2, random = ~ 1 + IV2|S.ID, data = DATASET, method = "REML")

You have to pay attention to what fitting method you will use for these models - REML (Restricted Maximum Likelihood) or ML (Maximum Likelihood). See, for example, How to decide whether to set REML to True or False?.

The fact that the variables DV1 and DV2 are allowed to interact in your model (e.g., Model M1) is specified via the term IV1*IV2. An equivalent but explicit way to specify the main effects of DV1 and DV2 and the interaction between DV1 and DV2 would be as follows:

M1.lme <- lme(DV ~ IV1 + IV2 + IV1:IV2, random = ~ 1|S.ID, data = DATASET, method = "REML")

For either model, you can construct an analysis of deviance table using these commands:

library(car)

Anova(M1)

Anova(M2)

You can also conduct post-hoc/contrast analyses, as explained for instance here: http://rcompanion.org/handbook/I_09.html.

lmer() in lme4

The same types of models as described above can also be fitted with the lmer() function from the lme4 package, though the syntax is slightly different (see https://www.r-bloggers.com/how-to-do-repeated-measures-anovas-in-r/):

library(lme4)
library(lmerTest)

M1.lmer <- lmer(DV ~ IV1*IV2 + (1|S.ID), data = DATASET, REML = TRUE)
anova(M1.lmer)

M2.lmer  <- lmer(DV ~ IV1*IV2 + (1 + IV2|S.ID),  data = DATASET, REML = TRUE)
anova(M1.lmer)

Note that the main effects of IV1 and IV2 and the interaction between them are assumed to be the same across all subjects in the model M1.lme (or M1.lmer). However, model M2.lme (or M2.lmer) assumes the main effect of DV2 to differ across subjects. In principle, it is also possible to allow the interaction between DV1 and DV2 to differ across subjects, though most people seem not to consider that possibility for some reason (likely related to the increased complexity of the model and the fact that they may not have enough data to support that complexity).

M3.lme <- lme(DV ~ IV1 + IV2 + IV1:IV2, random = ~ 1 + IV2 + IV1:IV2|S.ID, data = DATASET, REML = TRUE)
Anova(M1.lme)

M3.lmer <- lme(DV ~ IV1 + IV2 + IV1:IV2 + (1 + IV2 + IV1:IV2|S.ID), data = DATASET, REML = TRUE)
anova(M1.lme)

Anyway, this should give you enough to get started!

$\endgroup$
7
  • $\begingroup$ Thank you, this looks very helpful! However, I have one more follow-up question: can the measurement timepoint also be included in the model as an independent variable? Would that make sense and how would the code look like? $\endgroup$
    – J. Doe
    Commented Apr 30, 2019 at 12:06
  • $\begingroup$ Sure, MEAS_POINT can be included in the model as a within-subject variable (i.e., a variable whose values change across time for each subject). In this sense, MEAS_POINT will be similar to IV2, which is also a within-subject variable. Thus, just like IV2, MEAS_POINT can be listed in the fixed effects portion of the model either by itself or engaged in interactions with one or both of the variables IV1 and IV2. $\endgroup$ Commented Apr 30, 2019 at 21:43
  • $\begingroup$ If MEAS_POINT is engaged in interactions with both IV1 and IV2, you can add terms like MEAS_POINT, IV1:MEAS_POINT, IV2:MEAS_POINT and IV1:IV2:MEAS_POINT to your fixed effects portion of your model. Or simply list the condensed term IV1*IV2*MEAS_POINT in the fixed effects portion of your model, which expands behind the scenes to include the main effects of IV1, IV2 and MEAS_POINT, plus the two-way and three-way fixed effect interactions between these variables. $\endgroup$ Commented Apr 30, 2019 at 21:46
  • $\begingroup$ In the random effects portion of the model, you can in principle add random slopes for IV2 and/or MEAS_POINT. For example, (1 + IV2 + MEAS_POINT|S.ID) for lmer. If you have enough data, you can also add random slopes for interactions between IV2 and the other predictor variables, as well as random slopes for interactions between MEAS_POINT and the other predictor variables. $\endgroup$ Commented Apr 30, 2019 at 21:48
  • 1
    $\begingroup$ Thank you, this was extremely helpful! $\endgroup$
    – J. Doe
    Commented May 1, 2019 at 5:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.