Building Table 2

Author

Jeremy Selva

Learning Goals

In this section, we try to create a table that is close to Table 2.

We start first by choosing the relevant columns to make Table 2. Observe that we only keep rows with a birth outcome (Non-live birth and Live birth).

Pregnancy Duration and Birth Weight Summary Table

Data Required

Here is the dataset required.

Exercise (Table Creation)

In this exercise, we create the pregnancy duration and birth weight summary table.

Ensure that

  • The labels are set correctly,
  • Duration of pregnancy categories are displayed as count (percentage) with counts as integers and percentage in one decimal place.
  • Total birth weight is displayed as mean ± standard deviation in integers without big marks.
  • Birth weight categories are displayed as proportion (percentage) with proprtions as integers and percentages in one decimal place.
  • Fisher Test is done on pregnancy duration groups.
  • Wilcox Test is done on total birth weight.
  • Chi Square Test without Yates’ continuity correction is done on birth weight categories.
  • p values are displayed in two decimal places.
  • Variable group header are added for both pregnant duration and birth weight.
  • footnotes are removed.
NoteHint 1

Ensure the label parameter is something like this

label = list(
      "Preg_duration_32" ~ "<32 wk",
      "Preg_duration_35" ~ "<35 wk",
      "Preg_duration_37" ~ "<37 wk",
      "Birthweight" ~ "Total weight - g",
      "Birthweight_2500" ~ "<2500 g - no./total no. (%)",
      "Birthweight_1500" ~ "<1500 g - no./total no. (%)"
    )
NoteHint 2

Ensure the statistics parameter is something like this

statistic = list(
      gtsummary::all_continuous() ~ "{mean}±{sd}",
      c("Preg_duration_32", "Preg_duration_35", "Preg_duration_37") ~ "{n} ({p})",
      c("Birthweight_2500", "Birthweight_1500") ~ "{n}/{N} ({p})"
    )
NoteHint 3

Ensure the gtsummary::add_p parameter is something like this

digits = list(
      c("Birthweight") ~ gtsummary::label_style_number(digits = 0, big.mark = ""),
      c("Birthweight_2500", "Birthweight_1500") ~ c(0, 0, 1)
    )
NoteHint 4

Ensure the gtsummary::add_p is something like this

gtsummary::add_p(
    test = list(
      c("Preg_duration_32", "Preg_duration_35", "Preg_duration_37") ~ "fisher.test"
    ),
    pvalue_fun = gtsummary::label_style_pvalue(digits = 2)
  )
NoteHint 5

Ensure the gtsummary::add_variable_group_header is something like this

gtsummary::add_variable_group_header(
    header = "Duration of pregnancy - no. (%)",
    variables = starts_with("Preg_")
  )
