Skip to contents

A series of helpers for grouped tables generated by gtsummary::tbl_stack() or gtsummary::tbl_regression() in case of multinomial models, multi-components models or other grouped results. grouped_tbl_pivot_wider() allows to display results in a a wide format, with one set of columns per group. multinom_add_global_p_pivot_wider() is a specific case for multinomial models, when displaying global p-values in a wide format: it calls gtsummary::add_global_p(), followed by grouped_tbl_pivot_wider(), and then keep only the last column with p-values (see examples). Finally, as grouped regression tables doesn't have exactly the same structure as ungrouped tables, functions as gtsummary::bold_labels() do not always work properly. If the grouped table is kept in a long format, style_grouped_tbl() could be use to improve the output by styling variable labels, levels and/or group names. TO BE NOTED: to style group names, style_grouped_tbl() convert the table into a gt object with gtsummary::as_gt(). This function should therefore be used last. If the table is intended to be exported to another format, do not use style_grouped_tbl().

Usage

grouped_tbl_pivot_wider(x)

multinom_add_global_p_pivot_wider(
  x,
  ...,
  p_value_header = "**Likelihood-ratio test**"
)

style_grouped_tbl(
  x,
  bold_groups = TRUE,
  uppercase_groups = TRUE,
  bold_labels = FALSE,
  italicize_labels = TRUE,
  indent_labels = 4L,
  bold_levels = FALSE,
  italicize_levels = FALSE,
  indent_levels = 8L
)

Arguments

x

A grouped table generated with gtsummary::tbl_stack() or gtsummary::tbl_regression().

...

Additional arguments passed to gtsummary::add_global_p().

p_value_header

Header for the p-value column.

bold_groups

Bold group group names?

uppercase_groups

Convert group names to upper case?

bold_labels

Bold variable labels?

italicize_labels

Italicize variable labels?

indent_labels

Number of spaces to indent variable labels.

bold_levels

Bold levels?

italicize_levels

Italicize levels?

indent_levels

Number of spaces to indent levels.

Value

A gtsummary or a gt table.

Examples

# \donttest{
mod <- nnet::multinom(
  grade ~ stage + marker + age,
  data = gtsummary::trial,
  trace = FALSE
)
tbl <- mod |> gtsummary::tbl_regression(exponentiate = TRUE)
#> ℹ Multinomial models, multi-component models and other groups models have a
#>   different underlying structure than the models gtsummary was designed for.
#> • Functions designed to work with `tbl_regression()` objects may yield
#>   unexpected results.
#> ℹ Suppress this message with `?suppressMessages()`.
tbl
Characteristic OR 95% CI p-value
II
T Stage


    T1 — —
    T2 0.70 0.26, 1.86 0.5
    T3 0.35 0.12, 1.05 0.061
    T4 0.58 0.21, 1.64 0.3
Marker Level (ng/mL) 0.59 0.37, 0.94 0.027
Age 1.00 0.98, 1.03 0.7
III
T Stage


    T1 — —
    T2 1.23 0.44, 3.44 0.7
    T3 0.82 0.28, 2.42 0.7
    T4 1.30 0.46, 3.73 0.6
Marker Level (ng/mL) 0.86 0.57, 1.28 0.5
Age 1.01 0.99, 1.04 0.3
Abbreviations: CI = Confidence Interval, OR = Odds Ratio
tbl |> grouped_tbl_pivot_wider()
Characteristic
II
III
OR 95% CI p-value OR 95% CI p-value
T Stage





    T1 — —
— —
    T2 0.70 0.26, 1.86 0.5 1.23 0.44, 3.44 0.7
    T3 0.35 0.12, 1.05 0.061 0.82 0.28, 2.42 0.7
    T4 0.58 0.21, 1.64 0.3 1.30 0.46, 3.73 0.6
Marker Level (ng/mL) 0.59 0.37, 0.94 0.027 0.86 0.57, 1.28 0.5
Age 1.00 0.98, 1.03 0.7 1.01 0.99, 1.04 0.3
Abbreviations: CI = Confidence Interval, OR = Odds Ratio
tbl |> multinom_add_global_p_pivot_wider() |> gtsummary::bold_labels()
Characteristic
II
III
Likelihood-ratio test
OR 95% CI OR 95% CI p-value
T Stage



0.5
    T1 — — — —
    T2 0.70 0.26, 1.86 1.23 0.44, 3.44
    T3 0.35 0.12, 1.05 0.82 0.28, 2.42
    T4 0.58 0.21, 1.64 1.30 0.46, 3.73
Marker Level (ng/mL) 0.59 0.37, 0.94 0.86 0.57, 1.28 0.068
Age 1.00 0.98, 1.03 1.01 0.99, 1.04 0.6
Abbreviations: CI = Confidence Interval, OR = Odds Ratio
tbl |> style_grouped_tbl()
Characteristic OR 95% CI p-value
II
    T Stage


        T1 — —
        T2 0.70 0.26, 1.86 0.5
        T3 0.35 0.12, 1.05 0.061
        T4 0.58 0.21, 1.64 0.3
    Marker Level (ng/mL) 0.59 0.37, 0.94 0.027
    Age 1.00 0.98, 1.03 0.7
III
    T Stage


        T1 — —
        T2 1.23 0.44, 3.44 0.7
        T3 0.82 0.28, 2.42 0.7
        T4 1.30 0.46, 3.73 0.6
    Marker Level (ng/mL) 0.86 0.57, 1.28 0.5
    Age 1.01 0.99, 1.04 0.3
Abbreviations: CI = Confidence Interval, OR = Odds Ratio
t1 <- gtsummary::trial |> gtsummary::tbl_summary(include = grade, by = trt) t2 <- gtsummary::trial |> gtsummary::tbl_summary(include = stage, by = trt) gtsummary::tbl_stack(list(t1, t2), group_header = c("Table 1", "Table 2")) |> style_grouped_tbl()
Characteristic Drug A
N = 98
1
Drug B
N = 102
1
Table 1
    Grade

        I 35 (36%) 33 (32%)
        II 32 (33%) 36 (35%)
        III 31 (32%) 33 (32%)
Table 2
    T Stage

        T1 28 (29%) 25 (25%)
        T2 25 (26%) 29 (28%)
        T3 22 (22%) 21 (21%)
        T4 23 (23%) 27 (26%)
1 n (%)
# }