The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.

1.1 Introduction

The OMOP CDM is a person-centric model. The person table contains records that uniquely identify each individual along with some of their demographic information. Below we create a mock CDM reference which, as is standard, has a person table which contains fields which indicate an individual’s date of birth, gender, race, and ethnicity. Each of these, except for date of birth, are represented by a concept ID (and as the person table contains one record per person these fields are treated as time-invariant).

library(PatientProfiles)
library(duckdb)
library(dplyr)

cdm <- mockPatientProfiles(numberIndividuals = 10000)

cdm$person |>
  dplyr::glimpse()
## Rows: ??
## Columns: 5
## Database: DuckDB v1.1.0 [root@Darwin 24.1.0:R 4.4.1/:memory:]
## $ person_id            <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15…
## $ gender_concept_id    <int> 8507, 8532, 8507, 8507, 8507, 8532, 8532, 8532, 8…
## $ year_of_birth        <int> 1973, 1970, 1914, 1920, 1923, 1924, 1910, 1977, 1…
## $ race_concept_id      <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ ethnicity_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…

As well as the person table, every CDM reference will include an observation period table. This table contains spans of times during which an individual is considered to being under observation. Individuals can have multiple observation periods, but they cannot overlap.

cdm$observation_period |>
  dplyr::glimpse()
## Rows: ??
## Columns: 5
## Database: DuckDB v1.1.0 [root@Darwin 24.1.0:R 4.4.1/:memory:]
## $ person_id                     <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1…
## $ observation_period_start_date <date> 1973-01-01, 1970-01-01, 1914-01-01, 192…
## $ observation_period_end_date   <date> 2010-08-13, 1999-03-14, 1946-06-09, 194…
## $ period_type_concept_id        <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0…
## $ observation_period_id         <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1…

When performing analyses we will often be interested in working with the person and observation period tables to identify individuals’ characteristics on some date of interest. PatientProfiles provides a number of functions that can help us do this.

1.2 Adding characteristics to OMOP CDM tables

Let’s say we’re working with the condition occurrence table.

cdm$condition_occurrence |>
  glimpse()
## Rows: ??
## Columns: 6
## Database: DuckDB v1.1.0 [root@Darwin 24.1.0:R 4.4.1/:memory:]
## $ person_id                 <int> 1781, 9710, 7877, 9609, 8357, 3492, 3916, 88…
## $ condition_start_date      <date> 1924-06-18, 1992-02-04, 1986-02-25, 1940-04…
## $ condition_end_date        <date> 1924-12-20, 2011-08-08, 1986-10-02, 1944-10…
## $ condition_occurrence_id   <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1…
## $ condition_concept_id      <int> 3, 2, 9, 5, 7, 2, 7, 4, 1, 6, 9, 3, 10, 1, 3…
## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…

This table contains diagnoses of individuals and we might, for example, want to identify their age on their date of diagnosis. This involves linking back to the person table which contains their date of birth (split across three different columns). PatientProfiles provides a simple function for this. addAge() will add a new column to the table containing each patient’s age relative to the specified index date.

cdm$condition_occurrence <- cdm$condition_occurrence |>
  addAge(indexDate = "condition_start_date")

cdm$condition_occurrence |>
  glimpse()
## Rows: ??
## Columns: 7
## Database: DuckDB v1.1.0 [root@Darwin 24.1.0:R 4.4.1/:memory:]
## $ person_id                 <int> 9710, 7877, 8357, 3492, 3916, 8861, 8526, 79…
## $ condition_start_date      <date> 1992-02-04, 1986-02-25, 1986-03-09, 1920-08…
## $ condition_end_date        <date> 2011-08-08, 1986-10-02, 1986-07-09, 1937-06…
## $ condition_occurrence_id   <int> 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16,…
## $ condition_concept_id      <int> 2, 9, 7, 2, 7, 4, 1, 6, 9, 3, 10, 1, 7, 8, 7…
## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ age                       <int> 20, 18, 14, 9, 3, 11, 23, 30, 17, 23, 26, 0,…

As well as calculating age, we can also create age groups at the same time. Here we create three age groups: those aged 0 to 17, those 18 to 65, and those 66 or older.

