Statistics for Laboratory Scientists ( 140.615 )

Linear Mixed Effects - Longitudinal Data Analysis

The data used in the following examples are in the SPH.140.615 package.

library(SPH.140.615)

Use the function install.packages() if you haven’t installed the following packages, i.e. use install.packages(“lme4”) etc in the code chunk. Alternatively, hit the ‘Knit’ button, and you should be prompted about installing the packages.

library(lattice)
library(Matrix)
library(lme4)

Example 1

A figure of the data.

xyplot(y ~ x|id, data=dat.ex1, type=c("p","r"), layout=c(6,2), as.table=TRUE, xlab="Time", ylab="Response")

We can think of the data being described by a “random intercept” model, i.e.

Yij = b0 + Bi + b1xj + εij,

where i referes to the subject and xj to the jth time point.

lme.fit <- lmer(y ~ x + (1|id), data=dat.ex1)
summary(lme.fit)
## Linear mixed model fit by REML ['lmerMod']
## Formula: y ~ x + (1 | id)
##    Data: dat.ex1
## 
## REML criterion at convergence: 13
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.25586 -0.67363  0.06082  0.65787  2.17048 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  id       (Intercept) 1.03232  1.0160  
##  Residual             0.03483  0.1866  
## Number of obs: 120, groups:  id, 12
## 
## Fixed effects:
##              Estimate Std. Error t value
## (Intercept) 20.049705   0.295603   67.83
## x           -0.204096   0.005931  -34.41
## 
## Correlation of Fixed Effects:
##   (Intr)
## x -0.110

How to extract some of the statistics.

fixef(lme.fit)
## (Intercept)           x 
##  20.0497051  -0.2040963
vcov(lme.fit)
## 2 x 2 Matrix of class "dpoMatrix"
##               (Intercept)             x
## (Intercept)  0.0873809165 -0.0001934779
## x           -0.0001934779  0.0000351778
diag(vcov(lme.fit))
##  (Intercept)            x 
## 0.0873809165 0.0000351778
sqrt(diag(vcov(lme.fit)))
## (Intercept)           x 
## 0.295602633 0.005931088
fixef(lme.fit)/sqrt(diag(vcov(lme.fit)))
## (Intercept)           x 
##    67.82654   -34.41128

Testing the regression, fitting a reduced model with slope zero.

lme.fit.red <- lmer(y ~ 1 + (1|id), dat.ex1)
summary(lme.fit.red)
## Linear mixed model fit by REML ['lmerMod']
## Formula: y ~ 1 + (1 | id)
##    Data: dat.ex1
## 
## REML criterion at convergence: 273.6
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.03318 -0.81320  0.07943  0.76815  1.91393 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  id       (Intercept) 0.9942   0.9971  
##  Residual             0.4163   0.6452  
## Number of obs: 120, groups:  id, 12
## 
## Fixed effects:
##             Estimate Std. Error t value
## (Intercept)  18.9272     0.2938   64.42
anova(lme.fit,lme.fit.red)
## refitting model(s) with ML (instead of REML)
## Data: dat.ex1
## Models:
## lme.fit.red: y ~ 1 + (1 | id)
## lme.fit: y ~ x + (1 | id)
##             npar     AIC     BIC   logLik deviance  Chisq Df Pr(>Chisq)    
## lme.fit.red    3 278.920 287.282 -136.460  272.920                         
## lme.fit        4  11.951  23.101   -1.976    3.951 268.97  1  < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Note that the ‘lmer’ function does not return p-values for the fixed effects in the output. See the end of this markdown how to use the ‘lmerTest’ package to add those p-values.

Example 2

A figure of the data.

xyplot(y ~ x|id, data=dat.ex2, type=c("p","r"), layout=c(6,2), as.table=TRUE, xlab="Time", ylab="Response")

We can think of the data being described by a model with a random intercept and a random slope, i.e.

Yij = b0 + Bi + b1xj + Cixj + εij,

where i referes to the subject and xj to the jth time point.

