8  Exercises

Let’s build a full set of publication tables with new data. Each problem has an empty code cell; write your code there and run it with “Run Code”. Sample solutions are collapsed.

8.1 Data: survival data of lung cancer patients (lung)

This is data on 228 patients with advanced lung cancer, bundled with the survival package. It is awkward to use as is, so we first tidy it in the cell below (already run).

Tip

Variable labels can be set with attr(x, "label") <- "...". Using set_variable_labels() from the labelled package is even easier.

8.2 Problem 1: A basic Table 1

For lung_df, create a patient characteristics table by sex (sex). Requirements:

  • Variables: age, ecog, ph.karno, wt.loss, death
  • age as mean (SD), other continuous variables as median (Q1, Q3)
  • Add an overall column at the far left

8.3 Problem 2: Add p-values

Add p-values to the table from Problem 1. Requirements:

  • t-test for age, Fisher’s exact test for ecog, the default tests for the rest
  • Show p-values with 3 decimal places and bold those below 0.05

Fisher’s test for ecog can take a long time to compute when there are many cells. In that case you can use a simulated p-value with test.args = ecog ~ list(simulate.p.value = TRUE).

8.4 Problem 3: Polish the appearance

Polish the table from Problem 2 as follows.

  • Bold variable names
  • Group headers in the form “Male (line break) n = 138”
  • A spanning header “Sex” above the groups
  • Title “Table 1. Baseline characteristics”

all_stat_cols(stat_0 = FALSE) means “the group columns, excluding the overall column (stat_0)”.

8.5 Problem 4: A stratified table

Stratify by age group (age_grp) and create a table comparing ph.karno, wt.loss and death by sex within each stratum (with p-values, missing rows hidden).

8.6 Problem 5: A Cox regression table (univariable + multivariable)

With time to death Surv(time, death) as the outcome, create a table that places side by side the hazard ratios from

  • a univariable Cox regression for each of age, sex and ecog
  • a multivariable Cox regression including all three at once

There is only one patient with ECOG = 3, so the confidence interval for that HR is extremely wide. In a real paper, you would consider remedies such as combining levels (e.g., merging 2–3).

8.7 Problem 6: Cite numbers in the text

From the multivariable model table in Problem 5, extract the hazard ratio for women versus men (with 95% CI and p-value) using inline_text().

8.8 Challenges

  1. Set theme_gtsummary_journal("jama"), rebuild the table from Problem 3, and check the differences (don’t forget reset_gtsummary_theme() at the end)
  2. Add add_difference(everything() ~ "smd") to the table from Problem 1 to show the balance between sexes as SMDs
  3. Turn the multivariable Cox regression from Problem 5 into a forest plot using the methods of Chapter 7 (plot() is fine; ggplot2 / ggstats must be loaded)
  4. On your own PC, save the table from Problem 3 to Word with as_flex_table() |> flextable::save_as_docx(path = "table1.docx")

Great work!