TipFully worked solution:
opt_preg_duration_birth_weight_data |> gtsummary::tbl_summary( include = c( "Preg_duration_32", "Preg_duration_35", "Preg_duration_37", "Birthweight", "Birthweight_2500", "Birthweight_1500" ), by = "Group", missing = "no", label = list( "Preg_duration_32" ~ "<32 wk", "Preg_duration_35" ~ "<35 wk", "Preg_duration_37" ~ "<37 wk", "Birthweight" ~ "Total weight - g", "Birthweight_2500" ~ "<2500 g - no./total no. (%)", "Birthweight_1500" ~ "<1500 g - no./total no. (%)" ), statistic = list( gtsummary::all_continuous() ~ "{mean}±{sd}", c("Preg_duration_32", "Preg_duration_35", "Preg_duration_37") ~ "{n} ({p})", c("Birthweight_2500", "Birthweight_1500") ~ "{n}/{N} ({p})" ), digits = list( c("Birthweight") ~ gtsummary::label_style_number(digits = 0, big.mark = ""), c("Birthweight_2500", "Birthweight_1500") ~ c(0, 0, 1) ) ) |> gtsummary::add_p( test = list( c("Preg_duration_32", "Preg_duration_35", "Preg_duration_37") ~ "fisher.test" ), pvalue_fun = gtsummary::label_style_pvalue(digits = 2) ) |> gtsummary::add_variable_group_header( header = "Duration of pregnancy - no. (%)", variables = starts_with("Preg_") ) |> gtsummary::add_variable_group_header( header = "Birth weight", variables = c("Birthweight", "Birthweight_2500", "Birthweight_1500") ) |> gtsummary::remove_footnote_header()
opt_preg_duration_birth_weight_data |> 
  gtsummary::tbl_summary(
    include = c(
      "Preg_duration_32", "Preg_duration_35", "Preg_duration_37",
      "Birthweight", "Birthweight_2500", "Birthweight_1500"
    ),
    by = "Group",
    missing = "no",
    label = list(
      "Preg_duration_32" ~ "<32 wk",
      "Preg_duration_35" ~ "<35 wk",
      "Preg_duration_37" ~ "<37 wk",
      "Birthweight" ~ "Total weight - g",
      "Birthweight_2500" ~ "<2500 g - no./total no. (%)",
      "Birthweight_1500" ~ "<1500 g - no./total no. (%)"
    ),
    statistic = list(
      gtsummary::all_continuous() ~ "{mean}±{sd}",
      c("Preg_duration_32", "Preg_duration_35", "Preg_duration_37") ~ "{n} ({p})",
      c("Birthweight_2500", "Birthweight_1500") ~ "{n}/{N} ({p})"
    ),
    digits = list(
      c("Birthweight") ~ gtsummary::label_style_number(digits = 0, big.mark = ""),
      c("Birthweight_2500", "Birthweight_1500") ~ c(0, 0, 1)
    )
  ) |> 
  gtsummary::add_p(
    test = list(
      c("Preg_duration_32", "Preg_duration_35", "Preg_duration_37") ~ "fisher.test"
    ),
    pvalue_fun = gtsummary::label_style_pvalue(digits = 2)
  ) |> 
  gtsummary::add_variable_group_header(
    header = "Duration of pregnancy - no. (%)",
    variables = starts_with("Preg_")
  ) |>
  gtsummary::add_variable_group_header(
    header = "Birth weight",
    variables = c("Birthweight", "Birthweight_2500", "Birthweight_1500")
  ) |>
  gtsummary::remove_footnote_header()

Live Birth Summary Table

Data Required

Here is the dataset required.

Table Draft

We create our first draft using the code below. There are a few things that we need to resolve.

  • Convert >0.99 to 1.0 from the < 32 wk row to match Table 2.
  • Remove the p value of 0.036 from the Total row.
  • Find a way to add Live births - no. (%) above Total.

Exercise (Change >0.99)

We have done this before in the previous section. Ensure that

  • pvalue_fun = gtsummary::label_style_pvalue(digits = 2) is replaced with pvalue_fun = p_value_style in the function gtsummary::add_p.
  • p_value_style returns 1.0 when if p value is 1.
  • p_value_style returns gtsummary::style_pvalue(x, digits = 2) otherwise.
NoteHint

Ensure the p_value_style function is something like this

p_value_style <- function(x) {
  dplyr::case_when(
    x == 1 ~ "1.0",
    .default = gtsummary::style_pvalue(x, digits = 2)
  )
}
TipFully worked solution:
p_value_style <- function(x) { dplyr::case_when( x == 1 ~ "1.0", .default = gtsummary::style_pvalue(x, digits = 2) ) } opt_live_birth_data |> gtsummary::tbl_summary( include = c( "Have_Live_Birth", "Preterm_32", "Preterm_35", "Preterm_37" ), by = "Group", missing = "no", label = list( "Have_Live_Birth" ~ "Total", "Preterm_32" ~ "<32 wk", "Preterm_35" ~ "<35 wk", "Preterm_37" ~ "<37 wk" ), statistic = list( c("Have_Live_Birth", "Preterm_32", "Preterm_35", "Preterm_37") ~ "{n} ({p})" ), digits = list( c("Have_Live_Birth", "Preterm_32", "Preterm_35", "Preterm_37") ~ c(0, 1) ) ) |> gtsummary::add_p( test = list( c("Preterm_32", "Preterm_35", "Preterm_37") ~ "fisher.test" ), pvalue_fun = p_value_style ) |> gtsummary::add_variable_group_header( header = "Preterm", variables = c("Preterm_32", "Preterm_35", "Preterm_37") ) |> gtsummary::remove_footnote_header()
p_value_style <- function(x) {
  dplyr::case_when(
    x == 1 ~ "1.0",
    .default = gtsummary::style_pvalue(x, digits = 2)
  )
}

