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

## ----setup--------------------------------------------------------------------
#  library(OdysseusSurvivalModule)
#  
#  survivalData <- data.frame(
#    subject_id = 1:8,
#    time = c(15, 21, 40, 55, 60, 74, 90, 120),
#    status = c(1, 0, 1, 0, 1, 0, 1, 0),
#    age_years = c(44, 51, 67, 39, 73, 58, 62, 47),
#    gender = c("Female", "Male", "Female", "Male", "Female", "Male", "Female", "Male")
#  )

## ----cox-model----------------------------------------------------------------
#  coxFit <- singleEventSurvival(
#    survivalData = survivalData,
#    timeScale = "days",
#    model = "cox",
#    covariates = c("age_years")
#  )
#  
#  coxFit[["overall"]]$summary
#  head(coxFit[["overall"]]$data)

## ----parametric-models--------------------------------------------------------
#  weibullFit <- singleEventSurvival(
#    survivalData = survivalData,
#    timeScale = "days",
#    model = "weibull",
#    covariates = c("age_years")
#  )
#  
#  lognormalFit <- singleEventSurvival(
#    survivalData = survivalData,
#    timeScale = "days",
#    model = "lognormal",
#    covariates = c("age_years")
#  )
#  
#  weibullFit[["overall"]]$summary
#  lognormalFit[["overall"]]$summary

## ----model-comparison---------------------------------------------------------
#  modelNames <- c("km", "cox", "weibull", "lognormal")
#  
#  fits <- lapply(modelNames, function(modelName) {
#    singleEventSurvival(
#      survivalData = survivalData,
#      timeScale = "days",
#      model = modelName,
#      covariates = if (modelName == "km") NULL else c("age_years")
#    )
#  })
#  names(fits) <- modelNames
#  
#  comparison <- data.frame(
#    model = names(fits),
#    medianSurvival = vapply(fits, function(x) x[["overall"]]$summary$medianSurvival, numeric(1)),
#    meanSurvival = vapply(fits, function(x) x[["overall"]]$summary$meanSurvival, numeric(1)),
#    stringsAsFactors = FALSE
#  )
#  
#  comparison

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

## ----curve-plot---------------------------------------------------------------
#  curveData <- weibullFit[["overall"]]$data
#  
#  plot(
#    curveData$time,
#    curveData$survival,
#    type = "l",
#    xlab = "Time (days)",
#    ylab = "Survival probability",
#    main = "Weibull survival curve"
#  )

