Skip to contents

This function simulates observation periods for individuals based on their date of birth recorded in the 'person' table of the CDM object. It assigns random start and end dates for each observation period within a realistic timeframe up to a specified or default maximum date.

Usage

mockObservationPeriod(cdm, seed = 1)

Arguments

cdm

A `cdm_reference` object that must include a 'person' table with valid dates of birth. This object serves as the base CDM structure where the observation period data will be added. The function checks to ensure that the 'person' table is populated and uses the date of birth to generate observation periods.

seed

An optional integer used to set the seed for random number generation, ensuring reproducibility of the generated data. If provided, this seed allows 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 'observation_period' table added. This table includes the simulated observation periods for each person, ensuring that each record spans a realistic timeframe based on the person's date of birth.

Examples

library(omock)

# Create a mock CDM reference and add observation periods
cdm <- mockCdmReference() |>
  mockPerson(nPerson = 100) |>
  mockObservationPeriod()

# View the generated observation period data
print(cdm$observation_period)
#> # A tibble: 100 × 5
#>    observation_period_id person_id observation_period_s…¹ observation_period_e…²
#>  *                 <int>     <int> <date>                 <date>                
#>  1                     1         1 2000-06-03             2013-06-29            
#>  2                     2         2 1999-04-05             2003-06-15            
#>  3                     3         3 2015-01-15             2015-10-11            
#>  4                     4         4 1989-12-09             2013-12-31            
#>  5                     5         5 2012-03-18             2013-02-10            
#>  6                     6         6 2010-11-13             2015-04-15            
#>  7                     7         7 2014-03-04             2014-04-09            
#>  8                     8         8 1984-10-07             2009-03-10            
#>  9                     9         9 1985-12-16             2009-09-17            
#> 10                    10        10 2019-11-23             2019-12-26            
#> # ℹ 90 more rows
#> # ℹ abbreviated names: ¹​observation_period_start_date,
#> #   ²​observation_period_end_date
#> # ℹ 1 more variable: period_type_concept_id <int>