OHDSI / CohortConstructor

https://ohdsi.github.io/CohortConstructor/
Apache License 2.0
1 stars 0 forks source link

Handle leap year birthdays in demographicsCohort #171

Open edward-burn opened 5 months ago

edward-burn commented 5 months ago

@nmercadeb @catalamarti shouldn't min age be 18 in the case below?

library(CDMConnector)
library(CodelistGenerator)
library(PatientProfiles)
library(CohortConstructor)
#> 
#> Attaching package: 'CohortConstructor'
#> The following objects are masked from 'package:CDMConnector':
#> 
#>     intersectCohorts, unionCohorts
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

con <- DBI::dbConnect(duckdb::duckdb(), 
                      dbdir = eunomia_dir())
cdm <- cdm_from_con(con, cdm_schema = "main", 
                    write_schema = c(prefix = "my_study_", schema = "main"))

cdm$working_age_cohort <- demographicsCohort(cdm = cdm, 
                                             ageRange = c(18, 65), 
                                             name = "working_age_cohort")
#> ℹ Building new trimmed cohort
#> Adding birth date
#> Creating initial cohort
#> Trim age
#> ✔ Cohort trimmed

cdm$working_age_cohort |> 
  addAge(indexDate = "cohort_start_date") |> 
  summarise(min_start_age = min(age), 
            median_start_age = median(age), 
            max_start_age = max(age))
#> Warning: Missing values are always removed in SQL aggregation functions.
#> Use `na.rm = TRUE` to silence this warning
#> This warning is displayed once every 8 hours.
#> # Source:   SQL [1 x 3]
#> # Database: DuckDB v0.10.3-dev775 [eburn@Windows 10 x64:R 4.4.0/C:\Users\eburn\AppData\Local\Temp\RtmpUJzNcL\file48942f7e6037.duckdb]
#>   min_start_age median_start_age max_start_age
#>           <int>            <dbl>         <int>
#> 1            17               18            18

Created on 2024-05-22 with reprex v2.1.0

nmercadeb commented 5 months ago

This was one subject born on the 29th of February ahhaha When calculating the 18th birthday by adding 18 years to date of birth we got 28th of February, solved by adding 1 day to these edge cases in PR 127

library(CDMConnector)
library(CodelistGenerator)
library(PatientProfiles)
library(CohortConstructor)
#> 
#> Attaching package: 'CohortConstructor'
#> The following objects are masked from 'package:CDMConnector':
#> 
#>     intersectCohorts, unionCohorts
library(dplyr)
#> Warning: package 'dplyr' was built under R version 4.2.3
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
con <- DBI::dbConnect(duckdb::duckdb(), 
                      dbdir = eunomia_dir())
cdm <- cdm_from_con(con, cdm_schema = "main", 
                    write_schema = c(prefix = "my_study_", schema = "main"))

cdm$working_age_cohort <- demographicsCohort(cdm = cdm, 
                                             ageRange = c(18, 65), 
                                             name = "working_age_cohort")
#> ℹ Building new trimmed cohort
#> Adding birth date
#> Creating initial cohort
#> Trim age
#> ✔ Cohort trimmed
cdm$working_age_cohort |> 
  addAge(indexDate = "cohort_start_date") |> 
  summarise(min_start_age = min(age), 
            median_start_age = median(age), 
            max_start_age = max(age))
#> Warning: Missing values are always removed in SQL aggregation functions.
#> Use `na.rm = TRUE` to silence this warning
#> This warning is displayed once every 8 hours.
#> # Source:   SQL [1 x 3]
#> # Database: DuckDB v0.10.1 [nmercade@Windows 10 x64:R 4.2.2/C:\Users\nmercade\AppData\Local\Temp\Rtmp8KaPeO\file242c567f7787.duckdb]
#>   min_start_age median_start_age max_start_age
#>           <dbl>            <dbl>         <dbl>
#> 1            18               18            18

Created on 2024-05-22 with reprex v2.1.0

edward-burn commented 5 months ago

Hmm I'm not sure if the issue is not actually that patient profiles should consider 28 Feb the person's birthday if born on a leap year....