Symbols as the coefficient names in modelsummary

54 views Asked by At

I have the following, hopefully very easy to solve, problem. I came across the modelsummary package and find it really useful. But I am struggeling to rename the coefficients to symbols, like for example $$\alpha$$.

I tried every possible combination of $ and \ or \ but nothing seems to work. I also tried expression(), which works for me in the ggplot package. (expression(Z[paste(i,", ",2019)], for example)

This is the summary I want to create:

basic_reg <- modelsummary(
  list(reg_basis, reg_basis_ausr),
  stars = TRUE,
  coef_rename = c("$\alpha$", "a", "b", "c"),
  gof_omit = 'DF|Deviance|AIC|BIC|Log.Lik.|F|RMSE',
  title = 'Table 1: Basic Regressions',
  notes = list("Std. Errors in parentheses", "In (2) UK, Singapore, USA, China, France, Hong Kong and Luxembourg are removed and robust Std. Errors are added"),
  output = "gt")

gtsave(basic_reg, filename = "basic_reg.png")

I hope someone can help, because the tables look very nice.

1

There are 1 answers

2
stefan On BEST ANSWER

A simple approach would be to use the unicode, e.g. \u03B1 for alpha.

Using a minimal reprex based on the default example from modelsummary:

library(modelsummary)

url <- "https://vincentarelbundock.github.io/Rdatasets/csv/HistData/Guerry.csv"
dat <- read.csv(url)
dat$Small <- dat$Pop1831 > median(dat$Pop1831)
dat <- dat[
  c("Donations", "Literacy", "Commerce", "Crime_pers", "Crime_prop", "Clergy", "Small")
]

mod <- lm(Donations ~ Crime_prop + Crime_pers + Commerce, data = dat)

basic_reg <- modelsummary(
  list(mod, mod),
  stars = TRUE,
  coef_rename = c("\U03B1", "a", "b", "c"),
  gof_omit = "DF|Deviance|AIC|BIC|Log.Lik.|F|RMSE",
  title = "Table 1: Basic Regressions",
  notes = list(
    "Std. Errors in parentheses",
    paste(
      "In (2) UK, Singapore, USA, China, France,",
      "Hong Kong and Luxembourg are removed and",
      "robust Std. Errors are added"
    )
  ),
  output = "gt"
)

gt::gtsave(basic_reg, filename = "basic_reg.png")

enter image description here

EDIT A second approach would be to use gt::html. When you output to gt the result is a gt table object which can be manipulated using functions from gt, e.g. we can use gt::text_replace to replace text and we can use gt::html to add some HTML, which besides adding greek letters allows to add super- and subscripts via a <sup> or <sub> tag.

In the example code I replace a placeholder text alpha by the HTML entity for the alpha symbol, and a single letter "c" by "Delta X_2019":

basic_reg <- basic_reg |>
  gt::text_replace("alpha",
    gt::html(
      "&alpha;"
    ),
    locations = gt::cells_body()
  ) |>
  gt::text_replace("^c$",
    gt::html(
      paste0(
        "&Delta;",
        "X",
        "<sub>2019</sub>"
      )
    ),
    locations = gt::cells_body()
  )

Table