According to the documentation of the gtsummay package, you can use <br>
in add_significance_stars()
to break the labels of the table showing the results of the regression model in HTML, but it does not work for LaTeX.
I have tried other line break methods such as \n
, but it still does not work. How can I make line breaks in LaTeX?
Here is an example in HTML.
df <-
mtcars %>%
lm(mpg ~ ., data = .)
df %>%
tbl_regression() %>%
add_significance_stars(
hide_se = TRUE,
pattern = "{estimate}{stars}<br>({std.error})"
) %>%
modify_header(estimate ~ "OLS<br>result")
And here is a LaTeX example.
df %>%
tbl_regression() %>%
add_significance_stars(
hide_se = TRUE,
pattern = "{estimate}{stars}<br>({std.error})"
) %>%
modify_header(estimate ~ "OLS<br>result") %>%
as_kable_extra(
format = "latex",
booktabs = TRUE
)
I created a table based on the answer, but I found that this method causes the layout to be broken when using tbl_merge()
.
I will present the problem code again.
# make nested dataframe
nest_df <-
mtcars %>%
tibble() %>%
group_nest(vs)
# make function
mod_fun <- function(df){lm(mpg ~ ., data = df)}
# map function
nest_df <-
nest_df %>%
mutate(model = map(data, mod_fun))
# make table
nest_df <-
nest_df %>%
mutate(
tbl = map(
.x = model,
~ tbl_regression(
.x,
) %>%
add_significance_stars(
hide_se = TRUE,
pattern = "{estimate}{stars}\\\\&({std.error})"
) %>%
modify_header(estimate ~ "OLS\\\\&result")
)
)
# merge table
nest_df_m <-
tbl_merge(
tbls = nest_df$tbl,
tab_spanner = c("type1", "type2")
)
# output merged table
nest_df_m %>%
as_kable_extra(
format = "latex",
booktabs = TRUE,
escape = FALSE
) %>%
kable_styling(position = "center")
Maybe this fits your need. You could a line break by
\\\\
(which gives an \\
in the latex code),&
to put the std.error
in the same column as the estimate,escape=FALSE
in as_kable_extra
. df %>%
tbl_regression() %>%
add_significance_stars(
hide_se = TRUE,
pattern = "{estimate}{stars}\\\\&({std.error})"
) %>%
modify_header(estimate ~ "OLS\\\\&result") %>%
as_kable_extra(
format = "latex",
booktabs = TRUE,
escape = FALSE
)