Skip to contents

This function simulates drug exposure records for individuals within a specified cohort. It creates a realistic dataset by generating drug exposure records based on the specified number of records per person. Each drug exposure record is correctly associated with an individual within valid observation periods, ensuring the integrity of the data.

Usage

mockDrugExposure(cdm, recordPerson = 1, seed = NULL)

Arguments

cdm

A `cdm_reference` object that must already include 'person' and 'observation_period' tables. This object serves as the base CDM structure where the drug exposure data will be added. The 'person' and 'observation_period' tables must be populated as they are necessary for generating accurate drug exposure records.

recordPerson

An integer specifying the expected number of drug exposure records to generate per person. This parameter allows for the simulation of varying drug usage frequencies among individuals in the cohort, reflecting real-world variability in medication administration.

seed

An optional integer used to set the seed for random number generation, ensuring reproducibility of the generated data. If provided, this seed enables the function to produce consistent results each time it is run with the same inputs. If 'NULL', the seed is not set, which can lead to different outputs on each run.

Value

Returns the modified `cdm` object with the new 'drug_exposure' table added. This table includes the simulated drug exposure data for each person, ensuring that each record is correctly linked to individuals in the 'person' table and falls within valid observation periods.

Examples

library(omock)

# Create a mock CDM reference and add drug exposure records
cdm <- mockCdmReference() |>
  mockPerson() |>
  mockObservationPeriod() |>
  mockDrugExposure(recordPerson = 3)

# View the generated drug exposure data
print(cdm$drug_exposure)
#> # A tibble: 180 × 23
#>    drug_concept_id person_id drug_exposure_start_date drug_exposure_end_date
#>  *           <int>     <int> <date>                   <date>                
#>  1              10         3 2004-03-01               2008-02-14            
#>  2              10         1 1962-06-21               1988-08-13            
#>  3              10         4 2011-07-11               2012-06-26            
#>  4              10         4 2009-11-16               2011-12-09            
#>  5              10         2 1994-12-21               2003-04-02            
#>  6              10         6 1968-10-22               1968-11-27            
#>  7              10         7 2018-03-04               2018-10-21            
#>  8              10         5 2013-08-12               2015-10-26            
#>  9              10         9 1988-07-19               1989-10-23            
#> 10              10         8 2002-11-12               2006-01-17            
#> # ℹ 170 more rows
#> # ℹ 19 more variables: drug_exposure_id <int>, drug_type_concept_id <int>,
#> #   drug_exposure_start_datetime <dttm>, drug_exposure_end_datetime <dttm>,
#> #   verbatim_end_date <date>, stop_reason <chr>, refills <int>, quantity <dbl>,
#> #   days_supply <int>, sig <chr>, route_concept_id <int>, lot_number <chr>,
#> #   provider_id <int>, visit_occurrence_id <int>, visit_detail_id <int>,
#> #   drug_source_value <chr>, drug_source_concept_id <int>, …