library(lme4)
library(lmerTest)
library(performance)
library(ggeffects)
library(ggplot2)
library(dplyr)
library(jomo) #Imputation
library(mitml) #Imputation, running and pooling the results
library(MLMusingR) #Missing data analysis
library(mice)
library(psych)
library(readr)
data <-read_csv("~/Desktop/HLM/HW/final project/data_clean.csv",locale = locale(encoding = "big5"))
data_mc<-data[,c(1:14,16:23,25:33)]
# 資料框每欄的遺漏值數量
missing_per_column <- apply(data_mc, 2, function(column) sum(is.na(column)))
missing_per_column
## PseudoID grade school_id city post name sex t1
## 0 0 0 0 0 39 0 22
## t2 t3 t4 t5 t6 t7 c1 c2
## 28 34 18 27 18 25 7 16
## c3 c4 c5 c6 c7 c8 m1 m2
## 20 29 28 28 44 25 20 14
## m3 m4 m5 m6 m7 m8 m9
## 20 20 27 32 47 42 18
data2 <- na.omit(data_mc)
data2$studytime_school <- clusterMeans (data2$t1, data2$school_id)
data2$studytime_cwc<-data2$t1-data2$studytime_school
data2$playtime_m<-(data2$t2+data2$t3+data2$t4)/3
data2$playtime_school <- clusterMeans (data2$playtime_m, data2$school_id)
data2$playtime_cwc<-data2$playtime_m-data2$playtime_school
data2$timeplan_m<-(data2$t5+data2$t6+data2$t7)/3
data2$timeplan_school <- clusterMeans (data2$timeplan_m, data2$school_id)
data2$timeplan_cwc<-data2$timeplan_m-data2$timeplan_school
data2$nettime_m<-(data2$t3+data2$t4)/2
data2$nettime_school <- clusterMeans (data2$nettime_m, data2$school_id)
data2$nettime_cwc<-data2$nettime_m-data2$nettime_school
data2$efficacy_math<-(data2$m1+data2$m2+data2$m3+data2$m4+data2$m5+data2$m6+data2$m7+data2$m8+data2$m9)/9
data2$efficacy_chinese<-(data2$c1+data2$c2+data2$c3+data2$c4+data2$c5+data2$c6+data2$c7+data2$c8)/8
data2$grade<-as.factor(data2$grade)
data2$city<-as.factor(data2$city)
data2$sex<-as.factor(data2$sex)
#math
model0 <- lmer(efficacy_math ~ 1 + (1|school_id),data = data2,REML = FALSE)
summary(model0)
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
## method [lmerModLmerTest]
## Formula: efficacy_math ~ 1 + (1 | school_id)
## Data: data2
##
## AIC BIC logLik deviance df.resid
## 7804.7 7822.8 -3899.4 7798.7 3067
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -2.76247 -0.69065 -0.06463 0.68862 2.82989
##
## Random effects:
## Groups Name Variance Std.Dev.
## school_id (Intercept) 0.06864 0.2620
## Residual 0.71013 0.8427
## Number of obs: 3070, groups: school_id, 103
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) 2.89909 0.03023 101.50737 95.9 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
icc(model0)
## # Intraclass Correlation Coefficient
##
## Adjusted ICC: 0.088
## Unadjusted ICC: 0.088
model1 <- lmer(efficacy_math ~ 1 + studytime_cwc +nettime_cwc+timeplan_cwc+(1|school_id),data = data2,REML = FALSE)
summary(model1)
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
## method [lmerModLmerTest]
## Formula: efficacy_math ~ 1 + studytime_cwc + nettime_cwc + timeplan_cwc +
## (1 | school_id)
## Data: data2
##
## AIC BIC logLik deviance df.resid
## 7651.3 7687.5 -3819.7 7639.3 3064
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -3.02335 -0.68509 -0.09999 0.69461 3.02881
##
## Random effects:
## Groups Name Variance Std.Dev.
## school_id (Intercept) 0.06996 0.2645
## Residual 0.67299 0.8204
## Number of obs: 3070, groups: school_id, 103
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) 2.89932 0.03023 101.57317 95.894 < 2e-16 ***
## studytime_cwc 0.03509 0.01248 2967.25388 2.813 0.00495 **
## nettime_cwc -0.01256 0.01015 2967.25388 -1.237 0.21613
## timeplan_cwc 0.18173 0.01641 2967.25388 11.078 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) stdyt_ nttm_c
## studytm_cwc 0.000
## nettime_cwc 0.000 0.186
## timepln_cwc 0.000 -0.223 0.085
model2 <- lmer(efficacy_math ~ 1 + studytime_cwc + nettime_cwc + timeplan_cwc
+ sex + studytime_school + nettime_school + grade +city+ (1 | school_id), data = data2, REML = F)
summary(model2)
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
## method [lmerModLmerTest]
## Formula: efficacy_math ~ 1 + studytime_cwc + nettime_cwc + timeplan_cwc +
## sex + studytime_school + nettime_school + grade + city +
## (1 | school_id)
## Data: data2
##
## AIC BIC logLik deviance df.resid
## 7459.8 7532.2 -3717.9 7435.8 3058
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -3.4449 -0.6670 -0.0664 0.6908 3.2749
##
## Random effects:
## Groups Name Variance Std.Dev.
## school_id (Intercept) 0.02314 0.1521
## Residual 0.64413 0.8026
## Number of obs: 3070, groups: school_id, 103
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) 2.72135 0.31568 108.91791 8.621 5.89e-14 ***
## studytime_cwc 0.03561 0.01221 2969.99462 2.917 0.00356 **
## nettime_cwc -0.04863 0.01036 2989.45459 -4.693 2.81e-06 ***
## timeplan_cwc 0.19143 0.01607 2970.60467 11.913 < 2e-16 ***
## sex2 -0.38294 0.03127 3060.39979 -12.245 < 2e-16 ***
## studytime_school 0.23443 0.05206 105.16225 4.503 1.74e-05 ***
## nettime_school -0.07864 0.06010 102.84215 -1.308 0.19363
## grade2 -0.18910 0.06158 108.41871 -3.071 0.00270 **
## grade3 -0.26963 0.06555 105.28312 -4.113 7.76e-05 ***
## city2 -0.05041 0.04564 101.09197 -1.104 0.27201
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) stdytm_c nttm_c tmpln_ sex2 stdytm_s nttm_s grade2 grade3
## studytm_cwc 0.000
## nettime_cwc -0.031 0.178
## timepln_cwc 0.005 -0.223 0.068
## sex2 -0.108 -0.003 0.284 -0.049
## stdytm_schl -0.885 0.000 0.014 -0.002 0.048
## nettim_schl -0.856 0.000 0.015 -0.003 0.052 0.549
## grade2 0.269 0.000 0.000 0.000 -0.001 -0.129 -0.507
## grade3 0.467 0.000 0.002 0.000 0.006 -0.401 -0.564 0.642
## city2 -0.255 0.000 0.004 -0.001 0.016 0.312 0.043 0.007 -0.141
confint(model2, oldNames = F)
## 2.5 % 97.5 %
## sd_(Intercept)|school_id 0.11305021 0.19536473
## sigma 0.78259336 0.82342947
## (Intercept) 2.09638950 3.34487244
## studytime_cwc 0.01167792 0.05954214
## nettime_cwc -0.06895313 -0.02831462
## timeplan_cwc 0.15992842 0.22293984
## sex2 -0.44440994 -0.32146313
## studytime_school 0.13165832 0.33763067
## nettime_school -0.19760440 0.04019172
## grade2 -0.31093377 -0.06738260
## grade3 -0.39931430 -0.14000193
## city2 -0.14054874 0.04008761
model3 <- lmer(efficacy_math ~ 1 + studytime_cwc + nettime_cwc + timeplan_cwc
+ sex + studytime_school + nettime_school + grade + (1 | school_id), data = data2, REML = F)
summary(model3)
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
## method [lmerModLmerTest]
## Formula: efficacy_math ~ 1 + studytime_cwc + nettime_cwc + timeplan_cwc +
## sex + studytime_school + nettime_school + grade + (1 | school_id)
## Data: data2
##
## AIC BIC logLik deviance df.resid
## 7459.1 7525.4 -3718.5 7437.1 3059
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -3.4387 -0.6658 -0.0684 0.6963 3.2565
##
## Random effects:
## Groups Name Variance Std.Dev.
## school_id (Intercept) 0.02378 0.1542
## Residual 0.64409 0.8025
## Number of obs: 3070, groups: school_id, 103
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) 2.63224 0.30732 109.66598 8.565 7.52e-14 ***
## studytime_cwc 0.03561 0.01221 2970.45491 2.917 0.00356 **
## nettime_cwc -0.04856 0.01036 2989.74907 -4.687 2.90e-06 ***
## timeplan_cwc 0.19141 0.01607 2971.05967 11.912 < 2e-16 ***
## sex2 -0.38218 0.03128 3060.81664 -12.219 < 2e-16 ***
## studytime_school 0.25242 0.04981 106.87535 5.068 1.70e-06 ***
## nettime_school -0.07581 0.06047 103.13714 -1.254 0.21276
## grade2 -0.18863 0.06200 109.02089 -3.042 0.00294 **
## grade3 -0.27979 0.06535 104.07381 -4.281 4.15e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) stdytm_c nttm_c tmpln_ sex2 stdytm_s nttm_s grade2
## studytm_cwc 0.000
## nettime_cwc -0.030 0.178
## timepln_cwc 0.005 -0.223 0.068
## sex2 -0.107 -0.003 0.284 -0.049
## stdytm_schl -0.877 0.000 0.013 -0.002 0.045
## nettim_schl -0.875 0.000 0.014 -0.003 0.051 0.564
## grade2 0.281 0.000 0.000 0.000 -0.001 -0.138 -0.509
## grade3 0.450 0.000 0.002 0.000 0.008 -0.379 -0.564 0.649
confint(model3, oldNames = F)
## 2.5 % 97.5 %
## sd_(Intercept)|school_id 0.11531548 0.19744154
## sigma 0.78256633 0.82339778
## (Intercept) 2.02421006 3.23953431
## studytime_cwc 0.01167777 0.05954024
## nettime_cwc -0.06888082 -0.02824435
## timeplan_cwc 0.15991041 0.22291950
## sex2 -0.44364179 -0.32072023
## studytime_school 0.15406509 0.35108994
## nettime_school -0.19554862 0.04370013
## grade2 -0.31121664 -0.06601240
## grade3 -0.40898352 -0.15041937
anova(model2,model3)
## Data: data2
## Models:
## model3: efficacy_math ~ 1 + studytime_cwc + nettime_cwc + timeplan_cwc + sex + studytime_school + nettime_school + grade + (1 | school_id)
## model2: efficacy_math ~ 1 + studytime_cwc + nettime_cwc + timeplan_cwc + sex + studytime_school + nettime_school + grade + city + (1 | school_id)
## npar AIC BIC logLik deviance Chisq Df Pr(>Chisq)
## model3 11 7459.1 7525.4 -3718.5 7437.1
## model2 12 7459.8 7532.2 -3717.9 7435.8 1.2096 1 0.2714
anova(model1,model3)
## Data: data2
## Models:
## model1: efficacy_math ~ 1 + studytime_cwc + nettime_cwc + timeplan_cwc + (1 | school_id)
## model3: efficacy_math ~ 1 + studytime_cwc + nettime_cwc + timeplan_cwc + sex + studytime_school + nettime_school + grade + (1 | school_id)
## npar AIC BIC logLik deviance Chisq Df Pr(>Chisq)
## model1 6 7651.3 7687.5 -3819.7 7639.3
## model3 11 7459.1 7525.4 -3718.5 7437.1 202.26 5 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#chinese
model0_c <- lmer(efficacy_chinese ~ 1 + (1|school_id),data = data2,REML = FALSE)
summary(model0_c)
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
## method [lmerModLmerTest]
## Formula: efficacy_chinese ~ 1 + (1 | school_id)
## Data: data2
##
## AIC BIC logLik deviance df.resid
## 7766.3 7784.4 -3880.2 7760.3 3067
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -3.00779 -0.66840 -0.03309 0.66431 2.67908
##
## Random effects:
## Groups Name Variance Std.Dev.
## school_id (Intercept) 0.05535 0.2353
## Residual 0.70478 0.8395
## Number of obs: 3070, groups: school_id, 103
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) 3.08734 0.02796 99.82257 110.4 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
icc(model0_c)
## # Intraclass Correlation Coefficient
##
## Adjusted ICC: 0.073
## Unadjusted ICC: 0.073
model1_c <- lmer(efficacy_chinese ~ 1 + studytime_cwc +nettime_cwc+timeplan_cwc+(1|school_id),data = data2,REML = FALSE)
summary(model1_c)
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
## method [lmerModLmerTest]
## Formula: efficacy_chinese ~ 1 + studytime_cwc + nettime_cwc + timeplan_cwc +
## (1 | school_id)
## Data: data2
##
## AIC BIC logLik deviance df.resid
## 7454.2 7490.4 -3721.1 7442.2 3064
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -3.1890 -0.6369 -0.0275 0.6717 3.0518
##
## Random effects:
## Groups Name Variance Std.Dev.
## school_id (Intercept) 0.0580 0.2408
## Residual 0.6331 0.7957
## Number of obs: 3070, groups: school_id, 103
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) 3.088e+00 2.799e-02 9.996e+01 110.333 < 2e-16 ***
## studytime_cwc 1.800e-02 1.210e-02 2.966e+03 1.487 0.137
## nettime_cwc -5.761e-02 9.849e-03 2.966e+03 -5.849 5.48e-09 ***
## timeplan_cwc 2.458e-01 1.591e-02 2.966e+03 15.450 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) stdyt_ nttm_c
## studytm_cwc 0.000
## nettime_cwc 0.000 0.186
## timepln_cwc 0.000 -0.223 0.085
model2_c <- lmer(efficacy_chinese ~ 1 + studytime_cwc + nettime_cwc + timeplan_cwc
+ sex + studytime_school + nettime_school + grade +city+ (1 | school_id), data = data2, REML = F)
summary(model2_c)
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
## method [lmerModLmerTest]
## Formula: efficacy_chinese ~ 1 + studytime_cwc + nettime_cwc + timeplan_cwc +
## sex + studytime_school + nettime_school + grade + city +
## (1 | school_id)
## Data: data2
##
## AIC BIC logLik deviance df.resid
## 7389.1 7461.4 -3682.5 7365.1 3058
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -3.3746 -0.6429 -0.0150 0.6844 3.1900
##
## Random effects:
## Groups Name Variance Std.Dev.
## school_id (Intercept) 0.01907 0.1381
## Residual 0.63123 0.7945
## Number of obs: 3070, groups: school_id, 103
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) 2.32107 0.29980 109.46484 7.742 5.25e-12 ***
## studytime_cwc 0.01789 0.01208 2970.65864 1.481 0.138805
## nettime_cwc -0.05010 0.01026 2991.75654 -4.885 1.09e-06 ***
## timeplan_cwc 0.24382 0.01591 2971.32296 15.327 < 2e-16 ***
## sex2 0.07969 0.03090 3050.58795 2.579 0.009952 **
## studytime_school 0.19582 0.04940 105.38395 3.964 0.000135 ***
## nettime_school 0.10866 0.05700 102.98650 1.906 0.059411 .
## grade2 -0.39141 0.05848 109.04203 -6.693 9.80e-10 ***
## grade3 -0.50926 0.06221 105.50074 -8.186 6.64e-13 ***
## city2 0.04279 0.04327 101.08653 0.989 0.325121
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) stdytm_c nttm_c tmpln_ sex2 stdytm_s nttm_s grade2 grade3
## studytm_cwc 0.000
## nettime_cwc -0.032 0.178
## timepln_cwc 0.006 -0.223 0.068
## sex2 -0.113 -0.003 0.284 -0.049
## stdytm_schl -0.885 0.000 0.014 -0.002 0.050
## nettim_schl -0.857 0.000 0.015 -0.003 0.054 0.551
## grade2 0.268 0.000 0.000 0.000 -0.001 -0.130 -0.506
## grade3 0.467 0.000 0.002 0.000 0.005 -0.402 -0.564 0.644
## city2 -0.255 0.000 0.005 -0.001 0.017 0.311 0.045 0.005 -0.143
confint(model2_c, oldNames = F)
## 2.5 % 97.5 %
## sd_(Intercept)|school_id 0.098884056 0.18039064
## sigma 0.774716639 0.81513711
## (Intercept) 1.729822115 2.91569478
## studytime_cwc -0.005799464 0.04158286
## nettime_cwc -0.070206711 -0.02999187
## timeplan_cwc 0.212633032 0.27500939
## sex2 0.019110201 0.14027798
## studytime_school 0.097782660 0.29326917
## nettime_school -0.004324493 0.22124506
## grade2 -0.507102528 -0.27583208
## grade3 -0.632214412 -0.38612803
## city2 -0.042985672 0.12828115
model3_c<- lmer(efficacy_chinese ~ 1 + studytime_cwc + nettime_cwc + timeplan_cwc
+ sex + studytime_school + nettime_school + grade + (1 | school_id), data = data2, REML = F)
summary(model3_c)
## Linear mixed model fit by maximum likelihood . t-tests use Satterthwaite's
## method [lmerModLmerTest]
## Formula: efficacy_chinese ~ 1 + studytime_cwc + nettime_cwc + timeplan_cwc +
## sex + studytime_school + nettime_school + grade + (1 | school_id)
## Data: data2
##
## AIC BIC logLik deviance df.resid
## 7388.0 7454.4 -3683.0 7366.0 3059
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -3.3757 -0.6417 -0.0123 0.6835 3.2030
##
## Random effects:
## Groups Name Variance Std.Dev.
## school_id (Intercept) 0.01953 0.1398
## Residual 0.63119 0.7945
## Number of obs: 3070, groups: school_id, 103
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) 2.39710 0.29147 110.30410 8.224 4.26e-13 ***
## studytime_cwc 0.01789 0.01208 2971.15805 1.481 0.138779
## nettime_cwc -0.05015 0.01026 2992.14317 -4.890 1.06e-06 ***
## timeplan_cwc 0.24383 0.01591 2971.81883 15.329 < 2e-16 ***
## sex2 0.07920 0.03090 3050.77653 2.563 0.010425 *
## studytime_school 0.18054 0.04721 107.24384 3.824 0.000221 ***
## nettime_school 0.10610 0.05727 103.30061 1.853 0.066774 .
## grade2 -0.39171 0.05880 109.67064 -6.662 1.12e-09 ***
## grade3 -0.50044 0.06191 104.23622 -8.084 1.19e-12 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr) stdytm_c nttm_c tmpln_ sex2 stdytm_s nttm_s grade2
## studytm_cwc 0.000
## nettime_cwc -0.032 0.178
## timepln_cwc 0.006 -0.223 0.068
## sex2 -0.112 -0.003 0.284 -0.049
## stdytm_schl -0.877 0.000 0.013 -0.002 0.047
## nettim_schl -0.875 0.000 0.015 -0.003 0.053 0.565
## grade2 0.279 0.000 0.000 0.000 -0.001 -0.138 -0.507
## grade3 0.449 0.000 0.002 0.000 0.008 -0.380 -0.564 0.651
confint(model3_c, oldNames = F)
## 2.5 % 97.5 %
## sd_(Intercept)|school_id 0.100773972 0.18197545
## sigma 0.774697149 0.81511311
## (Intercept) 1.822115077 2.97488661
## studytime_cwc -0.005798153 0.04158288
## nettime_cwc -0.070252713 -0.03003845
## timeplan_cwc 0.212646323 0.27502101
## sex2 0.018611907 0.13979585
## studytime_school 0.086907585 0.27368170
## nettime_school -0.007357223 0.21924188
## grade2 -0.508083549 -0.27556112
## grade3 -0.622924380 -0.37799275
anova(model2_c,model3_c)
## Data: data2
## Models:
## model3_c: efficacy_chinese ~ 1 + studytime_cwc + nettime_cwc + timeplan_cwc + sex + studytime_school + nettime_school + grade + (1 | school_id)
## model2_c: efficacy_chinese ~ 1 + studytime_cwc + nettime_cwc + timeplan_cwc + sex + studytime_school + nettime_school + grade + city + (1 | school_id)
## npar AIC BIC logLik deviance Chisq Df Pr(>Chisq)
## model3_c 11 7388.0 7454.4 -3683.0 7366.0
## model2_c 12 7389.1 7461.4 -3682.5 7365.1 0.9712 1 0.3244
anova(model1_c,model3_c)
## Data: data2
## Models:
## model1_c: efficacy_chinese ~ 1 + studytime_cwc + nettime_cwc + timeplan_cwc + (1 | school_id)
## model3_c: efficacy_chinese ~ 1 + studytime_cwc + nettime_cwc + timeplan_cwc + sex + studytime_school + nettime_school + grade + (1 | school_id)
## npar AIC BIC logLik deviance Chisq Df Pr(>Chisq)
## model1_c 6 7454.2 7490.4 -3721.1 7442.2
## model3_c 11 7388.0 7454.4 -3683.0 7366.0 76.143 5 5.372e-15 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1