0

With the following code I produce the laTex table in the image below. As you might notice there are a few things wrong with the output.

  1. The title is missing
  2. P-values in the wrong place
  3. The footnote is misaligned

Any help is greatly appreciated!


library(tidyverse)
library(modelsummary)
library(gt)


data <- as.data.frame(ChickWeight)
mod_control <- lm(weight ~ Time , data = data)
mod_treat <- lm(weight ~ Time + Diet, data = data)


mod_one_list <- list(mod_control, mod_treat)

# coefmap
cm <- c("(Intercept)"="Konstant",
        "Time" = "Tid",
        "Num.Obs." = "n")

# gof_map 
gm <- list(list(raw = "nobs", clean = "N", fmt = 0))

# title
tit <- "En beskrivning här"


# produce table


modelsummary(mod_one_list, 
                          output = "gt",
                          stars = T,
                          title = tit,
                          coef_map = cm,
                          gof_map = gm,
                          vcov = "HC1") %>%  
  tab_spanner(label = '(1)', columns = 2) %>%
  tab_spanner(label = "(2)", columns = 3) %>% 
  tab_footnote("För standardfel använder vi HC1",
               locations = cells_body(rows = 1, columns = 2)) %>%
  as_latex() %>%
  cat()

enter image description here

Daniel D. Sjoberg
  • 8,820
  • 2
  • 12
  • 28
Tomas R
  • 440
  • 2
  • 10

1 Answers1

2

This is an issue with the gt package. When adding both footnotes and source notes (which is what modelsummary uses to report significance stars), gt puts both types of notes in different mini-pages. This breaks alignment in LaTeX.

You can see this by inspecting the code of this minimal example:

library(gt)

dat <- mtcars[1:4, 1:4]

gt(dat) |> 
  tab_source_note(source_note = "source note") |> 
  tab_footnote("footnote", locations = cells_body(rows = 1, columns = 2)) |> 
  as_latex() |> 
  cat()
## \captionsetup[table]{labelformat=empty,skip=1pt}
## \begin{longtable}{rrrr}
## \toprule
## mpg & cyl & disp & hp \\ 
## \midrule
## 21.0 & 6\textsuperscript{1} & 160 & 110 \\ 
## 21.0 & 6 & 160 & 110 \\ 
## 22.8 & 4 & 108 & 93 \\ 
## 21.4 & 6 & 258 & 110 \\ 
##  \bottomrule
## \end{longtable}
## \vspace{-5mm}
## \begin{minipage}{\linewidth}
## \textsuperscript{1}footnote \\ 
## \end{minipage}
## \begin{minipage}{\linewidth}
## source note\\ 
## \end{minipage}

I am not sure if the gt maintainers would consider this a “bug”, but it might be worth it to report it on their repository anyway: https://github.com/rstudio/gt/issues

For what it’s worth, I think that the default LaTeX output with modelsummary(model, output="latex") generally works better, because it uses kableExtra, which seems to prioritize LaTeX a bit more.

Vincent
  • 15,809
  • 7
  • 37
  • 39