## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
kbl <- function(df) knitr::kable(df)

## -----------------------------------------------------------------------------
kbl(data.frame(
  cohort = c("Heart failure","Atrial fibrillation","Obesity","Acetaminophen",
             "Oxycodone","Mech ventilation","CBC lymphocytes","Empty (edge)"),
  domain = c("condition","condition","condition","drug","drug","procedure",
             "measurement","condition"),
  n = c(25,30,20,89,64,45,91,0),
  status = c(rep("ok",7),"handled-error")
))

## -----------------------------------------------------------------------------
kbl(data.frame(
  phenotype = c("Type 2 diabetes","Cardiac valve + AF","Acute liver injury",
                "Drug-induced pancreatitis","Treatment-resistant depression"),
  cohort_id = c(288, 1103, 736, 253, 1009),
  n_target = c(645, 481, 849, 485, 775),
  status = "ok (4 checks)"
))

## ----eval = FALSE-------------------------------------------------------------
# con <- cdm_connect(
#   dbname = "FHIR", user = "...", password = "...",
#   cdm_schema = "mimiciv_omop",   # clinical
#   vocab_schema = "vocab"          # concept, concept_ancestor
# )

## ----eval = FALSE-------------------------------------------------------------
# DBI::dbExecute(con, "
#   CREATE TABLE mimiciv_omop.observation_period AS
#   SELECT ROW_NUMBER() OVER (ORDER BY person_id) AS observation_period_id,
#          person_id,
#          MIN(d) AS observation_period_start_date,
#          MAX(d) AS observation_period_end_date,
#          44814724 AS period_type_concept_id
#   FROM (
#     SELECT person_id, condition_start_date d FROM mimiciv_omop.condition_occurrence
#     UNION ALL SELECT person_id, drug_exposure_start_date FROM mimiciv_omop.drug_exposure
#     UNION ALL SELECT person_id, measurement_date FROM mimiciv_omop.measurement
#     UNION ALL SELECT person_id, procedure_date FROM mimiciv_omop.procedure_occurrence
#     UNION ALL SELECT person_id, visit_start_date FROM mimiciv_omop.visit_occurrence
#   ) e WHERE d IS NOT NULL GROUP BY person_id")

## ----eval = FALSE-------------------------------------------------------------
# DBI::dbExecute(con, "
#   INSERT INTO results.phenotype_cohorts
#   SELECT person_id, 288 AS cohort_definition_id, MIN(d), MAX(d) FROM (
#     SELECT person_id, condition_start_date d FROM mimiciv_omop.condition_occurrence
#      WHERE condition_concept_id IN (
#        SELECT descendant_concept_id FROM vocab.concept_ancestor
#         WHERE ancestor_concept_id IN ( /* gold seeds */ )
#           AND descendant_concept_id NOT IN (
#             SELECT descendant_concept_id FROM vocab.concept_ancestor
#              WHERE ancestor_concept_id IN ( /* excluded */ )))
#     /* UNION ALL the drug/measurement/procedure domains likewise */
#   ) e GROUP BY person_id")

## -----------------------------------------------------------------------------
seeds <- read.csv(system.file("extdata", "phenotype_seeds.csv",
                              package = "rwevalidate"))
kbl(seeds[, c("cohort_id", "phenotype", "n_seed", "n_excluded")])

## -----------------------------------------------------------------------------
# Example: the Type 2 diabetes seed concept ids
strsplit(seeds$seed_concept_ids[seeds$phenotype == "t2dm"], ";")[[1]]

## ----eval = FALSE-------------------------------------------------------------
# validate_cohort(
#   cdm_schema   = "mimiciv_omop",
#   cohort_table = "results.phenotype_cohorts",
#   cohort_id    = 288,
#   comparator_id = 10288,           # complement
#   concept_ids  = c(443238, 201820, 442793),  # seeds -> Module 1
#   concept_domain = "condition",
#   vocab_schema = "vocab",
#   output_dir   = "./validation_report"
# )

## -----------------------------------------------------------------------------
list.files(system.file("extdata", package = "rwevalidate"))

