CodexBloom - Programming Q&A Platform

R: How to properly use the `broom` package to tidy model outputs from `lme4` without losing random effects?

👀 Views: 1624 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-03
r lme4 broom R

I'm testing a new approach and I'm maintaining legacy code that I'm trying to debug I'm working on a project and hit a roadblock... I'm currently working on a mixed-effects model using the `lme4` package in R (version 1.1-27.1). After fitting my model with `lmer`, I want to tidy up the results using the `broom` package (version 0.7.10) to get a clean summary of both fixed and random effects. However, when I attempt to use `broom::tidy()` on my `lmer` model object, it seems to only return the fixed effects, and I need to find a way to extract the random effects along with the fixed ones. Here's a simplified version of my code: ```r library(lme4) library(broom) # Example data set.seed(123) dat <- data.frame(y = rnorm(100), x = rnorm(100), group = factor(rep(1:10, each = 10))) # Fit mixed effects model model <- lmer(y ~ x + (1 | group), data = dat) # Tidy the model output model_tidy <- tidy(model) print(model_tidy) ``` When I run this, the `model_tidy` only contains the coefficients for the fixed effects, and there's no mention of the random effects variance components. The output looks like this: ``` # A tibble: 2 x 5 term estimate std.behavior statistic p.value <chr> <dbl> <dbl> <dbl> <dbl> 1 (Intercept) 0.045 0.101 0.447 0.654 2 x 0.059 0.099 0.596 0.553 ``` I've tried looking into `broom.mixed` to see if that's what I need, but it seems like it adds unnecessary complexity for this particular task. Is there a straightforward way to include random effects in the tidy output? I'm hoping to get a summary that reflects both fixed and random effects without losing clarity. Any insights or workarounds would be greatly appreciated! My development environment is Windows. Has anyone else encountered this? I'm working with R in a Docker container on CentOS. Thanks, I really appreciate it!