cdm$condition_occurrence <- cdm$condition_occurrence |>
  addAge(
    indexDate = "condition_start_date",
    ageGroup = list(
      "0 to 17" = c(0, 17),
      "18 to 65" = c(18, 65),
      ">= 66" = c(66, Inf)
    )
  )

cdm$condition_occurrence |>
  glimpse()
## Rows: ??
## Columns: 8
## Database: DuckDB v1.1.0 [root@Darwin 24.1.0:R 4.4.1/:memory:]
## $ person_id                 <int> 9710, 8526, 5685, 1360, 1954, 523, 3699, 189…
## $ condition_start_date      <date> 1992-02-04, 1930-11-25, 1907-05-23, 1927-03…
## $ condition_end_date        <date> 2011-08-08, 1931-12-10, 1932-05-20, 1939-10…
## $ condition_occurrence_id   <int> 2, 9, 14, 18, 29, 32, 34, 61, 91, 98, 103, 1…
## $ condition_concept_id      <int> 2, 1, 1, 7, 2, 7, 10, 3, 4, 2, 2, 10, 3, 10,…
## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ age                       <int> 20, 23, 0, 16, 20, 15, 19, 37, 20, 19, 30, 2…
## $ age_group                 <chr> "18 to 65", "18 to 65", "0 to 17", "0 to 17"…

By default, when adding age the new column will have been called “age” and will have been calculated using all available information on date of birth contained in the person. We can though also alter these defaults. Here, for example, we impose that month of birth is January and day of birth is the 1st for all individuals.

cdm$condition_occurrence <- cdm$condition_occurrence |>
  addAge(
    indexDate = "condition_start_date",
    ageName = "age_from_year_of_birth",
    ageMissingMonth = 1,
    ageMissingDay = 1,
    ageImposeMonth = TRUE,
    ageImposeDay = TRUE
  )

cdm$condition_occurrence |>
  glimpse()
## Rows: ??
## Columns: 9
## Database: DuckDB v1.1.0 [root@Darwin 24.1.0:R 4.4.1/:memory:]
## $ person_id                 <int> 9710, 8526, 5685, 1360, 1954, 523, 3699, 189…
## $ condition_start_date      <date> 1992-02-04, 1930-11-25, 1907-05-23, 1927-03…
## $ condition_end_date        <date> 2011-08-08, 1931-12-10, 1932-05-20, 1939-10…
## $ condition_occurrence_id   <int> 2, 9, 14, 18, 29, 32, 34, 61, 91, 98, 103, 1…
## $ condition_concept_id      <int> 2, 1, 1, 7, 2, 7, 10, 3, 4, 2, 2, 10, 3, 10,…
## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ age                       <int> 20, 23, 0, 16, 20, 15, 19, 37, 20, 19, 30, 2…
## $ age_group                 <chr> "18 to 65", "18 to 65", "0 to 17", "0 to 17"…
## $ age_from_year_of_birth    <int> 20, 23, 0, 16, 20, 15, 19, 37, 20, 19, 30, 2…

As well as age at diagnosis, we might also want identify patients’ sex. PatientProfiles provides the addSex() function that will add this for us. Because this is treated as time-invariant, we will not have to specify any index variable.

cdm$condition_occurrence <- cdm$condition_occurrence |>
  addSex()

cdm$condition_occurrence |>
  glimpse()
## Rows: ??
## Columns: 10
## Database: DuckDB v1.1.0 [root@Darwin 24.1.0:R 4.4.1/:memory:]
## $ person_id                 <int> 9710, 8526, 5685, 1360, 1954, 523, 3699, 189…
## $ condition_start_date      <date> 1992-02-04, 1930-11-25, 1907-05-23, 1927-03…
## $ condition_end_date        <date> 2011-08-08, 1931-12-10, 1932-05-20, 1939-10…
## $ condition_occurrence_id   <int> 2, 9, 14, 18, 29, 32, 34, 61, 91, 98, 103, 1…
## $ condition_concept_id      <int> 2, 1, 1, 7, 2, 7, 10, 3, 4, 2, 2, 10, 3, 10,…
## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ age                       <int> 20, 23, 0, 16, 20, 15, 19, 37, 20, 19, 30, 2…
## $ age_group                 <chr> "18 to 65", "18 to 65", "0 to 17", "0 to 17"…
## $ age_from_year_of_birth    <int> 20, 23, 0, 16, 20, 15, 19, 37, 20, 19, 30, 2…
## $ sex                       <chr> "Male", "Male", "Male", "Female", "Male", "M…