lme.fit <- lmer(y ~ x + (x|id), dat.ex2)
summary(lme.fit)
## Linear mixed model fit by REML ['lmerMod']
## Formula: y ~ x + (x | id)
##    Data: dat.ex2
## 
## REML criterion at convergence: 33.1
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -3.08758 -0.50793 -0.01624  0.54409  2.27667 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev. Corr 
##  id       (Intercept) 0.880584 0.93839       
##           x           0.003622 0.06018  -0.25
##  Residual             0.034004 0.18440       
## Number of obs: 120, groups:  id, 12
## 
## Fixed effects:
##             Estimate Std. Error t value
## (Intercept) 20.00217    0.27332  73.182
## x           -0.17860    0.01834  -9.741
## 
## Correlation of Fixed Effects:
##   (Intr)
## x -0.270

Do we need the random slope?

lme.fit.red <- lmer(y ~ x + (1|id), dat.ex2)
summary(lme.fit.red)
## Linear mixed model fit by REML ['lmerMod']
## Formula: y ~ x + (1 | id)
##    Data: dat.ex2
## 
## REML criterion at convergence: 77
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -3.12097 -0.60125  0.01364  0.51411  2.38342 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  id       (Intercept) 0.83364  0.9130  
##  Residual             0.06472  0.2544  
## Number of obs: 120, groups:  id, 12
## 
## Fixed effects:
##              Estimate Std. Error t value
## (Intercept) 20.002171   0.268304   74.55
## x           -0.178603   0.008086  -22.09
## 
## Correlation of Fixed Effects:
##   (Intr)
## x -0.166
anova(lme.fit,lme.fit.red)
## refitting model(s) with ML (instead of REML)
## Data: dat.ex2
## Models:
## lme.fit.red: y ~ x + (1 | id)
## lme.fit: y ~ x + (x | id)
##             npar    AIC    BIC  logLik deviance  Chisq Df Pr(>Chisq)    
## lme.fit.red    4 76.375 87.525 -34.188   68.375                         
## lme.fit        6 38.068 54.793 -13.034   26.068 42.307  2  6.504e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Note the degrees of freedom - there is also a correlation parameter between the random effects in the full model.

Example 1 continued

The same data as above, but now with a treatment and a control group.

xyplot(y~x|group:id,data=dat.ex1,type=c("p","r"),layout=c(6,2),as.table=TRUE,
       xlab="Time",ylab="Response",strip=strip.custom(var.name=c("id","group")))

We can again think of the data being described by a random intercept model, with possible differences in the average intercept between treatment and control groups:

Yij = b0 + c0I{subject i is in treatment} + Bi + b1xj + εij,

where i referes to the subject and xj to the jth time point, and I{subject i is in treatment} indicates whether subject i is in the treatment or control arm.

lme.fit <- lmer(y ~ group + x + (1|id), data=dat.ex1)
summary(lme.fit)
## Linear mixed model fit by REML ['lmerMod']
## Formula: y ~ group + x + (1 | id)
##    Data: dat.ex1
## 
## REML criterion at convergence: 10.3
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.26018 -0.66584  0.06588  0.66638  2.17797 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  id       (Intercept) 0.93697  0.9680  
##  Residual             0.03483  0.1866  
## Number of obs: 120, groups:  id, 12
## 
## Fixed effects:
##                 Estimate Std. Error t value
## (Intercept)    19.642547   0.397247  49.447
## grouptreatment  0.814317   0.559895   1.454
## x              -0.204096   0.005931 -34.411
## 
## Correlation of Fixed Effects:
##             (Intr) grptrt
## grouptrtmnt -0.705       
## x           -0.082  0.000

Testing for treatment effect:

lme.fit.red <- lmer(y ~ x + (1|id), dat.ex1)
summary(lme.fit.red)
## Linear mixed model fit by REML ['lmerMod']
## Formula: y ~ x + (1 | id)
##    Data: dat.ex1
## 
## REML criterion at convergence: 13
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.25586 -0.67363  0.06082  0.65787  2.17048 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  id       (Intercept) 1.03232  1.0160  
##  Residual             0.03483  0.1866  
## Number of obs: 120, groups:  id, 12
## 
## Fixed effects:
##              Estimate Std. Error t value
## (Intercept) 20.049705   0.295603   67.83
## x           -0.204096   0.005931  -34.41
## 
## Correlation of Fixed Effects:
##   (Intr)
## x -0.110
anova(lme.fit,lme.fit.red)
## refitting model(s) with ML (instead of REML)
## Data: dat.ex1
## Models:
## lme.fit.red: y ~ x + (1 | id)
## lme.fit: y ~ group + x + (1 | id)
##             npar    AIC    BIC  logLik deviance  Chisq Df Pr(>Chisq)
## lme.fit.red    4 11.951 23.101 -1.9757   3.9514                     
## lme.fit        5 11.649 25.586 -0.8244   1.6488 2.3026  1     0.1292

