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-dev165 [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> 8532, 8532, 8507, 8507, 8532, 8507, 8532, 8507, 8…
## $ year_of_birth <int> 1914, 1935, 1914, 1902, 1945, 1919, 1903, 1910, 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-dev165 [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> 1914-01-01, 1935-01-01, 1914-01-01, 190…
## $ observation_period_end_date <date> 1955-10-24, 1951-12-30, 1943-11-18, 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.
Let’s say we’re working with the condition occurrence table.
## Rows: ??
## Columns: 6
## Database: DuckDB v1.1.3-dev165 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## $ person_id <int> 3718, 8154, 5670, 1615, 354, 469, 1568, 2429…
## $ condition_start_date <date> 1981-11-12, 1991-04-27, 2001-06-07, 1974-09…
## $ condition_end_date <date> 1986-08-02, 2007-07-04, 2007-10-06, 1984-10…
## $ condition_occurrence_id <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1…
## $ condition_concept_id <int> 4, 10, 4, 4, 6, 2, 6, 2, 6, 2, 2, 8, 3, 3, 5…
## $ 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-dev165 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## $ person_id <int> 3718, 8154, 5670, 1615, 354, 469, 1568, 2429…
## $ condition_start_date <date> 1981-11-12, 1991-04-27, 2001-06-07, 1974-09…
## $ condition_end_date <date> 1986-08-02, 2007-07-04, 2007-10-06, 1984-10…
## $ condition_occurrence_id <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1…
## $ condition_concept_id <int> 4, 10, 4, 4, 6, 2, 6, 2, 6, 2, 2, 8, 3, 3, 5…
## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ age <int> 27, 14, 21, 16, 28, 8, 16, 25, 2, 18, 15, 5,…
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-dev165 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## $ person_id <int> 5670, 469, 2021, 6736, 7405, 332, 2998, 7184…
## $ condition_start_date <date> 2001-06-07, 1914-11-10, 1936-02-25, 1918-01…
## $ condition_end_date <date> 2007-10-06, 1927-08-12, 1947-02-14, 1922-05…
## $ condition_occurrence_id <int> 3, 6, 9, 12, 19, 21, 29, 34, 36, 45, 52, 53,…
## $ condition_concept_id <int> 4, 2, 6, 8, 9, 4, 5, 7, 2, 2, 3, 4, 7, 10, 9…
## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ age <int> 21, 8, 2, 5, 33, 43, 14, 11, 6, 0, 5, 1, 15,…
## $ age_group <chr> "18 to 65", "0 to 17", "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-dev165 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## $ person_id <int> 5670, 469, 2021, 6736, 7405, 332, 2998, 7184…
## $ condition_start_date <date> 2001-06-07, 1914-11-10, 1936-02-25, 1918-01…
## $ condition_end_date <date> 2007-10-06, 1927-08-12, 1947-02-14, 1922-05…
## $ condition_occurrence_id <int> 3, 6, 9, 12, 19, 21, 29, 34, 36, 45, 52, 53,…
## $ condition_concept_id <int> 4, 2, 6, 8, 9, 4, 5, 7, 2, 2, 3, 4, 7, 10, 9…
## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ age <int> 21, 8, 2, 5, 33, 43, 14, 11, 6, 0, 5, 1, 15,…
## $ age_group <chr> "18 to 65", "0 to 17", "0 to 17", "0 to 17",…
## $ age_from_year_of_birth <int> 21, 8, 2, 5, 33, 43, 14, 11, 6, 0, 5, 1, 15,…
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-dev165 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## $ person_id <int> 5670, 469, 2021, 6736, 7405, 332, 2998, 7184…
## $ condition_start_date <date> 2001-06-07, 1914-11-10, 1936-02-25, 1918-01…
## $ condition_end_date <date> 2007-10-06, 1927-08-12, 1947-02-14, 1922-05…
## $ condition_occurrence_id <int> 3, 6, 9, 12, 19, 21, 29, 34, 36, 45, 52, 53,…
## $ condition_concept_id <int> 4, 2, 6, 8, 9, 4, 5, 7, 2, 2, 3, 4, 7, 10, 9…
## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ age <int> 21, 8, 2, 5, 33, 43, 14, 11, 6, 0, 5, 1, 15,…
## $ age_group <chr> "18 to 65", "0 to 17", "0 to 17", "0 to 17",…
## $ age_from_year_of_birth <int> 21, 8, 2, 5, 33, 43, 14, 11, 6, 0, 5, 1, 15,…
## $ sex <chr> "Male", "Female", "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-dev165 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## $ person_id <int> 5670, 469, 2021, 6736, 7405, 332, 2998, 7184…
## $ condition_start_date <date> 2001-06-07, 1914-11-10, 1936-02-25, 1918-01…
## $ condition_end_date <date> 2007-10-06, 1927-08-12, 1947-02-14, 1922-05…
## $ condition_occurrence_id <int> 3, 6, 9, 12, 19, 21, 29, 34, 36, 45, 52, 53,…
## $ condition_concept_id <int> 4, 2, 6, 8, 9, 4, 5, 7, 2, 2, 3, 4, 7, 10, 9…
## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ age <int> 21, 8, 2, 5, 33, 43, 14, 11, 6, 0, 5, 1, 15,…
## $ age_group <chr> "18 to 65", "0 to 17", "0 to 17", "0 to 17",…
## $ age_from_year_of_birth <int> 21, 8, 2, 5, 33, 43, 14, 11, 6, 0, 5, 1, 15,…
## $ sex <chr> "Male", "Female", "Male", "Female", "Female"…
## $ in_observation <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
## $ prior_observation <int> 7828, 3235, 785, 1829, 12188, 15776, 5211, 4…
## $ future_observation <int> 2490, 7571, 7890, 16338, 926, 1942, 1908, 86…
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-dev165 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## $ person_id <int> 5670, 469, 2021, 6736, 7405, 332, 2998, 7184…
## $ condition_start_date <date> 2001-06-07, 1914-11-10, 1936-02-25, 1918-01…
## $ condition_end_date <date> 2007-10-06, 1927-08-12, 1947-02-14, 1922-05…
## $ condition_occurrence_id <int> 3, 6, 9, 12, 19, 21, 29, 34, 36, 45, 52, 53,…
## $ condition_concept_id <int> 4, 2, 6, 8, 9, 4, 5, 7, 2, 2, 3, 4, 7, 10, 9…
## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ age <int> 21, 8, 2, 5, 33, 43, 14, 11, 6, 0, 5, 1, 15,…
## $ age_group <chr> "18 to 65", "0 to 17", "0 to 17", "0 to 17",…
## $ age_from_year_of_birth <int> 21, 8, 2, 5, 33, 43, 14, 11, 6, 0, 5, 1, 15,…
## $ sex <chr> "Male", "Female", "Male", "Female", "Female"…
## $ prior_observation <int> 7828, 3235, 785, 1829, 12188, 15776, 5211, 4…
## $ future_observation <int> 2490, 7571, 7890, 16338, 926, 1942, 1908, 86…
## $ 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-dev165 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## $ person_id <int> 5670, 469, 2021, 6736, 7405, 332, 2998, 7184…
## $ condition_start_date <date> 2001-06-07, 1914-11-10, 1936-02-25, 1918-01…
## $ condition_end_date <date> 2007-10-06, 1927-08-12, 1947-02-14, 1922-05…
## $ condition_occurrence_id <int> 3, 6, 9, 12, 19, 21, 29, 34, 36, 45, 52, 53,…
## $ condition_concept_id <int> 4, 2, 6, 8, 9, 4, 5, 7, 2, 2, 3, 4, 7, 10, 9…
## $ condition_type_concept_id <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ age <int> 21, 8, 2, 5, 33, 43, 14, 11, 6, 0, 5, 1, 15,…
## $ age_group <chr> "18 to 65", "0 to 17", "0 to 17", "0 to 17",…
## $ age_from_year_of_birth <int> 21, 8, 2, 5, 33, 43, 14, 11, 6, 0, 5, 1, 15,…
## $ sex <chr> "Male", "Female", "Male", "Female", "Female"…
## $ prior_observation <int> 7828, 3235, 785, 1829, 12188, 15776, 5211, 4…
## $ future_observation <int> 2490, 7571, 7890, 16338, 926, 1942, 1908, 86…
## $ 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-dev165 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## $ cohort_definition_id <int> 3, 3, 1, 2, 1, 3, 2, 3, 2, 1, 2, 3, 3, 3, 2, 2, 3…
## $ subject_id <int> 2068, 3596, 3924, 8291, 3898, 6138, 8612, 9276, 8…
## $ cohort_start_date <date> 1934-12-09, 1971-04-27, 1957-03-10, 1975-01-14, …
## $ cohort_end_date <date> 1965-05-10, 1988-04-23, 2000-04-26, 1981-08-04, …
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-dev165 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## $ cohort_definition_id <int> 1, 1, 2, 2, 1, 1, 1, 3, 3, 3, 3, 3, 1, 3, 2, 2, 1…
## $ subject_id <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14, 15, 16, 17…
## $ cohort_start_date <date> 1948-12-18, 1938-10-01, 1936-02-24, 1926-03-03, …
## $ cohort_end_date <date> 1951-10-27, 1949-08-07, 1937-04-19, 1937-12-27, …
## $ age <int> 34, 3, 22, 24, 7, 27, 43, 6, 0, 50, 11, 0, 38, 1,…
## $ age_group <chr> "18 to 65", "0 to 17", "18 to 65", "18 to 65", "0…
## $ sex <chr> "Female", "Female", "Male", "Male", "Female", "Ma…
## $ in_observation <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ prior_observation <int> 12770, 1369, 8089, 8827, 2841, 10105, 15844, 2291…
## $ future_observation <int> 2501, 4838, 2824, 6462, 5671, 5093, 1681, 10892, …
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-dev165 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## $ cohort_definition_id <int> 1, 3, 1, 2, 3, 1, 3, 2, 1, 1, 3, 1, 2, 2, 2, 1, 3…
## $ subject_id <int> 9885, 4725, 2748, 7500, 3192, 6071, 3373, 1670, 9…
## $ cohort_start_date <date> 1974-06-23, 1940-09-01, 1976-03-31, 1927-06-07, …
## $ cohort_end_date <date> 1984-07-17, 1949-01-24, 1982-06-25, 1928-05-31, …
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_1732516166> [?? x 10]
## # Database: DuckDB v1.1.3-dev165 [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 2 2 1943-10-28 1944-06-03 8
## 2 3 3 1918-05-31 1935-05-14 4
## 3 2 4 1919-04-30 1929-12-14 17
## 4 1 5 1952-06-14 1959-05-13 7
## 5 3 6 1937-03-10 1952-05-30 18
## 6 3 7 1910-10-18 1913-09-03 7
## 7 2 8 1918-03-05 1935-09-29 8
## 8 1 10 1997-09-11 2002-11-26 40
## 9 2 11 1948-07-17 1950-11-14 4
## 10 1 12 1980-01-14 1980-10-15 30
## # ℹ 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-dev165 [unknown@Linux 6.5.0-1025-azure:R 4.4.2/:memory:]
## $ cohort_definition_id <int> 2, 3, 2, 1, 3, 3, 2, 1, 2, 1, 3, 3, 2, 2, 2, 2, 2…
## $ subject_id <int> 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 17, …
## $ cohort_start_date <date> 1943-10-28, 1918-05-31, 1919-04-30, 1952-06-14, …
## $ cohort_end_date <date> 1944-06-03, 1935-05-14, 1929-12-14, 1959-05-13, …
## $ age <int> 8, 4, 17, 7, 18, 7, 8, 40, 4, 30, 21, 7, 35, 28, …
## $ age_group <chr> "0 to 17", "0 to 17", "0 to 17", "0 to 17", "18 t…
## $ sex <chr> "Female", "Male", "Male", "Female", "Male", "Fema…
## $ prior_observation <int> 3222, 1611, 6328, 2721, 6643, 2847, 2985, 14863, …
## 0.463 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.