Similarly, we could also identify whether an individual was in observation at the time of their diagnosis (i.e. had an observation period that overlaps with their diagnosis date), as well as identifying how much prior observation time they had on this date and how much they have following it.

cdm$condition_occurrence <- cdm$condition_occurrence |>
  addInObservation(indexDate = "condition_start_date") |>
  addPriorObservation(indexDate = "condition_start_date") |>
  addFutureObservation(indexDate = "condition_start_date")

cdm$condition_occurrence |>
  glimpse()
## Rows: ??
## Columns: 13
## Database: DuckDB v1.1.0 [root@Darwin 24.1.0:R 4.4.1/:memory:]
## $ person_id                 <int> 9710, 8526, 5685, 1360, 1954, 523, 3699, 189…
## $ condition_start_date      <date> 1992-02-04, 1930-11-25, 1907-05-23, 1927-03…
## $ condition_end_date        <date> 2011-08-08, 1931-12-10, 1932-05-20, 1939-10…
## $ condition_occurrence_id   <int> 2, 9, 14, 18, 29, 32, 34, 61, 91, 98, 103, 1…
## $ condition_concept_id      <int> 2, 1, 1, 7, 2, 7, 10, 3, 4, 2, 2, 10, 3, 10,…
## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ age                       <int> 20, 23, 0, 16, 20, 15, 19, 37, 20, 19, 30, 2…
## $ age_group                 <chr> "18 to 65", "18 to 65", "0 to 17", "0 to 17"…
## $ age_from_year_of_birth    <int> 20, 23, 0, 16, 20, 15, 19, 37, 20, 19, 30, 2…
## $ sex                       <chr> "Male", "Male", "Male", "Female", "Male", "M…
## $ in_observation            <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
## $ prior_observation         <int> 7339, 8729, 142, 5918, 7477, 5618, 7205, 135…
## $ future_observation        <int> 7397, 3742, 16246, 6745, 7809, 2614, 9296, 5…

For these functions which work with information from the observation table, it is important to note that the results will be based on the observation period during which the index date falls within. Moreover, if a patient is not under observation at the specified date, addPriorObservation() and addFutureObservation() functions will return NA.

When checking whether someone is in observation the default is that we are checking whether someone was in observation on the index date. We could though expand this and consider a window of time around this date. For example here we add a variable indicating whether someone was in observation from 180 days before the index date to 30 days following it.

cdm$condition_occurrence |>
  addInObservation(
    indexDate = "condition_start_date",
    window = c(-180, 30)
  ) |>
  glimpse()
## Rows: ??
## Columns: 13
## Database: DuckDB v1.1.0 [root@Darwin 24.1.0:R 4.4.1/:memory:]
## $ person_id                 <int> 9710, 8526, 5685, 1360, 1954, 523, 3699, 189…
## $ condition_start_date      <date> 1992-02-04, 1930-11-25, 1907-05-23, 1927-03…
## $ condition_end_date        <date> 2011-08-08, 1931-12-10, 1932-05-20, 1939-10…
## $ condition_occurrence_id   <int> 2, 9, 14, 18, 29, 32, 34, 61, 91, 98, 103, 1…
## $ condition_concept_id      <int> 2, 1, 1, 7, 2, 7, 10, 3, 4, 2, 2, 10, 3, 10,…
## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ age                       <int> 20, 23, 0, 16, 20, 15, 19, 37, 20, 19, 30, 2…
## $ age_group                 <chr> "18 to 65", "18 to 65", "0 to 17", "0 to 17"…
## $ age_from_year_of_birth    <int> 20, 23, 0, 16, 20, 15, 19, 37, 20, 19, 30, 2…
## $ sex                       <chr> "Male", "Male", "Male", "Female", "Male", "M…
## $ prior_observation         <int> 7339, 8729, 142, 5918, 7477, 5618, 7205, 135…
## $ future_observation        <int> 7397, 3742, 16246, 6745, 7809, 2614, 9296, 5…
## $ in_observation            <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…

