title: “Survival Analysis Report”
author: “Generated by R”
date: “2025-02-27”
output: word_document

Simulating Data

set.seed(123)
n <- 500
data <- data.frame(
  time = rexp(n, rate = 0.1),
  status = sample(0:1, n, replace = TRUE),
  arm = sample(c("A", "B"), n, replace = TRUE),
  ctDNA_9p24.1 = sample(c(0, 1), n, replace = TRUE),
  MTB_baseline = runif(n, 0, 100),
  MTB_C3D1 = rnorm(n, mean = 0, sd = 1),
  MTB_EOT = sample(c(0, 1), n, replace = TRUE),
  EBV_ctDNA = sample(c(0, 1), n, replace = TRUE),
  EBER_ISH = sample(c(0, 1), n, replace = TRUE)
)

Survival Analysis Function

do_survival_analysis <- function(data, variable, threshold, cohort_label) {
  data <- data %>%
    mutate(Group = factor(ifelse(get(variable) > threshold, "High", "Low")))
  
  fit <- survfit(Surv(time, status) ~ Group, data = data)
  ggsurvplot(fit, data = data, pval = TRUE)
  
  cox_fit <- tryCatch({
    coxph(Surv(time, status) ~ Group, data = data)
  }, error = function(e) return(NULL))
  
  if (!is.null(cox_fit)) {
    cox_results <- summary(cox_fit)$coefficients %>%
      as.data.frame() %>%
      tibble::rownames_to_column(var = "Variable") %>%
      mutate(across(where(is.numeric), ~ signif(.x, 3))) # Round numbers to 3 significant figures
    
    # Extract hazard ratio and confidence intervals
    hr <- signif(exp(cox_results$`coef`), 3) # Convert log HR to HR
    ci_lower <- signif(exp(cox_results$`coef` - 1.96 * cox_results$`se(coef)`), 3)
    ci_upper <- signif(exp(cox_results$`coef` + 1.96 * cox_results$`se(coef)`), 3)
    
    cat("\n### Survival Analysis for", cohort_label, "\n")
    cat("We performed a Cox proportional hazards regression for", variable, "in", cohort_label, ". The estimated hazard ratio (HR) was", 
        hr, "with a 95% confidence interval of (", ci_lower, ",", ci_upper, ").\n")
    
    # Format table for Word with enhanced styling
    table_flex <- flextable(cox_results) %>%
      theme_vanilla() %>%
      set_caption(paste("Cox Regression Results for", cohort_label)) %>%
      autofit() %>%
      bold(part = "header") %>%
      align(align = "center", part = "all") %>%
      fontsize(size = 12, part = "all")
    
    print(table_flex)
  } else {
    message("Survival analysis for ", variable, " in ", cohort_label, " skipped due to insufficient data.")
  }
}

Perform Survival Analyses

