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

## ----setup--------------------------------------------------------------------
#  library(OdysseusSurvivalModule)
#  library(DatabaseConnector)
#  library(Eunomia)

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

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

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

## ----inspect-output-----------------------------------------------------------
#  overall_curve <- kmFit[["overall"]]$data
#  female_curve <- kmFit[["gender=Female"]]$data
#  
#  head(overall_curve)
#  head(female_curve)

## ----plot-km------------------------------------------------------------------
#  plot(
#    kmFit[["overall"]]$data$time,
#    kmFit[["overall"]]$data$survival,
#    type = "s",
#    xlab = "Time (days)",
#    ylab = "Survival probability",
#    main = "Overall Kaplan-Meier curve"
#  )

## ----other-models-------------------------------------------------------------
#  coxFit <- singleEventSurvival(
#    survivalData = survivalData,
#    timeScale = "days",
#    model = "cox",
#    covariates = c("age_years"),
#    confInt = 0.95
#  )
#  
#  weibullFit <- singleEventSurvival(
#    survivalData = survivalData,
#    timeScale = "days",
#    model = "weibull",
#    covariates = c("age_years")
#  )
#  
#  coxFit[["overall"]]$summary
#  weibullFit[["overall"]]$summary

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

