Home > OS >  Lmer for repeated measures
Lmer for repeated measures

Time:10-31

Here is an example of my dataset:

df <- data.frame(
id  = c(13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 29, 30, 31, 32, 33, 
           34, 35, 36, 37, 38, 39, 40, 62, 63, 64, 65, 13, 14, 15, 16, 17, 18, 
           19, 20, 21, 22, 23, 24, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 
           40, 62, 63, 64, 65, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 
           29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 62, 63, 64, 65), 
collection_point       = c(rep(c("Baseline", "Immediate", "3M"), each=28)), 
intervention = c(rep(c("B", "A", "C", "B", "C", "A", "A", "B", "A", "C", "B", "C", 
                  "A", "A", "B", "A", "C", "B", "C", "A", "A"), each = 4)), 
scale_A       = c(6.5, 7.0, 6.25, 6.0, NA, 7.5, 7.5, 
            8.0, 7.5, 6.75, 7.5, 6.75, 6.75, 6.5, 
            5.75, 6.75, 7.75, 7.5, 7.75, 7.25, 7.75, 
            7.25, 7.25, 5.75, 6.75, NA, 6.75, 7.5, 
            6.75, 7.0, 6.5, 7.0, 7.5, 7.5, 7.5, 
            7.75, 7.25, 7.25, 7.25, 7.5, 6.5, 6.25, 
            6.25, 7.25, 7.5, 6.75, 7.25, 7.25, 7.5, 
            7.25, 7.5, 7.25, NA, 7.0, 7.5, 7.5, 
            6.75, 7.25, 6.5, 7.0, 7.5, 7.5, 7.5, 
            7.75, 7.5, 7.5, 7.5, 7.5, 6.5, 5.75, 
            6.25, 6.75, 7.5, 7.25, 7.25, 7.5, 7.75, 
            7.75, 7.75, 7.5, NA, NA, NA, NA))

where,

id = participant

collection_point = times data was collected from participant (repeated measure)

intervention = group each participant was randomized to (fixed effect)

scale_A = questionnaire score that each participant completed at each data collection point (outcome)

Participants were randomized to one of three interventions and completed the same scale (scale A) at three different time points to determine any improvements over time.

To account for collection_point as a repeated measure, I originally did this:

mixed.lmer.A<-lmer(scale_A~intervention collection_point intervention*collection_point (1|collection_point), data = df)

From what I've read, generally a variable isn't both a fixed and random effect, but I wasn't sure how else to specify collection_point as a repeated measure. Also, when I run the model R says there's 500 observations. Which makes sense if you add all observations from baseline to 3M, but there are only 200 participants (some participants dropped out, hence fewer observations than expected). So I'm thinking R isn't accounting for the fact that it's the same participants that repeated the questionnaire?

I've also tried this:

mixed.lmer.A2<-lmer(scale_A~intervention collection_point intervention*collection_point (1 collection_point|id), data = df)

But I get an error message stating number of obs. <= number of random effects.

Lastly, I tried this but I'm not sure if this is the way to go about it either:

mixed.lmer.A3<-lmer(scale_A~intervention collection_point intervention*collection_point (1|collection_point/id), data = df)

Any help would be greatly appreciated! I'm still learning R and have had some difficulty with lmer. Thanks!

CodePudding user response:

You have repeated measures within both a collection_point and id grouping (and it's also not balanced which will make plotting somewhat difficult.) (Note: the groupings are not independent, see the note at the bottom of this answer)

It appears you are considering the intervention to be the fixed effect of interest. Since you probably want any display of the collection points to be in the order that you presented it to the dataframe, you would need it to have a levels value and an ordered flag.

 collection_point = factor( c(rep(c("Baseline", "Immediate", "3M"), each=28)), 
                          levels=c("Baseline", "Immediate", "3M"), ordered=TRUE)

For the model building consider this, and for authoritative consultation refer to BBolker's page on the subject: https://bbolker.github.io/mixedmodels-misc/glmmFAQ.html#model-definition

> mixed.lmer.A1<-lmer(scale_A~intervention (1|id)  (1|collection_point), data = df)
> summary(mixed.lmer.A1)
Linear mixed model fit by REML ['lmerMod']
Formula: scale_A ~ intervention   (1 | id)   (1 | collection_point)
   Data: df

REML criterion at convergence: 70.4

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-3.9506 -0.3416  0.1162  0.5891  1.4978 

Random effects:
 Groups           Name        Variance Std.Dev.
 id               (Intercept) 0.042109 0.20521 
 collection_point (Intercept) 0.008454 0.09195 
 Residual                     0.099734 0.31581 
Number of obs: 77, groups:  id, 28; collection_point, 3

Fixed effects:
              Estimate Std. Error t value
(Intercept)    7.38963    0.10002  73.880
interventionB -0.81671    0.12886  -6.338
interventionC -0.04588    0.12886  -0.356

Correlation of Fixed Effects:
            (Intr) intrvB
interventnB -0.558       
interventnC -0.558  0.433

Another option might be to build a model with both an intervention effect and a linear trend estimate across the ordered timings at the three collection points:

mixed.lmer.inter.trend <- lmer(scale_A~intervention as.numeric(collection_point) 
         (1|id), 
               data = df)

The last suggestion was made after trying to model collection point as a fixed effect but got both a linear and quadratic estimate (caused by the default handling of the ordered factor).

It's also the case that you may only need the (1|id) since no member of than id grouping participates in more than one series of collections:

with(df, table( collection_point, id))
                id
collection_point 13 14 15 16 17 18 19 20 21 22 23 24 29 30 31 32 33 34 35 36 37 38 39 40 62 63 64 65
       Baseline   1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
       Immediate  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
       3M         1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
  • Related