1 data input

dta <- read.table("reading_piat.txt", header = T)
head(dta)
##   ID Wave Age_G     Age PIAT
## 1  1    1   6.5  6.0000   18
## 2  1    2   8.5  8.3333   35
## 3  1    3  10.5 10.3333   59
## 4  2    1   6.5  6.0000   18
## 5  2    2   8.5  8.5000   25
## 6  2    3  10.5 10.5833   28
str(dta)
## 'data.frame':    267 obs. of  5 variables:
##  $ ID   : int  1 1 1 2 2 2 3 3 3 4 ...
##  $ Wave : int  1 2 3 1 2 3 1 2 3 1 ...
##  $ Age_G: num  6.5 8.5 10.5 6.5 8.5 10.5 6.5 8.5 10.5 6.5 ...
##  $ Age  : num  6 8.33 10.33 6 8.5 ...
##  $ PIAT : int  18 35 59 18 25 28 18 23 32 18 ...

2 data management

dta$ID <- factor(dta$ID)
dta$Wave <- factor(dta$Wave)
dta$Age2 = dta$Age -6.5 #shifted by 6.5
str(dta)
## 'data.frame':    267 obs. of  6 variables:
##  $ ID   : Factor w/ 89 levels "1","2","3","4",..: 1 1 1 2 2 2 3 3 3 4 ...
##  $ Wave : Factor w/ 3 levels "1","2","3": 1 2 3 1 2 3 1 2 3 1 ...
##  $ Age_G: num  6.5 8.5 10.5 6.5 8.5 10.5 6.5 8.5 10.5 6.5 ...
##  $ Age  : num  6 8.33 10.33 6 8.5 ...
##  $ PIAT : int  18 35 59 18 25 28 18 23 32 18 ...
##  $ Age2 : num  -0.5 1.83 3.83 -0.5 2 ...
head(dta)
##   ID Wave Age_G     Age PIAT    Age2
## 1  1    1   6.5  6.0000   18 -0.5000
## 2  1    2   8.5  8.3333   35  1.8333
## 3  1    3  10.5 10.3333   59  3.8333
## 4  2    1   6.5  6.0000   18 -0.5000
## 5  2    2   8.5  8.5000   25  2.0000
## 6  2    3  10.5 10.5833   28  4.0833

3 ggplot, Eighty-nine children’s scores on PIAT

library(ggplot2)
p <- ggplot(dta, 
            aes(Age2, PIAT)) +
 geom_line(aes(group=ID), alpha=0.5) +
 stat_smooth(aes(group=Age2), 
             method="lm", 
             formula= y ~ x) +
 geom_point(alpha=0.5) +
 labs(x="Age (in years, shifted by 6.5)", 
      y="PIAT score") +
 theme_minimal()
suppressWarnings(suppressMessages(ggplot2:::print.ggplot(p)))

4 m0

library(lme4)
## Loading required package: Matrix
m0 <- lmer(PIAT ~ Age2 + ( 1 |ID), dta)
summary(m0)
## Linear mixed model fit by REML ['lmerMod']
## Formula: PIAT ~ Age2 + (1 | ID)
##    Data: dta
## 
## REML criterion at convergence: 1859
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.04325 -0.56799 -0.05908  0.55574  2.88911 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  ID       (Intercept) 26.73    5.170   
##  Residual             44.10    6.641   
## Number of obs: 267, groups:  ID, 89
## 
## Fixed effects:
##             Estimate Std. Error t value
## (Intercept)  21.0625     0.8439   24.96
## Age2          4.5411     0.2219   20.46
## 
## Correlation of Fixed Effects:
##      (Intr)
## Age2 -0.588
sjPlot::tab_model(m0, show.obs=F, show.ngroups=F, show.r2=FALSE, show.re.var=F,show.icc=F, show.se=T)
  PIAT
Predictors Estimates std. Error CI p
(Intercept) 21.06 0.84 19.41 – 22.72 <0.001
Age2 4.54 0.22 4.11 – 4.98 <0.001

5 m1

m1 <- lmer(PIAT ~ Age2 + ( Age2 |ID), dta)
summary(m1)
## Linear mixed model fit by REML ['lmerMod']
## Formula: PIAT ~ Age2 + (Age2 | ID)
##    Data: dta
## 
## REML criterion at convergence: 1804.1
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.0042 -0.4893 -0.1383  0.4067  3.6892 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev. Corr
##  ID       (Intercept)  5.507   2.347        
##           Age2         3.377   1.838    0.53
##  Residual             27.400   5.235        
## Number of obs: 267, groups:  ID, 89
## 
## Fixed effects:
##             Estimate Std. Error t value
## (Intercept)  21.0621     0.5630   37.41
## Age2          4.5399     0.2622   17.32
## 
## Correlation of Fixed Effects:
##      (Intr)
## Age2 -0.288
sjPlot::tab_model(m1, show.obs=F, show.ngroups=F, show.r2=FALSE, show.re.var=F,show.icc=F, show.se=T)
  PIAT
Predictors Estimates std. Error CI p
(Intercept) 21.06 0.56 19.96 – 22.17 <0.001
Age2 4.54 0.26 4.03 – 5.05 <0.001

6 Compare model

sjPlot::tab_model(m0, m1, show.obs=F, show.ngroups=F, show.r2=T, show.re.var=T,show.icc=T, show.se=T)
  PIAT PIAT
Predictors Estimates std. Error CI p Estimates std. Error CI p
(Intercept) 21.06 0.84 19.41 – 22.72 <0.001 21.06 0.56 19.96 – 22.17 <0.001
Age2 4.54 0.22 4.11 – 4.98 <0.001 4.54 0.26 4.03 – 5.05 <0.001
Random Effects
σ2 44.10 27.40
τ00 26.73 ID 5.51 ID
τ11   3.38 ID.Age2
ρ01   0.53 ID
ICC 0.38 0.62
Marginal R2 / Conditional R2 0.499 / 0.688 0.496 / 0.807
anova(m0,m1)
## refitting model(s) with ML (instead of REML)
## Data: dta
## Models:
## m0: PIAT ~ Age2 + (1 | ID)
## m1: PIAT ~ Age2 + (Age2 | ID)
##    npar    AIC    BIC  logLik deviance  Chisq Df Pr(>Chisq)    
## m0    4 1866.9 1881.3 -929.46   1858.9                         
## m1    6 1815.9 1837.4 -901.95   1803.9 55.021  2  1.128e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

7 The end