Example 2 continued

The same data as above, but now with a treatment and a control group.

xyplot(y ~ x|group:id, data=dat.ex2, type=c("p","r"), layout=c(6,2), as.table=TRUE,
       xlab="Time", ylab="Response", strip=strip.custom(var.name=c("id","group")))

We can think of the data being described by a model with a random intercept and a random slope, with possible differences in the average intercept and slope between treatment and control groups.

Yij = b0 + c0I{subject i is in treatment} + Bi + b1xj + c1I{subject i is in treatment}xj + Cixj + εij,

where i referes to the subject and xj to the jth time point, and I{subject i is in treatment} indicates whether subject i is in the treatment or control arm.

lme.fit <- lmer(y ~ group*x + (x|id), dat.ex2)
summary(lme.fit)
## Linear mixed model fit by REML ['lmerMod']
## Formula: y ~ group * x + (x | id)
##    Data: dat.ex2
## 
## REML criterion at convergence: 34.6
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -3.04693 -0.51271 -0.02664  0.59745  2.25953 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev. Corr 
##  id       (Intercept) 0.803667 0.89648       
##           x           0.003463 0.05885  -0.11
##  Residual             0.034005 0.18440       
## Number of obs: 120, groups:  id, 12
## 
## Fixed effects:
##                  Estimate Std. Error t value
## (Intercept)      19.62970    0.36958  53.114
## grouptreatment    0.74493    0.52267   1.425
## x                -0.15696    0.02542  -6.176
## grouptreatment:x -0.04329    0.03594  -1.204
## 
## Correlation of Fixed Effects:
##             (Intr) grptrt x     
## grouptrtmnt -0.707              
## x           -0.145  0.102       
## grptrtmnt:x  0.102 -0.145 -0.707

Testing for differences in intercepts:

lme.fit.red <- lmer(y ~ x + group:x + (x|id), dat.ex2)
summary(lme.fit.red)
## Linear mixed model fit by REML ['lmerMod']
## Formula: y ~ x + group:x + (x | id)
##    Data: dat.ex2
## 
## REML criterion at convergence: 37.1
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -3.05343 -0.51777 -0.02044  0.58514  2.26374 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev. Corr 
##  id       (Intercept) 0.880517 0.93836       
##           x           0.003471 0.05891  -0.12
##  Residual             0.034005 0.18440       
## Number of obs: 120, groups:  id, 12
## 
## Fixed effects:
##                  Estimate Std. Error t value
## (Intercept)      20.00217    0.27331  73.185
## x                -0.16066    0.02529  -6.352
## x:grouptreatment -0.03588    0.03556  -1.009
## 
## Correlation of Fixed Effects:
##             (Intr) x     
## x           -0.108       
## x:grptrtmnt  0.000 -0.703
anova(lme.fit,lme.fit.red)
## refitting model(s) with ML (instead of REML)
## Data: dat.ex2
## Models:
## lme.fit.red: y ~ x + group:x + (x | id)
## lme.fit: y ~ group * x + (x | id)
##             npar    AIC    BIC  logLik deviance  Chisq Df Pr(>Chisq)
## lme.fit.red    7 39.094 58.606 -12.547   25.094                     
## lme.fit        8 38.875 61.175 -11.437   22.875 2.2192  1     0.1363

Testing for differences in slopes:

