## Log 16.08 – Random / fixed slopes / effects / intercepts! And why? Plus R Formulas

- ger.: Mehrebenenanalye – Hierarchisch Lineare Modellierung
- engl.: multilevel modelling – random effects modelling – hierachical linear modelling – mixed modelling – random coefficient modelling

x

### What does random intercept mean? Is there a difference between random effect and random intercept model?

A random intercept model estimates separate

interceptsfor each unit of each level at which the intercept is permitted to vary. This is one kind of random effect model. Another kind of random effect model also includesrandom slopes, and estimates separate slopes (i.e. coefficients, betas, effects, etc. depending on your discipline) for eachvariablefor each unit of each level at which that slope is permitted to vary.

Random effect modelling uses random intercepts to accommodate for multi level structure within data. Random slopes are an extension allowing to describe differences between slopes and correlation between intercepts and slopes.

# Why Random Intercepts?

Why not simply include L2 variable in the model as a fixed factor/ control variable?

- fine if L2 has few categories
- not really interested in difference between counties
- if a lot of dummy categories -> eat up a lot of degrees of freedom

# Random Intercept + Fixed Slope/ Effects

#### what?

- the slopes for groups are assumed to be of the same value
- the structural part of the model is also called fixed effects

#### why?

- The researcher is interested in the characteristics of the sample, not the population
- weak effects??

# Random Intercept + Random Slope/ Effects

#### what?

- Effects are modelled allowing the slopes to vary between groups.

#### why?

- Enhance computation of fixed effects
- Not interested in L2 effects within sample, but in the effects in general population
- (might) fit the data better

#### report?

- variance in intercepts
- variance of slopes
- covariance of slopes and intercepts

# Missing Data

- partially missing data not a big problem
- at least two points needed for calculating slope
- if only one data point only intercept is computed

# Formulas and Code in R

grand mean centering predictors (makes interactions easier to interpret)

`cgrade <- df$grade - mean(df$grade)`

### relevant packages and formula variations:

#### nlme

```
# lme
## Simple multi level linear null model
lme(fixed = grade ~ 1, random = ~1|school, data = df)
## Simple multi level linear null model with one predictor
lme(fixed = grade ~ intervention, random = ~1|school, data = df)
## Simple multi level linear null model with several predictors
lme(fixed = grade ~ intervention + other_grade, random = ~1|school, data = df)
## Random coefficient model
lme(fixed = grade ~ intervention, random = ~intervention|school, data = df)
## Random coefficient model with several random slopes
lme(fixed = grade ~ intervention + other_grade, random = ~intervention + other_grade|school, data = df)
## Interactions on Level 1
lme(fixed = grade ~ intervention + other_grade + intervention*other_grade, random = ~1|school, data = df)
## Crosslevel Interactions
lme(fixed = grade ~ intervention + context_L2 + intervention*context_L2 , random = ~1|school, data = df)
## Use Maximum Likelihood (ML) instead of Restricted Maximum Likelihood (REML)
lme(fix = grade ~ intervention, random = ~1|school, data = df, method = "ML")
## Three-Level Model
lme(fix = grade ~ 1, random = ~1|school/class, data = df)
## Three-Level Model with random effects for specific coefficient (here: other_grade)
lme(fix = grade ~ intervention + other_grade, random = list(school = ~1, class = ~other_grade, data = df)
```

#### glmmPQL

```
## Random Intercept Logistic Regression
glmmPQL(grade ~ intervention, random = ~1|school, family = "binomial")</pre>
```

#### lme4

```
# lmer
## Random Intercept Models
lmer(grade ~ intervention + (1|school), data = df)
lmer(grade ~ intervention + other_grade + (1|school), data = df)
## P Values with MCMC, requires packages coda, languageR
pvals.fnc(lmer_fit, nsim = 10000, withMCMC = T)
## Random Coefficient Models
lmer(grade ~ intervention + (intervention|school), data = df)
## Correlated Random Coefficients
lmer(grade ~ intervention + other_grade + (intervention + other_grade|school), data = df)
## Uncorrelated Random Coefficients
lmer(grade ~ intervention + other_grade + (intervention|school) + (other_grade|school), data = df)
## Use Maximum Likelihood (ML) instead of Restricted Maximum Likelihood (REML)
lmer(grade ~ intervention + (1|school), data = df, REML = F)
## Three-Level (null) model - post: pvals.func()
lmer(grade ~ 1 + (1|school/class), data = df)
```

```
# glmer
## Multi Level Logistic Model
glmer(grade ~ intervention) + (1|school), family = binomial, nAGQ = 25, data = df)
```

#### ordinal

```
# clmm
## Multi Level ordinal regression
clmm(grade ~ intervention + (1|school), data = df)
# uses lme4 style formulas
```

#### MCMCglmm

`MCMCglmm(grade ~ intervention, random = ~school, data = df)`

#### Comparing model fits with ANOVA

`anova(model1, model2)`