1

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?

2 Answers2

1

You can save the modelsummary as an object then edit the coefficient out. Here I have used a random seed of 123.

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

MS[[5]] <- gsub("-1.150", "      ", MS[[5]])
TheN
  • 523
  • 3
  • 7
1

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

Vincent
  • 15,809
  • 7
  • 37
  • 39
  • 1
    Oh, yes, this is wonderful. I was hoping to avoid messing with latex output! And, for posterity, there are also statistics in tmp[[2]][[2]]$glance (like N and R2) that one can edit similarly. Thank you. – George B. Y. Jun 16 '23 at 22:40