Preparation for Table 1

Author

Jeremy Selva

Learning Goals

In this tutorial, we try to create a table that is close to Table 1 from the journal Treatment of Periodontal Disease and the Risk of Preterm Birth.

During the session “Cleaning Obstetrics and Periodontal Therapy Dataset”, we have taken the raw data from medicaldata::opt and process it so that we can use with gtsummary to create the tables. The processed data can be obtained

We start first by choosing the relevant columns to make Table 1.

As the final output looks complex, let’s break the task down to simpler tasks.

Age, Race and Education Data

This task involves creating a summary table for age, race and number of years of education stratified by treatment group.

Here is the dataset required.

Using tbl_summary

Let’s try to build a summary table from opt_age_race_data using the default parameters of gtsummary::tbl_summary. We can see that table is giving a descriptive statistics of the overall population. It even display the statistics the patient ID.

include and by parameter

To exclude the patient ID being displayed, we set the parameter include a vector of column names to include in the summary table. To stratify the table by treatment group, we set the parameter by = "Group".

type parameter

Observe that the (continuous) variable Age is expressed as Median (Q1, Q3) while categorical variables are expressed as n (%). Variables with two categories (dichotomous) like White and Hisp are displayed differently to those with multiple categories like Education.

We can use the function gtsummary::assign_summary_type to identify the default summary type for each variable

On the other hand, the type parameter in the gtsummary::tbl_summary allows us to specify the summary type for each variable. The type parameter accepts a list of formulas as input.

Below is an example which we change the variable White and Black from dichotomous to categorical.

statistics parameter

Next, we need to ensure that - age is expressed in “means±SD”. - race and education is expressed in “no. (%)” (already done in default mode).

This can be achieved by using the statistic parameter which help users specify the statistics presented in the table. Like the type parameter, it accepts a list of formulas as input.

Below are some examples on how it can be done.

Click on the different tabs.

We can list each variable individually as a character vector.

We can use all_ helper functions like gtsummary::all_continuous() and gtsummary::all_categorical() to make the code concise.

It can be observed that the displayed values {mean}±{sd} and {n} ({p}) are interpreted using glue::glue() syntax: a name that appears between curly brackets will be interpreted as a function name and the formatted result of that function will be placed in the table.

In gtsummary, some pre-calculated statistics available for display are

For categorical/dichotomous variable:

  • {n} for frequency.
  • {N} for denominator.
  • {p} for percent.

