Two data sets are generated for you to investigate how the estimate of grand mean depend on the intra-class correlation in a simple random-effects model.

Column 1: Score Column 2: School ID Column 3: Student ID

1.資料管理

#載入套件
pacman::p_load(tidyverse, lme4, merTools, dplyr)
#載入資料
setwd("C:/Users/Username/Desktop/111-1/Multilevel Analysis/HomeWork/0924/Q4")
demo1 <- read.table("demo1.txt", header = T)
demo2 <- read.table("demo2.txt", header = T)
#查看資料
head(demo1)
##   Score School Pupil
## 1    11     S1    P1
## 2    13     S1    P2
## 3    51     S2    P3
## 4    53     S2    P4
## 5    55     S2    P5
## 6    91     S3    P6
head(demo2)
##   Score School Pupil
## 1    11     S1    P1
## 2    51     S1    P2
## 3    13     S2    P3
## 4    55     S2    P4
## 5    91     S2    P5
## 6    51     S3    P6

2.資料計算

Demo 1展示了學校的變異數為1679,標準差為40.976;Demo 2學校的變異數為104.2,標準差為10.21。

#Model 1
summary(m1 <- lmer(Score ~  1 + (1 | School), data=demo1))
## Linear mixed model fit by REML ['lmerMod']
## Formula: Score ~ 1 + (1 | School)
##    Data: demo1
## 
## 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
#Model 2
summary(m2 <- lmer(Score ~  1 + (1 | School), data=demo2))
## Linear mixed model fit by REML ['lmerMod']
## Formula: Score ~ 1 + (1 | School)
##    Data: demo2
## 
## 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