lme.fit.red <- lmer(y ~ group + x + (x|id), dat.ex2)
summary(lme.fit.red)
## Linear mixed model fit by REML ['lmerMod']
## Formula: y ~ group + x + (x | id)
##    Data: dat.ex2
## 
## REML criterion at convergence: 31.2
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -3.07811 -0.51819 -0.01486  0.55877  2.28594 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev. Corr 
##  id       (Intercept) 0.804338 0.89685       
##           x           0.003622 0.06018  -0.12
##  Residual             0.034005 0.18440       
## Number of obs: 120, groups:  id, 12
## 
## Fixed effects:
##                Estimate Std. Error t value
## (Intercept)    19.67526    0.36771  53.507
## grouptreatment  0.65382    0.51715   1.264
## x              -0.17860    0.01834  -9.741
## 
## Correlation of Fixed Effects:
##             (Intr) grptrt
## grouptrtmnt -0.703       
## x           -0.105  0.000
anova(lme.fit,lme.fit.red)
## refitting model(s) with ML (instead of REML)
## Data: dat.ex2
## Models:
## lme.fit.red: y ~ group + x + (x | id)
## lme.fit: y ~ group * x + (x | id)
##             npar    AIC    BIC  logLik deviance  Chisq Df Pr(>Chisq)
## lme.fit.red    7 38.500 58.013 -12.250   24.500                     
## lme.fit        8 38.875 61.175 -11.437   22.875 1.6256  1     0.2023

Testing jointly for differences in intercepts and slopes:

lme.fit.red <- lmer(y ~ x + (x|id), dat.ex2)
summary(lme.fit.red)
## Linear mixed model fit by REML ['lmerMod']
## Formula: y ~ x + (x | id)
##    Data: dat.ex2
## 
## REML criterion at convergence: 33.1
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -3.08758 -0.50793 -0.01624  0.54409  2.27667 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev. Corr 
##  id       (Intercept) 0.880584 0.93839       
##           x           0.003622 0.06018  -0.25
##  Residual             0.034004 0.18440       
## Number of obs: 120, groups:  id, 12
## 
## Fixed effects:
##             Estimate Std. Error t value
## (Intercept) 20.00217    0.27332  73.182
## x           -0.17860    0.01834  -9.741
## 
## Correlation of Fixed Effects:
##   (Intr)
## x -0.270
anova(lme.fit,lme.fit.red)
## refitting model(s) with ML (instead of REML)
## Data: dat.ex2
## Models:
## lme.fit.red: y ~ x + (x | id)
## lme.fit: y ~ group * x + (x | id)
##             npar    AIC    BIC  logLik deviance  Chisq Df Pr(>Chisq)
## lme.fit.red    6 38.068 54.793 -13.034   26.068                     
## lme.fit        8 38.875 61.175 -11.437   22.875 3.1936  2     0.2025

Example 3

An example with large differences in slopes, comparing treatment and control group.

xyplot(y ~ x|group:id, data=dat.ex3, type=c("p","r"), layout=c(6,2),as.table=TRUE, 
       xlab="Time" ,ylab="Response", strip=strip.custom(var.name=c("id","group")))

As above, we can think of the data being described by a model with a random intercept and a random slope, with possible differences in the average intercept and slope between treatment and control groups.

Yij = b0 + c0I{subject i is in treatment} + Bi + b1xj + c1I{subject i is in treatment}xj + Cixj + εij,

where i referes to the subject and xj to the jth time point, and I{subject i is in treatment} indicates whether subject i is in the treatment or control arm.

lme.fit <- lmer(y ~ group*x + (x|id), dat.ex3)
summary(lme.fit)
## Linear mixed model fit by REML ['lmerMod']
## Formula: y ~ group * x + (x | id)
##    Data: dat.ex3
## 
## REML criterion at convergence: 54.4
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.22100 -0.63843  0.02772  0.70797  2.01538 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev. Corr 
##  id       (Intercept) 0.874956 0.9354        
##           x           0.001376 0.0371   -0.24
##  Residual             0.044818 0.2117        
## Number of obs: 120, groups:  id, 12
## 
## Fixed effects:
##                  Estimate Std. Error t value
## (Intercept)      19.67384    0.38641  50.915
## grouptreatment    0.76479    0.54646   1.400
## x                -0.06544    0.01789  -3.658
## grouptreatment:x -0.23799    0.02530  -9.408
## 
## Correlation of Fixed Effects:
##             (Intr) grptrt x     
## grouptrtmnt -0.707              
## x           -0.271  0.192       
## grptrtmnt:x  0.192 -0.271 -0.707

Testing for differences in intercepts:

