Fitting a Linear Mixed-Effects Model
Let’s consider a simple example where we have data on students’ test scores from multiple schools. Here, we want to model the test scores (response variable) based on students’ study hours (fixed effect) while accounting for variations between schools (random effect).
# Simulated data
set.seed(123)
data <- data.frame(
school = factor(rep(1:5, each=20)),
hours = rnorm(100, mean=5, sd=2),
score = rnorm(100, mean=70, sd=10)
)
# Introduce random effects
data$score <- data$score + rep(rnorm(5, sd=5), each=20)
# Fit the linear mixed-effects model
model <- lmer(score ~ hours + (1 | school), data = data)
# Summary of the model
summary(model)
Output:
Linear mixed model fit by REML ['lmerMod']
Formula: score ~ hours + (1 | school)
Data: data
REML criterion at convergence: 741.8
Scaled residuals:
Min 1Q Median 3Q Max
-1.9552 -0.6207 -0.1108 0.5834 3.1477
Random effects:
Groups Name Variance Std.Dev.
school (Intercept) 21.44 4.630
Residual 95.24 9.759
Number of obs: 100, groups: school, 5
Fixed effects:
Estimate Std. Error t value
(Intercept) 73.4114 3.6362 20.189
hours -0.2146 0.5453 -0.394
Correlation of Fixed Effects:
(Intr)
hours -0.777
In this model score ~ hours specifies the fixed effect of study hours on test scores. (1 | school) specifies the random intercept for schools, allowing each school to have a different baseline test score.
Diagnostic Plots of Linear Mixed-Effects Models
It’s crucial to check the assumptions and fit of the model using diagnostic plots. The lattice package can be used for plotting random effects.
# Load necessary package
library(lattice)
# Plot random effects
dotplot(ranef(model, condVar=TRUE))
Output:
The plot shows the variability in intercepts across different schools, indicating how each school’s intercept deviates from the overall average intercept.
- Schools 1 and 5 have relatively precise estimates, as shown by their shorter error bars.
- School 4 has the least precise estimate, indicated by the long error bar.
The intercepts for most schools are close to zero, suggesting that the deviations from the average intercept are not very large.
Advanced Features for Linear Mixed-Effects Model
The lme4 package also supports more complex models, including those with multiple random effects and nested random effects. For example, you might model data with random slopes and intercepts:
# Model with random slopes and intercepts
model_advanced <- lmer(score ~ hours + (hours | school), data = data)
# Summary of the advanced model
summary(model_advanced)
Output:
Linear mixed model fit by REML ['lmerMod']
Formula: score ~ hours + (hours | school)
Data: data
REML criterion at convergence: 741.3
Scaled residuals:
Min 1Q Median 3Q Max
-2.0129 -0.6187 -0.0875 0.5554 3.1986
Random effects:
Groups Name Variance Std.Dev. Corr
school (Intercept) 4.5096 2.1236
hours 0.2242 0.4735 1.00
Residual 94.6249 9.7275
Number of obs: 100, groups: school, 5
Fixed effects:
Estimate Std. Error t value
(Intercept) 73.5871 3.1190 23.593
hours -0.2339 0.5826 -0.402
Correlation of Fixed Effects:
(Intr)
hours -0.728
optimizer (nloptwrap) convergence code: 0 (OK)
boundary (singular) fit: see help('isSingular')
The model includes random slopes and intercepts for hours
by school
, allowing both to vary across schools.
- Fixed effects indicate the average relationship between
hours
andscore
across all schools. - Random effects show variability in intercepts and slopes across schools and their correlation.
- Model diagnostics help assess the fit and compare with other models.
By examining both the fixed and random effects, this model provides a comprehensive understanding of how hours
impacts score
on average and how this relationship varies across different schools.
Linear Mixed-Effects Models (LME) In R
Linear Mixed-Effects Models (LME) are powerful tools used in statistical analysis to handle data that involve both fixed and random effects. These models are particularly useful in dealing with hierarchical or grouped data, where observations within the same group may be correlated. In R, the lme4 package provides robust functions to fit linear mixed-effects models. This article will guide you through the concepts of LME, how to implement them in R Programming Language and provide practical examples to illustrate their use.
Contact Us