0
$\begingroup$

I'm trying to run a simple mixed model by subject id: y=week. Since it's repeated measures, I assume that I need to use mixed model even though I want to run the model by subject id? In addition, since the measurements are equally spaced, I tried to use AR(1) covariance structure. However, my code(below) didn't work and I don't know what's the problem. I'm new to R and my questions are:

  1. How to run mixed model by study id, so that each model is for each participant?
  2. Since I want to run each model by study id, there is only within subject variability. Is nlme:lme with AR structure over complicated ?

fm <- lme(y ~ week , correlation = corAR1(), data = test)

enter image description here

$\endgroup$

1 Answer 1

0
$\begingroup$

I can't speak to how you would fit this model with the lme function, as my experience is more with the lme4 and lmerTest packages. However, it seems based off context that you don't have subject random effects modeled into your fit, so I will show you how thats normally done with lme4. First, I load the essential libraries. The languageR package contains a dataset to show the example. The lmerTest package contains the mixed model functions along with additional significance summaries.

#### Load Libaries ####
library(languageR)
library(lmerTest)

Then I fit the model here. The dependent variable goes left of the ~, the fixed effects (here just Rating) goes directly to the right, and random effects are modeled with the (1|factor) section, followed by the data. Here the random effect specifies that we are just estimating the random intercepts of each subject to see how they vary on average.

#### Fit Model ####
fit <- lmer(
  RT ~ Rating + (1|Subject), 
  data = primingHeidPrevRT
)

After, we can summarize the results and random effects.

#### Summarize and Inspect Subject Random Effects ####
summary(fit)
ranef(fit)

The summary suggests that the 26 subjects vary in reaction time by a meager .20 standard deviations. Rating has a significant effect on reaction times.

Linear mixed model fit by REML. t-tests use Satterthwaite's method [
lmerModLmerTest]
Formula: RT ~ Rating + (1 | Subject)
   Data: primingHeidPrevRT

REML criterion at convergence: -100.7

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-2.6915 -0.7174 -0.1329  0.5611  4.3139 

Random effects:
 Groups   Name        Variance Std.Dev.
 Subject  (Intercept) 0.04053  0.2013  
 Residual             0.04624  0.2150  
Number of obs: 832, groups:  Subject, 26

Fixed effects:
             Estimate Std. Error        df t value Pr(>|t|)    
(Intercept)   6.77206    0.05079  61.11161 133.341  < 2e-16 ***
Rating       -0.05963    0.01170 805.87591  -5.096 4.33e-07 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Correlation of Fixed Effects:
       (Intr)
Rating -0.608

Inspection of the random effects with the ranef(fit) function reveals why there is very little deviation between subjects.

$Subject
      (Intercept)
pp1   0.089140220
pp10 -0.134503361
pp11 -0.060034402
pp12  0.150133737
pp13  0.164660518
pp14 -0.251403934
pp15 -0.006350700
pp16 -0.221486346
pp17  0.326582373
pp18  0.009414531
pp19 -0.040134104
pp2  -0.247769521
pp20 -0.015899970
pp21  0.220808419
pp22 -0.097461296
pp23 -0.015546564
pp24 -0.008721349
pp25 -0.225292904
pp28 -0.129187352
pp3   0.096531984
pp4   0.499137579
pp5   0.191453237
pp6  -0.239985845
pp7   0.022286278
pp8   0.214860305
pp9  -0.291231534

with conditional variances for “Subject”

There are a number of tweaks you can make as well. If we wanted to add random slopes, where subject reaction times vary based on the length in letters of the words they observe, we could use the (1 + x | factor) syntax.

#### Fit Model with Random Slopes ####
fit2 <- lmer(
  RT ~ Rating + (1 + LengthInLetters|Subject), 
  data = primingHeidPrevRT
)

Here the summary looks slightly different, with the random slope term adding some redundancy in variance but having a heavy negative correlation with subject intercepts.

Linear mixed model fit by REML. t-tests use Satterthwaite's method [
lmerModLmerTest]
Formula: RT ~ Rating + (1 + LengthInLetters | Subject)
   Data: primingHeidPrevRT

REML criterion at convergence: -102

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-2.7666 -0.6954 -0.1346  0.5620  4.3334 

Random effects:
 Groups   Name            Variance  Std.Dev. Corr 
 Subject  (Intercept)     0.0989711 0.31460       
          LengthInLetters 0.0002176 0.01475  -0.84
 Residual                 0.0459557 0.21437       
Number of obs: 832, groups:  Subject, 26

Fixed effects:
             Estimate Std. Error        df t value Pr(>|t|)    
(Intercept)   6.79296    0.05144  64.32748 132.055  < 2e-16 ***
Rating       -0.06106    0.01172 797.13370  -5.209 2.43e-07 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Correlation of Fixed Effects:
       (Intr)
Rating -0.636

I would suggest reading the lme4 package documentation for specification of random effects to get a better idea of how this works.

$\endgroup$

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.