knitr::opts_chunk$set(echo = TRUE)

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:

library(foreign)
library(multilevel)
## Loading required package: nlme
## Loading required package: MASS
library(lme4)
## Warning: package 'lme4' was built under R version 3.3.2
## Loading required package: Matrix
## 
## Attaching package: 'lme4'
## The following object is masked from 'package:nlme':
## 
##     lmList
library(MASS)
library(rockchalk)
## 
## Attaching package: 'rockchalk'
## The following object is masked from 'package:MASS':
## 
##     mvrnorm
library(psych)
library(sjstats)
## Warning in checkMatrixPackageVersion(): Package version inconsistency detected.
## TMB was built with Matrix version 1.2.12
## Current Matrix version is 1.2.7.1
## Please re-install 'TMB' from source or ask CRAN for a binary version of 'TMB' matching CRAN's 'Matrix' package
## 
## Attaching package: 'sjstats'
## The following objects are masked from 'package:psych':
## 
##     pca, phi
setwd("/Users/yahyaalshehri/Desktop/PhD Courses/Multilevel Modeling/mlmus3")

high_survey <- read.dta ("hsb.dta")
colnames(high_survey)
##  [1] "minority" "female"   "ses"      "mathach"  "size"     "sector"  
##  [7] "pracad"   "disclim"  "himinty"  "schoolid" "mean"     "sd"      
## [13] "sdalt"    "junk"     "sdalt2"   "num"      "se"       "sealt"   
## [19] "sealt2"   "t2"       "t2alt"    "pickone"  "mmses"    "mnses"   
## [25] "xb"       "resid"
summarize(high_survey)
## $numerics
##            disclim    female   himinty      junk   mathach      mean
## 0%         -2.4160    0.0000    0.0000    0.0000   -2.8320    4.2398
## 25%        -0.8170    0.0000    0.0000    7.0704    7.2750   10.8837
## 50%        -0.2310    1.0000    0.0000   30.6254   13.1310   13.1601
## 75%         0.4600    1.0000    1.0000   75.1511   18.3170   14.6967
## 100%        2.7560    1.0000    1.0000  239.2892   24.9930   19.7191
## mean       -0.1319    0.5282    0.2800   47.3160   12.7479   12.7479
## sd          0.9440    0.4992    0.4490   48.8976    6.8782    3.0058
## var         0.8911    0.2492    0.2016 2390.9763   47.3103    9.0349
## skewness    0.2394   -0.1129    0.9796    1.2998   -0.1805   -0.2712
## kurtosis   -0.1588   -1.9875   -1.0405    1.4620   -0.9216   -0.0507
## NA's        0.0000    0.0000    0.0000    0.0000    0.0000    0.0000
## N        7185.0000 7185.0000 7185.0000 7185.0000 7185.0000 7185.0000
##           minority     mmses     mnses       num   pickone    pracad
## 0%          0.0000   -1.1939   -1.1939   14.0000    0.0000    0.0000
## 25%         0.0000   -0.3230   -0.3230   41.0000    0.0000    0.3200
## 50%         0.0000    0.0320    0.0320   51.0000    0.0000    0.5300
## 75%         1.0000    0.3269    0.3269   57.0000    0.0000    0.7000
## 100%        1.0000    0.8250    0.8250   67.0000    1.0000    1.0000
## mean        0.2747    0.0001    0.0001   48.0163    0.0223    0.5345
## sd          0.4464    0.4135    0.4135   10.8222    0.1476    0.2512
## var         0.1993    0.1710    0.1710  117.1196    0.0218    0.0631
## skewness    1.0091   -0.2685   -0.2685   -0.5793    6.4739    0.1596
## kurtosis   -0.9819   -0.4789   -0.4789   -0.3704   39.9171   -0.8859
## NA's        0.0000    0.0000    0.0000    0.0000    0.0000    0.0000
## N        7185.0000 7185.0000 7185.0000 7185.0000 7185.0000 7185.0000
##              resid     schoolid        sd     sdalt    sdalt2        se
## 0%        -19.4889    1224.0000    3.5410    6.2563   48.3936    0.5059
## 25%        -4.7643    3020.0000    5.5903    6.2563   48.3936    0.7798
## 50%         0.2358    5192.0000    6.2985    6.2563   48.3936    0.8939
## 75%         5.1619    7342.0000    6.7990    6.2563   48.3936    1.0323
## 100%       16.4446    9586.0000    8.4811    6.2563   48.3936    1.8237
## mean        0.0624    5277.8978    6.1975    6.2563   48.3936    0.9190
## sd          6.4595    2499.5778    0.8637    0.0000    0.0000    0.2017
## var        41.7246 6247889.1555    0.7460    0.0000    0.0000    0.0407
## skewness   -0.1360       0.1074   -0.2358       NaN       NaN    1.1132
## kurtosis   -0.6856      -1.2546    0.2158       NaN       NaN    2.3226
## NA's        0.0000       0.0000    0.0000    0.0000    0.0000    0.0000
## N        7185.0000    7185.0000 7185.0000 7185.0000 7185.0000 7185.0000
##              sealt    sealt2    sector       ses        size        t2
## 0%          0.7643    0.8499    0.0000   -3.7580    100.0000    0.0007
## 25%         0.8287    0.9214    0.0000   -0.5380    565.0000    1.0800
## 50%         0.8761    0.9741    0.0000    0.0020   1016.0000    5.7493
## 75%         0.9771    1.0864    1.0000    0.6020   1436.0000   15.7333
## 100%        1.6721    1.8592    1.0000    2.6920   2713.0000  195.8106
## mean        0.9246    1.0281    0.4931    0.0001   1056.8618   14.6558
## sd          0.1292    0.1437    0.5000    0.7794    604.1725   26.4160
## var         0.0167    0.0206    0.2500    0.6074 365024.4089  697.8054
## skewness    1.5869    1.5869    0.0276   -0.2281      0.5715    3.6684
## kurtosis    3.2496    3.2496   -1.9995   -0.3804     -0.3649   16.8883
## NA's        0.0000    0.0000    0.0000    0.0000      0.0000    0.0000
## N        7185.0000 7185.0000 7185.0000 7185.0000   7185.0000 7185.0000
##              t2alt        xb
## 0%          0.0007    5.6839
## 25%         0.9578   10.7907
## 50%         4.3592   12.8722
## 75%        11.4854   14.6015
## 100%       52.8246   17.5219
## mean        8.5376   12.6855
## sd         11.0627    2.4248
## var       122.3842    5.8798
## skewness    2.0632   -0.2685
## kurtosis    4.2433   -0.4789
## NA's        0.0000    0.0000
## N        7185.0000 7185.0000
## 
## $factors
## NULL
head(high_survey)
##   minority female    ses mathach size sector pracad disclim himinty
## 1        0      1 -1.528   5.876  842      0   0.35   1.597       0
## 2        0      1 -0.588  19.708  842      0   0.35   1.597       0
## 3        0      0 -0.528  20.349  842      0   0.35   1.597       0
## 4        0      0 -0.668   8.781  842      0   0.35   1.597       0
## 5        0      0 -0.158  17.898  842      0   0.35   1.597       0
## 6        0      0  0.022   4.583  842      0   0.35   1.597       0
##   schoolid     mean       sd    sdalt     junk   sdalt2 num       se
## 1     1224 9.715446 7.592785 6.256328 45.71077 48.39363  47 1.107522
## 2     1224 9.715446 7.592785 6.256328 49.99941 48.39363  47 1.107522
## 3     1224 9.715446 7.592785 6.256328 59.47536 48.39363  47 1.107522
## 4     1224 9.715446 7.592785 6.256328 14.86853 48.39363  47 1.107522
## 5     1224 9.715446 7.592785 6.256328 27.67840 48.39363  47 1.107522
## 6     1224 9.715446 7.592785 6.256328 64.86649 48.39363  47 1.107522
##       sealt   sealt2       t2    t2alt pickone     mmses     mnses
## 1 0.9125792 1.014718 6.958498 8.289523       1 -0.434383 -0.434383
## 2 0.9125792 1.014718 6.958498 8.289523       0 -0.434383 -0.434383
## 3 0.9125792 1.014718 6.958498 8.289523       0 -0.434383 -0.434383
## 4 0.9125792 1.014718 6.958498 8.289523       0 -0.434383 -0.434383
## 5 0.9125792 1.014718 6.958498 8.289523       0 -0.434383 -0.434383
## 6 0.9125792 1.014718 6.958498 8.289523       0 -0.434383 -0.434383
##         xb     resid
## 1 10.13759 -4.261589
## 2 10.13759  9.570412
## 3 10.13759 10.211412
## 4 10.13759 -1.356588
## 5 10.13759  7.760412
## 6 10.13759 -5.554588
describe(high_survey)
##          vars    n    mean      sd  median trimmed     mad     min     max
## minority    1 7185    0.27    0.45    0.00    0.22    0.00    0.00    1.00
## female      2 7185    0.53    0.50    1.00    0.54    0.00    0.00    1.00
## ses         3 7185    0.00    0.78    0.00    0.02    0.85   -3.76    2.69
## mathach     4 7185   12.75    6.88   13.13   12.91    8.12   -2.83   24.99
## size        5 7185 1056.86  604.17 1016.00 1011.71  661.24  100.00 2713.00
## sector      6 7185    0.49    0.50    0.00    0.49    0.00    0.00    1.00
## pracad      7 7185    0.53    0.25    0.53    0.53    0.27    0.00    1.00
## disclim     8 7185   -0.13    0.94   -0.23   -0.16    0.94   -2.42    2.76
## himinty     9 7185    0.28    0.45    0.00    0.23    0.00    0.00    1.00
## schoolid   10 7185 5277.90 2499.58 5192.00 5254.46 3192.04 1224.00 9586.00
## mean       11 7185   12.75    3.01   13.16   12.83    3.00    4.24   19.72
## sd         12 7185    6.20    0.86    6.30    6.22    0.95    3.54    8.48
## sdalt      13 7185    6.26    0.00    6.26    6.26    0.00    6.26    6.26
## junk       14 7185   47.32   48.90   30.63   39.84   40.62    0.00  239.29
## sdalt2     15 7185   48.39    0.00   48.39   48.39    0.00   48.39   48.39
## num        16 7185   48.02   10.82   51.00   48.72   10.38   14.00   67.00
## se         17 7185    0.92    0.20    0.89    0.90    0.18    0.51    1.82
## sealt      18 7185    0.92    0.13    0.88    0.91    0.08    0.76    1.67
## sealt2     19 7185    1.03    0.14    0.97    1.01    0.09    0.85    1.86
## t2         20 7185   14.66   26.42    5.75    8.37    7.69    0.00  195.81
## t2alt      21 7185    8.54   11.06    4.36    6.18    5.87    0.00   52.82
## pickone    22 7185    0.02    0.15    0.00    0.00    0.00    0.00    1.00
## mmses      23 7185    0.00    0.41    0.03    0.01    0.47   -1.19    0.82
## mnses      24 7185    0.00    0.41    0.03    0.01    0.47   -1.19    0.82
## xb         25 7185   12.69    2.42   12.87   12.75    2.76    5.68   17.52
## resid      26 7185    0.06    6.46    0.24    0.17    7.35  -19.49   16.44
##            range  skew kurtosis    se
## minority    1.00  1.01    -0.98  0.01
## female      1.00 -0.11    -1.99  0.01
## ses         6.45 -0.23    -0.38  0.01
## mathach    27.83 -0.18    -0.92  0.08
## size     2613.00  0.57    -0.36  7.13
## sector      1.00  0.03    -2.00  0.01
## pracad      1.00  0.16    -0.89  0.00
## disclim     5.17  0.24    -0.16  0.01
## himinty     1.00  0.98    -1.04  0.01
## schoolid 8362.00  0.11    -1.25 29.49
## mean       15.48 -0.27    -0.05  0.04
## sd          4.94 -0.24     0.22  0.01
## sdalt       0.00   NaN      NaN  0.00
## junk      239.29  1.30     1.46  0.58
## sdalt2      0.00   NaN      NaN  0.00
## num        53.00 -0.58    -0.37  0.13
## se          1.32  1.11     2.32  0.00
## sealt       0.91  1.59     3.25  0.00
## sealt2      1.01  1.59     3.25  0.00
## t2        195.81  3.67    16.89  0.31
## t2alt      52.82  2.06     4.24  0.13
## pickone     1.00  6.47    39.92  0.00
## mmses       2.02 -0.27    -0.48  0.00
## mnses       2.02 -0.27    -0.48  0.00
## xb         11.84 -0.27    -0.48  0.03
## resid      35.93 -0.14    -0.69  0.08
str(high_survey)
## 'data.frame':    7185 obs. of  26 variables:
##  $ minority: int  0 0 0 0 0 0 0 0 0 0 ...
##  $ female  : int  1 1 0 0 0 0 1 0 1 0 ...
##  $ ses     : num  -1.528 -0.588 -0.528 -0.668 -0.158 ...
##  $ mathach : num  5.88 19.71 20.35 8.78 17.9 ...
##  $ size    : int  842 842 842 842 842 842 842 842 842 842 ...
##  $ sector  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ pracad  : num  0.35 0.35 0.35 0.35 0.35 ...
##  $ disclim : num  1.6 1.6 1.6 1.6 1.6 ...
##  $ himinty : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ schoolid: num  1224 1224 1224 1224 1224 ...
##  $ mean    : num  9.72 9.72 9.72 9.72 9.72 ...
##  $ sd      : num  7.59 7.59 7.59 7.59 7.59 ...
##  $ sdalt   : num  6.26 6.26 6.26 6.26 6.26 ...
##  $ junk    : num  45.7 50 59.5 14.9 27.7 ...
##  $ sdalt2  : num  48.4 48.4 48.4 48.4 48.4 ...
##  $ num     : num  47 47 47 47 47 47 47 47 47 47 ...
##  $ se      : num  1.11 1.11 1.11 1.11 1.11 ...
##  $ sealt   : num  0.913 0.913 0.913 0.913 0.913 ...
##  $ sealt2  : num  1.01 1.01 1.01 1.01 1.01 ...
##  $ t2      : num  6.96 6.96 6.96 6.96 6.96 ...
##  $ t2alt   : num  8.29 8.29 8.29 8.29 8.29 ...
##  $ pickone : int  1 0 0 0 0 0 0 0 0 0 ...
##  $ mmses   : num  -0.434 -0.434 -0.434 -0.434 -0.434 ...
##  $ mnses   : num  -0.434 -0.434 -0.434 -0.434 -0.434 ...
##  $ xb      : num  10.1 10.1 10.1 10.1 10.1 ...
##  $ resid   : num  -4.26 9.57 10.21 -1.36 7.76 ...
##  - attr(*, "datalabel")= chr ""
##  - attr(*, "time.stamp")= chr "23 Nov 2007 10:26"
##  - attr(*, "formats")= chr  "%8.0g" "%8.0g" "%9.0g" "%9.0g" ...
##  - attr(*, "types")= int  251 251 254 254 252 251 254 254 251 254 ...
##  - attr(*, "val.labels")= chr  "" "" "" "" ...
##  - attr(*, "var.labels")= chr  "" "" "" "" ...
##  - attr(*, "version")= int 12

