1. R Markdown Setup

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.3     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(janitor)
## 
## Attaching package: 'janitor'
## 
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
library(survival)
library(gtsummary)

2. Load Data

lung2 <- survival::lung %>%
  clean_names()

3. Label Variables

lung3 <- lung2 %>%
  mutate(
            status = as_factor(case_match(status, 1 ~ "Censored", 2 ~ "Death")),
            sex = as_factor(case_match(sex, 1 ~ "Male", 2 ~ "Female")),
            ph_ecog = as_factor(ph_ecog),
            ph_karno = as.numeric(ph_karno),
            pat_karno = as.numeric(pat_karno)  
            )
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `status = as_factor(case_match(status, 1 ~ "Censored", 2 ~
##   "Death"))`.
## Caused by warning:
## ! `case_match()` was deprecated in dplyr 1.2.0.
## ℹ Please use `recode_values()` instead.

4. Summary Stats Overall

nrow(lung3)
## [1] 228
lung3 %>%
  select(time, age, meal_cal, wt_loss) %>%
  summary()
##       time             age           meal_cal         wt_loss       
##  Min.   :   5.0   Min.   :39.00   Min.   :  96.0   Min.   :-24.000  
##  1st Qu.: 166.8   1st Qu.:56.00   1st Qu.: 635.0   1st Qu.:  0.000  
##  Median : 255.5   Median :63.00   Median : 975.0   Median :  7.000  
##  Mean   : 305.2   Mean   :62.45   Mean   : 928.8   Mean   :  9.832  
##  3rd Qu.: 396.5   3rd Qu.:69.00   3rd Qu.:1150.0   3rd Qu.: 15.750  
##  Max.   :1022.0   Max.   :82.00   Max.   :2600.0   Max.   : 68.000  
##                                   NAs    :47       NAs    :14

The overall N = 228. Age’s median and mean are practically identical given the age range of 39 to 82 years old. Time and age have no missing values, however mean_cal has 47 missing values and wt_loss has 14 missing values.

5. Choose A Grouping Variable

The grouping variable I am choosing to analyze is sex.

6. Table 1

tbl1_bysex <- lung3 %>%
  dplyr::select(sex, age, time, status, ph_ecog, ph_karno, pat_karno, meal_cal, wt_loss) %>%
  tbl_summary(
    by = sex,
    statistic = all_continuous() ~ '{median} [{p25}. {p75}]',
    digits = all_continuous() ~ 1,
    label = list(
      age ~ 'Age (years)',
      time ~ 'Follow-up Time (days)',
      status ~ 'Vital Status',
      ph_ecog ~ 'Physician-Rated ECOG Performance Status',
      ph_karno ~ 'Physician-Rated Karnofsky Score (0-100)',
      pat_karno ~ 'Patient-Rated Karnofsky Score (0-100)',
      meal_cal ~ 'Calories Consumed at Meals (kcal)',
      wt_loss ~ 'Weight Lost in the Last 6 Months (6 lbs)'
    ),
    missing = 'ifany'
  ) %>%
  add_overall() %>%
  bold_labels

tbl1_bysex
Characteristic Overall
N = 228
1
Male
N = 138
1
Female
N = 90
1
Age (years) 63.0 [56.0. 69.0] 64.0 [57.0. 70.0] 61.0 [55.0. 68.0]
Follow-up Time (days) 255.5 [166.5. 399.0] 224.0 [144.0. 371.0] 292.5 [194.0. 450.0]
Vital Status


    Death 165 (72%) 112 (81%) 53 (59%)
    Censored 63 (28%) 26 (19%) 37 (41%)
Physician-Rated ECOG Performance Status


    0 63 (28%) 36 (26%) 27 (30%)
    1 113 (50%) 71 (52%) 42 (47%)
    2 50 (22%) 29 (21%) 21 (23%)
    3 1 (0.4%) 1 (0.7%) 0 (0%)
    Unknown 1 1 0
Physician-Rated Karnofsky Score (0-100)


    50 6 (2.6%) 4 (2.9%) 2 (2.2%)
    60 19 (8.4%) 11 (8.0%) 8 (8.9%)
    70 32 (14%) 20 (15%) 12 (13%)
    80 67 (30%) 40 (29%) 27 (30%)
    90 74 (33%) 45 (33%) 29 (32%)
    100 29 (13%) 17 (12%) 12 (13%)
    Unknown 1 1 0
Patient-Rated Karnofsky Score (0-100)


    30 2 (0.9%) 1 (0.7%) 1 (1.1%)
    40 2 (0.9%) 1 (0.7%) 1 (1.1%)
    50 4 (1.8%) 2 (1.5%) 2 (2.2%)
    60 30 (13%) 18 (13%) 12 (13%)
    70 41 (18%) 30 (22%) 11 (12%)
    80 51 (23%) 32 (24%) 19 (21%)
    90 60 (27%) 31 (23%) 29 (33%)
    100 35 (16%) 21 (15%) 14 (16%)
    Unknown 3 2 1
Calories Consumed at Meals (kcal) 975.0 [635.0. 1,150.0] 1,025.0 [768.0. 1,175.0] 925.0 [588.0. 1,075.0]
    Unknown 47 24 23
Weight Lost in the Last 6 Months (6 lbs) 7.0 [0.0. 16.0] 8.0 [0.5. 19.0] 4.0 [0.0. 11.0]
    Unknown 14 10 4
1 Median [Q1. Q3]; n (%)

7. Figure

fig1_AgevsTime <- ggplot(lung3, aes(x = age, y = time, color = sex)) +
  geom_point(alpha = 0.6) +
  geom_smooth(method = 'lm', se = FALSE) +
  scale_color_brewer(palette = 'Set2') +
  labs(title = 'Survival Time vs Age based on Sex',
       x = 'Age (years)',
       y = 'Survival Time (days)',
       color = 'Sex') +
  theme_minimal(base_size = 12) +
  theme(legend.position = 'bottom',
        plot.title = element_text(face = 'bold', hjust = 0.5))

fig1_AgevsTime
## `geom_smooth()` using formula = 'y ~ x'

8. Interpretation

The overall study included 228 patients (138 male and 90 female) with median age of 63 years. The scatterplot shows little relationship between age and survival time. The fitted line declines slightly with men, however, it is mostly flat across women, Survival times span from near 0 to over 1000 days at nearly every age. The female line sits at or above the male line for most of the age range, which matches the sex difference described in Table 1.