lme4

star 5

R lme4 package for mixed-effects models. Use for fitting linear and generalized linear mixed-effects models.

LeoLin990405 By LeoLin990405 schedule Updated 1/30/2026

name: lme4 description: R lme4 package for mixed-effects models. Use for fitting linear and generalized linear mixed-effects models.

lme4

Linear mixed-effects models.

Linear Mixed Models

library(lme4)

# Random intercept
model <- lmer(y ~ x + (1 | group), data = df)

# Random intercept and slope
model <- lmer(y ~ x + (1 + x | group), data = df)

# Uncorrelated random effects
model <- lmer(y ~ x + (1 | group) + (0 + x | group), data = df)

# Multiple grouping factors
model <- lmer(y ~ x + (1 | group1) + (1 | group2), data = df)

# Nested groups
model <- lmer(y ~ x + (1 | group1/group2), data = df)

# Crossed random effects
model <- lmer(y ~ x + (1 | subject) + (1 | item), data = df)

Generalized Linear Mixed Models

# Logistic
model <- glmer(y ~ x + (1 | group), data = df, family = binomial)

# Poisson
model <- glmer(count ~ x + (1 | group), data = df, family = poisson)

# Negative binomial
library(MASS)
model <- glmer.nb(count ~ x + (1 | group), data = df)

Model Summary

# Summary
summary(model)

# Fixed effects
fixef(model)
coef(summary(model))

# Random effects
ranef(model)
VarCorr(model)

# Confidence intervals
confint(model)
confint(model, method = "boot", nsim = 1000)

Model Comparison

# Likelihood ratio test
model1 <- lmer(y ~ x + (1 | group), data = df)
model2 <- lmer(y ~ x + z + (1 | group), data = df)
anova(model1, model2)

# AIC/BIC
AIC(model1, model2)
BIC(model1, model2)

Predictions

# Predictions with random effects
predict(model)
predict(model, newdata = new_df)

# Predictions without random effects (population level)
predict(model, re.form = NA)

# Predictions for new groups
predict(model, newdata = new_df, allow.new.levels = TRUE)

Diagnostics

# Residuals
residuals(model)
residuals(model, type = "pearson")

# Fitted values
fitted(model)

# Influence measures
influence(model)

# Plot diagnostics
plot(model)
qqnorm(resid(model))

Bootstrapping

# Bootstrap confidence intervals
boot_ci <- confint(model, method = "boot", nsim = 500)

# Bootstrap predictions
bootMer(model, FUN = fixef, nsim = 100)

Convergence Issues

# Increase iterations
model <- lmer(y ~ x + (1 | group), data = df,
  control = lmerControl(optCtrl = list(maxfun = 100000))
)

# Different optimizer
model <- lmer(y ~ x + (1 | group), data = df,
  control = lmerControl(optimizer = "bobyqa")
)

# Scale predictors
df$x_scaled <- scale(df$x)
model <- lmer(y ~ x_scaled + (1 | group), data = df)

Effect Sizes

# R-squared
library(MuMIn)
r.squaredGLMM(model)

# ICC
library(performance)
icc(model)
Install via CLI
npx skills add https://github.com/LeoLin990405/r-analytics-skill --skill lme4
Repository Details
star Stars 5
call_split Forks 0
navigation Branch main
article Path SKILL.md
More from Creator
LeoLin990405
LeoLin990405 Explore all skills →