# Building a mock from data

# Introduction

Most of the functionalities of omock are used to build specific mock tables (e.g. `mockPerson()`, `mockObservationPeriod()`, …), this allows the user to create mock cdm objects combining all those functions with some room for customisation. There are times where the user will want to create a mock CDM reference from its own bespoke tables. The `mockCdmFromTables()` function is designed to facilitates the creation of mock CDM reference from bespoke tables. This functionality will be useful to create a mock CDM from a `cohort_table` or a `drug_exposure` table, or with incomplete data (e.g. missing columns).

    library(omock)
    library(dplyr, warn.conflicts = FALSE)
    #> Warning: package 'dplyr' was built under R version 4.3.3
    library(PatientProfiles)

# Create a mock cdm from a cohort table

For example if you want to create a CDM reference based on below bespoke cohorts. You can do it simple using the mockCdmFromTable() functions in a few lines of code.

    # Define a list of user-defined cohort tables
    cohortTables <- list(
      cohort1 = tibble(
        subject_id = 1:10L,
        cohort_definition_id = rep(1L, 10),
        cohort_start_date = as.Date("2020-01-01") + 1:10,
        cohort_end_date = as.Date("2020-01-01") + 11:20
      ),
      cohort2 = tibble(
        subject_id = 11:20L,
        cohort_definition_id = rep(2L, 10),
        cohort_start_date = as.Date("2020-02-01") + 1:10,
        cohort_end_date = as.Date("2020-02-01") + 11:20
      )
    )

    # Create a mock CDM object from the user-defined tables
    cdm <- mockCdmFromTables(tables = cohortTables)

    cdm
    #>
    #> ── # OMOP CDM reference (local) of mock database ───────────────────────────────
    #> • omop tables: cdm_source, concept, concept_ancestor, concept_relationship,
    #> concept_synonym, drug_strength, observation_period, person, vocabulary
    #> • cohort tables: cohort1, cohort2
    #> • achilles tables: -
    #> • other tables: -

The generated CDM object will build the `person`, `observation_period` and vocabulary tables so that all the cohorts are in observation:

    cdm$cohort1 |>
      addInObservation()
    #> # A tibble: 10 × 5
    #>    cohort_definition_id subject_id cohort_start_date cohort_end_date
    #>  *                <int>      <int> <date>            <date>
    #>  1                    1          1 2020-01-02        2020-01-12
    #>  2                    1          2 2020-01-03        2020-01-13
    #>  3                    1          3 2020-01-04        2020-01-14
    #>  4                    1          4 2020-01-05        2020-01-15
    #>  5                    1          5 2020-01-06        2020-01-16
    #>  6                    1          6 2020-01-07        2020-01-17
    #>  7                    1          7 2020-01-08        2020-01-18
    #>  8                    1          8 2020-01-09        2020-01-19
    #>  9                    1          9 2020-01-10        2020-01-20
    #> 10                    1         10 2020-01-11        2020-01-21
    #> # ℹ 1 more variable: in_observation <int>
    cdm$observation_period
    #> # A tibble: 20 × 5
    #>    person_id observation_period_s…¹ observation_period_e…² observation_period_id
    #>  *     <int> <date>                 <date>                                 <int>
    #>  1         1 2016-12-17             2020-10-14                                 1
    #>  2         2 2019-04-28             2020-04-23                                 2
    #>  3         3 2016-08-22             2020-07-10                                 3
    #>  4         4 2019-04-16             2020-04-28                                 4
    #>  5         5 2019-11-18             2020-08-13                                 5
    #>  6         6 2016-05-26             2020-03-08                                 6
    #>  7         7 2017-07-31             2021-07-16                                 7
    #>  8         8 2015-09-19             2020-07-29                                 8
    #>  9         9 2018-05-11             2020-03-21                                 9
    #> 10        10 2019-08-07             2021-06-18                                10
    #> 11        11 2017-06-25             2023-01-08                                11
    #> 12        12 2019-03-10             2020-12-12                                12
    #> 13        13 2019-04-18             2020-07-14                                13
    #> 14        14 2016-09-05             2020-03-08                                14
    #> 15        15 2005-04-03             2020-07-01                                15
    #> 16        16 2018-07-01             2020-12-21                                16
    #> 17        17 2018-06-30             2021-05-25                                17
    #> 18        18 2007-07-26             2020-07-17                                18
    #> 19        19 2012-07-30             2021-05-11                                19
    #> 20        20 2019-11-05             2022-03-31                                20
    #> # ℹ abbreviated names: ¹​observation_period_start_date,
    #> #   ²​observation_period_end_date
    #> # ℹ 1 more variable: period_type_concept_id <int>

# Create a mock CDM from drug_exposure

Now we will create a CDM around a `drug_exposure` table, this functionality is quite useful to obtain mock datasets for testing purposes only specifying part of the information. In this case we will partially define `person` table to impose all individuals are women:

    person <- tibble(person_id = 1:5L, gender_concept_id = 8532L, year_of_birth = 1992)

and we will also create the records of the `drug_exposure` table:

    drugExposure <- tibble(
      person_id = rep(1:5L, 2),
      drug_concept_id = 19073188L,
      drug_exposure_start_date = rep(as.Date(c("2000-01-01", "2000-06-1")), each = 5),
      drug_exposure_end_date = drug_exposure_start_date + c(10L, 20L, 100L, 140L, 30L, 50L, 30L, 20L, 45L, 35L)
    )

Then `mockCdmFromTables()` will populate the missing columns with interpolated data and add all the tables necessary to create a minimum viable CDM (it will contain at least `person`, `observation_period` and the vocabulary tables):

    cdm <- mockCdmFromTables(tables = list(person = person, drug_exposure = drugExposure))

    cdm

As before all the records of `drug_exposure` will be in observation:

    cdm$drug_exposure |>
      addInObservation() |>
      group_by(in_observation) |>
      tally()