1

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.

Ferdi
  • 4,882
  • 7
  • 42
  • 62
J. Doe
  • 223
  • 1
  • 6

1 Answers1

5

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!

Isabella Ghement
  • 18,164
  • 2
  • 22
  • 46
  • 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? – J. Doe Apr 30 '19 at 12:06
  • 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. – Isabella Ghement Apr 30 '19 at 21:43
  • 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. – Isabella Ghement Apr 30 '19 at 21:46
  • 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. – Isabella Ghement Apr 30 '19 at 21:48
  • Which model you consider for your data depends on what your research questions are. – Isabella Ghement Apr 30 '19 at 21:49
  • The book "Longitudinal Data Analysis for the Behavioural Sciences Using R" by Jeffrey D. Long (Sage, 2012) has a nice discussion on working with dynamic predictors (i.e., within-subject predictors, whose values change over time for each subject). The options are to: 1. Include DV2 but exclude MEAS_POINT, 2. Include DV2 and MEAS_POINT as main effects only and 3. Include the main effects of DV2 and MEAS_POINT as well as their interaction. Of course, you also have to worry about DV1 and whether it interacts with both DV2 and MEAS_POINT or neither. See Chapter 13 of recommended book. – Isabella Ghement Apr 30 '19 at 21:57
  • 1
    Thank you, this was extremely helpful! – J. Doe May 01 '19 at 05:37