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

## -----------------------------------------------------------------------------
if (requireNamespace("nanoparquet", quietly = TRUE)) {
  studyDir <- system.file("example", "st", package = "CohortContrast")
  study <- CohortContrast::loadCohortContrastStudy("lc500", pathToResults = studyDir)

  firstEventDay <- function(x) {
    values <- strsplit(as.character(x), ",")[[1]]
    as.numeric(trimws(values[1]))
  }

  patientData <- study$data_patients
  patientData$FIRST_TIME_TO_EVENT <- vapply(
    patientData$TIME_TO_EVENT,
    firstEventDay,
    numeric(1)
  )

  conceptCounts <- sort(table(patientData$CONCEPT_NAME), decreasing = TRUE)
  topConcepts <- names(conceptCounts)[1:5]
  trajectoryPreview <- patientData[patientData$CONCEPT_NAME %in% topConcepts, ]

  aggregate(
    FIRST_TIME_TO_EVENT ~ CONCEPT_NAME,
    data = trajectoryPreview,
    FUN = median
  )
}

