Dayne
Dayne

Reputation: 502

How to add arguments from kable() in modelsummary() with output as kableExtra

I recently discovered R package modelsummary from answer to this question. It appears that the output of summary table created by modelsummary can be of class kable_knitr which can be fed into other functions from kableExtra, such as kable_styling.

However, I am stuck at how to specify values for other options from the function kable() into the modelsummary() function. One obvious way could be to get a data.frame output and than input that into kable. Is there a better alternative (so that I can avoid fixing the data.frame output?

I am particularly looking for kable() options, escape=F (to insert greek letters in latex output - see this) and booktabs=T.

A reproducable example:

x=rnorm(100)
mod1=arima(x,order = c(1,0,1))
modelsummary(mod1, output = 'kableExtra', coef_rename = c('ar1'='$x_{t-1}$', 
                                                           'ma1'='$\\epsilon_{t-1}$')) %>% 
  kable_styling(latex_options = 'HOLD_position')

In the above table, I am trying to rename the variables to math style, which would work in kable() if the names were given as they have been.

Upvotes: 0

Views: 870

Answers (1)

Vincent
Vincent

Reputation: 17725

The ... ellipses are pushed forward to the kbl function automatically. Valid arguments like escape can be passed directly to modelsummary.

Note that for the reasons explained in the "post-processing" note of the output argument documentation, I prefer to specify directly what output format I need before post-processing functions, otherwise kableExtra will prep an HTML table by default:

library(kableExtra)
library(modelsummary)
x=rnorm(100)
mod1=arima(x,order = c(1,0,1))

modelsummary(
  mod1, 
  output = "latex",
  coef_rename = c('ar1'='$x_{t-1}$', 'ma1'='$\\epsilon_{t-1}$'),
  escape = FALSE)
\begin{table}[H]
\centering
\begin{tabular}[t]{lc}
\toprule
  & Model 1\\
\midrule
$x_{t-1}$ & 0.337\\
 & (0.368)\\
$\epsilon_{t-1}$ & -0.490\\
 & (0.335)\\
intercept & 0.007\\
 & (0.068)\\
\midrule
Num.Obs. & 100\\
AIC & 267.6\\
BIC & 278.0\\
Log.Lik. & -129.808\\
\bottomrule
\end{tabular}
\end{table}

Also note that modelsummary already calls kable_styling once internally, and that adding kable_styling to a latex table produced by kableExtra can sometimes produce weird results because kableExtra will nest tables within tables. (Note: I plan to submit a bugfix to kableExtra about this.)

data.frame(a = 1:2, b = 2:3) %>%
  kbl("latex") %>%
  kable_styling() %>% 
  kable_styling() %>%
  kable_styling()
\begin{table}[H]
\centering\begin{table}[H]
\centering\begin{table}[H]
\centering
\begin{tabular}[t]{r|r}
\hline
a & b\\
\hline
1 & 2\\
\hline
2 & 3\\
\hline
\end{tabular}
\end{table}
\end{table}
\end{table}

Upvotes: 1

Related Questions