dat = read.csv("~/Desktop/R GS Tuấn 2020/TDTU Datasets for 2020 Workshop/imm10.csv", header = T)
head(dat)
##   school id   ses    meanses homework white parented public ratio percmin math
## 1   7472  3 -0.13 -0.4826087        1     1        2      1    19       0   48
## 2   7472  8 -0.39 -0.4826087        0     1        2      1    19       0   48
## 3   7472 13 -0.80 -0.4826087        0     1        2      1    19       0   53
## 4   7472 17 -0.72 -0.4826087        1     1        2      1    19       0   42
## 5   7472 27 -0.74 -0.4826087        2     1        2      1    19       0   43
## 6   7472 28 -0.58 -0.4826087        1     1        2      1    19       0   57
##   sex race sctype cstr scsize urban region schnum
## 1   2    4      1    2      3     2      2      1
## 2   1    4      1    2      3     2      2      1
## 3   1    4      1    2      3     2      2      1
## 4   1    4      1    2      3     2      2      1
## 5   2    4      1    2      3     2      2      1
## 6   2    4      1    2      3     2      2      1

Triển khai bằng R

library(lme4)
## Loading required package: Matrix
m = lmer(math ~ homework + (1 | school), data=dat)
library(jtools)
m = lmer(math ~ homework + (1 | school), data=dat)
summ(m)
## MODEL INFO:
## Observations: 260
## Dependent Variable: math
## Type: Mixed effects linear regression 
## 
## MODEL FIT:
## AIC = 1847.95, BIC = 1862.19
## Pseudo-R² (fixed effects) = 0.12
## Pseudo-R² (total) = 0.36 
## 
## FIXED EFFECTS:
## ---------------------------------------------------------
##                      Est.   S.E.   t val.     d.f.      p
## ----------------- ------- ------ -------- -------- ------
## (Intercept)         44.98   1.80    24.95    12.67   0.00
## homework             2.21   0.38     5.82   257.15   0.00
## ---------------------------------------------------------
## 
## p values calculated using Satterthwaite d.f.
## 
## RANDOM EFFECTS:
## ------------------------------------
##   Group      Parameter    Std. Dev. 
## ---------- ------------- -----------
##   school    (Intercept)     5.02    
##  Residual                   8.03    
## ------------------------------------
## 
## Grouping variables:
## --------------------------
##  Group    # groups   ICC  
## -------- ---------- ------
##  school      10      0.28 
## --------------------------

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.