opt_live_birth_data |>
  gtsummary::tbl_summary(
    include = c(
      "Have_Live_Birth", "Preterm_32", "Preterm_35", "Preterm_37"
    ),
    by = "Group",
    missing = "no",
    label = list(
      "Have_Live_Birth" ~ "Total",
      "Preterm_32" ~ "<32 wk",
      "Preterm_35" ~ "<35 wk",
      "Preterm_37" ~ "<37 wk"
    ),
    statistic = list(
      c("Have_Live_Birth", "Preterm_32", "Preterm_35", "Preterm_37") ~ "{n} ({p})"
    ),
    digits = list(
      c("Have_Live_Birth", "Preterm_32", "Preterm_35", "Preterm_37") ~ c(0, 1)
    )
  ) |> 
  gtsummary::add_p(
    test = list(
      c("Preterm_32", "Preterm_35", "Preterm_37") ~ "fisher.test"
    ),
    pvalue_fun = p_value_style
  ) |> 
  gtsummary::add_variable_group_header(
    header = "Preterm",
    variables = c("Preterm_32", "Preterm_35", "Preterm_37")
  ) |>
  gtsummary::remove_footnote_header()

Exercise (Remove p value from Total)

Here is live_birth and its corresponding table_body that we have so far.

Click on the different tabs.

Use gtsummary::modify_table_body to remove the p value from the Total.

NoteHint

Ensure the dplyr::case_when function is something like this

dplyr::case_when(
  .data[["variable"]] == "Have_Live_Birth" ~ NA,
  .default = .data[["p.value"]]
)
TipFully worked solution:
live_birth |> gtsummary::modify_table_body( ~ .x |> dplyr::mutate( `p.value` = dplyr::case_when( .data[["variable"]] == "Have_Live_Birth" ~ NA, .default = .data[["p.value"]] ) ) )
live_birth |>
  gtsummary::modify_table_body(
    ~ .x |> 
      dplyr::mutate(
        `p.value` = dplyr::case_when(
          .data[["variable"]] == "Have_Live_Birth" ~ NA,
          .default = .data[["p.value"]]
        )
      )
  )

Exercise (Live births - no. (%) above Total)

Here is live_birth and its corresponding table_body that we have so far.

Click on the different tabs.

Use gtsummary::modify_table_body to add the header Live births - no. (%).

NoteHint

Ensure the tibble::add_row function is something like this

tibble::add_row(
   row_type = "variable_group", 
   label = "Live births - no. (%)", 
   .before = 1
)
TipFully worked solution:
live_birth |> gtsummary::modify_table_body( ~ .x |> tibble::add_row( row_type = "variable_group", label = "Live births - no. (%)", .before = 1 ) )
live_birth |>
  gtsummary::modify_table_body(
    ~ .x |> 
      tibble::add_row(
        row_type = "variable_group", 
        label = "Live births - no. (%)", 
        .before = 1
      )
  )

Modify Indentation

Here is what we have so far.

Here is live_birth and its corresponding table_body that we have so far.

Click on the different tabs.

Finally to resolve the indentation issue, gtsummary has a function called gtsummary::modify_indent to customise the indentation for columns. We need to select the right columns and rows to perform the indentation.

Combine

We have completed creating our two tables as shown below. It is time to combine them

Click on the different tabs to see the tables

We use gtsummary::tbl_stack to append the two tables together and gtsummary::modify_header to update the header names to match Table 2.

Next Steps

Similar to Table 1, adding footnotes for the title as well as some rows in the tables varies based on what kind of output (html or word document) you wish the table to be represented as. gtsummary has its limitations in this and need the help of other R packages such as gt and flextable. Below are examples on how this can be done.

Export in html

We proceed to export the table as html by using the R package gt.

Add footnotes in table

Save as html

Export in word

We proceed to export the table as html by using the R package flextable.

Add footnotes in table

Footnotes in the table are this time added using gtsummary::modify_table_body as though we can add footnote in flextable, they are alwats represented as superscript.

Save as word

We save it into word using the code below. However, the code will not work as we cannot export files in this interactive session.

table2_word |> 
  flextable::save_as_docx(path = "data/table2.docx")