pacman::p_load(mlmRev, tidyverse, lme4, nlme, irr)

1 Input Data

dta_1 <- read.table("C:/Users/HANK/Desktop/HOMEWORK/demo1.txt", header = T)
dta_2 <- read.table("C:/Users/HANK/Desktop/HOMEWORK/demo2.txt", header = T)
# mean 
summary(dta_1$Score)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   11.00   51.00   55.00   62.11   93.00   97.00
summary(dta_2$Score)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   11.00   51.00   53.00   57.44   91.00   97.00
dta_1 <- dta_1 %>% 
  group_by(School) %>% 
  mutate( ave_Score = mean(Score),
          c_Score = ave_Score - Score)
dta_2 <- dta_2 %>% 
  group_by(School) %>% 
  mutate( ave_Score = mean(Score),
          c_Score = ave_Score - Score)
summary(m0_1 <- lmer(Score ~ (1|School), data = dta_1))
## Linear mixed model fit by REML ['lmerMod']
## Formula: Score ~ (1 | School)
##    Data: dta_1
## 
## 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 = dta_2))
## Linear mixed model fit by REML ['lmerMod']
## Formula: Score ~ (1 | School)
##    Data: dta_2
## 
## 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
summary(m0_1_1 <- lm(Score ~ School:c_Score, data = dta_1))
## 
## Call:
## lm(formula = Score ~ School:c_Score, data = dta_1)
## 
## Residuals:
##       1       2       3       4       5       6       7       8       9 
## -50.111 -50.111  -9.111  -9.111  -9.111  31.889  31.889  31.889  31.889 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)   
## (Intercept)        62.111     14.406   4.311  0.00763 **
## SchoolS1:c_Score   -1.000     30.560  -0.033  0.97516   
## SchoolS2:c_Score   -1.000     15.280  -0.065  0.95036   
## SchoolS3:c_Score   -1.000      9.664  -0.103  0.92161   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 43.22 on 5 degrees of freedom
## Multiple R-squared:  0.003202,   Adjusted R-squared:  -0.5949 
## F-statistic: 0.005354 on 3 and 5 DF,  p-value: 0.9994
summary(m0_2_2 <- lm(Score ~ School:c_Score, data = dta_2))
## 
## Call:
## lm(formula = Score ~ School:c_Score, data = dta_2)
## 
## Residuals:
##       1       2       3       4       5       6       7       8       9 
## -26.444 -26.444  -4.444  -4.444  -4.444  16.556  16.556  16.556  16.556 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       57.4444     7.5340   7.625 0.000617 ***
## SchoolS1:c_Score  -1.0000     0.7991  -1.251 0.266142    
## SchoolS2:c_Score  -1.0000     0.4094  -2.443 0.058462 .  
## SchoolS3:c_Score  -1.0000     0.5131  -1.949 0.108844    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 22.6 on 5 degrees of freedom
## Multiple R-squared:  0.6938, Adjusted R-squared:  0.5101 
## F-statistic: 3.777 on 3 and 5 DF,  p-value: 0.09333
sjPlot::tab_model(m0_1_1, m0_2_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) 62.11 14.41 57.44 7.53
School [S1] : c_Score -1.00 30.56 -1.00 0.80
School [S2] : c_Score -1.00 15.28 -1.00 0.41
School [S3] : c_Score -1.00 9.66 -1.00 0.51

2 The end