Skip to content
Back to skills

Lme4

ASecurity

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

  • 5 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added June 4, 2026
dataperformance

Security analysis

A100/100

Scanned June 4, 2026

npx -y skills add LeoLin990405/r-analytics-skill --skill lme4 --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Lme4?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Lme4
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/leolin990405-lme4/badge)](https://www.skillsdirectory.com/skills/leolin990405-lme4)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
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

```r
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

```r
# 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

```r
# 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

```r
# 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

```r
# 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

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

# Fitted values
fitted(model)

# Influence measures
influence(model)

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

## Bootstrapping

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

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

## Convergence Issues

```r
# 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
# R-squared
library(MuMIn)
r.squaredGLMM(model)

# ICC
library(performance)
icc(model)
```

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…