1

I am using the modelsummary package to create a table for a multilevel logistic regression model estimated via glmer from the lme4 package. I would know like to include the number of groups in my table. The information does not appear in get_gof(model), but it exists if I type in summary(model). In the example below, I would like to have one row with the information: Number of groups: 18.

A minimum reproducible example:

# Load the required packages
library(lme4)

# Load the sleepstudy dataset
data(sleepstudy)

# Convert the dependent variable to a binary outcome
sleepstudy$binary_outcome <- ifelse(sleepstudy$Reaction > median(sleepstudy$Reaction), 1, 0)

# Run the multilevel logistic regression model using glmer
model <- glmer(binary_outcome ~ Days + (1 | Subject), data = sleepstudy, family = binomial())

# Print the model summary
summary(model)

# Use Modelsummary to print the table
modelsummary(models, 
             output = "table.docx", 
             coef_map = cm, title = 'Table 1', 
             exponentiate = TRUE,
             stars = T)
flxflks
  • 498
  • 2
  • 13

1 Answers1

1

You can use the glance_custom mechanism to add any custom goodness-of-fit statistic to a table. I paste a solution to your question below. You can find detailed explanations and many more examples on the modelsummary website:

https://vincentarelbundock.github.io/modelsummary/articles/modelsummary.html#new-models-and-custom-statistics

library(lme4)
library(tibble)
library(modelsummary)
data(sleepstudy)
sleepstudy$binary_outcome <- ifelse(sleepstudy$Reaction > median(sleepstudy$Reaction), 1, 0)
model <- glmer(binary_outcome ~ Days + (1 | Subject), data = sleepstudy, family = binomial())

glance_custom.glmerMod <- function(x, ...) {
  tibble(`# Groups` = summary(x)$ngrps)
}

modelsummary(model)
(1)
(Intercept) -3.460
(0.937)
Days 0.743
(0.130)
SD (Intercept Subject) 2.820
Num.Obs. 180
R2 Marg. 0.289
R2 Cond. 0.792
AIC 158.6
BIC 168.2
ICC 0.7
RMSE 0.28
# Groups 18
Vincent
  • 15,809
  • 7
  • 37
  • 39
  • Thank you Vincent. I had indeed read the great and extensive documentation on your website including the glace_custom. functions. However, I did not know how to access the number of groups argument from the glmer. If accessing these arguments is the same across all models, maybe it is worth to add that to the documentation? :) – flxflks May 03 '23 at 09:15
  • Thanks for taking the time to read it. Unfortunately, AFAICT, it is not the same across all models... – Vincent May 03 '23 at 11:06