# Filtering cohorts

    library(CohortConstructor)
    library(CohortCharacteristics)
    library(ggplot2)

For this example we’ll use the Eunomia synthetic data from the CDMConnector package.

    con <- DBI::dbConnect(duckdb::duckdb(), dbdir = eunomia_dir())
    cdm <- cdm_from_con(con, cdm_schema = "main",
                        write_schema = c(prefix = "my_study_", schema = "main"))

Let’s start by creating two drug cohorts, one for users of diclofenac and another for users of acetaminophen.

    cdm$medications <- conceptCohort(cdm = cdm,
                                     conceptSet = list("diclofenac" = 1124300,
                                                       "acetaminophen" = 1127433),
                                     name = "medications")
    cohortCount(cdm$medications)
    #> # A tibble: 2 × 3
    #>   cohort_definition_id number_records number_subjects
    #>                  <int>          <int>           <int>
    #> 1                    1           9365            2580
    #> 2                    2            830             830

We can take a sample from a cohort table using the function `sampleCohort()`. This allows us to specify the number of individuals in each cohort.

    cdm$medications |> sampleCohorts(cohortId = NULL, n = 100)
    #> # Source:   table<main.my_study_medications> [?? x 4]
    #> # Database: DuckDB v1.0.0 [eburn@Windows 10 x64:R 4.4.0/C:\Users\eburn\AppData\Local\Temp\Rtmpa806Vz\file2d98524a2d18.duckdb]
    #>    cohort_definition_id subject_id cohort_start_date cohort_end_date
    #>                   <int>      <int> <date>            <date>
    #>  1                    1       2839 2012-11-06        2012-11-13
    #>  2                    2       3761 1990-11-29        1990-11-29
    #>  3                    2       4570 1972-08-02        1972-08-02
    #>  4                    2       4643 1944-06-22        1944-06-22
    #>  5                    1        789 1989-05-07        1989-05-21
    #>  6                    1       2541 2003-11-06        2003-12-06
    #>  7                    1       2909 1990-07-15        1990-07-29
    #>  8                    1       3569 1977-01-14        1977-01-28
    #>  9                    2       2044 1997-01-18        1997-01-18
    #> 10                    2       4525 2007-08-04        2007-08-04
    #> # ℹ more rows

    cohortCount(cdm$medications)
    #> # A tibble: 2 × 3
    #>   cohort_definition_id number_records number_subjects
    #>                  <int>          <int>           <int>
    #> 1                    1            374             100
    #> 2                    2            100             100

When cohortId = NULL all cohorts in the table are used. Note that this function does not reduced the number of records in each cohort, only the number of individuals.

It is also possible to only sample one cohort within cohort table, however the remaining cohorts will still remain.

    cdm$medications <- cdm$medications |> sampleCohorts(cohortId = 2, n = 100)

    cohortCount(cdm$medications)
    #> # A tibble: 2 × 3
    #>   cohort_definition_id number_records number_subjects
    #>                  <int>          <int>           <int>
    #> 1                    1           9365            2580
    #> 2                    2            100             100

The chosen cohort (users of diclofenac) has been reduced to 100 individuals, as specified in the function, however all individuals from cohort 1 (users of acetaminophen) and their records remain.

If you want to filter the cohort table to only include individuals and records from a specified cohort, you can use the function `subsetCohorts`.

    cdm$medications <- cdm$medications |> subsetCohorts(cohortId = 2)
    cohortCount(cdm$medications)
    #> # A tibble: 1 × 3
    #>   cohort_definition_id number_records number_subjects
    #>                  <int>          <int>           <int>
    #> 1                    2            830             830

The cohort table has been filtered so it now only includes individuals and records from cohort 2. If you want to take a sample of the filtered cohort table then you can use the `sampleCohorts` function.

    cdm$medications <- cdm$medications |> sampleCohorts(cohortId = 2, n = 100)

    cohortCount(cdm$medications)
    #> # A tibble: 1 × 3
    #>   cohort_definition_id number_records number_subjects
    #>                  <int>          <int>           <int>
    #> 1                    2            100             100