RSocialScience

Kurs zur Nutzung von R in den Sozialwissenschaften

Wie sehen die Daten aus?

Andres Gutierrez - Multilevel Modeling of Educational Data using R

Beispiel Mehrebenenmodelle

Untersuchungsgegenstand

Fragen hierzu

Beispiel in Goldstein (2010), Kapitel 1.2

Evaluierung der Effektivität von Schulen

Mehrebenen-Modelle:

Unterscheidung

Bibliotheken

# Linear Mixed-Effects Models using 'Eigen' and S4
install.packages("lme4")

# Data Visualization for Statistics in Social Science
install.packages("sjPlot")
library(ggplot2)
# Miscellaneous Functions for "Grid" Graphics
library(gridExtra)
library(lme4)

## Loading required package: Matrix

library(sjPlot)
# A Grammar of Data Manipulation
library(dplyr)

## 
## Attaching package: 'dplyr'

## The following object is masked from 'package:gridExtra':
## 
##     combine

## The following objects are masked from 'package:stats':
## 
##     filter, lag

## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

Beispieldaten

mlexdat <- read.csv(
"https://github.com/Japhilko/RSocialScience/
raw/master/data/mlexdat.csv") 
X SES Score ID
1 18.62733 -55.120574 A
2 33.64915 -92.375273 A
3 22.26931 -48.783447 A
4 36.49052 38.099329 A
5 38.21402 339.701754 A
6 11.36669 2.286978 A

Formalistisch

<!–

–>

Die Gesamtvariation wird in zwei Teile zerlegt:

Der R-code für dieses Nullmodell

HLM0 <- lmer(Score ~ (1 | ID), data = mlexdat)

Nullmodell Ergebnis - Koeffizienten

coef(HLM0)

## $ID
##   (Intercept)
## A     45.7893
## B    430.7218
## C   1182.1760
## D   2145.2329
## E   3489.1408
## 
## attr(,"class")
## [1] "coef.mer"

Nullmodell Ergebnis - Zusammenfassung

summary(HLM0)

## Linear mixed model fit by REML ['lmerMod']
## Formula: Score ~ (1 | ID)
##    Data: mlexdat
## 
## REML criterion at convergence: 7130.6
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.74559 -0.69317 -0.00757  0.68337  2.96511 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev.
##  ID       (Intercept) 1931758  1389.9  
##  Residual               87346   295.5  
## Number of obs: 500, groups:  ID, 5
## 
## Fixed effects:
##             Estimate Std. Error t value
## (Intercept)   1458.6      621.7   2.346

Interpratation des Nullmodells

100 * 87346 / (87346 + 1931757)

## [1] 4.32598

Ein weiteres Modell

Rcode für dieses Modell

HLM1 <- lmer(Score ~ SES + (SES | ID), data = mlexdat)
coef(HLM1)

## $ID
##   (Intercept)        SES
## A    36.46401  0.3798185
## B    37.21549  9.7596237
## C    38.10719 20.8897245
## D    38.85566 30.2320132
## E    39.70159 40.7907386
## 
## attr(,"class")
## [1] "coef.mer"

Zusammenfassung zweites Modell

summary(HLM1)

## Linear mixed model fit by REML ['lmerMod']
## Formula: Score ~ SES + (SES | ID)
##    Data: mlexdat
## 
## REML criterion at convergence: 6742.1
## 
## Scaled residuals: 
##      Min       1Q   Median       3Q      Max 
## -2.83274 -0.64740  0.02662  0.69063  2.67309 
## 
## Random effects:
##  Groups   Name        Variance Std.Dev. Corr
##  ID       (Intercept)     1.65   1.285      
##           SES           257.09  16.034  1.00
##  Residual             40400.24 200.998      
## Number of obs: 500, groups:  ID, 5
## 
## Fixed effects:
##             Estimate Std. Error t value
## (Intercept)   38.069     45.863   0.830
## SES           20.410      7.236   2.821
## 
## Correlation of Fixed Effects:
##     (Intr)
## SES -0.119

# 1% - BS variance
# 99% - WS variance
100 * 40400.24 / (40400.24 + 257.09 + 1.65)

## [1] 99.36363

# Percentage of variation explained by SES between schools
1 - ((257.09 + 1.65) / 1931757)

## [1] 0.9998661

# Percentage of variation explained by SES within schools
1 - (40400.24 / 87346)

## [1] 0.5374689

Was heißt das? - Schulsegregation

Ein weiteres Beispiel zur Spezifikation von Multilevel Modellen

library(lme4)
library(mlmRev)

Der Datensatz

data(Exam)
# names(Exam)
school normexam schgend schavg vr intake standLRT sex type student
1 0.2613242 mixed 0.1661752 mid 50% bottom 25% 0.6190592 F Mxd 143
1 0.1340672 mixed 0.1661752 mid 50% mid 50% 0.2058022 F Mxd 145
1 -1.7238820 mixed 0.1661752 mid 50% top 25% -1.3645760 M Mxd 142
1 0.9675862 mixed 0.1661752 mid 50% mid 50% 0.2058022 F Mxd 141
1 0.5443412 mixed 0.1661752 mid 50% mid 50% 0.3711052 F Mxd 138
1 1.7348992 mixed 0.1661752 mid 50% bottom 25% 2.1894372 M Mxd 155

Zufälliger Intercept und fixed predictor auf individeller Ebene

lmer(normexam ~ standLRT + (1 | school), data=Exam)

Random intercept, Random slope

Varying intercept model

MLexamp.6<-lmer(extro~open+agree+ social + (1 | school), 
                data = lmm.data)

Varying slope model

MLexamp.9<-lmer(extro~open + agree + social + 
                  (1 + open | school/class), 
                data = lmm.data)

Links —–