fit a model for mathach with a fixed effect for SES and a random intercept for school.

first.model <- lmer(mathach ~ ses + (1|schoolid), data = high_survey, REML = FALSE )

icc(first.model)
## 
## Linear mixed model
##  Family: gaussian (identity)
## Formula: mathach ~ ses + (1 | schoolid)
## 
##   ICC (schoolid): 0.113235
ranef(first.model)
## $schoolid
##       (Intercept)
## 1224 -1.631506601
## 1288  0.428228846
## 1296 -3.442447786
## 1308  1.678092414
## 1317 -0.262907201
## 1358 -1.113650532
## 1374 -2.265341448
## 1433  4.378982867
## 1436  3.487146179
## 1461  2.072951618
## 1462 -0.493396258
## 1477  1.055844310
## 1499 -3.383122533
## 1637 -3.059222221
## 1906  1.831397282
## 1909  0.916190517
## 1942  3.009565169
## 1946  0.200802861
## 2030 -1.169839905
## 2208  1.534728007
## 2277 -1.362883737
## 2305 -0.016116528
## 2336  2.403568676
## 2458  0.688684599
## 2467 -1.496222460
## 2526  3.177070824
## 2626  0.741285032
## 2629  2.267769625
## 2639 -3.148632702
## 2651 -1.429500891
## 2655  1.186820101
## 2658 -0.264055020
## 2755  2.133608016
## 2768 -1.250730960
## 2771 -0.001495812
## 2818  0.805198844
## 2917 -2.342990217
## 2990  3.631999209
## 2995 -2.042318974
## 3013  0.051382260
## 3020  1.048155028
## 3039  2.530877186
## 3088 -1.983276529
## 3152  0.414728699
## 3332  0.131164456
## 3351 -1.642504084
## 3377 -1.764089741
## 3427  5.769878188
## 3498  1.878028582
## 3499 -0.378929059
## 3533 -1.656729798
## 3610  2.147302243
## 3657 -1.373098559
## 3688  0.871332061
## 3705 -2.459178273
## 3716 -1.077159480
## 3838  2.670365507
## 3881 -0.851031696
## 3967 -0.073164443
## 3992  0.970360559
## 3999 -1.278238203
## 4042  0.620491034
## 4173 -0.008058032
## 4223  1.865205278
## 4253 -2.071401169
## 4292  1.222125962
## 4325  0.609776662
## 4350 -0.943302643
## 4383 -1.040932015
## 4410  0.491043449
## 4420  1.408204696
## 4458 -3.728194581
## 4511  0.887765204
## 4523 -3.580724676
## 4530 -1.934053716
## 4642  1.475857798
## 4868 -0.984338167
## 4931  0.229834467
## 5192 -1.637708063
## 5404  0.689661660
## 5619  1.567411901
## 5640  0.813101168
## 5650  1.330663745
## 5667  0.104973677
## 5720  1.347667986
## 5761 -0.649323353
## 5762 -4.520629593
## 5783  0.062131660
## 5815 -2.863164902
## 5819 -0.823966979
## 5838  0.525045010
## 5937  2.016910173
## 6074  1.564526199
## 6089  2.187351358
## 6144 -2.593804124
## 6170  1.635355657
## 6291 -1.064458886
## 6366  1.989378812
## 6397  0.580473208
## 6415 -0.305312225
## 6443 -1.853939357
## 6464 -3.109104237
## 6469  3.515445397
## 6484  0.570007682
## 6578  0.501610600
## 6600 -0.724398272
## 6808 -2.666842534
## 6816  0.538983817
## 6897  1.383023306
## 6990 -4.800417270
## 7011  0.994809345
## 7101 -0.644248682
## 7172 -3.308800177
## 7232  0.087365043
## 7276 -0.144024742
## 7332  1.090320181
## 7341 -2.142124591
## 7342 -0.370239954
## 7345 -1.227000353
## 7364  1.467108235
## 7635  1.583645683
## 7688  4.646320159
## 7697  1.965537918
## 7734 -0.719417846
## 7890 -2.658287362
## 7919  0.906049585
## 8009  0.166293936
## 8150  1.242182161
## 8165  2.633834671
## 8175 -0.443756810
## 8188 -0.197515797
## 8193  3.381197090
## 8202 -0.939883429
## 8357  1.535404481
## 8367 -5.236724550
## 8477  0.275336523
## 8531 -0.088081805
## 8627 -1.763967217
## 8628  3.726663963
## 8707 -0.124375471
## 8775 -2.044715906
## 8800 -2.912704578
## 8854 -5.308865537
## 8857  1.249627813
## 8874  0.174430144
## 8946 -1.270504595
## 8983 -0.834642285
## 9021  0.474119137
## 9104  2.097816496
## 9158 -2.745098897
## 9198  4.197645133
## 9225  1.152930394
## 9292 -0.677459076
## 9340 -0.416446570
## 9347  0.329677898
## 9359  1.538850228
## 9397 -2.248306683
## 9508  1.019050292
## 9550 -1.334852708
## 9586  0.636148228
fixef(first.model)
## (Intercept)         ses 
##    12.65762     2.39150
coef(first.model)
## $schoolid
##      (Intercept)    ses
## 1224   11.026117 2.3915
## 1288   13.085852 2.3915
## 1296    9.215176 2.3915
## 1308   14.335716 2.3915
## 1317   12.394716 2.3915
## 1358   11.543973 2.3915
## 1374   10.392282 2.3915
## 1433   17.036606 2.3915
## 1436   16.144770 2.3915
## 1461   14.730575 2.3915
## 1462   12.164227 2.3915
## 1477   13.713468 2.3915
## 1499    9.274501 2.3915
## 1637    9.598401 2.3915
## 1906   14.489021 2.3915
## 1909   13.573814 2.3915
## 1942   15.667188 2.3915
## 1946   12.858426 2.3915
## 2030   11.487783 2.3915
## 2208   14.192351 2.3915
## 2277   11.294740 2.3915
## 2305   12.641507 2.3915
## 2336   15.061192 2.3915
## 2458   13.346308 2.3915
## 2467   11.161401 2.3915
## 2526   15.834694 2.3915
## 2626   13.398908 2.3915
## 2629   14.925393 2.3915
## 2639    9.508991 2.3915
## 2651   11.228122 2.3915
## 2655   13.844443 2.3915
## 2658   12.393568 2.3915
## 2755   14.791231 2.3915
## 2768   11.406892 2.3915
## 2771   12.656128 2.3915
## 2818   13.462822 2.3915
## 2917   10.314633 2.3915
## 2990   16.289623 2.3915
## 2995   10.615304 2.3915
## 3013   12.709006 2.3915
## 3020   13.705778 2.3915
## 3039   15.188501 2.3915
## 3088   10.674347 2.3915
## 3152   13.072352 2.3915
## 3332   12.788788 2.3915
## 3351   11.015119 2.3915
## 3377   10.893534 2.3915
## 3427   18.427502 2.3915
## 3498   14.535652 2.3915
## 3499   12.278694 2.3915
## 3533   11.000894 2.3915
## 3610   14.804926 2.3915
## 3657   11.284525 2.3915
## 3688   13.528955 2.3915
## 3705   10.198445 2.3915
## 3716   11.580464 2.3915
## 3838   15.327989 2.3915
## 3881   11.806592 2.3915
## 3967   12.584459 2.3915
## 3992   13.627984 2.3915
## 3999   11.379385 2.3915
## 4042   13.278114 2.3915
## 4173   12.649565 2.3915
## 4223   14.522829 2.3915
## 4253   10.586222 2.3915
## 4292   13.879749 2.3915
## 4325   13.267400 2.3915
## 4350   11.714321 2.3915
## 4383   11.616691 2.3915
## 4410   13.148667 2.3915
## 4420   14.065828 2.3915
## 4458    8.929429 2.3915
## 4511   13.545389 2.3915
## 4523    9.076899 2.3915
## 4530   10.723570 2.3915
## 4642   14.133481 2.3915
## 4868   11.673285 2.3915
## 4931   12.887458 2.3915
## 5192   11.019915 2.3915
## 5404   13.347285 2.3915
## 5619   14.225035 2.3915
## 5640   13.470724 2.3915
## 5650   13.988287 2.3915
## 5667   12.762597 2.3915
## 5720   14.005291 2.3915
## 5761   12.008300 2.3915
## 5762    8.136994 2.3915
## 5783   12.719755 2.3915
## 5815    9.794458 2.3915
## 5819   11.833656 2.3915
## 5838   13.182668 2.3915
## 5937   14.674533 2.3915
## 6074   14.222150 2.3915
## 6089   14.844975 2.3915
## 6144   10.063819 2.3915
## 6170   14.292979 2.3915
## 6291   11.593164 2.3915
## 6366   14.647002 2.3915
## 6397   13.238097 2.3915
## 6415   12.352311 2.3915
## 6443   10.803684 2.3915
## 6464    9.548519 2.3915
## 6469   16.173069 2.3915
## 6484   13.227631 2.3915
## 6578   13.159234 2.3915
## 6600   11.933225 2.3915
## 6808    9.990781 2.3915
## 6816   13.196607 2.3915
## 6897   14.040647 2.3915
## 6990    7.857206 2.3915
## 7011   13.652433 2.3915
## 7101   12.013375 2.3915
## 7172    9.348823 2.3915
## 7232   12.744988 2.3915
## 7276   12.513599 2.3915
## 7332   13.747944 2.3915
## 7341   10.515499 2.3915
## 7342   12.287383 2.3915
## 7345   11.430623 2.3915
## 7364   14.124732 2.3915
## 7635   14.241269 2.3915
## 7688   17.303943 2.3915
## 7697   14.623161 2.3915
## 7734   11.938205 2.3915
## 7890    9.999336 2.3915
## 7919   13.563673 2.3915
## 8009   12.823917 2.3915
## 8150   13.899805 2.3915
## 8165   15.291458 2.3915
## 8175   12.213867 2.3915
## 8188   12.460108 2.3915
## 8193   16.038820 2.3915
## 8202   11.717740 2.3915
## 8357   14.193028 2.3915
## 8367    7.420899 2.3915
## 8477   12.932960 2.3915
## 8531   12.569542 2.3915
## 8627   10.893656 2.3915
## 8628   16.384287 2.3915
## 8707   12.533248 2.3915
## 8775   10.612907 2.3915
## 8800    9.744919 2.3915
## 8854    7.348758 2.3915
## 8857   13.907251 2.3915
## 8874   12.832053 2.3915
## 8946   11.387119 2.3915
## 8983   11.822981 2.3915
## 9021   13.131742 2.3915
## 9104   14.755440 2.3915
## 9158    9.912524 2.3915
## 9198   16.855268 2.3915
## 9225   13.810554 2.3915
## 9292   11.980164 2.3915
## 9340   12.241177 2.3915
## 9347   12.987301 2.3915
## 9359   14.196474 2.3915
## 9397   10.409317 2.3915
## 9508   13.676674 2.3915
## 9550   11.322771 2.3915
## 9586   13.293772 2.3915
## 
## attr(,"class")
## [1] "coef.mer"
summary(first.model)
## Linear mixed model fit by maximum likelihood  ['lmerMod']
## Formula: mathach ~ ses + (1 | schoolid)
##    Data: high_survey
## 
##      AIC      BIC   logLik deviance df.resid 
##  46649.0  46676.5 -23320.5  46641.0     7181 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -3.1263 -0.7277  0.0220  0.7578  2.9186 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  schoolid (Intercept)  4.729   2.175   
##  Residual             37.030   6.085   
## Number of obs: 7185, groups:  schoolid, 160
## 
## Fixed effects:
##             Estimate Std. Error t value
## (Intercept)  12.6576     0.1873   67.57
## ses           2.3915     0.1057   22.63
## 
## Correlation of Fixed Effects:
##     (Intr)
## ses 0.003

explore the between-school and within-school variability of SES.

second.model <- lmer(ses ~ (1|schoolid), data = high_survey, REML = FALSE )

summary(second.model)
## Linear mixed model fit by maximum likelihood  ['lmerMod']
## Formula: ses ~ (1 | schoolid)
##    Data: high_survey
## 
##      AIC      BIC   logLik deviance df.resid 
##  15047.0  15067.7  -7520.5  15041.0     7182 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -5.4793 -0.6724  0.0179  0.7065  4.2632 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  schoolid (Intercept) 0.1597   0.3997  
##  Residual             0.4462   0.6680  
## Number of obs: 7185, groups:  schoolid, 160
## 
## Fixed effects:
##              Estimate Std. Error t value
## (Intercept) -0.005674   0.032647  -0.174
icc(second.model)
## 
## Linear mixed model
##  Family: gaussian (identity)
## Formula: ses ~ (1 | schoolid)
## 
##   ICC (schoolid): 0.263612

Produce a variable, mnـses, equal to the schools’ mean SES and another variable, devـses, equal to the difference between the students’ SES and the mean SES for their school.

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