We can also specify a window and require that an individual is present for only some days within it. Here we add a variable indicating whether the individual was in observation at least a year in the future,

cdm$condition_occurrence |>
  addInObservation(
    indexDate = "condition_start_date",
    window = c(365, Inf),
    completeInterval = FALSE
  ) |>
  glimpse()
## Rows: ??
## Columns: 13
## Database: DuckDB v1.1.0 [root@Darwin 24.1.0:R 4.4.1/:memory:]
## $ person_id                 <int> 9710, 8526, 5685, 1360, 1954, 523, 3699, 189…
## $ condition_start_date      <date> 1992-02-04, 1930-11-25, 1907-05-23, 1927-03…
## $ condition_end_date        <date> 2011-08-08, 1931-12-10, 1932-05-20, 1939-10…
## $ condition_occurrence_id   <int> 2, 9, 14, 18, 29, 32, 34, 61, 91, 98, 103, 1…
## $ condition_concept_id      <int> 2, 1, 1, 7, 2, 7, 10, 3, 4, 2, 2, 10, 3, 10,…
## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ age                       <int> 20, 23, 0, 16, 20, 15, 19, 37, 20, 19, 30, 2…
## $ age_group                 <chr> "18 to 65", "18 to 65", "0 to 17", "0 to 17"…
## $ age_from_year_of_birth    <int> 20, 23, 0, 16, 20, 15, 19, 37, 20, 19, 30, 2…
## $ sex                       <chr> "Male", "Male", "Male", "Female", "Male", "M…
## $ prior_observation         <int> 7339, 8729, 142, 5918, 7477, 5618, 7205, 135…
## $ future_observation        <int> 7397, 3742, 16246, 6745, 7809, 2614, 9296, 5…
## $ in_observation            <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…

1.3 Adding characteristics to a cohort tables

The above functions can be used on both standard OMOP CDM tables and cohort tables. Note as the default index date in the functions is “cohort_start_date” we can now omit this.

cdm$cohort1 |>
  glimpse()
## Rows: ??
## Columns: 4
## Database: DuckDB v1.1.0 [root@Darwin 24.1.0:R 4.4.1/:memory:]
## $ cohort_definition_id <int> 2, 2, 2, 2, 3, 2, 1, 3, 3, 2, 1, 2, 3, 2, 2, 2, 2…
## $ subject_id           <int> 5006, 7640, 8507, 4792, 4319, 9881, 714, 1686, 84…
## $ cohort_start_date    <date> 1952-06-02, 1965-04-05, 1918-01-21, 1957-03-22, …
## $ cohort_end_date      <date> 1963-03-24, 1976-02-18, 1918-08-10, 1969-01-02, …
cdm$cohort1 <- cdm$cohort1 |>
  addAge(ageGroup = list(
    "0 to 17" = c(0, 17),
    "18 to 65" = c(18, 65),
    ">= 66" = c(66, Inf)
  )) |>
  addSex() |>
  addInObservation() |>
  addPriorObservation() |>
  addFutureObservation()

cdm$cohort1 |>
  glimpse()
## Rows: ??
## Columns: 10
## Database: DuckDB v1.1.0 [root@Darwin 24.1.0:R 4.4.1/:memory:]
## $ cohort_definition_id <int> 1, 2, 1, 1, 1, 1, 3, 1, 2, 3, 3, 3, 3, 1, 2, 2, 3…
## $ subject_id           <int> 1, 2, 3, 4, 5, 6, 8, 10, 11, 12, 13, 14, 15, 16, …
## $ cohort_start_date    <date> 1995-10-30, 1993-12-13, 1917-04-11, 1931-05-09, …
## $ cohort_end_date      <date> 2009-07-27, 1995-09-23, 1932-08-14, 1932-05-02, …
## $ age                  <int> 22, 23, 3, 11, 4, 1, 37, 26, 9, 1, 22, 6, 20, 22,…
## $ age_group            <chr> "18 to 65", "18 to 65", "0 to 17", "0 to 17", "0 …
## $ sex                  <chr> "Male", "Female", "Male", "Male", "Male", "Female…
## $ in_observation       <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ prior_observation    <int> 8337, 8747, 1196, 4146, 1538, 696, 13737, 9564, 3…
## $ future_observation   <int> 5401, 1917, 10651, 4689, 13229, 5018, 5233, 4082,…