For continuous variable:

  • {median} for median.
  • {mean} for mean.
  • {sd} for standard deviation.
  • {min} for minimum.
  • {max} for maximum.
  • {p##} for percentile where ## is an integer from 0 to 100.For example, {p25} is the 25th percentile calculating using p25: quantile(probs=0.25, type=2).

A full list is provided here.

Feel free to experiment with the different statistics below.

Click on the different tabs.

Sometimes, there may be a need to display continuous variables statistical summaries in multiple lines. To do that, you can change the variable type from continuous to continuous2.

Formatting in gtsummary

Before we get to the digits parameter, let’s take a look at some default formatting from gtsummary

Click on the different tabs.

gtsummary::style_number is the default style that gtsummary used internally to style the number. It rounds numbers to a fixed number of decimal places specified by the digits argument.

It has many flexible parameter to use and explore. The digits parameter can accept a vector of numbers to indicate how many decimal places to express for each number.

gtsummary::style_sigfig rounds numbers to a minimum number of significant figures (significant digits) via digits, adapting the decimal count dynamically depending on the magnitude of the number.

Unlike gtsummary::style_number, the digits parameter can only accept one number.

gtsummary::style_percent is the default style that gtsummary used internally to style percentages in proportions of categorical variables.

Observe that

  • All values are multiplied by 100.
  • Negative values are set to NA.
  • Positive numbers less than 0.1% are represented as <0.1.
  • Numbers less than 10% (exclusive) are rounded to digits + 1 places.

gtsummary::style_ratio is the default style that gtsummary used internally to format relative risk and odds ratio.

Observe that

  • Values between -1 and 1 will display digits number of significant digits based on gtsummary::style_sigfig.
  • Values greater than 1 (or less than -1) will display digits + 1 number of significant digits based on gtsummary::style_sigfig.

gtsummary::style_pvalue is the default style that gtsummary used internally to format p values.

Observe that

  • Values greater than 1 are set to NA.
  • Negative values are set to NA.
  • digits can only hold value 1, 2, or 3. It determines how many decimal places are presented.
  • High p values have a limit based on the value of digits.
    • When digits is 1, values more than 0.9 is represented as >0.9.
    • When digits is 2, values more than 0.99 is represented as >0.99.
    • When digits is 3, values more than 0.999 is represented as >0.999.
  • p values less than 0.001 are represented as <0.001.

Similar to the above style_*()functions, gtsummary has label_style_*() functions that takes the same inputs as the style_*()functions but returns a “wrapper” function that perform the styling.

Click on the different tabs.

digits parameter

If we take a look at (Table 1 from the journal Treatment of Periodontal Disease and the Risk of Preterm Birth, the mean, standard deviation (sd) and percentage are expressed in 1 decimal place.

In gtsummary, we can specify the numerical format to display a given statistical result using the digits parameter. Though it accepts a list of formulas as input, it is also restricted based on what input is given to the statistic parameter.

Looking at the example below,

  • In the case of the statistic is {mean} ({sd}), passing the value 1 or c(1, 1) is the same as passing the output of gtsummary::label_style_number(digits=1) for both {mean} and {sd}.
  • In the case of the statistic is {n} ({p}), passing the value c(0, 1) is the same as passing the output of gtsummary::label_style_number(digits=0) for both {n} and gtsummary::label_style_percent(digits=1) for {p}.

label parameter

Currently, the column names of the dataset opt_age_race_data are used as the descriptive labels in the output table. We can update the descriptive labels printed using the label parameter. It accepts a list of formulas as input.

In the example below, we rename the following to match the (Table 1 in the publication.

  • Age to Age - yr.
  • Hisp to Hispanic.
  • Education to Education - no. (%).

Variable Group Header

You will observe in Table 1, there is a group header added above White, Black and Hispanic called Race or ethnic group - no. (%)". Unlike Education, we cannot create a categorical column Race or ethnic group - no. (%)" because some patients have mixed race/ethnicity (Both White and Hispanic).

Instead, we can use add_variable_group_header function in gtsummary to insert a custom section header row into a summary table and indents a chosen group of variables underneath it.

In the example below, we indicate the desired header name to the header parameter and the column names to group to the variables parameter.

p values

We then use gtsummary::add_p to add p values to tables created using gtsummary::tbl_summary.

Please note that gtsummary::add_p requires the R package broom for it to work.

Click on the different tabs.

By default based on the documentation, as

  • the by variable used (Group) in gtsummary::tbl_summary has two variables (Control and Treatment).
  • group variable in gtsummary::add_p is NULL (not doing paired t test).
  • adj.vars variable in gtsummary::add_p is NULL (not doing ANCOVA).
  • Each categorical groups to compare has at least 5 people.

Wilcoxon rank-sum test (wilcox.test) is applied to all continuous variables and Pearson’s Chi-squared Test for Count Data without continuity correction (chisq.test.no.correct) is applied to all categorical variables.

Also, the number of decimal place used for the p values by default is 1, hence we update the pvalue_fun input to gtsummary::label_style_pvalue(digits = 2).

List of comparison tests that available within gtsummary or ways to create custom statistical tests can be found here.

Remove footnotes

Finally, we use gtsummary::remove_footnote_header to remove any unnecessary footnote as this will be added in final stage.

Previous Pregnancy Data

In the next section, we proceed to build the previous pregnancy summary tables.

Here is the dataset needed.

Missingness Display

It is observed that this time we have to deal with missing values. By default, tbl_summary display missing values titled as “Unknown” followed by presenting its counts (number of missing values).

There are three main parameters that you can use to control what is displayed for missing values. They are the missing, missing_text, and missing_stat.

  • The missing parameter accepts a list of formulas as input to help users specify any of three options c("ifany", "no", "always") to each of the variables to determine in what situation will the missing values be displayed.
    • ifany means we only add a row for missingness if the variables have missing values.
    • no means we do not provide a row for missingness regardless if the variables have missing values or not.
    • always means we always provide a row for missingness regardless if the variables have missing values or not.
    • the default parameter for missing is all variables set to ifany or missing = gtsummary::everything() ~ "ifany".
  • The missing_text parameter accepts a string to determine what label to display for missing value. It is currently set to “Unknown” by default.
  • The missing_stats parameter accepts a string to determine what statistic to show on missing row. It is currently set to “{N_miss}” by default to highlight the frequency of missing values. Summary statistics on the number of available to display are
    • {N_obs} for total number of observations.
    • {N_miss} for number of missing observations.
    • {N_nonmiss} for number of non-missing observations.
    • {p_miss} for percentage of observations missing.
    • {p_nonmiss} for percentage of observations not missing

To match Table 1, what we need is to simply set missing = "no".

Exercise

Let’s make use of what we have learned in the previous section to replicate the previous pregnancy section in Table 1

Try out this exercise below. Ensure that

  • The labels are set correctly,
  • Statistics are displayed as counts (as integers) and percentages (in one decimal place).
  • p values are displayed in two decimal places.
NoteHint

Take a look at how age race and education summary table is created and copy its parameters to the previous pregnancy table.

TipFully worked solution:
opt_previous_preg_data |> gtsummary::tbl_summary( include = c( "Prev.preg", "previous_live_preterm_birth", "Spont.ab", "Induced.ab", "Any.stillbirth" ), by = "Group", missing = "no", label = list( "Prev.preg" ~ "Any pregnancy", "previous_live_preterm_birth" ~ "Live preterm birth", "Spont.ab" ~ "Spontaneous abortion", "Induced.ab" ~ "Induced abortion", "Any.stillbirth" ~ "Stillbirth" ), statistic = list( gtsummary::all_categorical() ~ "{n} ({p})" ), digits = list( gtsummary::all_categorical() ~ c(0, 1) ) ) |> gtsummary::add_variable_group_header( header = "Previous pregnancies - no. (%)", variables = c( "Prev.preg", "previous_live_preterm_birth", "Spont.ab", "Induced.ab", "Any.stillbirth" ) )|> gtsummary::add_p( pvalue_fun = gtsummary::label_style_pvalue(digits = 2) ) |> gtsummary::remove_footnote_header()
opt_previous_preg_data |>
  gtsummary::tbl_summary(
    include = c(
      "Prev.preg",
      "previous_live_preterm_birth",
      "Spont.ab", "Induced.ab", "Any.stillbirth"
    ),
    by = "Group",
    missing = "no",
    label = list(
      "Prev.preg" ~ "Any pregnancy",
      "previous_live_preterm_birth" ~ "Live preterm birth",
      "Spont.ab" ~ "Spontaneous abortion",
      "Induced.ab" ~ "Induced abortion",
      "Any.stillbirth" ~ "Stillbirth"
    ),
    statistic = list(
      gtsummary::all_categorical() ~ "{n} ({p})"
    ),
    digits = list(
      gtsummary::all_categorical() ~ c(0, 1)
    )
  ) |> 
  gtsummary::add_variable_group_header(
    header = "Previous pregnancies - no. (%)",
    variables = c(
      "Prev.preg",
      "previous_live_preterm_birth",
      "Spont.ab", "Induced.ab", "Any.stillbirth"
    )
  )|> 
  gtsummary::add_p(
    pvalue_fun = gtsummary::label_style_pvalue(digits = 2)
  ) |> 
  gtsummary::remove_footnote_header()

Clinical Data

In the next section, we proceed to build the clinical summary tables.

Here is the dataset needed.

Customised p value format

Let’s create the clinical summary table using tbl_summary.

It is observed that using pvalue_fun = gtsummary::label_style_pvalue(digits = 2) give >0.99 for Self-reported alcohol use. One way to match Table 1 is to create a customised function to control how p values are reported.

Below is an example of how a customised function p_value_style is created and used to ensure it matches the p value reporting style you see in Table 1 and later Table 2.

Steps ahead

From this section, we have managed to create the summary table for age, race and education, previous pregnancy and clinical for Table 1. In the next section, we proceed to combine the three tables with further modification to match Table 1.