## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = FALSE
)

## ----setup--------------------------------------------------------------------
#  library(OdysseusSurvivalModule)
#  
#  survivalData <- data.frame(
#    subject_id = 1:12,
#    time = c(20, 35, 42, 50, 63, 70, 74, 85, 91, 105, 118, 140),
#    status = c(1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0),
#    age_years = c(34, 37, 41, 48, 52, 58, 61, 66, 71, 73, 77, 82),
#    gender = c("Female", "Male", "Female", "Male", "Female", "Male", "Female", "Male", "Female", "Male", "Female", "Male")
#  )

## ----gender-strata------------------------------------------------------------
#  fitGender <- singleEventSurvival(
#    survivalData = survivalData,
#    timeScale = "days",
#    model = "km",
#    strata = "gender"
#  )
#  
#  names(fitGender)
#  fitGender[["overall"]]$summary
#  fitGender[["gender=Female"]]$summary
#  fitGender[["gender=Male"]]$summary
#  fitGender$logrank_test_gender

## ----age-strata---------------------------------------------------------------
#  fitAge <- singleEventSurvival(
#    survivalData = survivalData,
#    timeScale = "days",
#    model = "km",
#    strata = "age_group",
#    ageBreaks = list(c(18, 49), c(50, 64), c(65, Inf))
#  )
#  
#  names(fitAge)
#  fitAge[["age_group=18-49"]]$summary
#  fitAge[["age_group=50-64"]]$summary
#  fitAge[["age_group=65+"]]$summary
#  fitAge$logrank_test_age_group

## ----both-strata--------------------------------------------------------------
#  fitBoth <- singleEventSurvival(
#    survivalData = survivalData,
#    timeScale = "days",
#    model = "km",
#    strata = c("gender", "age_group"),
#    ageBreaks = list(c(18, 49), c(50, 64), c(65, Inf))
#  )
#  
#  names(fitBoth)
#  fitBoth$logrank_test_gender
#  fitBoth$logrank_test_age_group

## ----stratum-curves-----------------------------------------------------------
#  femaleCurve <- fitGender[["gender=Female"]]$data
#  olderCurve <- fitAge[["age_group=65+"]]$data
#  
#  head(femaleCurve)
#  head(olderCurve)

## ----plot-strata--------------------------------------------------------------
#  plot(
#    fitGender[["gender=Female"]]$data$time,
#    fitGender[["gender=Female"]]$data$survival,
#    type = "s",
#    col = "firebrick",
#    xlab = "Time (days)",
#    ylab = "Survival probability",
#    ylim = c(0, 1),
#    main = "Gender-specific Kaplan-Meier curves"
#  )
#  
#  lines(
#    fitGender[["gender=Male"]]$data$time,
#    fitGender[["gender=Male"]]$data$survival,
#    type = "s",
#    col = "steelblue"
#  )
#  
#  legend(
#    "topright",
#    legend = c("Female", "Male"),
#    col = c("firebrick", "steelblue"),
#    lty = 1,
#    bty = "n"
#  )

## ----stratified-other-models--------------------------------------------------
#  fitCox <- singleEventSurvival(
#    survivalData = survivalData,
#    timeScale = "days",
#    model = "cox",
#    covariates = c("age_years"),
#    strata = "gender"
#  )
#  
#  fitWeibull <- singleEventSurvival(
#    survivalData = survivalData,
#    timeScale = "days",
#    model = "weibull",
#    covariates = c("age_years"),
#    strata = "age_group",
#    ageBreaks = list(c(18, 49), c(50, 64), c(65, Inf))
#  )
#  
#  fitCox[["gender=Female"]]$summary
#  fitWeibull[["age_group=65+"]]$summary