1.4 Getting multiple characteristics at once

The above functions, which are chained together, each fetch the related information one by one. In the cases where we are interested in adding multiple characteristics, we can add these all at the same time using the more general addDemographics() functions. This will be more efficient that adding characteristics as it requires fewer joins between our table of interest and the person and observation period tables.

cdm$cohort2 |>
  glimpse()
## Rows: ??
## Columns: 4
## Database: DuckDB v1.1.0 [root@Darwin 24.1.0:R 4.4.1/:memory:]
## $ cohort_definition_id <int> 2, 1, 3, 2, 3, 2, 1, 2, 2, 2, 3, 3, 3, 1, 1, 3, 1…
## $ subject_id           <int> 6391, 8257, 1635, 3988, 5220, 7645, 5793, 5236, 2…
## $ cohort_start_date    <date> 1994-02-12, 1910-11-11, 1940-05-13, 1921-11-11, …
## $ cohort_end_date      <date> 2001-11-09, 1912-08-07, 1944-03-16, 1925-04-10, …
tictoc::tic()
cdm$cohort2 |>
  addAge(ageGroup = list(
    "0 to 17" = c(0, 17),
    "18 to 65" = c(18, 65),
    ">= 66" = c(66, Inf)
  )) |>
  addSex() |>
  addInObservation() |>
  addPriorObservation() |>
  addFutureObservation()
## # Source:   table<og_270_1734018502> [?? x 10]
## # Database: DuckDB v1.1.0 [root@Darwin 24.1.0:R 4.4.1/:memory:]
##    cohort_definition_id subject_id cohort_start_date cohort_end_date   age
##                   <int>      <int> <date>            <date>          <int>
##  1                    3          1 2001-02-09        2004-07-13         28
##  2                    2          2 1973-08-13        1982-07-02          3
##  3                    3          3 1914-11-23        1923-03-29          0
##  4                    1          4 1936-02-07        1942-11-21         16
##  5                    1          5 1958-01-28        1962-08-01         35
##  6                    2          6 1939-05-07        1939-08-20         15
##  7                    2          7 1912-02-11        1913-07-18          2
##  8                    2          8 2010-06-23        2022-03-05         33
##  9                    1          9 2001-10-23        2003-02-07         34
## 10                    1         10 1918-09-23        1922-09-27         17
## # ℹ more rows
## # ℹ 5 more variables: age_group <chr>, sex <chr>, in_observation <int>,
## #   prior_observation <int>, future_observation <int>
tictoc::toc()
## 0.452 sec elapsed
tictoc::tic()
cdm$cohort2 |>
  addDemographics(
    age = TRUE,
    ageName = "age",
    ageGroup = list(
      "0 to 17" = c(0, 17),
      "18 to 65" = c(18, 65),
      ">= 66" = c(66, Inf)
    ),
    sex = TRUE,
    sexName = "sex",
    priorObservation = TRUE,
    priorObservationName = "prior_observation",
    futureObservation = FALSE,
  ) |>
  glimpse()
## Rows: ??
## Columns: 8
## Database: DuckDB v1.1.0 [root@Darwin 24.1.0:R 4.4.1/:memory:]
## $ cohort_definition_id <int> 3, 2, 3, 1, 1, 2, 2, 2, 1, 1, 2, 1, 3, 1, 3, 2, 3…
## $ subject_id           <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16…
## $ cohort_start_date    <date> 2001-02-09, 1973-08-13, 1914-11-23, 1936-02-07, …
## $ cohort_end_date      <date> 2004-07-13, 1982-07-02, 1923-03-29, 1942-11-21, …
## $ age                  <int> 28, 3, 0, 16, 35, 15, 2, 33, 34, 17, 5, 11, 3, 38…
## $ age_group            <chr> "18 to 65", "0 to 17", "0 to 17", "0 to 17", "18 …
## $ sex                  <chr> "Male", "Female", "Male", "Male", "Male", "Female…
## $ prior_observation    <int> 10266, 1320, 326, 5881, 12811, 5605, 771, 12226, …
tictoc::toc()
## 0.172 sec elapsed

In our small mock dataset we see a small improvement in performance, but this difference will become much more noticeable when working with real data that will typically be far larger.

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.