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

## ----setup-common-------------------------------------------------------------
#  library(OdysseusSurvivalModule)
#  library(DatabaseConnector)
#  library(Eunomia)
#  
#  connectionDetails <- Eunomia::getEunomiaConnectionDetails()
#  connection <- DatabaseConnector::connect(connectionDetails)
#  
#  cdmDatabaseSchema <- Eunomia::getEunomiaCdmDatabaseSchema()
#  cohortDatabaseSchema <- Eunomia::getEunomiaResultsSchema()

## ----example-build------------------------------------------------------------
#  survivalData <- OdysseusSurvivalModule:::addCohortSurvival(
#    connection = connection,
#    cdmDatabaseSchema = cdmDatabaseSchema,
#    cohortDatabaseSchema = cohortDatabaseSchema,
#    targetCohortTable = "cohort",
#    targetCohortId = 1,
#    outcomeCohortTable = "cohort",
#    outcomeCohortId = 2,
#    followUpDays = 365,
#    includeAge = TRUE,
#    includeGender = TRUE
#  )
#  
#  head(survivalData)

## ----example-km-overall-------------------------------------------------------
#  kmOverall <- singleEventSurvival(
#    survivalData = survivalData,
#    timeScale = "days",
#    model = "km"
#  )
#  
#  kmOverall[["overall"]]$summary
#  head(kmOverall[["overall"]]$data)

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

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

## ----example-models-----------------------------------------------------------
#  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
#  
#  summaryTable <- 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
#  )
#  
#  summaryTable

## ----example-report-----------------------------------------------------------
#  reportTable <- data.frame(
#    group = c("Overall", "Female", "Male"),
#    n = c(
#      kmGender[["overall"]]$summary$n,
#      kmGender[["gender=Female"]]$summary$n,
#      kmGender[["gender=Male"]]$summary$n
#    ),
#    events = c(
#      kmGender[["overall"]]$summary$events,
#      kmGender[["gender=Female"]]$summary$events,
#      kmGender[["gender=Male"]]$summary$events
#    ),
#    medianSurvival = c(
#      kmGender[["overall"]]$summary$medianSurvival,
#      kmGender[["gender=Female"]]$summary$medianSurvival,
#      kmGender[["gender=Male"]]$summary$medianSurvival
#    ),
#    stringsAsFactors = FALSE
#  )
#  
#  reportTable

## ----example-plot-------------------------------------------------------------
#  plot(
#    kmGender[["gender=Female"]]$data$time,
#    kmGender[["gender=Female"]]$data$survival,
#    type = "s",
#    col = "firebrick",
#    xlab = "Time (days)",
#    ylab = "Survival probability",
#    ylim = c(0, 1),
#    main = "Kaplan-Meier curve by gender"
#  )
#  
#  lines(
#    kmGender[["gender=Male"]]$data$time,
#    kmGender[["gender=Male"]]$data$survival,
#    type = "s",
#    col = "steelblue"
#  )

## ----cleanup-all--------------------------------------------------------------
#  DatabaseConnector::disconnect(connection)

