Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combining output modelsummary with multinomial and logistic models #821

Open
lorenzoFabbri opened this issue Oct 7, 2024 · 1 comment
Open

Comments

@lorenzoFabbri
Copy link

I have a list of models, some of them are multinomial models from nnet, other are logistic models from base R's glm. I encounter an error when using this list as input for modelsummary:

Error: Group columns (response) were not found in the extracted data. The "group" argument must be
  a column name in the data.frame produced by `get_estimates(model)`.  If you wish to combine
  models with and without grouped estimates, you will find examples on the modelsummary
  website:
  https://modelsummary.com

The issue is that get_estimates has a missing column for response since there are only two levels. A simple example:

df <- mtcars
df$cyl <- factor(df$cyl, levels = c(4, 6, 8))
df$am <- as.factor(df$am)

multi <- nnet::multinom(
  formula = cyl ~ drat,
  data = df,
  Hess = FALSE
)

bin <- glm(
  formula = am ~ drat,
  data = df,
  family = binomial()
)

ll <- list(multi, bin)

modelsummary::modelsummary(
  ll[[1]],
  shape = model + term ~ response,
  estimate = "{estimate} ({p.value}) {stars}",
  statistic = NULL,
  coef_rename = \(.x) modelsummary::coef_rename(
    x = .x,
    factor_name = TRUE,
    backticks = TRUE,
    underscore = TRUE,
    titlecase = FALSE
  ),
  coef_omit = "Intercept",
  include_reference = TRUE,
  gof_map = NA,
  notes = list(
    'Format: estimate (p-value).',
    'Significance: "+" = .1, "*" = .05, "**" = .01, "***" = 0.001.'
  ),
  fmt = modelsummary::fmt_decimal(
    digits = 1,
    pdigits = 2
  )
)

Is there a way to force get_estimates to include a dummy response column so that the same function (with shape = model + term ~ response,) can be used for all elements of this list?

@lorenzoFabbri lorenzoFabbri closed this as not planned Won't fix, can't repro, duplicate, stale Oct 11, 2024
@vincentarelbundock
Copy link
Owner

@lorenzoFabbri did you find a solution for this?

I re-opened the issue because I think it's an interesting problem, but I can't think of an easy solution right now. Initially I thought that something like below would work, but in my quick attempt I couldn't get it through:

library(modelsummary)

df <- mtcars
df$cyl <- factor(df$cyl, levels = c(4, 6, 8))
df$am <- as.factor(df$am)

multi <- nnet::multinom(
  formula = cyl ~ drat,
  data = df,
  Hess = FALSE
)

bin <- glm(
  formula = am ~ drat,
  data = df,
  family = binomial()
)

tidy_custom.glm <- function(x, ...) {
    x <- broom::tidy(x, ...)
    x$response <- "6"
    x
}

ll <- list(multi, bin)
modelsummary(ll, shape = term + response ~ model)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants