Skip to contents

Compute demographic characteristics at a certain date

Usage

addDemographics(
  x,
  indexDate = "cohort_start_date",
  age = TRUE,
  ageName = "age",
  ageMissingMonth = 1,
  ageMissingDay = 1,
  ageImposeMonth = FALSE,
  ageImposeDay = FALSE,
  ageUnit = "years",
  ageGroup = NULL,
  missingAgeGroupValue = "None",
  sex = TRUE,
  sexName = "sex",
  missingSexValue = "None",
  priorObservation = TRUE,
  priorObservationName = "prior_observation",
  priorObservationType = "days",
  futureObservation = TRUE,
  futureObservationName = "future_observation",
  futureObservationType = "days",
  dateOfBirth = FALSE,
  dateOfBirthName = "date_of_birth",
  name = NULL,
  type = "numeric"
)

Arguments

x

A table containing individuals in a CDM reference.

indexDate

Name of a date column in x, or a single date to use for all rows, used as the reference date.

age

If TRUE, age is calculated relative to indexDate.

ageName

Name of the age column to add.

ageMissingMonth

Month of the year assigned when month of birth is missing.

ageMissingDay

Day of the month assigned when day of birth is missing.

ageImposeMonth

If TRUE, month of birth is treated as missing for all individuals.

ageImposeDay

If TRUE, day of birth is treated as missing for all individuals.

ageUnit

Unit in which to express age: "years", "months", or "days".

ageGroup

If not NULL, a list of age-group vectors.

missingAgeGroupValue

Value to use when age is missing.

sex

If TRUE, sex is identified.

sexName

Name of the sex column to add.

missingSexValue

Value to use when sex is missing.

priorObservation

If TRUE, the time between the start of the current observation period and indexDate is calculated.

priorObservationName

Name of the prior-observation column to add.

priorObservationType

Whether to return a "date" or a number of "days".

futureObservation

If TRUE, the time between indexDate and the end of the current observation period is calculated.

futureObservationName

Name of the future-observation column to add.

futureObservationType

Whether to return a "date" or a number of "days".

dateOfBirth

If TRUE, date of birth is returned.

dateOfBirthName

Name of the date-of-birth column to add.

name

Name of the new table. If NULL, a temporary table is returned.

type

Type of the created column(s). Counts, days, age, and observation durations can be "numeric" or "integer". Flag columns can also be "logical". Field columns can use "auto" to preserve the source type, or can be converted to "numeric", "integer", "logical", or "character".

Value

cohort table with the added demographic information columns.

Examples

# \donttest{
library(PatientProfiles)

cdm <- mockPatientProfiles(source = "duckdb")

cdm$cohort1 |>
  addDemographics()
#> # A query:  ?? x 8
#> # Database: DuckDB 1.5.4 [unknown@Linux 6.17.0-1020-azure:R 4.6.1/:memory:]
#>    cohort_definition_id subject_id cohort_start_date cohort_end_date   age sex  
#>                   <int>      <int> <date>            <date>          <dbl> <chr>
#>  1                    3          1 1935-12-31        1936-01-19          1 Fema…
#>  2                    3          2 1916-06-05        1922-10-03          1 Fema…
#>  3                    3          3 1959-01-19        1979-08-18          5 Male 
#>  4                    3          4 1962-11-02        1976-09-12          6 Fema…
#>  5                    3          5 1966-01-15        1976-05-09         11 Male 
#>  6                    3          6 1941-03-12        1946-06-28         12 Fema…
#>  7                    3          7 1977-01-10        1980-01-21          0 Fema…
#>  8                    1          8 1926-07-08        1935-03-14          8 Fema…
#>  9                    2          9 1989-11-29        1992-11-26         24 Male 
#> 10                    3         10 1977-07-25        1983-02-04          4 Fema…
#> # ℹ 2 more variables: prior_observation <dbl>, future_observation <dbl>

cdm$cohort1 |>
  addDemographics(indexDate = as.Date("2010-01-01"))
#> # A query:  ?? x 8
#> # Database: DuckDB 1.5.4 [unknown@Linux 6.17.0-1020-azure:R 4.6.1/:memory:]
#>    cohort_definition_id subject_id cohort_start_date cohort_end_date   age sex  
#>                   <int>      <int> <date>            <date>          <dbl> <chr>
#>  1                    3          1 1935-12-31        1936-01-19         76 Fema…
#>  2                    3          2 1916-06-05        1922-10-03         95 Fema…
#>  3                    3          3 1959-01-19        1979-08-18         56 Male 
#>  4                    3          4 1962-11-02        1976-09-12         54 Fema…
#>  5                    3          5 1966-01-15        1976-05-09         55 Male 
#>  6                    3          6 1941-03-12        1946-06-28         81 Fema…
#>  7                    3          7 1977-01-10        1980-01-21         33 Fema…
#>  8                    1          8 1926-07-08        1935-03-14         92 Fema…
#>  9                    2          9 1989-11-29        1992-11-26         45 Male 
#> 10                    3         10 1977-07-25        1983-02-04         37 Fema…
#> # ℹ 2 more variables: prior_observation <dbl>, future_observation <dbl>

# }