```{r setup, include=FALSE} knitr::opts_chunk$set(echo=TRUE, fig.align="center") ``` #### 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. ```{r} 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. ```{r} library(lattice) library(Matrix) library(lme4) ``` #### Example 1 A figure of the data. ```{r} 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. ```{r} lme.fit <- lmer(y ~ x + (1|id), data=dat.ex1) summary(lme.fit) ``` How to extract some of the statistics. ```{r} fixef(lme.fit) vcov(lme.fit) diag(vcov(lme.fit)) sqrt(diag(vcov(lme.fit))) fixef(lme.fit)/sqrt(diag(vcov(lme.fit))) ``` Testing the regression, fitting a reduced model with slope zero. ```{r} lme.fit.red <- lmer(y ~ 1 + (1|id), dat.ex1) summary(lme.fit.red) anova(lme.fit,lme.fit.red) ``` 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. ```{r} 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. ```{r} lme.fit <- lmer(y ~ x + (x|id), dat.ex2) summary(lme.fit) ``` Do we need the random slope? ```{r} lme.fit.red <- lmer(y ~ x + (1|id), dat.ex2) summary(lme.fit.red) anova(lme.fit,lme.fit.red) ``` 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. ```{r} 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. ```{r} lme.fit <- lmer(y ~ group + x + (1|id), data=dat.ex1) summary(lme.fit) ``` Testing for treatment effect: ```{r} lme.fit.red <- lmer(y ~ x + (1|id), dat.ex1) summary(lme.fit.red) anova(lme.fit,lme.fit.red) ``` #### Example 2 continued The same data as above, but now with a treatment and a control group. ```{r} 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. ```{r} lme.fit <- lmer(y ~ group*x + (x|id), dat.ex2) summary(lme.fit) ``` Testing for differences in intercepts: ```{r} lme.fit.red <- lmer(y ~ x + group:x + (x|id), dat.ex2) summary(lme.fit.red) anova(lme.fit,lme.fit.red) ``` Testing for differences in slopes: ```{r} lme.fit.red <- lmer(y ~ group + x + (x|id), dat.ex2) summary(lme.fit.red) anova(lme.fit,lme.fit.red) ``` Testing jointly for differences in intercepts and slopes: ```{r} lme.fit.red <- lmer(y ~ x + (x|id), dat.ex2) summary(lme.fit.red) anova(lme.fit,lme.fit.red) ``` #### Example 3 An example with large differences in slopes, comparing treatment and control group. ```{r} 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. ```{r} lme.fit <- lmer(y ~ group*x + (x|id), dat.ex3) summary(lme.fit) ``` Testing for differences in intercepts: ```{r} lme.fit.red <- lmer(y ~ x + group:x + (x|id), dat.ex3) summary(lme.fit.red) anova(lme.fit,lme.fit.red) ``` Testing for differences in slopes: ```{r} lme.fit.red <- lmer(y ~ x + group + (x|id), dat.ex3) summary(lme.fit.red) anova(lme.fit,lme.fit.red) ``` Testing jointly for differences in intercepts and slopes: ```{r} lme.fit.red <- lmer(y ~ x + (x|id), dat.ex3) summary(lme.fit.red) anova(lme.fit,lme.fit.red) ``` ### 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. ```{r} library(lmerTest) ``` #### Example 1 ```{r} 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) ``` #### Example 2 ```{r} 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) ``` #### Example 3 ```{r} 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) ```