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)
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:
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 |