inst/doc/a03_cohorts.R

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

## ----eval=FALSE---------------------------------------------------------------
# library(syrona)
# 
# # Connect with write access (needed to create cohort table)
# db <- syrona_connect_pg(
#   dbname = "omop",
#   user = "analyst",
#   cdm_schema = "ohdsi_cdm_202511",
#   write_schema = "results_analyst"
# )
# 
# # Find available care sites
# list_care_sites(db$con, cdm_schema = "ohdsi_cdm_202511")
# #>   care_site_id    care_site_name  n_patients
# #> 1          101   Central Hospital      45000
# #> 2          205   University Clinic     28000
# #> 3          312   Regional Hospital     15000

## ----eval=FALSE---------------------------------------------------------------
# # Create a cohort for each hospital
# create_caresite_cohort(
#   con = db$con,
#   care_site_id = 101,
#   cohort_id = 1,
#   cohort_schema = "results_analyst",
#   cdm_schema = "ohdsi_cdm_202511"
# )
# 
# create_caresite_cohort(
#   con = db$con,
#   care_site_id = 205,
#   cohort_id = 2,
#   cohort_schema = "results_analyst",
#   cdm_schema = "ohdsi_cdm_202511"
# )

## ----eval=FALSE---------------------------------------------------------------
# # Extract each hospital's data
# extract_all("Central_Hospital", db = db,
#             cohort_id = 1, cohort_schema = "results_analyst")
# 
# extract_all("University_Clinic", db = db,
#             cohort_id = 2, cohort_schema = "results_analyst")
# 
# # Then compare
# compare_all("Central_Hospital", "University_Clinic")

## ----eval=FALSE---------------------------------------------------------------
# cohort_summary(db$con, cohort_id = 1, cohort_schema = "results_analyst")
# #>   cohort_definition_id n_entries n_persons  min_start    max_end
# #> 1                    1     45000     45000 2005-01-03 2023-11-28

## ----eval=FALSE---------------------------------------------------------------
# delete_cohort(db$con, cohort_id = 1, cohort_schema = "results_analyst")

## ----eval=FALSE---------------------------------------------------------------
# # Build a cohort data frame
# cohort_df <- data.frame(
#   cohort_definition_id = 1L,
#   subject_id = c(1001L, 1002L, 1003L),
#   cohort_start_date = as.Date("2015-01-01"),
#   cohort_end_date = as.Date("2020-12-31")
# )
# 
# # Upload to the CDM (requires writeSchema)
# db$cdm <- insert_cohort(db$cdm, cohort_df, name = "my_cohort")
# 
# # The cohort is now a table in the database, usable by Syrona and other OHDSI tools

## ----eval=FALSE---------------------------------------------------------------
# # Default: one row per person (first visit start to last visit end)
# create_caresite_cohort(..., collapse_strategy = "person_span")
# 
# # Alternative: one row per visit (multiple cohort entries per person)
# create_caresite_cohort(..., collapse_strategy = "visit_occurrence")

## ----eval=FALSE---------------------------------------------------------------
# # Default: clip to observation period overlap
# create_caresite_cohort(..., restrict_to_observation = TRUE)
# 
# # Skip clipping (use raw visit dates)
# create_caresite_cohort(..., restrict_to_observation = FALSE)

## ----eval=FALSE---------------------------------------------------------------
# # Example: use CohortConstructor to create a matched control cohort
# library(CohortConstructor)
# db$cdm$matched <- matchCohorts(db$cdm$my_cohort, ratio = 2, name = "matched")

Try the syrona package in your browser

Any scripts or data that you put into this service are public.

syrona documentation built on Sept. 5, 2026, 1:06 a.m.