# load package
pacman::p_load(mlmRev, tidyverse, lme4, nlme)
# input data
dta1 <- read.table("demo1.txt", header = T)
dta2 <- read.table("demo2.txt", header = T)
# grand mean
summary(dta1$Score)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 11.00 51.00 55.00 62.11 93.00 97.00
summary(dta2$Score)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 11.00 51.00 53.00 57.44 91.00 97.00
# sd for overall Math scores
sd(dta1$Score)
## [1] 34.2215
sd(dta2$Score)
## [1] 32.29207
dta1所有學校平均分數為62.11標準差34.22
dta2所有學校平均分數為57.44標準差32.29
summary(m0_1 <- lmer(Score ~ (1|School), data = dta1))
## Linear mixed model fit by REML ['lmerMod']
## Formula: Score ~ (1 | School)
## Data: dta1
##
## REML criterion at convergence: 51.5
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.3280 -0.4745 0.0000 0.4608 1.3553
##
## Random effects:
## Groups Name Variance Std.Dev.
## School (Intercept) 1679 40.976
## Residual 5 2.236
## Number of obs: 9, groups: School, 3
##
## Fixed effects:
## Estimate Std. Error t value
## (Intercept) 53.01 23.67 2.24
summary(m0_2 <- lmer(Score ~ (1|School), data = dta2))
## Linear mixed model fit by REML ['lmerMod']
## Formula: Score ~ (1 | School)
## Data: dta2
##
## REML criterion at convergence: 80.4
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -1.36734 -0.34286 -0.02776 1.07153 1.13999
##
## Random effects:
## Groups Name Variance Std.Dev.
## School (Intercept) 104.2 10.21
## Residual 967.8 31.11
## Number of obs: 9, groups: School, 3
##
## Fixed effects:
## Estimate Std. Error t value
## (Intercept) 56.36 12.01 4.692
sjPlot::tab_model(m0_1, m0_2, show.p=FALSE, show.r2=FALSE, show.obs=FALSE, show.ngroups=FALSE, show.se=TRUE, show.ci=FALSE)
Score | Score | |||
---|---|---|---|---|
Predictors | Estimates | std. Error | Estimates | std. Error |
(Intercept) | 53.01 | 23.67 | 56.36 | 12.01 |
Random Effects | ||||
σ2 | 5.00 | 967.76 | ||
τ00 | 1679.06 School | 104.17 School | ||
ICC | 1.00 | 0.10 |
dta1:
The estimated mean of scores for all children is 53.01 with as SD of (5+1679.06)^1/2. The correlation between scores of pupil attending the same school is 1.00
dta2:
The estimated mean of scores for all children is 56.36 with as SD of (967.76+104.17)^1/2. The correlation between scores of pupil attending the same school is 0.10
整體而言dta1與dta2學生分數相似,但可以觀察到dta1在同一個學校內的學生分數相近,但dta2則是在同一個學校內的學生分數差異很大。因此,在random effect分析結果表也呈現,dta1學校層級解釋了學生分數大多數的變異量,個人間的變異則很小。dta2則相反,學生分數間差異大,學校間的效果反而小。