do_survival_analysis(data, "ctDNA_9p24.1", 0, "Entire Cohort")
## 
## ### Survival Analysis for Entire Cohort 
## We performed a Cox proportional hazards regression for ctDNA_9p24.1 in Entire Cohort . The estimated hazard ratio (HR) was 1.01 with a 95% confidence interval of ( 0.786 , 1.3 ).
## a flextable object.
## col_keys: `Variable`, `coef`, `exp(coef)`, `se(coef)`, `z`, `Pr(>|z|)` 
## header has 1 row(s) 
## body has 1 row(s) 
## original dataset sample: 
##   Variable   coef exp(coef) se(coef)      z Pr(>|z|)
## 1 GroupLow 0.0103      1.01    0.128 0.0806    0.936
for (arm in unique(data$arm)) {
  do_survival_analysis(filter(data, arm == arm), "ctDNA_9p24.1", 0, paste("Arm", arm))
}
## 
## ### Survival Analysis for Arm A 
## We performed a Cox proportional hazards regression for ctDNA_9p24.1 in Arm A . The estimated hazard ratio (HR) was 1.01 with a 95% confidence interval of ( 0.786 , 1.3 ).
## a flextable object.
## col_keys: `Variable`, `coef`, `exp(coef)`, `se(coef)`, `z`, `Pr(>|z|)` 
## header has 1 row(s) 
## body has 1 row(s) 
## original dataset sample: 
##   Variable   coef exp(coef) se(coef)      z Pr(>|z|)
## 1 GroupLow 0.0103      1.01    0.128 0.0806    0.936
## 
## ### Survival Analysis for Arm B 
## We performed a Cox proportional hazards regression for ctDNA_9p24.1 in Arm B . The estimated hazard ratio (HR) was 1.01 with a 95% confidence interval of ( 0.786 , 1.3 ).
## a flextable object.
## col_keys: `Variable`, `coef`, `exp(coef)`, `se(coef)`, `z`, `Pr(>|z|)` 
## header has 1 row(s) 
## body has 1 row(s) 
## original dataset sample: 
##   Variable   coef exp(coef) se(coef)      z Pr(>|z|)
## 1 GroupLow 0.0103      1.01    0.128 0.0806    0.936
do_survival_analysis(data, "MTB_baseline", median(data$MTB_baseline, na.rm = TRUE), "Entire Cohort")
## 
## ### Survival Analysis for Entire Cohort 
## We performed a Cox proportional hazards regression for MTB_baseline in Entire Cohort . The estimated hazard ratio (HR) was 0.969 with a 95% confidence interval of ( 0.756 , 1.24 ).
## a flextable object.
## col_keys: `Variable`, `coef`, `exp(coef)`, `se(coef)`, `z`, `Pr(>|z|)` 
## header has 1 row(s) 
## body has 1 row(s) 
## original dataset sample: 
##   Variable   coef exp(coef) se(coef)      z Pr(>|z|)
## 1 GroupLow -0.031     0.969    0.127 -0.245    0.807
for (arm in unique(data$arm)) {
  do_survival_analysis(filter(data, arm == arm), "MTB_baseline", median(data$MTB_baseline, na.rm = TRUE), paste("Arm", arm))
}
## 
## ### Survival Analysis for Arm A 
## We performed a Cox proportional hazards regression for MTB_baseline in Arm A . The estimated hazard ratio (HR) was 0.969 with a 95% confidence interval of ( 0.756 , 1.24 ).
## a flextable object.
## col_keys: `Variable`, `coef`, `exp(coef)`, `se(coef)`, `z`, `Pr(>|z|)` 
## header has 1 row(s) 
## body has 1 row(s) 
## original dataset sample: 
##   Variable   coef exp(coef) se(coef)      z Pr(>|z|)
## 1 GroupLow -0.031     0.969    0.127 -0.245    0.807
## 
## ### Survival Analysis for Arm B 
## We performed a Cox proportional hazards regression for MTB_baseline in Arm B . The estimated hazard ratio (HR) was 0.969 with a 95% confidence interval of ( 0.756 , 1.24 ).
## a flextable object.
## col_keys: `Variable`, `coef`, `exp(coef)`, `se(coef)`, `z`, `Pr(>|z|)` 
## header has 1 row(s) 
## body has 1 row(s) 
## original dataset sample: 
##   Variable   coef exp(coef) se(coef)      z Pr(>|z|)
## 1 GroupLow -0.031     0.969    0.127 -0.245    0.807
do_survival_analysis(data, "EBV_ctDNA", 0, "Entire Cohort")
## 
## ### Survival Analysis for Entire Cohort 
## We performed a Cox proportional hazards regression for EBV_ctDNA in Entire Cohort . The estimated hazard ratio (HR) was 0.931 with a 95% confidence interval of ( 0.726 , 1.19 ).
## a flextable object.
## col_keys: `Variable`, `coef`, `exp(coef)`, `se(coef)`, `z`, `Pr(>|z|)` 
## header has 1 row(s) 
## body has 1 row(s) 
## original dataset sample: 
##   Variable    coef exp(coef) se(coef)      z Pr(>|z|)
## 1 GroupLow -0.0716     0.931    0.127 -0.564    0.573
for (arm in unique(data$arm)) {
  do_survival_analysis(filter(data, arm == arm), "EBV_ctDNA", 0, paste("Arm", arm))
}
## 
## ### Survival Analysis for Arm A 
## We performed a Cox proportional hazards regression for EBV_ctDNA in Arm A . The estimated hazard ratio (HR) was 0.931 with a 95% confidence interval of ( 0.726 , 1.19 ).
## a flextable object.
## col_keys: `Variable`, `coef`, `exp(coef)`, `se(coef)`, `z`, `Pr(>|z|)` 
## header has 1 row(s) 
## body has 1 row(s) 
## original dataset sample: 
##   Variable    coef exp(coef) se(coef)      z Pr(>|z|)
## 1 GroupLow -0.0716     0.931    0.127 -0.564    0.573
## 
## ### Survival Analysis for Arm B 
## We performed a Cox proportional hazards regression for EBV_ctDNA in Arm B . The estimated hazard ratio (HR) was 0.931 with a 95% confidence interval of ( 0.726 , 1.19 ).
## a flextable object.
## col_keys: `Variable`, `coef`, `exp(coef)`, `se(coef)`, `z`, `Pr(>|z|)` 
## header has 1 row(s) 
## body has 1 row(s) 
## original dataset sample: 
##   Variable    coef exp(coef) se(coef)      z Pr(>|z|)
## 1 GroupLow -0.0716     0.931    0.127 -0.564    0.573
do_survival_analysis(data, "EBER_ISH", 0, "Validation Cohort")
## 
## ### Survival Analysis for Validation Cohort 
## We performed a Cox proportional hazards regression for EBER_ISH in Validation Cohort . The estimated hazard ratio (HR) was 0.938 with a 95% confidence interval of ( 0.73 , 1.2 ).
## a flextable object.
## col_keys: `Variable`, `coef`, `exp(coef)`, `se(coef)`, `z`, `Pr(>|z|)` 
## header has 1 row(s) 
## body has 1 row(s) 
## original dataset sample: 
##   Variable    coef exp(coef) se(coef)      z Pr(>|z|)
## 1 GroupLow -0.0645     0.938    0.128 -0.502    0.615