lme.fit.red <- lmer(y ~ x + group:x + (x|id), dat.ex3)
summary(lme.fit.red)
## Linear mixed model fit by REML ['lmerMod']
## Formula: y ~ x + group:x + (x | id)
##    Data: dat.ex3
## 
## REML criterion at convergence: 57
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.18712 -0.63842  0.02218  0.71625  2.03571 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev. Corr 
##  id       (Intercept) 0.953040 0.97624       
##           x           0.001389 0.03727  -0.25
##  Residual             0.044818 0.21170       
## Number of obs: 120, groups:  id, 12
## 
## Fixed effects:
##                  Estimate Std. Error t value
## (Intercept)      20.05623    0.28489  70.400
## x                -0.07023    0.01759  -3.994
## x:grouptreatment -0.22840    0.02435  -9.380
## 
## Correlation of Fixed Effects:
##             (Intr) x     
## x           -0.203       
## x:grptrtmnt  0.000 -0.692
anova(lme.fit,lme.fit.red)
## refitting model(s) with ML (instead of REML)
## Data: dat.ex3
## Models:
## lme.fit.red: y ~ x + group:x + (x | id)
## lme.fit: y ~ group * x + (x | id)
##             npar    AIC    BIC  logLik deviance  Chisq Df Pr(>Chisq)
## lme.fit.red    7 57.506 77.019 -21.753   43.506                     
## lme.fit        8 57.360 79.660 -20.680   41.360 2.1464  1     0.1429

Testing for differences in slopes:

lme.fit.red <- lmer(y ~ x + group + (x|id), dat.ex3)
summary(lme.fit.red)
## Linear mixed model fit by REML ['lmerMod']
## Formula: y ~ x + group + (x | id)
##    Data: dat.ex3
## 
## REML criterion at convergence: 74
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.07830 -0.66540 -0.03376  0.69802  2.07654 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev. Corr 
##  id       (Intercept) 1.39784  1.1823        
##           x           0.01665  0.1290   -0.64
##  Residual             0.04482  0.2117        
## Number of obs: 120, groups:  id, 12
## 
## Fixed effects:
##                Estimate Std. Error t value
## (Intercept)    20.37011    0.43291  47.054
## x              -0.18443    0.03785  -4.873
## grouptreatment -0.62775    0.52604  -1.193
## 
## Correlation of Fixed Effects:
##             (Intr) x     
## x           -0.512       
## grouptrtmnt -0.608  0.000
anova(lme.fit,lme.fit.red)
## refitting model(s) with ML (instead of REML)
## Data: dat.ex3
## Models:
## lme.fit.red: y ~ x + group + (x | id)
## lme.fit: y ~ group * x + (x | id)
##             npar    AIC    BIC  logLik deviance  Chisq Df Pr(>Chisq)    
## lme.fit.red    7 82.811 102.32 -34.405   68.811                         
## lme.fit        8 57.360  79.66 -20.680   41.360 27.451  1  1.611e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Testing jointly for differences in intercepts and slopes:

lme.fit.red <- lmer(y ~ x + (x|id), dat.ex3)
summary(lme.fit.red)
## Linear mixed model fit by REML ['lmerMod']
## Formula: y ~ x + (x | id)
##    Data: dat.ex3
## 
## REML criterion at convergence: 74.7
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.10794 -0.64942 -0.02818  0.69732  2.06154 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev. Corr 
##  id       (Intercept) 0.95302  0.9762        
##           x           0.01665  0.1290   -0.45
##  Residual             0.04482  0.2117        
## Number of obs: 120, groups:  id, 12
## 
## Fixed effects:
##             Estimate Std. Error t value
## (Intercept) 20.05623    0.28489  70.400
## x           -0.18443    0.03785  -4.873
## 
## Correlation of Fixed Effects:
##   (Intr)
## x -0.463
anova(lme.fit,lme.fit.red)
## refitting model(s) with ML (instead of REML)
## Data: dat.ex3
## Models:
## lme.fit.red: y ~ x + (x | id)
## lme.fit: y ~ group * x + (x | id)
##             npar    AIC    BIC  logLik deviance  Chisq Df Pr(>Chisq)    
## lme.fit.red    6 80.983 97.708 -34.491   68.983                         
## lme.fit        8 57.360 79.660 -20.680   41.360 27.623  2  1.004e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Linear mixed effects: longitudinal data analysis with lmerTest

