rmodelsummary

Skip a model in a modelsummary table


I am trying to print a three-panel table of 3 regression models, in which instead of one of the results (the middle regression for the middle panel) it should be "NA" or "--".

For example, I can run:

library(modelsummary)
y <- rnorm(5)

models <- list()
for (i in 1:3) {
  models[[length(models) + 1]] <- lm(y ~ rnorm(5))
  models[[length(models) + 1]] <- lm(y ~ rnorm(5))
  models[[length(models) + 1]] <- lm(y ~ rnorm(5))
}
models <- list(models[1:3], models[4:6], models[7:9])

modelsummary(models, shape = 'rbind', gof_map = c(""), 
              coef_omit = "Intercept", estimate  = "{estimate}", statistic = NULL)

to produce the following table:

enter image description here

I don't need the second regression from Panel B. I need the respective coefficient to be NA, or a dash. Is there a way to pass something to modelsummary to tell it to skip printing that particular model?


Solution

  • This is a similar idea to the one proposed by @TheN:

    1. Save your models to an intermediate modelsummary_list representation.
    2. Modify the values you want manually.
    3. Feed the modelsummary_list back to modelsummary().

    The benefit of this approach is that it will work with any “final” output format, whereas the gsub solution only works for some output types.

    library(modelsummary)
    y <- rnorm(5)
    
    models <- list()
    for (i in 1:3) {
      models[[length(models) + 1]] <- lm(y ~ rnorm(5))
      models[[length(models) + 1]] <- lm(y ~ rnorm(5))
      models[[length(models) + 1]] <- lm(y ~ rnorm(5))
    }
    models <- list(models[1:3], models[4:6], models[7:9])
    
    tmp <- lapply(models, modelsummary, output = "modelsummary_list")
    tmp[[2]][[2]]$tidy$estimate[2] <- tmp[[2]][[2]]$tidy$std.error[2] <- "-"
    
    modelsummary(
        tmp, shape = 'rbind', gof_map = c(""), 
        coef_omit = "Intercept", estimate  = "{estimate}", statistic = NULL)
    

    enter image description here