1 input data

data(classroom, package="WWGbook")
dta <- classroom[,c(11,10,12,4,1,2,3,5,6)]
str(dta)
## 'data.frame':    1190 obs. of  9 variables:
##  $ schoolid: int  1 1 1 1 1 1 1 1 1 1 ...
##  $ classid : int  160 160 160 217 217 217 217 217 217 217 ...
##  $ childid : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ mathgain: int  32 109 56 83 53 65 51 66 88 -7 ...
##  $ sex     : int  1 0 1 0 0 1 0 0 1 0 ...
##  $ minority: int  1 1 1 1 1 1 1 1 1 1 ...
##  $ mathkind: int  448 460 511 449 425 450 452 443 422 480 ...
##  $ ses     : num  0.46 -0.27 -0.03 -0.38 -0.03 0.76 -0.03 0.2 0.64 0.13 ...
##  $ yearstea: num  1 1 1 2 2 2 2 2 2 2 ...
head(dta)
##   schoolid classid childid mathgain sex minority mathkind   ses yearstea
## 1        1     160       1       32   1        1      448  0.46        1
## 2        1     160       2      109   0        1      460 -0.27        1
## 3        1     160       3       56   1        1      511 -0.03        1
## 4        1     217       4       83   0        1      449 -0.38        2
## 5        1     217       5       53   0        1      425 -0.03        2
## 6        1     217       6       65   1        1      450  0.76        2
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
dta <- dta %>% 
  mutate(mathkind_a = mean(mathkind)) %>%
  group_by(schoolid) %>% 
  mutate(mathkind_sa=mean(mathkind) - mathkind_a,
         mathkind_c=mathkind - mean(mathkind))

m0 <- lme4::lmer(mathgain ~ (1 | schoolid), data=dta)
sjPlot::tab_model(m0, show.p=FALSE, show.r2=FALSE)
  mathgain
Predictors Estimates CI
(Intercept) 57.58 54.76 – 60.41
Random Effects
σ2 1094.00
τ00 schoolid 109.24
ICC 0.09
N schoolid 107
Observations 1190
library(lmerTest)
## Loading required package: lme4
## Loading required package: Matrix
## 
## Attaching package: 'lmerTest'
## The following object is masked from 'package:lme4':
## 
##     lmer
## The following object is masked from 'package:stats':
## 
##     step
m1 <- lmer(mathgain ~ 1 + (1 | schoolid/classid), data=dta)
sjPlot::tab_model(m0, m1, show.p=FALSE, show.r2=FALSE, show.obs=FALSE, show.ngroups=FALSE, show.se=TRUE, show.ci=FALSE)
  mathgain mathgain
Predictors Estimates std. Error Estimates std. Error
(Intercept) 57.58 1.44 57.43 1.44
Random Effects
σ2 1094.00 1028.23
τ00 109.24 schoolid 99.22 classid:schoolid
  77.50 schoolid
ICC 0.09 0.15
m2 <- lme4::lmer(mathgain ~ mathkind_sa + (1 | schoolid), data=dta)
sjPlot::tab_model(m0, m2, show.p=FALSE, show.r2=FALSE, show.obs=FALSE, show.ngroups=FALSE, show.se=TRUE, show.ci=FALSE)
  mathgain mathgain
Predictors Estimates std. Error Estimates std. Error
(Intercept) 57.58 1.44 57.36 1.30
mathkind_sa -0.29 0.06
Random Effects
σ2 1094.00 1092.20
τ00 109.24 schoolid 70.89 schoolid
ICC 0.09 0.06
m3L0 <- lmer(mathgain ~ 1 + (1 | schoolid/classid), data=dta)
sjPlot::tab_model(m0, m3L0, show.p=FALSE, show.r2=FALSE, show.obs=FALSE, show.ngroups=FALSE, show.se=TRUE, show.ci=FALSE)
  mathgain mathgain
Predictors Estimates std. Error Estimates std. Error
(Intercept) 57.58 1.44 57.43 1.44
Random Effects
σ2 1094.00 1028.23
τ00 109.24 schoolid 99.22 classid:schoolid
  77.50 schoolid
ICC 0.09 0.15
m3L3 <- lmer(mathgain ~ mathkind_c + mathkind_sa + minority + (1 | schoolid) + (1| classid:schoolid), data=dta)
sjPlot::tab_model(m3L0, m3L3, show.p=FALSE, show.r2=FALSE, show.obs=FALSE, show.ngroups=FALSE, show.se=TRUE, show.ci=FALSE)
  mathgain mathgain
Predictors Estimates std. Error Estimates std. Error
(Intercept) 57.43 1.44 63.33 2.11
mathkind_c -0.47 0.02
mathkind_sa -0.36 0.06
minority -8.98 2.38
Random Effects
σ2 1028.23 745.50
τ00 99.22 classid:schoolid 86.63 classid:schoolid
77.50 schoolid 69.05 schoolid
ICC 0.15 0.17
m4 <- lmer(mathgain ~ mathkind_c + mathkind_sa + minority + sex + ses + (1 | schoolid) + (1| classid:schoolid), data=dta)
sjPlot::tab_model(m3L0, m4, show.p=FALSE, show.r2=FALSE, show.obs=FALSE, show.ngroups=FALSE, show.se=TRUE, show.ci=FALSE)
  mathgain mathgain
Predictors Estimates std. Error Estimates std. Error
(Intercept) 57.43 1.44 63.03 2.27
mathkind_c -0.48 0.02
mathkind_sa -0.38 0.06
minority -7.41 2.40
sex -1.23 1.66
ses 5.28 1.24
Random Effects
σ2 1028.23 734.67
τ00 99.22 classid:schoolid 82.90 classid:schoolid
77.50 schoolid 73.09 schoolid
ICC 0.15 0.18