The ‘lmerTest’ package overwrites the ‘lmer’ function in the ‘lme4’ package, and adds p-values to the fixed effects in the output.

library(lmerTest)
## 
## Attaching package: 'lmerTest'
## The following object is masked from 'package:lme4':
## 
##     lmer
## The following object is masked from 'package:stats':
## 
##     step

Example 1

xyplot(y ~ x|group:id, data=dat.ex1, type=c("p","r"), layout=c(6,2), as.table=TRUE,
       xlab="Time", ylab="Response", strip=strip.custom(var.name=c("id","group")))

lme.fit <- lmer(y ~ group + x + (1|id), data=dat.ex1)
summary(lme.fit)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method ['lmerModLmerTest']
## Formula: y ~ group + x + (1 | id)
##    Data: dat.ex1
## 
## REML criterion at convergence: 10.3
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.26018 -0.66584  0.06588  0.66638  2.17797 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  id       (Intercept) 0.93697  0.9680  
##  Residual             0.03483  0.1866  
## Number of obs: 120, groups:  id, 12
## 
## Fixed effects:
##                  Estimate Std. Error         df t value Pr(>|t|)    
## (Intercept)     19.642547   0.397247  10.136199  49.447 2.02e-13 ***
## grouptreatment   0.814317   0.559895  10.000000   1.454    0.176    
## x               -0.204096   0.005931 107.000000 -34.411  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) grptrt
## grouptrtmnt -0.705       
## x           -0.082  0.000

Example 2

xyplot(y ~ x|group:id, data=dat.ex2, type=c("p","r"), layout=c(6,2), as.table=TRUE,
       xlab="Time", ylab="Response", strip=strip.custom(var.name=c("id","group")))

lme.fit <- lmer(y ~ group*x + (x|id), dat.ex2)
summary(lme.fit)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method ['lmerModLmerTest']
## Formula: y ~ group * x + (x | id)
##    Data: dat.ex2
## 
## REML criterion at convergence: 34.6
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -3.04693 -0.51271 -0.02664  0.59745  2.25953 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev. Corr 
##  id       (Intercept) 0.803667 0.89648       
##           x           0.003463 0.05885  -0.11
##  Residual             0.034005 0.18440       
## Number of obs: 120, groups:  id, 12
## 
## Fixed effects:
##                  Estimate Std. Error       df t value Pr(>|t|)    
## (Intercept)      19.62970    0.36958 10.00011  53.114 1.35e-13 ***
## grouptreatment    0.74493    0.52267 10.00011   1.425 0.184538    
## x                -0.15696    0.02542  9.99973  -6.176 0.000105 ***
## grouptreatment:x -0.04329    0.03594  9.99973  -1.204 0.256145    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) grptrt x     
## grouptrtmnt -0.707              
## x           -0.145  0.102       
## grptrtmnt:x  0.102 -0.145 -0.707

Example 3

xyplot(y ~ x|group:id, data=dat.ex3, type=c("p","r"), layout=c(6,2), as.table=TRUE,
       xlab="Time", ylab="Response", strip=strip.custom(var.name=c("id","group")))

lme.fit <- lmer(y ~ group*x + (x|id), dat.ex3)
summary(lme.fit)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method ['lmerModLmerTest']
## Formula: y ~ group * x + (x | id)
##    Data: dat.ex3
## 
## REML criterion at convergence: 54.4
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.22100 -0.63843  0.02772  0.70797  2.01538 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev. Corr 
##  id       (Intercept) 0.874956 0.9354        
##           x           0.001376 0.0371   -0.24
##  Residual             0.044818 0.2117        
## Number of obs: 120, groups:  id, 12
## 
## Fixed effects:
##                  Estimate Std. Error       df t value Pr(>|t|)    
## (Intercept)      19.67384    0.38641 10.00024  50.915 2.06e-13 ***
## grouptreatment    0.76479    0.54646 10.00024   1.400   0.1919    
## x                -0.06544    0.01789  9.99992  -3.658   0.0044 ** 
## grouptreatment:x -0.23799    0.02530  9.99992  -9.408 2.77e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) grptrt x     
## grouptrtmnt -0.707              
## x           -0.271  0.192       
## grptrtmnt:x  0.192 -0.271 -0.707