OHDSI / CohortConstructor

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

sample cohort should leave original cohort table unchanged #344

Closed edward-burn closed 1 month ago

edward-burn commented 1 month ago

Currently the attrition is being updated even if creating as a new table

library(CohortConstructor)
cdm_local <- omock::mockCdmReference() |>
  omock::mockPerson(nPerson = 100) |>
  omock::mockObservationPeriod() 

db <- DBI::dbConnect(duckdb::duckdb())
cdm <- CDMConnector::copyCdmTo(con = db, cdm = cdm_local,
                               schema ="main", overwrite = TRUE)

cdm$cohort <- demographicsCohort(cdm, name = "cohort")
#> ! cohort columns will be reordered to match the expected order:
#>   cohort_definition_id, subject_id, cohort_start_date, and cohort_end_date.
#> ℹ Building new trimmed cohort
#> Creating initial cohort
#> ✔ Cohort trimmed
attrition_before <- attrition(cdm$cohort)
attrition_before |> dplyr::glimpse()
#> Rows: 1
#> Columns: 7
#> $ cohort_definition_id <int> 1
#> $ number_records       <int> 100
#> $ number_subjects      <int> 100
#> $ reason_id            <int> 1
#> $ reason               <chr> "Initial qualifying events"
#> $ excluded_records     <int> 0
#> $ excluded_subjects    <int> 0

cdm$new_cohort <- cdm$cohort |> 
  CohortConstructor::sampleCohorts(name = "new_cohort", n = 50)
attrition_after<- attrition(cdm$cohort)
attrition_after |> dplyr::glimpse()
#> Rows: 2
#> Columns: 7
#> $ cohort_definition_id <int> 1, 1
#> $ number_records       <int> 100, 50
#> $ number_subjects      <int> 100, 50
#> $ reason_id            <int> 1, 2
#> $ reason               <chr> "Initial qualifying events", "Sample 50 individua…
#> $ excluded_records     <int> 0, 50
#> $ excluded_subjects    <int> 0, 50

Created on 2024-10-15 with reprex v2.0.2