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.3 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## $ person_id <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15…
## $ gender_concept_id <int> 8507, 8507, 8507, 8532, 8507, 8532, 8532, 8532, 8…
## $ year_of_birth <int> 1928, 1948, 1917, 1975, 1902, 1926, 1964, 1925, 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.
## Rows: ??
## Columns: 5
## Database: DuckDB v1.1.3 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## $ person_id <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1…
## $ observation_period_start_date <date> 1928-01-01, 1948-01-01, 1917-01-01, 197…
## $ observation_period_end_date <date> 1951-09-20, 1959-01-17, 1962-05-05, 199…
## $ 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.
Let’s say we’re working with the condition occurrence table.
## Rows: ??
## Columns: 6
## Database: DuckDB v1.1.3 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## $ person_id <int> 6834, 2926, 718, 4606, 3170, 320, 3402, 142,…
## $ condition_start_date <date> 1935-09-16, 1957-07-17, 1952-07-19, 1972-02…
## $ condition_end_date <date> 1936-01-07, 1958-01-06, 1958-03-11, 1977-01…
## $ condition_occurrence_id <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1…
## $ condition_concept_id <int> 1, 3, 1, 9, 3, 1, 7, 9, 10, 10, 5, 6, 10, 6,…
## $ 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.3 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## $ person_id <int> 6834, 2926, 718, 4606, 3170, 320, 3402, 142,…
## $ condition_start_date <date> 1935-09-16, 1957-07-17, 1952-07-19, 1972-02…
## $ condition_end_date <date> 1936-01-07, 1958-01-06, 1958-03-11, 1977-01…
## $ condition_occurrence_id <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1…
## $ condition_concept_id <int> 1, 3, 1, 9, 3, 1, 7, 9, 10, 10, 5, 6, 10, 6,…
## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ age <int> 16, 9, 2, 29, 19, 20, 15, 29, 24, 3, 15, 39,…
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.3 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## $ person_id <int> 25, 41, 42, 48, 49, 54, 62, 63, 70, 75, 94, …
## $ condition_start_date <date> 1909-04-24, 1976-07-23, 1907-05-29, 1935-06…
## $ condition_end_date <date> 1911-03-06, 1988-03-14, 1928-07-22, 1943-12…
## $ condition_occurrence_id <int> 973, 1018, 739, 1212, 573, 514, 759, 700, 69…
## $ condition_concept_id <int> 10, 1, 5, 2, 3, 9, 2, 8, 1, 5, 4, 6, 4, 5, 9…
## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ age <int> 4, 18, 0, 6, 31, 21, 16, 1, 3, 9, 18, 21, 23…
## $ age_group <chr> "0 to 17", "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.3 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## $ person_id <int> 25, 41, 42, 48, 49, 54, 62, 63, 70, 75, 94, …
## $ condition_start_date <date> 1909-04-24, 1976-07-23, 1907-05-29, 1935-06…
## $ condition_end_date <date> 1911-03-06, 1988-03-14, 1928-07-22, 1943-12…
## $ condition_occurrence_id <int> 973, 1018, 739, 1212, 573, 514, 759, 700, 69…
## $ condition_concept_id <int> 10, 1, 5, 2, 3, 9, 2, 8, 1, 5, 4, 6, 4, 5, 9…
## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ age <int> 4, 18, 0, 6, 31, 21, 16, 1, 3, 9, 18, 21, 23…
## $ age_group <chr> "0 to 17", "18 to 65", "0 to 17", "0 to 17",…
## $ age_from_year_of_birth <int> 4, 18, 0, 6, 31, 21, 16, 1, 3, 9, 18, 21, 23…
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.3 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## $ person_id <int> 25, 41, 42, 48, 49, 54, 62, 63, 70, 75, 94, …
## $ condition_start_date <date> 1909-04-24, 1976-07-23, 1907-05-29, 1935-06…
## $ condition_end_date <date> 1911-03-06, 1988-03-14, 1928-07-22, 1943-12…
## $ condition_occurrence_id <int> 973, 1018, 739, 1212, 573, 514, 759, 700, 69…
## $ condition_concept_id <int> 10, 1, 5, 2, 3, 9, 2, 8, 1, 5, 4, 6, 4, 5, 9…
## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ age <int> 4, 18, 0, 6, 31, 21, 16, 1, 3, 9, 18, 21, 23…
## $ age_group <chr> "0 to 17", "18 to 65", "0 to 17", "0 to 17",…
## $ age_from_year_of_birth <int> 4, 18, 0, 6, 31, 21, 16, 1, 3, 9, 18, 21, 23…
## $ sex <chr> "Female", "Male", "Male", "Female", "Female"…
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.3 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## $ person_id <int> 25, 41, 42, 48, 49, 54, 62, 63, 70, 75, 94, …
## $ condition_start_date <date> 1909-04-24, 1976-07-23, 1907-05-29, 1935-06…
## $ condition_end_date <date> 1911-03-06, 1988-03-14, 1928-07-22, 1943-12…
## $ condition_occurrence_id <int> 973, 1018, 739, 1212, 573, 514, 759, 700, 69…
## $ condition_concept_id <int> 10, 1, 5, 2, 3, 9, 2, 8, 1, 5, 4, 6, 4, 5, 9…
## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ age <int> 4, 18, 0, 6, 31, 21, 16, 1, 3, 9, 18, 21, 23…
## $ age_group <chr> "0 to 17", "18 to 65", "0 to 17", "0 to 17",…
## $ age_from_year_of_birth <int> 4, 18, 0, 6, 31, 21, 16, 1, 3, 9, 18, 21, 23…
## $ sex <chr> "Female", "Male", "Male", "Female", "Female"…
## $ in_observation <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
## $ prior_observation <int> 1574, 6778, 148, 2366, 11451, 7964, 5960, 40…
## $ future_observation <int> 1961, 6188, 10094, 6388, 2787, 6720, 5211, 2…
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.3 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## $ person_id <int> 25, 41, 42, 48, 49, 54, 62, 63, 70, 75, 94, …
## $ condition_start_date <date> 1909-04-24, 1976-07-23, 1907-05-29, 1935-06…
## $ condition_end_date <date> 1911-03-06, 1988-03-14, 1928-07-22, 1943-12…
## $ condition_occurrence_id <int> 973, 1018, 739, 1212, 573, 514, 759, 700, 69…
## $ condition_concept_id <int> 10, 1, 5, 2, 3, 9, 2, 8, 1, 5, 4, 6, 4, 5, 9…
## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ age <int> 4, 18, 0, 6, 31, 21, 16, 1, 3, 9, 18, 21, 23…
## $ age_group <chr> "0 to 17", "18 to 65", "0 to 17", "0 to 17",…
## $ age_from_year_of_birth <int> 4, 18, 0, 6, 31, 21, 16, 1, 3, 9, 18, 21, 23…
## $ sex <chr> "Female", "Male", "Male", "Female", "Female"…
## $ prior_observation <int> 1574, 6778, 148, 2366, 11451, 7964, 5960, 40…
## $ future_observation <int> 1961, 6188, 10094, 6388, 2787, 6720, 5211, 2…
## $ 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.3 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## $ person_id <int> 25, 41, 42, 48, 49, 54, 62, 63, 70, 75, 94, …
## $ condition_start_date <date> 1909-04-24, 1976-07-23, 1907-05-29, 1935-06…
## $ condition_end_date <date> 1911-03-06, 1988-03-14, 1928-07-22, 1943-12…
## $ condition_occurrence_id <int> 973, 1018, 739, 1212, 573, 514, 759, 700, 69…
## $ condition_concept_id <int> 10, 1, 5, 2, 3, 9, 2, 8, 1, 5, 4, 6, 4, 5, 9…
## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ age <int> 4, 18, 0, 6, 31, 21, 16, 1, 3, 9, 18, 21, 23…
## $ age_group <chr> "0 to 17", "18 to 65", "0 to 17", "0 to 17",…
## $ age_from_year_of_birth <int> 4, 18, 0, 6, 31, 21, 16, 1, 3, 9, 18, 21, 23…
## $ sex <chr> "Female", "Male", "Male", "Female", "Female"…
## $ prior_observation <int> 1574, 6778, 148, 2366, 11451, 7964, 5960, 40…
## $ future_observation <int> 1961, 6188, 10094, 6388, 2787, 6720, 5211, 2…
## $ in_observation <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
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.
## Rows: ??
## Columns: 4
## Database: DuckDB v1.1.3 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## $ cohort_definition_id <int> 1, 2, 1, 1, 1, 3, 3, 3, 2, 3, 3, 1, 1, 3, 2, 2, 2…
## $ subject_id <int> 6479, 5608, 4344, 3934, 4925, 2810, 5161, 1082, 2…
## $ cohort_start_date <date> 1932-06-18, 1962-02-07, 1935-04-15, 1984-06-25, …
## $ cohort_end_date <date> 1951-05-01, 1966-02-25, 1936-12-14, 1985-10-18, …
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.3 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## $ cohort_definition_id <int> 3, 1, 1, 3, 3, 3, 3, 1, 2, 3, 2, 3, 1, 1, 3, 3, 1…
## $ subject_id <int> 1, 3, 4, 5, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17, …
## $ cohort_start_date <date> 1943-02-11, 1935-06-19, 1978-10-09, 1911-10-14, …
## $ cohort_end_date <date> 1945-04-17, 1941-10-06, 1992-09-09, 1923-09-02, …
## $ age <int> 15, 18, 3, 9, 24, 3, 3, 9, 8, 4, 10, 12, 9, 45, 7…
## $ age_group <chr> "0 to 17", "18 to 65", "0 to 17", "0 to 17", "18 …
## $ sex <chr> "Male", "Male", "Female", "Male", "Female", "Fema…
## $ in_observation <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ prior_observation <int> 5520, 6743, 1377, 3573, 8886, 1138, 1135, 3443, 3…
## $ future_observation <int> 3143, 9817, 5884, 4678, 1444, 18114, 2339, 5746, …
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.
## Rows: ??
## Columns: 4
## Database: DuckDB v1.1.3 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## $ cohort_definition_id <int> 3, 1, 2, 1, 3, 3, 2, 3, 3, 1, 3, 2, 2, 2, 3, 1, 3…
## $ subject_id <int> 149, 1678, 7899, 7906, 5959, 3588, 6325, 6596, 44…
## $ cohort_start_date <date> 1918-03-24, 1991-02-15, 1957-11-23, 1980-08-04, …
## $ cohort_end_date <date> 1937-07-10, 2017-06-30, 1963-04-02, 1985-12-16, …
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_1734615541> [?? x 10]
## # Database: DuckDB v1.1.3 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## cohort_definition_id subject_id cohort_start_date cohort_end_date age
## <int> <int> <date> <date> <int>
## 1 1 1 1948-05-22 1951-06-13 20
## 2 1 2 1948-12-10 1956-08-16 0
## 3 1 3 1946-06-16 1959-04-07 29
## 4 1 4 1977-09-22 1988-12-16 2
## 5 2 5 1918-11-06 1922-02-27 16
## 6 1 6 1937-05-12 1938-05-27 11
## 7 3 7 1991-04-25 1991-08-08 27
## 8 2 8 1963-10-28 1973-09-08 38
## 9 2 9 1972-05-10 1973-01-20 8
## 10 2 10 1917-06-02 1929-07-14 8
## # ℹ more rows
## # ℹ 5 more variables: age_group <chr>, sex <chr>, in_observation <int>,
## # prior_observation <int>, future_observation <int>
## 1.187 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.3 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## $ cohort_definition_id <int> 1, 1, 1, 1, 2, 1, 3, 2, 2, 2, 1, 3, 2, 2, 1, 1, 2…
## $ subject_id <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15…
## $ cohort_start_date <date> 1948-05-22, 1948-12-10, 1946-06-16, 1977-09-22, …
## $ cohort_end_date <date> 1951-06-13, 1956-08-16, 1959-04-07, 1988-12-16, …
## $ age <int> 20, 0, 29, 2, 16, 11, 27, 38, 8, 8, 1, 17, 0, 6, …
## $ age_group <chr> "18 to 65", "0 to 17", "18 to 65", "0 to 17", "0 …
## $ sex <chr> "Male", "Male", "Male", "Female", "Male", "Female…
## $ prior_observation <int> 7447, 344, 10758, 995, 6153, 4149, 9976, 14179, 3…
## 0.466 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.