diff --git a/DESCRIPTION b/DESCRIPTION index 53439b8e..29c6a3a3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -33,7 +33,7 @@ Imports: glue, magrittr, omopgenerics (>= 0.2.1), - PatientProfiles (>= 1.1.0), + PatientProfiles (>= 1.2.0), purrr, rlang, tidyr, @@ -68,5 +68,4 @@ Depends: R (>= 4.1) URL: https://ohdsi.github.io/CohortConstructor/ Remotes: - darwin-eu-dev/PatientProfiles darwin-eu/visOmopResults diff --git a/R/mockCohortConstructor.R b/R/mockCohortConstructor.R index cd0859ac..3a9e1a40 100644 --- a/R/mockCohortConstructor.R +++ b/R/mockCohortConstructor.R @@ -46,10 +46,10 @@ mockCohortConstructor <- function(nPerson = 10, if (is.null(tables)) { cdm <- omock::mockCdmReference() |> omock::mockVocabularyTables(concept = conceptTable) |> - omock::mockPerson(nPerson = nPerson) |> - omock::mockObservationPeriod() |> - omock::mockCohort(name = "cohort1") |> - omock::mockCohort(name = "cohort2", numberCohorts = 2) + omock::mockPerson(nPerson = nPerson,seed = seed) |> + omock::mockObservationPeriod(seed = seed) |> + omock::mockCohort(name = "cohort1", seed = seed) |> + omock::mockCohort(name = "cohort2", numberCohorts = 2, seed = seed) } else { cdm <- omock::mockCdmFromTables(tables = tables, seed = seed) |> omock::mockVocabularyTables(concept = conceptTable) @@ -60,19 +60,19 @@ mockCohortConstructor <- function(nPerson = 10, } if (drugExposure) { - cdm <- cdm |> omock::mockDrugExposure() + cdm <- cdm |> omock::mockDrugExposure(seed = seed) } if (conditionOccurrence) { - cdm <- cdm |> omock::mockConditionOccurrence() + cdm <- cdm |> omock::mockConditionOccurrence(seed = seed) } if (death) { - cdm <- cdm |> omock::mockDeath() + cdm <- cdm |> omock::mockDeath(seed = seed) } if (measurement) { - cdm <- cdm |> omock::mockMeasurement() + cdm <- cdm |> omock::mockMeasurement(seed = seed) } if (!is.null(otherTables)) { diff --git a/R/requireConceptIntersect.R b/R/requireConceptIntersect.R index cc926fb1..e9df0844 100644 --- a/R/requireConceptIntersect.R +++ b/R/requireConceptIntersect.R @@ -23,7 +23,7 @@ #' cdm <- mockCohortConstructor(conditionOccurrence = TRUE) #' cdm$cohort2 <- requireConceptIntersect( #' cohort = cdm$cohort1, -#' conceptSet = list(a = 1), +#' conceptSet = list(a = 194152), #' window = c(-Inf, 0), #' name = "cohort2") #' } diff --git a/R/sampleCohorts.R b/R/sampleCohorts.R index f9064a7d..02f84ce0 100644 --- a/R/sampleCohorts.R +++ b/R/sampleCohorts.R @@ -5,7 +5,7 @@ #' people. All records of these individuals are preserved. #' #' @inheritParams cohortDoc -#' @inheritParams cohortIdSubsetDoc +#' @inheritParams cohortIdModifyDoc #' @inheritParams nameDoc #' @param n Number of people to be sampled for each included cohort. #' diff --git a/R/unionCohorts.R b/R/unionCohorts.R index 36cddf04..45a9cc88 100644 --- a/R/unionCohorts.R +++ b/R/unionCohorts.R @@ -65,15 +65,17 @@ unionCohorts <- function(cohort, tmpTable <- omopgenerics::uniqueTableName() unionedCohort <- cohort |> dplyr::filter(.data$cohort_definition_id %in% .env$cohortId) |> - joinOverlap(name = name, by = "subject_id", gap = gap) |> + joinOverlap(name = tmpTable, + by = "subject_id", + gap = gap) |> dplyr::mutate(cohort_definition_id = 1L) |> + dplyr::relocate(dplyr::all_of(omopgenerics::cohortColumns("cohort"))) |> dplyr::compute(name = tmpTable, temporary = FALSE) cohCodelist <- attr(cohort, "cohort_codelist") if (!is.null(cohCodelist)) { cohCodelist <- cohCodelist |> dplyr::mutate("cohort_definition_id" = 1L) } unionedCohort <- unionedCohort |> - dplyr::relocate(dplyr::all_of(omopgenerics::cohortColumns("cohort"))) |> omopgenerics::newCohortTable( cohortSetRef = cohSet, cohortAttritionRef = NULL, @@ -88,6 +90,7 @@ unionCohorts <- function(cohort, cdm <- bind(cohort, unionedCohort, name = name) } + CDMConnector::dropTable(cdm, name = tmpTable) return(cdm[[name]]) } diff --git a/README.Rmd b/README.Rmd index 0d7a6b35..be24754c 100644 --- a/README.Rmd +++ b/README.Rmd @@ -18,6 +18,7 @@ knitr::opts_chunk$set( [![CRAN status](https://www.r-pkg.org/badges/version/CohortConstructor)](https://CRAN.R-project.org/package=CohortConstructor) [![R-CMD-check](https://github.com/OHDSI/CohortConstructor/workflows/R-CMD-check/badge.svg)](https://github.com/OHDSI/CohortConstructor/actions) +[![Codecov test coverage](https://codecov.io/gh/OHDSI/CohortConstructor/branch/main/graph/badge.svg)](https://app.codecov.io/gh/OHDSI/CohortConstructor?branch=main) [![Lifecycle:Experimental](https://img.shields.io/badge/Lifecycle-Experimental-339999)](https://lifecycle.r-lib.org/articles/stages.html#experimental) diff --git a/README.md b/README.md index 99779494..e7d4e7ca 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ [![CRAN status](https://www.r-pkg.org/badges/version/CohortConstructor)](https://CRAN.R-project.org/package=CohortConstructor) [![R-CMD-check](https://github.com/OHDSI/CohortConstructor/workflows/R-CMD-check/badge.svg)](https://github.com/OHDSI/CohortConstructor/actions) +[![Codecov test +coverage](https://codecov.io/gh/OHDSI/CohortConstructor/branch/main/graph/badge.svg)](https://app.codecov.io/gh/OHDSI/CohortConstructor?branch=main) [![Lifecycle:Experimental](https://img.shields.io/badge/Lifecycle-Experimental-339999)](https://lifecycle.r-lib.org/articles/stages.html#experimental) @@ -112,14 +114,14 @@ cohort_count(cdm$fractures) %>% glimpse() #> Rows: 3 #> Columns: 3 #> $ cohort_definition_id 1, 2, 3 -#> $ number_records 462, 565, 137 -#> $ number_subjects 426, 508, 132 +#> $ number_records 464, 569, 138 +#> $ number_subjects 427, 510, 132 attrition(cdm$fractures) %>% glimpse() #> Rows: 9 #> Columns: 7 #> $ cohort_definition_id 1, 1, 1, 2, 2, 2, 3, 3, 3 -#> $ number_records 462, 462, 462, 565, 565, 565, 137, 137, 137 -#> $ number_subjects 426, 426, 426, 508, 508, 508, 132, 132, 132 +#> $ number_records 464, 464, 464, 569, 569, 569, 138, 138, 138 +#> $ number_subjects 427, 427, 427, 510, 510, 510, 132, 132, 132 #> $ reason_id 1, 2, 3, 1, 2, 3, 1, 2, 3 #> $ reason "Initial qualifying events", "cohort requirements… #> $ excluded_records 0, 0, 0, 0, 0, 0, 0, 0, 0 @@ -134,21 +136,28 @@ our three cohorts to create this overall cohort like so: ``` r cdm$fractures <- unionCohorts(cdm$fractures, - cohortName = "any_fracture", - name ="fractures") + cohortName = "any_fracture", + keepOriginalCohorts = TRUE, + name ="fractures") ``` ``` r settings(cdm$fractures) -#> # A tibble: 1 × 3 -#> cohort_definition_id cohort_name gap -#> -#> 1 1 any_fracture 0 +#> # A tibble: 4 × 5 +#> cohort_definition_id cohort_name cdm_version vocabulary_version gap +#> +#> 1 1 ankle_fracture 5.3 v5.0 18-JAN-19 NA +#> 2 2 forearm_fracture 5.3 v5.0 18-JAN-19 NA +#> 3 3 hip_fracture 5.3 v5.0 18-JAN-19 NA +#> 4 4 any_fracture 0 cohortCount(cdm$fractures) -#> # A tibble: 1 × 3 +#> # A tibble: 4 × 3 #> cohort_definition_id number_records number_subjects #> -#> 1 1 1164 922 +#> 1 1 464 427 +#> 2 2 569 510 +#> 3 3 138 132 +#> 4 4 1171 924 ``` ### Require in date range @@ -168,11 +177,11 @@ attributes have been updated ``` r cohort_count(cdm$fractures) %>% glimpse() -#> Rows: 1 +#> Rows: 4 #> Columns: 3 -#> $ cohort_definition_id 1 -#> $ number_records 315 -#> $ number_subjects 282 +#> $ cohort_definition_id 1, 2, 3, 4 +#> $ number_records 108, 152, 62, 322 +#> $ number_subjects 104, 143, 60, 287 attrition(cdm$fractures) %>% filter(reason == "cohort_start_date between 2000-01-01 & 2020-01-01") %>% glimpse() @@ -205,28 +214,28 @@ criteria. attrition(cdm$fractures) %>% filter(reason == "Age requirement: 40 to 65") %>% glimpse() -#> Rows: 1 +#> Rows: 4 #> Columns: 7 -#> $ cohort_definition_id 1 -#> $ number_records 124 -#> $ number_subjects 118 -#> $ reason_id 4 -#> $ reason "Age requirement: 40 to 65" -#> $ excluded_records 191 -#> $ excluded_subjects 164 +#> $ cohort_definition_id 1, 2, 3, 4 +#> $ number_records 43, 64, 22, 129 +#> $ number_subjects 43, 62, 22, 122 +#> $ reason_id 6, 6, 6, 4 +#> $ reason "Age requirement: 40 to 65", "Age requirement: 40… +#> $ excluded_records 65, 88, 40, 193 +#> $ excluded_subjects 61, 81, 38, 165 attrition(cdm$fractures) %>% filter(reason == "Sex requirement: Female") %>% glimpse() -#> Rows: 1 +#> Rows: 4 #> Columns: 7 -#> $ cohort_definition_id 1 -#> $ number_records 64 -#> $ number_subjects 62 -#> $ reason_id 5 -#> $ reason "Sex requirement: Female" -#> $ excluded_records 60 -#> $ excluded_subjects 56 +#> $ cohort_definition_id 1, 2, 3, 4 +#> $ number_records 19, 37, 12, 68 +#> $ number_subjects 19, 36, 12, 65 +#> $ reason_id 7, 7, 7, 5 +#> $ reason "Sex requirement: Female", "Sex requirement: Fema… +#> $ excluded_records 24, 27, 10, 61 +#> $ excluded_subjects 24, 26, 10, 57 ``` ### Require presence in another cohort @@ -251,15 +260,15 @@ cdm$fractures <- cdm$fractures %>% attrition(cdm$fractures) %>% filter(reason == "Not in cohort gibleed between -Inf & 0 days relative to cohort_start_date") %>% glimpse() -#> Rows: 1 +#> Rows: 4 #> Columns: 7 -#> $ cohort_definition_id 1 -#> $ number_records 64 -#> $ number_subjects 62 -#> $ reason_id 8 +#> $ cohort_definition_id 1, 2, 3, 4 +#> $ number_records 14, 30, 10, 54 +#> $ number_subjects 14, 30, 10, 52 +#> $ reason_id 10, 10, 10, 8 #> $ reason "Not in cohort gibleed between -Inf & 0 days rela… -#> $ excluded_records 0 -#> $ excluded_subjects 0 +#> $ excluded_records 5, 7, 2, 14 +#> $ excluded_subjects 5, 6, 2, 13 ``` ``` r diff --git a/man/requireConceptIntersect.Rd b/man/requireConceptIntersect.Rd index 7aafb03e..0b46db85 100644 --- a/man/requireConceptIntersect.Rd +++ b/man/requireConceptIntersect.Rd @@ -65,7 +65,7 @@ library(CohortConstructor) cdm <- mockCohortConstructor(conditionOccurrence = TRUE) cdm$cohort2 <- requireConceptIntersect( cohort = cdm$cohort1, - conceptSet = list(a = 1), + conceptSet = list(a = 194152), window = c(-Inf, 0), name = "cohort2") } diff --git a/man/sampleCohorts.Rd b/man/sampleCohorts.Rd index 68dbd93c..128b6b0b 100644 --- a/man/sampleCohorts.Rd +++ b/man/sampleCohorts.Rd @@ -9,9 +9,10 @@ sampleCohorts(cohort, cohortId = NULL, n, name = tableName(cohort)) \arguments{ \item{cohort}{A cohort table in a cdm reference.} -\item{cohortId}{Vector identifying which cohorts to include -(cohort_definition_id or cohort_name). Cohorts not included will be -removed from the cohort set.} +\item{cohortId}{Vector identifying which cohorts to modify +(cohort_definition_id or cohort_name). If NULL, all cohorts will be +used; otherwise, only the specified cohorts will be modified, and the +rest will remain unchanged.} \item{n}{Number of people to be sampled for each included cohort.} diff --git a/tests/testthat/test-conceptCohort.R b/tests/testthat/test-conceptCohort.R index 54ed9f72..26dcdac6 100644 --- a/tests/testthat/test-conceptCohort.R +++ b/tests/testthat/test-conceptCohort.R @@ -283,8 +283,8 @@ test_that("excluded concepts in codelist", { test_that("out of observation", { testthat::skip_on_cran() cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() + omock::mockPerson(n = 4, seed = 1) |> + omock::mockObservationPeriod(seed = 1) cdm_local$concept <- dplyr::tibble( "concept_id" = c(1, 2), "concept_name" = c("my concept 1", "my concept 2"), @@ -343,8 +343,8 @@ test_that("out of observation", { # event starts out, end in (subject 3) # no concept 2 cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() + omock::mockPerson(n = 4, seed = 1) |> + omock::mockObservationPeriod(seed = 1) cdm_local$concept <- dplyr::tibble( "concept_id" = c(1, 2), "concept_name" = c("my concept 1", "my concept 2"), @@ -384,8 +384,8 @@ test_that("out of observation", { # out of observation (subject 3) # overlapping (subject 4) cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() + omock::mockPerson(n = 4, seed = 1) |> + omock::mockObservationPeriod(seed = 1) cdm_local$concept <- dplyr::tibble( "concept_id" = c(1, 2), "concept_name" = c("my concept 1", "my concept 2"), diff --git a/tests/testthat/test-exitAtDate.R b/tests/testthat/test-exitAtDate.R index 67ba1724..5db1387f 100644 --- a/tests/testthat/test-exitAtDate.R +++ b/tests/testthat/test-exitAtDate.R @@ -1,24 +1,24 @@ test_that("exit at observation end", { cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> - omock::mockCohort(name = c("cohort"), numberCohorts = 2) + omock::mockPerson(n = 4,seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(name = c("cohort"), numberCohorts = 2,seed = 1) cdm <- cdm_local |> copyCdm() # simple example - test it works cdm$cohort1 <- cdm$cohort |> exitAtObservationEnd(name = "cohort1") expect_true(all(cdm$cohort1 |> dplyr::pull(cohort_start_date) |> sort() == - c("1997-10-22", "2000-06-23", "2001-03-30", "2015-03-05", "2015-03-25"))) + c("1999-05-03", "2001-03-24", "2003-05-17", "2015-02-25"))) expect_true(all(cdm$cohort1 |> dplyr::pull(cohort_end_date) |> sort() == - c("2013-06-29", "2013-06-29", "2013-12-31", "2015-10-11", "2015-10-11"))) - expect_true(all(cdm$cohort1 |> dplyr::pull(subject_id) |> sort() == c(1, 1, 3, 3, 4))) + c("2003-06-15", "2013-06-29", "2013-06-29", "2015-10-11"))) + expect_true(all(cdm$cohort1 |> dplyr::pull(subject_id) |> sort() == c(1, 1, 2, 3))) # test cohort id and name cdm$cohort <- cdm$cohort |> exitAtObservationEnd(cohortId = 1) expect_true(all(cdm$cohort |> dplyr::pull(cohort_start_date) |> sort() == - c("1997-10-22", "2000-06-23", "2001-03-30", "2001-07-16", "2001-12-04", "2015-03-05", "2015-03-25"))) + c("1999-05-03", "2001-03-24", "2001-11-28", "2002-01-30", "2002-06-13", "2003-05-17", "2015-02-25"))) expect_true(all(cdm$cohort |> dplyr::pull(cohort_end_date) |> sort() == - c("2001-07-15", "2001-12-03", "2006-09-27", "2013-06-29", "2013-12-31", "2015-07-06", "2015-10-11"))) - expect_true(all(cdm$cohort |> dplyr::pull(subject_id) |> sort() == c(1, 1, 1, 1, 3, 3, 4))) + c("2001-11-27", "2002-01-29", "2002-06-12", "2003-06-15", "2005-01-15", "2013-06-29", "2015-10-11"))) + expect_true(all(cdm$cohort |> dplyr::pull(subject_id) |> sort() == c(1, 1, 1, 1, 1, 2, 3))) expect_true(all(attrition(cdm$cohort)$reason == c("Initial qualifying events", "Exit at observation period end date", "Initial qualifying events"))) # additional columns warning @@ -35,9 +35,9 @@ test_that("exit at observation end", { test_that("exit at death date", { testthat::skip_on_cran() cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> - omock::mockCohort(name = c("cohort"), numberCohorts = 2) + omock::mockPerson(n = 4,seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(name = c("cohort"), numberCohorts = 2,seed = 1) cdm_local$death <- dplyr::tibble( person_id = 1:2, death_date = as.Date(c("2013-06-29", "2003-06-15")), @@ -48,30 +48,30 @@ test_that("exit at death date", { # simple example - require death TRUE works cdm$cohort1 <- cdm$cohort |> exitAtDeath(requireDeath = TRUE, name = "cohort1") expect_true(all(cdm$cohort1 |> dplyr::pull(cohort_start_date) |> sort() == - c("2000-06-23", "2001-03-30"))) + c("1999-05-03", "2001-03-24", "2003-05-17"))) expect_true(all(cdm$cohort1 |> dplyr::pull(cohort_end_date) |> sort() == - c("2013-06-29", "2013-06-29"))) - expect_true(all(cdm$cohort1 |> dplyr::pull(subject_id) |> sort() == c(1, 1))) + c("2003-06-15", "2013-06-29", "2013-06-29"))) + expect_true(all(cdm$cohort1 |> dplyr::pull(subject_id) |> sort() == c(1, 1, 2))) expect_true(all(attrition(cdm$cohort1)$reason == c("Initial qualifying events", "No death recorded", "Exit at death", "Initial qualifying events", "No death recorded", "Exit at death"))) # simple example - require death FALSE works cdm$cohort2 <- cdm$cohort |> exitAtDeath(requireDeath = FALSE, name = "cohort2") expect_true(all(cdm$cohort2 |> dplyr::pull(cohort_start_date) |> sort() == - c("1997-10-22", "2000-06-23", "2001-03-30", "2015-03-05", "2015-03-25"))) + c("1999-05-03", "2001-03-24", "2003-05-17", "2015-02-25"))) expect_true(all(cdm$cohort2 |> dplyr::pull(cohort_end_date) |> sort() == - c("1999-05-28", "2013-06-29", "2013-06-29", "2015-04-14", "2015-07-06"))) - expect_true(all(cdm$cohort2 |> dplyr::pull(subject_id) |> sort() == c(1, 1, 3, 3, 4))) + c("2003-06-15", "2013-06-29", "2013-06-29", "2015-04-30"))) + expect_true(all(cdm$cohort2 |> dplyr::pull(subject_id) |> sort() == c(1, 1, 2, 3))) expect_true(all(attrition(cdm$cohort2)$reason == c("Initial qualifying events", "Exit at death", "Initial qualifying events", "Exit at death"))) # cohort ID and name cdm$cohort <- cdm$cohort |> exitAtDeath(cohortId = 1, requireDeath = TRUE) expect_true(all(cdm$cohort |> dplyr::pull(cohort_start_date) |> sort() == - c("2000-06-23", "2001-03-30", "2001-07-16", "2001-12-04", "2015-03-05"))) + c("1999-05-03", "2001-03-24", "2001-11-28", "2002-01-30", "2002-06-13", "2003-05-17"))) expect_true(all(cdm$cohort |> dplyr::pull(cohort_end_date) |> sort() == - c("2001-07-15", "2001-12-03", "2006-09-27", "2013-06-29", "2015-07-06"))) - expect_true(all(cdm$cohort |> dplyr::pull(subject_id) |> sort() == c(1, 1, 1, 1, 3))) + c("2001-11-27", "2002-01-29", "2002-06-12", "2003-06-15", "2005-01-15", "2013-06-29"))) + expect_true(all(cdm$cohort |> dplyr::pull(subject_id) |> sort() == c(1, 1, 1, 1, 1, 2))) expect_true(all(attrition(cdm$cohort)$reason == c("Initial qualifying events", "No death recorded", "Exit at death", "Initial qualifying events"))) diff --git a/tests/testthat/test-intersectCohorts.R b/tests/testthat/test-intersectCohorts.R index 23c2ef70..c7a7a0d7 100644 --- a/tests/testthat/test-intersectCohorts.R +++ b/tests/testthat/test-intersectCohorts.R @@ -385,8 +385,8 @@ test_that("returnNonOverlappingCohorts - three cohorts", { test_that("attrition and cohortId", { testthat::skip_on_cran() cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> + omock::mockPerson(n = 4, seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> omock::mockCohort(name = c("cohort1"), numberCohorts = 4, seed = 2) cdm_local$person <- cdm_local$person |> dplyr::mutate(dplyr::across(dplyr::ends_with("of_birth"), ~ as.numeric(.x))) @@ -416,13 +416,13 @@ test_that("attrition and cohortId", { )) expect_true(all(omopgenerics::attrition(cdm$cohort1)$reason_id == c(1, 1:6, 1:6))) expect_true(all(omopgenerics::attrition(cdm$cohort1)$number_records |> sort() == - c(0, 0, 0, 0, 1, 1, 1, 4, 4, 4, 4, 4, 4))) + c(0, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4))) expect_true(all(omopgenerics::attrition(cdm$cohort1)$number_subjects |> sort() == - c(0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3))) + c(0, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3))) expect_true(all(omopgenerics::attrition(cdm$cohort1)$excluded_records |> sort() == - c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4))) + c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3))) expect_true(all(omopgenerics::attrition(cdm$cohort1)$excluded_subjects |> sort() == - c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2))) + c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2))) expect_true(all(omopgenerics::settings(cdm$cohort1)$cohort_name |> sort() == c("cohort_1_cohort_2", "only_in_cohort_1", "only_in_cohort_2"))) diff --git a/tests/testthat/test-measurementCohort.R b/tests/testthat/test-measurementCohort.R index dabb6899..d934076d 100644 --- a/tests/testthat/test-measurementCohort.R +++ b/tests/testthat/test-measurementCohort.R @@ -1,5 +1,5 @@ test_that("mearurementCohorts works", { - cdm <- mockCohortConstructor(con = NULL) + cdm <- mockCohortConstructor(con = NULL, seed = 1) cdm$concept <- cdm$concept |> dplyr::union_all( dplyr::tibble( @@ -207,7 +207,7 @@ test_that("mearurementCohorts works", { test_that("expected errors", { testthat::skip_on_cran() - cdm <- mockCohortConstructor(con = NULL) + cdm <- mockCohortConstructor(con = NULL, seed = 1) cdm$concept <- cdm$concept |> dplyr::union_all( dplyr::tibble( diff --git a/tests/testthat/test-requireCohortIntersect.R b/tests/testthat/test-requireCohortIntersect.R index 5084bbc6..cc33782c 100644 --- a/tests/testthat/test-requireCohortIntersect.R +++ b/tests/testthat/test-requireCohortIntersect.R @@ -1,8 +1,8 @@ test_that("requiring presence in another cohort", { cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> - omock::mockCohort(name = c("cohort1"), numberCohorts = 2) |> + omock::mockPerson(n = 4, seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(name = c("cohort1"), numberCohorts = 2, seed = 1) |> omock::mockCohort(name = c("cohort2"), numberCohorts = 2, seed = 2) cdm <- cdm_local |> copyCdm() @@ -69,9 +69,9 @@ test_that("requiring presence in another cohort", { window = c(0, Inf), censorDate = "cohort_end_date", name = "cohort5") - expect_true(all(cdm$cohort5 |> dplyr::pull("cohort_start_date") == c("2015-04-14", "2015-02-23"))) - expect_true(all(cdm$cohort5 |> dplyr::pull("subject_id") == c("3", "3"))) - expect_true(all(cdm$cohort5 |> dplyr::pull("cohort_definition_id") == c("1", "2"))) + expect_true(all(cdm$cohort5 |> dplyr::pull("cohort_start_date") == c("2003-05-08", "2000-06-17", "2004-12-12"))) + expect_true(all(cdm$cohort5 |> dplyr::pull("subject_id") == c("1", "1", "1"))) + expect_true(all(cdm$cohort5 |> dplyr::pull("cohort_definition_id") == c("1", "2", "2"))) expect_true(all(omopgenerics::attrition(cdm$cohort5)$reason == c("Initial qualifying events", "In cohort cohort_2 between 0 & Inf days relative to cohort_start_date between 1 and Inf times, censoring at cohort_end_date", @@ -87,10 +87,10 @@ test_that("requiring presence in another cohort", { censorDate = "cohort_end_date", name = "cohort6") expect_true(all(cdm$cohort6 |> dplyr::pull("cohort_start_date") |> sort() == - c("1993-01-06", "1999-06-23", "2000-03-06", "2003-07-21", - "2015-02-23", "2015-04-14"))) - expect_true(all(cdm$cohort6 |> dplyr::pull("subject_id") |> sort() == c("1", "2", "2", "3", "3", "4"))) - expect_true(all(cdm$cohort6 |> dplyr::pull("cohort_definition_id") |> sort() == c(rep("1", 4), rep("2", 2)))) + c("1999-07-11", "2000-01-11", "2000-05-28", "2000-06-17", + "2003-05-08", "2004-12-12", "2015-01-25", "2015-02-02"))) + expect_true(all(cdm$cohort6 |> dplyr::pull("subject_id") |> sort() == c("1","1","1", "2", "2", "2", "3", "3"))) + expect_true(all(cdm$cohort6 |> dplyr::pull("cohort_definition_id") |> sort() == c(rep("1", 4), rep("2", 4)))) expect_true(all(omopgenerics::attrition(cdm$cohort6)$reason == c("Initial qualifying events", "Initial qualifying events", @@ -105,10 +105,9 @@ test_that("requiring presence in another cohort", { censorDate = "cohort_end_date", name = "cohort7") expect_true(all(cdm$cohort7 |> dplyr::pull("cohort_start_date") |> sort() == - c("1999-06-23", "2000-03-06", "2003-07-21", "2015-02-02", - "2015-02-08", "2015-04-14"))) - expect_true(all(cdm$cohort7 |> dplyr::pull("subject_id") |> sort() == c("1", "2", "2", "3", "3", "3"))) - expect_true(all(cdm$cohort7 |> dplyr::pull("cohort_definition_id") |> sort() == c(rep("1", 4), rep("2", 2)))) + c("2000-01-11", "2000-05-28", "2003-05-08", "2015-01-25"))) + expect_true(all(cdm$cohort7 |> dplyr::pull("subject_id") |> sort() == c("1", "2", "2", "3"))) + expect_true(all(cdm$cohort7 |> dplyr::pull("cohort_definition_id") |> sort() == c(rep("1", 4)))) expect_true(all(omopgenerics::attrition(cdm$cohort7)$reason == c("Initial qualifying events", "Initial qualifying events", @@ -143,9 +142,9 @@ test_that("requiring presence in another cohort", { test_that("requiring absence in another cohort", { testthat::skip_on_cran() cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> - omock::mockCohort(name = c("cohort1"), numberCohorts = 2) |> + omock::mockPerson(n = 4,seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(name = c("cohort1"), numberCohorts = 2, seed = 1) |> omock::mockCohort(name = c("cohort2"), numberCohorts = 2, seed = 2) cdm <- cdm_local |> copyCdm() @@ -201,7 +200,7 @@ test_that("different intersection count requirements", { as.Date('2019-01-06')) ) cdm_local <- omock::mockCdmReference() |> omock::mockCdmFromTables(tables = list("cohort1" = cohort1, - "cohort2" = cohort2)) + "cohort2" = cohort2), seed = 1) cdm <- cdm_local |> copyCdm() # no intersections - people not in cohort2 diff --git a/tests/testthat/test-requireConceptIntersect.R b/tests/testthat/test-requireConceptIntersect.R index 43b2efef..eb599eb6 100644 --- a/tests/testthat/test-requireConceptIntersect.R +++ b/tests/testthat/test-requireConceptIntersect.R @@ -1,8 +1,8 @@ test_that("require flag in concept", { cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> - omock::mockCohort(name = c("cohort1"), numberCohorts = 2) + omock::mockPerson(n = 4, seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(name = c("cohort1"), numberCohorts = 2, seed = 1) cdm_local$concept <- dplyr::tibble( "concept_id" = 1, "concept_name" = "my concept", @@ -38,7 +38,7 @@ test_that("require flag in concept", { expect_true(all(cdm$cohort3 |> dplyr::pull("subject_id") == rep(1, 5))) expect_true(all(cdm$cohort3 |> dplyr::pull("cohort_start_date") |> sort() == - c("2000-06-23", "2001-03-30", "2001-07-16", "2001-12-04", "2003-06-15"))) + c("2001-03-24", "2001-11-28", "2002-01-30", "2002-06-13", "2003-05-17", "2004-03-11"))) expect_true(all(omopgenerics::attrition(cdm$cohort3)$reason == c("Initial qualifying events", @@ -52,9 +52,9 @@ test_that("require flag in concept", { window = c(-Inf, Inf), name = "cohort4") expect_true(all(cdm$cohort4 |> dplyr::pull("subject_id") == - c(rep(1, 5), 3))) + c(rep(1, 6)))) expect_true(all(cdm$cohort4 |> dplyr::pull("cohort_start_date") |> sort() == - c("2000-06-23", "2001-03-30", "2001-07-16", "2001-12-04", "2003-06-15", "2015-03-05"))) + c("2001-03-24", "2001-11-28", "2002-01-30", "2002-06-13", "2003-05-17", "2004-03-11"))) expect_true(all(omopgenerics::attrition(cdm$cohort4)$reason == c("Initial qualifying events", "Concept a between -Inf & Inf days relative to cohort_start_date between 1 and Inf", @@ -113,9 +113,9 @@ test_that("require flag in concept", { test_that("requiring absence in another cohort", { testthat::skip_on_cran() cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> - omock::mockCohort(name = c("cohort1"), numberCohorts = 2) + omock::mockPerson(n = 4, seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(name = c("cohort1"), numberCohorts = 2, seed = 1) cdm_local$concept <- dplyr::tibble( "concept_id" = 1, "concept_name" = "my concept", @@ -179,9 +179,9 @@ test_that("requiring absence in another cohort", { name = "cohort3_exclusion_partial" ) expect_true(all(cdm$cohort3_exclusion_partial |> dplyr::pull("subject_id") |> sort() == - c(1, 1, 1, 3, 3, 4))) + c(1, 1, 1, 1, 2, 3))) expect_true(all(cdm$cohort3_exclusion_partial |> dplyr::pull("cohort_start_date") |> sort() == - c("1997-10-22", "2000-06-23", "2001-07-16", "2001-12-04", "2015-03-05", "2015-03-25"))) + c("1999-05-03", "2001-03-24", "2001-11-28", "2002-01-30", "2002-06-13", "2015-02-25"))) expect_true(all(omopgenerics::attrition(cdm$cohort3_exclusion_partial)$reason == c("Initial qualifying events", "Not in concept a between -Inf & Inf days relative to cohort_start_date", @@ -201,7 +201,7 @@ test_that("different intersection count requirements", { cohort_end_date = as.Date('2020-01-01')) cdm_local <- omock::mockCdmReference() |> - omock::mockCdmFromTables(tables = list("cohort1" = cohort1)) + omock::mockCdmFromTables(tables = list("cohort1" = cohort1), seed = 1) cdm_local$concept <- dplyr::tibble( "concept_id" = 1, diff --git a/tests/testthat/test-requireDateRange.R b/tests/testthat/test-requireDateRange.R index c9b848f3..1a659bee 100644 --- a/tests/testthat/test-requireDateRange.R +++ b/tests/testthat/test-requireDateRange.R @@ -1,9 +1,9 @@ test_that("requireDateRange", { testthat::skip_on_cran() cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> - omock::mockCohort(name = c("cohort1"), numberCohorts = 2) |> + omock::mockPerson(n = 4, seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(name = c("cohort1"), numberCohorts = 2,seed = 1) |> omock::mockCohort(name = c("cohort2"), numberCohorts = 2, seed = 2) cdm <- cdm_local |> copyCdm() @@ -22,7 +22,7 @@ test_that("requireDateRange", { expect_true(all(cdm$cohort1 %>% dplyr::arrange(.data$cohort_start_date) %>% dplyr::pull("cohort_start_date") == - c("2015-04-14", "2015-02-02", "2015-02-08", "2015-02-23"))) + c("2015-01-25", "2015-02-02"))) # index date cdm$cohort3 <- cdm$cohort2 %>% @@ -43,17 +43,17 @@ test_that("requireDateRange", { "cohort_start_date after 2000-01-01", "cohort_start_date before 2001-01-01", "Initial qualifying events"))) - expect_true(all(cohortCount(cdm$cohort4)$number_records == c(1,4))) - expect_true(all(cohortCount(cdm$cohort4)$number_subjects == c(1,2))) + expect_true(all(cohortCount(cdm$cohort4)$number_records == c(2,4))) + expect_true(all(cohortCount(cdm$cohort4)$number_subjects == c(1,3))) expect_true(all(cdm$cohort4 |> dplyr::pull("cohort_start_date") |> sort() == - c("1993-01-06", "2000-03-06", "2015-02-02", "2015-02-08", "2015-02-23"))) + c("1999-07-11", "2000-01-11", "2000-05-28", "2000-06-17", "2004-12-12", "2015-02-02"))) # NA expect_no_error( cdm$cohort5 <- cdm$cohort2 %>% requireInDateRange(dateRange = as.Date(c(NA, "2010-01-01")), name = "cohort5") ) expect_true(all(cdm$cohort5 |> dplyr::pull("cohort_start_date") |> sort() == - c("1993-01-06", "1999-06-23", "2000-03-06", "2003-07-21"))) + c("1999-07-11", "2000-01-11", "2000-05-28", "2000-06-17", "2003-05-08", "2004-12-12"))) expect_true(all(attrition(cdm$cohort5)$reason == c("Initial qualifying events", "cohort_start_date before 2010-01-01", @@ -65,7 +65,7 @@ test_that("requireDateRange", { requireInDateRange(dateRange = as.Date(c("2000-01-01", NA)), name = "cohort6") ) expect_true(all(cdm$cohort6 |> dplyr::pull("cohort_start_date") |> sort() == - c("2000-03-06", "2003-07-21", "2015-02-02", "2015-02-08", "2015-02-23", "2015-04-14"))) + c("2000-01-11", "2000-05-28", "2000-06-17", "2003-05-08", "2004-12-12", "2015-01-25", "2015-02-02"))) expect_true(all(attrition(cdm$cohort6)$reason == c("Initial qualifying events", "cohort_start_date after 2000-01-01", @@ -101,9 +101,9 @@ test_that("requireDateRange", { test_that("trim cohort dates", { testthat::skip_on_cran() cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> - omock::mockCohort(name = c("cohort1"), numberCohorts = 2) |> + omock::mockPerson(n = 4, seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(name = c("cohort1"), numberCohorts = 2, seed = 1) |> omock::mockCohort(name = c("cohort2"), numberCohorts = 2, seed = 2) cdm <- cdm_local |> copyCdm() @@ -111,22 +111,22 @@ test_that("trim cohort dates", { trimToDateRange(dateRange = as.Date(c("2001-01-01", "2005-01-01"))) expect_equal(sort(cdm$cohort1 %>% - dplyr::pull("subject_id")), c(1, 1, 1, 1, 1)) + dplyr::pull("subject_id")), c(1, 1, 1, 1, 1, 1, 2)) expect_true(all(cdm$cohort1 %>% dplyr::pull("cohort_start_date") == - c("2001-03-30", "2003-06-15", "2001-01-01", "2001-07-16", "2001-12-04"))) + c("2003-05-17", "2004-03-11", "2001-01-01", "2001-03-24", "2001-11-28", "2002-01-30", "2002-06-13"))) expect_true(all(cdm$cohort1 %>% dplyr::pull("cohort_end_date") == - c("2003-06-14", "2005-01-01", "2001-07-15", "2001-12-03", "2005-01-01"))) + c("2004-03-10", "2005-01-01", "2001-06-15", "2001-11-27", "2002-01-29", "2002-06-12", "2005-01-01"))) # cohort id cdm$cohort3 <- cdm$cohort2 %>% trimToDateRange(dateRange = as.Date(c("2001-01-01", "2005-01-01")), cohortId = "cohort_1", name = "cohort3") - expect_true(omopgenerics::cohortCount(cdm$cohort3)$number_records[1] == 1) + expect_true(omopgenerics::cohortCount(cdm$cohort3)$number_records[1] == 2) expect_equal(sort(cdm$cohort3 %>% - dplyr::pull("subject_id")), c(1, 3, 3, 3, 4)) + dplyr::pull("subject_id")), c(1, 1, 1, 2, 2, 3)) expect_equal(omopgenerics::attrition(cdm$cohort3)$reason[ omopgenerics::attrition(cdm$cohort3)$cohort_definition_id == 1], c("Initial qualifying events", "cohort_start_date >= 2001-01-01", "cohort_end_date <= 2005-01-01") @@ -142,7 +142,7 @@ test_that("trim cohort dates", { cohortId = 1, name = "cohort4") expect_equal(sort(cdm$cohort4 %>% dplyr::pull("cohort_end_date")), - as.Date(c("2000-02-12", "2000-03-05", "2000-08-23", "2005-01-01", "2015-02-07", "2015-02-22", "2015-04-17"))) + as.Date(c("2000-05-27", "2001-09-08", "2002-03-26", "2004-12-11", "2005-01-01", "2007-09-06", "2015-08-12"))) expect_equal(omopgenerics::attrition(cdm$cohort4)$reason, c("Initial qualifying events", "cohort_end_date <= 2005-01-01", "Initial qualifying events") ) @@ -152,7 +152,7 @@ test_that("trim cohort dates", { cohortId = 1, name = "cohort5") expect_equal(sort(cdm$cohort5 %>% dplyr::pull("cohort_start_date")), - as.Date(c("1993-01-06", "2005-01-01", "2015-02-02", "2015-02-08", "2015-02-23", "2015-04-14"))) + as.Date(c("1999-07-11", "2000-06-17", "2004-12-12", "2005-01-01", "2015-01-25", "2015-02-02"))) expect_equal(omopgenerics::attrition(cdm$cohort5)$reason, c("Initial qualifying events", "cohort_start_date >= 2005-01-01", "Initial qualifying events") ) diff --git a/tests/testthat/test-requireDeathFlag.R b/tests/testthat/test-requireDeathFlag.R index 822a797f..d1ac6c55 100644 --- a/tests/testthat/test-requireDeathFlag.R +++ b/tests/testthat/test-requireDeathFlag.R @@ -1,8 +1,8 @@ test_that("requiring death", { testthat::skip_on_cran() cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> + omock::mockPerson(n = 4, seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> omock::mockCohort(name = c("cohort1"), numberCohorts = 2) cdm_local$death <- dplyr::tibble( person_id = c(1,3), @@ -53,10 +53,10 @@ test_that("requiring death", { cohortId = 1, window = c(0, 365), name = "cohort6") - expect_true(all(cdm$cohort6 |> dplyr::pull("subject_id") |> sort() %in% c(1, 1, 1, 3, 3))) + expect_true(all(cdm$cohort6 |> dplyr::pull("subject_id") |> sort() %in% c(2,2,2,2))) expect_true(all( cdm$cohort6 |> dplyr::pull("cohort_start_date") |> sort() == - c("2000-06-23", "2001-07-16", "2001-12-04", "2015-03-05", "2015-03-25") + c("1999-04-19", "2000-02-04", "2000-03-12", "2000-08-05") )) expect_true(all(omopgenerics::attrition(cdm$cohort6)$reason == c("Initial qualifying events", @@ -77,8 +77,8 @@ test_that("requiring death", { test_that("not death", { testthat::skip_on_cran() cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> + omock::mockPerson(n = 4, seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> omock::mockCohort(name = c("cohort1"), numberCohorts = 2, seed = 3) cdm_local$death <- dplyr::tibble( person_id = c(1,3), @@ -107,10 +107,10 @@ test_that("not death", { window = c(0, Inf), name = "cohort4", negate = TRUE) - expect_true(all(cdm$cohort4 |> dplyr::pull("subject_id") |> sort() == c(1, 1, 2, 4, 4, 4))) + expect_true(all(cdm$cohort4 |> dplyr::pull("subject_id") |> sort() == c(1, 1, 1, 2, 4, 4))) expect_true(all( cdm$cohort4 |> dplyr::pull("cohort_start_date") |> sort() == - c("1990-10-29", "1992-08-05", "1997-04-25", "2000-01-06", "2003-05-31", "2003-07-20") + c("1992-02-15", "1992-04-20", "1999-07-17", "2001-09-10", "2002-08-02", "2004-06-18") )) expect_true(all(omopgenerics::attrition(cdm$cohort4)$reason == c("Initial qualifying events", "Alive between 0 & Inf days relative to cohort_start_date", diff --git a/tests/testthat/test-requireDemographics.R b/tests/testthat/test-requireDemographics.R index 90df5b72..53d303cc 100644 --- a/tests/testthat/test-requireDemographics.R +++ b/tests/testthat/test-requireDemographics.R @@ -1,9 +1,9 @@ test_that("test it works and expected errors", { testthat::skip_on_cran() cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 10) |> - omock::mockObservationPeriod() |> - omock::mockCohort() + omock::mockPerson(n = 10, seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(seed = 1) # to remove in new omock cdm_local$person <- cdm_local$person |> dplyr::mutate(dplyr::across(dplyr::ends_with("of_birth"), ~ as.numeric(.x))) @@ -129,17 +129,16 @@ test_that("test it works and expected errors", { test_that("restrictions applied to single cohort", { testthat::skip_on_cran() cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 1) |> - omock::mockObservationPeriod() |> - omock::mockCohort(recordPerson = 3) + omock::mockPerson(n = 1,seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(recordPerson = 3,seed = 1) # to remove in new omock cdm_local$person <- cdm_local$person |> dplyr::mutate(dplyr::across(dplyr::ends_with("of_birth"), ~ as.numeric(.x))) cdm <- cdm_local |> copyCdm() - cdm$cohort1 <- cdm$cohort %>% requireDemographics(ageRange = list(c(0, 5)), name = "cohort1") - expect_true("2001-07-30" == cdm$cohort1 %>% dplyr::pull("cohort_start_date")) + expect_true(all(c("2001-03-30", "2003-06-15") == cdm$cohort1 %>% dplyr::pull("cohort_start_date"))) expect_true(all( c("Initial qualifying events", "Age requirement: 0 to 5", "Sex requirement: Both", "Prior observation requirement: 0 days", "Future observation requirement: 0 days") == @@ -176,9 +175,9 @@ test_that("restrictions applied to single cohort", { test_that("ignore existing cohort extra variables", { testthat::skip_on_cran() cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 1) |> - omock::mockObservationPeriod() |> - omock::mockCohort(recordPerson = 3) + omock::mockPerson(n = 1,seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(recordPerson = 3,seed = 1) # to remove in new omock cdm_local$person <- cdm_local$person |> dplyr::mutate(dplyr::across(dplyr::ends_with("of_birth"), ~ as.numeric(.x))) @@ -215,9 +214,9 @@ test_that("ignore existing cohort extra variables", { test_that("external columns kept after requireDemographics", { testthat::skip_on_cran() cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 1) |> - omock::mockObservationPeriod() |> - omock::mockCohort(recordPerson = 3) + omock::mockPerson(n = 1,seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(recordPerson = 3,seed = 1) cdm_local$cohort <- cdm_local$cohort %>% dplyr::mutate( col_extra1 = as.numeric(subject_id) + 1, @@ -240,23 +239,23 @@ test_that("external columns kept after requireDemographics", { test_that("cohortIds", { testthat::skip_on_cran() cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 3) |> - omock::mockObservationPeriod() |> - omock::mockCohort(numberCohorts = 3) + omock::mockPerson(n = 3,seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(numberCohorts = 3,seed = 1) # to remove in new omock cdm_local$person <- cdm_local$person |> dplyr::mutate(dplyr::across(dplyr::ends_with("of_birth"), ~ as.numeric(.x))) cdm <- cdm_local |> copyCdm() - cdm$new_cohort <- requireSex(cohort = cdm$cohort, cohortId = 1, sex = "Male") |> + cdm$new_cohort <- requireSex(cohort = cdm$cohort, cohortId = 1, sex = "Male", name = "new_cohort") |> requirePriorObservation(cohortId = "cohort_3", minPriorObservation = 1000, name = "new_cohort") expect_true(all( omopgenerics::attrition(cdm$new_cohort)$reason == c("Initial qualifying events", "Sex requirement: Male", "Initial qualifying events" , "Initial qualifying events", "Prior observation requirement: 1000 days") )) - expect_true(all(cdm$new_cohort |> dplyr::pull("cohort_definition_id") == c(2,2,2,3))) - expect_true(all(cdm$new_cohort |> dplyr::pull("subject_id") == c(2,2,2,1))) + expect_true(all(cdm$new_cohort |> dplyr::pull("cohort_definition_id") == c(2,2,2))) + expect_true(all(cdm$new_cohort |> dplyr::pull("subject_id") == c(1,1,1))) PatientProfiles::mockDisconnect(cdm) }) @@ -264,9 +263,9 @@ test_that("cohortIds", { test_that("test more than one restriction", { testthat::skip_on_cran() cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 3) |> - omock::mockObservationPeriod() |> - omock::mockCohort(numberCohorts = 3) + omock::mockPerson(n = 3,seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(numberCohorts = 3,seed = 1) # to remove in new omock cdm_local$person <- cdm_local$person |> dplyr::mutate(dplyr::across(dplyr::ends_with("of_birth"), ~ as.numeric(.x))) @@ -277,21 +276,21 @@ test_that("test more than one restriction", { requireAge(ageRange = list(c(0,19), c(20, 40), c(0, 40)), name = "cohort1") expect_true(all( cdm$cohort1 |> dplyr::pull("cohort_definition_id") |> sort() == - c(1, 1, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 8, 8, 8, 9, 9) + c(1, 1, 2, 2, 2, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 9, 9, 9) )) expect_true(all( cdm$cohort1 |> dplyr::pull("cohort_start_date") |> sort() == - c('1999-08-27', '1999-08-27', '2000-01-07', '2000-01-07', '2000-04-10', - '2000-04-10', '2001-02-13', '2001-02-13', '2001-07-30', '2001-07-30', - '2003-03-02', '2003-03-02', '2004-01-21', '2004-01-21', '2015-01-25', - '2015-01-25', '2015-04-09', '2015-04-09') + c("1999-11-16", "1999-11-16", "1999-12-17", "1999-12-17", "1999-12-19", + "1999-12-19", "2000-05-15", "2000-05-15", "2000-06-23", "2000-06-23", + "2001-07-16", "2001-07-16", "2001-12-04", "2001-12-04", "2003-06-15", + "2003-06-15", "2004-09-11", "2004-09-11") )) expect_true(all( cdm$cohort1 |> dplyr::pull("cohort_end_date") |> sort() == - c('2000-04-09', '2000-04-09', '2001-02-12', '2001-02-12', '2001-03-23', - '2001-03-23', '2001-07-15', '2001-07-15', '2004-01-20', '2004-01-20', - '2004-07-30', '2004-07-30', '2005-05-04', '2005-05-04', '2015-06-16', - '2015-06-16', '2015-06-26', '2015-06-26') + c("1999-12-18", "1999-12-18", "2000-05-14", "2000-05-14", "2001-02-23", + "2001-02-23", "2001-07-15", "2001-07-15", "2001-08-26", "2001-08-26", + "2001-12-03", "2001-12-03", "2004-09-10", "2004-09-10", "2005-07-25", + "2005-07-25", "2006-09-27", "2006-09-27") )) expect_true(all( attrition(cdm$cohort1)$reason |> sort() == @@ -304,7 +303,7 @@ test_that("test more than one restriction", { )) expect_true(all( attrition(cdm$cohort1)$number_records |> sort() == - c(0, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3) + c(0, 0, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3) )) expect_true(all(settings(cdm$cohort1)$age_range |> unique() == c("0_19", "0_40", "20_40"))) expect_true(all(settings(cdm$cohort1)$cohort_name |> unique() == @@ -316,23 +315,25 @@ test_that("test more than one restriction", { requireAge(ageRange = list(c(0,19), c(20, 40), c(0, 40), c(0, 150)), name = "cohort2") expect_true(all( cdm$cohort2 |> dplyr::pull("cohort_definition_id") |> sort() == - c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 11, 11, 11, 12, 12) + c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 5, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 12, 12, 12) )) expect_true(all( cdm$cohort2 |> dplyr::pull("cohort_start_date") |> sort() == - c('1999-08-27', '1999-08-27', '1999-08-27', '2000-01-07', '2000-01-07', '2000-01-07', - '2000-04-10', '2000-04-10', '2000-04-10', '2001-02-13', '2001-02-13', '2001-02-13', - '2001-07-30', '2001-07-30', '2001-07-30', '2003-03-02', '2003-03-02', '2003-03-02', - '2004-01-21', '2004-01-21', '2004-01-21', '2015-01-25', '2015-01-25', '2015-01-25', - '2015-04-09', '2015-04-09', '2015-04-09') + c("1999-11-16", "1999-11-16", "1999-11-16", "1999-12-17", "1999-12-17", + "1999-12-17", "1999-12-19", "1999-12-19", "1999-12-19", "2000-05-15", + "2000-05-15", "2000-05-15", "2000-06-23", "2000-06-23", "2000-06-23", + "2001-07-16", "2001-07-16", "2001-07-16", "2001-12-04", "2001-12-04", + "2001-12-04", "2003-06-15", "2003-06-15", "2003-06-15", "2004-09-11", + "2004-09-11", "2004-09-11") )) expect_true(all( cdm$cohort2 |> dplyr::pull("cohort_end_date") |> sort() == - c('2000-04-09', '2000-04-09', '2000-04-09', '2001-02-12', '2001-02-12', '2001-02-12', - '2001-03-23', '2001-03-23', '2001-03-23', '2001-07-15', '2001-07-15', '2001-07-15', - '2004-01-20', '2004-01-20', '2004-01-20', '2004-07-30', '2004-07-30', '2004-07-30', - '2005-05-04', '2005-05-04', '2005-05-04', '2015-06-16', '2015-06-16', '2015-06-16', - '2015-06-26', '2015-06-26', '2015-06-26') + c("1999-12-18", "1999-12-18", "1999-12-18", "2000-05-14", "2000-05-14", + "2000-05-14", "2001-02-23", "2001-02-23", "2001-02-23", "2001-07-15", + "2001-07-15", "2001-07-15", "2001-08-26", "2001-08-26", "2001-08-26", + "2001-12-03", "2001-12-03", "2001-12-03", "2004-09-10", "2004-09-10", + "2004-09-10", "2005-07-25", "2005-07-25", "2005-07-25", "2006-09-27", + "2006-09-27", "2006-09-27") )) expect_true(all(settings(cdm$cohort2)$age_range |> unique() == c("0_150", "0_19", "0_40", "20_40"))) expect_true(all(colnames(settings(cdm$cohort2)) %in% c("cohort_definition_id", "cohort_name", "age_range"))) @@ -347,8 +348,8 @@ test_that("test more than one restriction", { # one empty output cohort cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 3) |> - omock::mockObservationPeriod() |> + omock::mockPerson(n = 3,seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> omock::mockCohort(numberCohorts = 3, seed = 4) # to remove in new omock cdm_local$person <- cdm_local$person |> @@ -413,8 +414,8 @@ test_that("test more than one restriction", { test_that("codelist kept with >1 requirement", { testthat::skip_on_cran() cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() + omock::mockPerson(n = 4,seed = 1) |> + omock::mockObservationPeriod(seed = 1) # to remove in new omock cdm_local$person <- cdm_local$person |> dplyr::mutate(dplyr::across(dplyr::ends_with("of_birth"), ~ as.numeric(.x))) @@ -462,8 +463,8 @@ test_that("codelist kept with >1 requirement", { test_that("settings with extra columns", { testthat::skip_on_cran() cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 3) |> - omock::mockObservationPeriod() |> + omock::mockPerson(n = 3,seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> omock::mockCohort(numberCohorts = 3, seed = 4) # to remove in new omock cdm_local$person <- cdm_local$person |> @@ -494,8 +495,8 @@ test_that("settings with extra columns", { test_that("requireInteractions", { testthat::skip_on_cran() cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 3) |> - omock::mockObservationPeriod() |> + omock::mockPerson(n = 3,seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> omock::mockCohort(numberCohorts = 3, seed = 4) # to remove in new omock cdm_local$person <- cdm_local$person |> diff --git a/tests/testthat/test-requireIsEntry.R b/tests/testthat/test-requireIsEntry.R index 179358f6..8bc22eb1 100644 --- a/tests/testthat/test-requireIsEntry.R +++ b/tests/testthat/test-requireIsEntry.R @@ -1,9 +1,9 @@ test_that("test restrict to first entry works", { cdm <- omock::mockCdmReference() |> - omock::mockPerson(n = 3) |> - omock::mockObservationPeriod() |> - omock::mockCohort(name = "cohort1", numberCohorts = 1, recordPerson = 2) |> - omock::mockCohort(name = "cohort2", numberCohorts = 2, recordPerson = 2) + omock::mockPerson(n = 3, seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(name = "cohort1", numberCohorts = 1, recordPerson = 2,seed = 1) |> + omock::mockCohort(name = "cohort2", numberCohorts = 2, recordPerson = 2,seed = 1) expect_true(all(cdm$cohort1 |> CohortConstructor::requireIsFirstEntry() |> dplyr::pull(cohort_start_date) == c("2001-05-29", "1999-07-30", "2015-01-23"))) @@ -13,19 +13,19 @@ test_that("test restrict to first entry works", { expect_true(all(cdm$cohort2 |> CohortConstructor::requireIsFirstEntry() |> dplyr::pull(cohort_start_date) == - c("2001-05-29", "1999-07-30", "2015-01-23", "2002-10-09", "1999-04-16"))) + c("2001-05-29", "1999-07-30", "2015-01-23", "2002-10-09", "1999-04-16", "2015-02-22"))) expect_true(all(cdm$cohort2 |> CohortConstructor::requireIsFirstEntry() |> - dplyr::pull(subject_id) == c(1:3, 1:2))) + dplyr::pull(subject_id) == c(1:3, 1:3))) }) test_that("requireIsFirstEntry, cohortIds & name arguments", { testthat::skip_on_cran() cdm <- omock::mockCdmReference() |> - omock::mockPerson(n = 3) |> - omock::mockObservationPeriod() |> - omock::mockCohort(numberCohorts = 3, recordPerson = 2) + omock::mockPerson(n = 3,seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(numberCohorts = 3, recordPerson = 2,seed = 1) expect_no_error( cdm$new_cohort <- CohortConstructor::requireIsFirstEntry( @@ -45,13 +45,13 @@ test_that("requireIsFirstEntry, cohortIds & name arguments", { dplyr::tibble(cohort_definition_id = 1, number_records = 3, number_subjects = 3)) expect_true(all(cdm$new_cohort |> dplyr::pull(cohort_start_date) == c("2001-05-29", "1999-07-30", "2015-01-23", "2002-10-09", "2003-09-12", - "1999-04-16", "2000-03-09", "2000-05-05", "2015-02-22", "2015-03-24", - "2002-09-28", "1999-08-25", "1999-12-14", "1999-12-24"))) + "1999-04-16", "2000-03-09", "2000-05-05", "2015-02-22", "2002-09-28", + "1999-08-25", "1999-12-14", "1999-12-24", "2000-04-02", "2000-08-11"))) expect_true(all(cdm$new_cohort |> dplyr::pull(cohort_end_date) == c("2002-10-23", "2001-10-02", "2015-02-16", "2003-09-11", "2009-03-19", - "2000-03-08", "2000-05-04", "2001-04-01", "2015-03-23", "2015-06-11", - "2008-10-13", "1999-12-13", "1999-12-23", "2000-04-01"))) - expect_true(all(cdm$new_cohort |> dplyr::pull(subject_id) == c(1, 2, 3, 1, 1, 2, 2, 2, 3, 3, 1, 2, 2, 2))) + "2000-03-08", "2000-05-04", "2001-04-01", "2015-03-23", "2008-10-13", + "1999-12-13", "1999-12-23", "2000-04-01", "2000-08-10", "2000-09-13"))) + expect_true(all(cdm$new_cohort |> dplyr::pull(subject_id) == c(1, 2, 3, 1, 1, 2, 2, 2, 3, 1, 2, 2, 2, 2,2))) expect_true(all( omopgenerics::attrition(cdm$new_cohort)$reason == c("Initial qualifying events", "Restricted to first entry", @@ -63,9 +63,9 @@ test_that("requireIsFirstEntry, cohortIds & name arguments", { test_that("errors", { testthat::skip_on_cran() cdm <- omock::mockCdmReference() |> - omock::mockPerson(n = 3) |> - omock::mockObservationPeriod() |> - omock::mockCohort(numberCohorts = 1, recordPerson = 2) + omock::mockPerson(n = 3,seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(numberCohorts = 1, recordPerson = 2,seed = 1) expect_error(cdm$cohort |> requireIsFirstEntry(name = 1)) expect_error(cdm$cohort1 <- cdm$cohort |> requireIsFirstEntry(name = "cohort2")) @@ -77,9 +77,9 @@ test_that("errors", { test_that("requireIsLastEntry", { testthat::skip_on_cran() cdm <- omock::mockCdmReference() |> - omock::mockPerson(n = 3) |> - omock::mockObservationPeriod() |> - omock::mockCohort(numberCohorts = 3, recordPerson = 2) + omock::mockPerson(n = 3,seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(numberCohorts = 3, recordPerson = 2,seed = 1) cdm$new_cohort <- CohortConstructor::requireIsLastEntry( cohort = cdm$cohort, @@ -96,15 +96,15 @@ test_that("requireIsLastEntry", { expect_equal(counts_new |> dplyr::filter(cohort_definition_id == 1), dplyr::tibble(cohort_definition_id = 1, number_records = 3, number_subjects = 3)) expect_true(all(cdm$new_cohort |> dplyr::pull(cohort_start_date) == - c("2004-01-08", "1999-07-30", "2015-03-11", "2002-10-09", "2003-09-12", - "1999-04-16", "2000-03-09", "2000-05-05", "2015-02-22", "2015-03-24", - "2002-09-28", "1999-08-25", "1999-12-14", "1999-12-24"))) + c("2004-01-08", "1999-07-30", "2015-02-17", "2002-10-09", "2003-09-12", + "1999-04-16", "2000-03-09", "2000-05-05", "2015-02-22", "2002-09-28", + "1999-08-25", "1999-12-14", "1999-12-24", "2000-04-02", "2000-08-11"))) expect_true(all(cdm$new_cohort |> dplyr::pull(cohort_end_date) == - c("2009-10-03", "2001-10-02", "2015-04-13", "2003-09-11", "2009-03-19", - "2000-03-08", "2000-05-04", "2001-04-01", "2015-03-23", "2015-06-11", - "2008-10-13", "1999-12-13", "1999-12-23", "2000-04-01"))) + c("2009-10-03", "2001-10-02", "2015-03-10", "2003-09-11", "2009-03-19", + "2000-03-08", "2000-05-04", "2001-04-01", "2015-03-23", "2008-10-13", + "1999-12-13", "1999-12-23", "2000-04-01", "2000-08-10", "2000-09-13"))) expect_true(all(cdm$new_cohort |> dplyr::pull(subject_id) == - c(1, 2, 3, 1, 1, 2, 2, 2, 3, 3, 1, 2, 2, 2))) + c(1, 2, 3, 1, 1, 2, 2, 2, 3, 1, 2, 2, 2, 2, 2))) expect_true(all(omopgenerics::attrition(cdm$new_cohort)$reason == c( c("Initial qualifying events", "Restricted to last entry", "Initial qualifying events", "Initial qualifying events")) @@ -122,11 +122,11 @@ test_that("requireIsLastEntry", { test_that("requireEntry", { testthat::skip_on_cran() cdm <- omock::mockCdmReference() |> - omock::mockPerson(n = 10) |> - omock::mockObservationPeriod() |> + omock::mockPerson(n = 10,seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> omock::mockCohort(name = "cohort1", numberCohorts = 1, - recordPerson = 4) + recordPerson = 4,seed = 1) # 1 to inf will leave the cohort table unchanged cdm$cohort1_a <- requireIsEntry( @@ -177,11 +177,12 @@ test_that("requireEntry", { # mock cohort cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 10) |> - omock::mockObservationPeriod() |> + omock::mockPerson(n = 10,seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> omock::mockCohort(name = "cohort1", numberCohorts = 1, - recordPerson = 4) + recordPerson = 4, + seed = 1) cdm <- cdm_local |> copyCdm() cdm <- omopgenerics::insertTable(cdm, "observation_period", data.frame(observation_period_id = c(1,2), diff --git a/tests/testthat/test-requireTableIntersect.R b/tests/testthat/test-requireTableIntersect.R index 07bca365..917d0bde 100644 --- a/tests/testthat/test-requireTableIntersect.R +++ b/tests/testthat/test-requireTableIntersect.R @@ -1,11 +1,11 @@ test_that("requiring presence in another table", { testthat::skip_on_cran() cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> - omock::mockCohort(name = c("cohort1"), numberCohorts = 2) + omock::mockPerson(n = 4,seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(name = c("cohort1"), numberCohorts = 2,seed = 1) cdm_local$table <- dplyr::tibble( - person_id = c(1, 3, 4), + person_id = c(1, 3, 2), date_start = as.Date(c("2002-01-01", "2015-10-01", "2000-01-01")), date_end = as.Date(c("2002-01-01", "2015-10-01", "2000-01-01")) ) @@ -35,9 +35,9 @@ test_that("requiring presence in another table", { targetEndDate = "date_end", window = c(0, Inf), name = "cohort3") - expect_true(all(cdm$cohort3 |> dplyr::pull("subject_id") == c(1, 3, 4, 1, 1, 1, 3))) + expect_true(all(cdm$cohort3 |> dplyr::pull("subject_id") == c(2,3,1,1))) expect_true(all(cdm$cohort3 |> dplyr::pull("cohort_start_date") == - c("2001-03-30", "2015-03-25", "1997-10-22", "2000-06-23", "2001-07-16", "2001-12-04", "2015-03-05"))) + c("1999-05-03", "2015-02-25", "2001-03-24", "2001-11-28"))) expect_equal(omopgenerics::attrition(cdm$cohort3)$reason, c("Initial qualifying events", "In table table between 0 & Inf days relative to cohort_start_date between 1 and Inf", @@ -52,8 +52,8 @@ test_that("requiring presence in another table", { window = c(-Inf, 0), censorDate = "cohort_end_date", name = "cohort4") - expect_true(cdm$cohort4 |> dplyr::pull("subject_id") == 1) - expect_true(cdm$cohort4 |> dplyr::pull("cohort_start_date") == "2003-06-15") + expect_true(all(cdm$cohort4 |> dplyr::pull("subject_id") == c(1,1,1,1))) + expect_true(all(cdm$cohort4 |> dplyr::pull("cohort_start_date") == c("2003-05-17", "2004-03-11", "2002-01-30", "2002-06-13"))) expect_equal(omopgenerics::attrition(cdm$cohort4)$reason, c("Initial qualifying events", "In table table between -Inf & 0 days relative to cohort_start_date between 1 and Inf, censoring at cohort_end_date", @@ -69,9 +69,9 @@ test_that("requiring presence in another table", { window = c(-Inf, 0), censorDate = "cohort_end_date", name = "cohort5") - expect_true(all(cdm$cohort5 |> dplyr::pull("subject_id") == c(1, 1, 1, 1, 3))) + expect_true(all(cdm$cohort5 |> dplyr::pull("subject_id") == c(1, 1, 1, 1, 1,1))) expect_true(all(cdm$cohort5 |> dplyr::pull("cohort_start_date") |> sort() == - c("2000-06-23", "2001-07-16", "2001-12-04", "2003-06-15", "2015-03-05"))) + c("2001-03-24", "2001-11-28", "2002-01-30", "2002-06-13", "2003-05-17", "2004-03-11"))) expect_equal(omopgenerics::attrition(cdm$cohort5)$reason, c("Initial qualifying events", "In table table between -Inf & 0 days relative to cohort_start_date between 1 and Inf, censoring at cohort_end_date", @@ -101,9 +101,9 @@ test_that("requiring presence in another table", { test_that("requiring absence in another table", { testthat::skip_on_cran() cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> - omock::mockCohort(name = c("cohort1"), numberCohorts = 2) + omock::mockPerson(n = 4,seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(name = c("cohort1"), numberCohorts = 2,seed = 1) cdm_local$table <- dplyr::tibble( person_id = c(1, 3, 4), date_start = as.Date(c("2002-01-01", "2015-10-01", "2000-01-01")), @@ -119,7 +119,7 @@ test_that("requiring absence in another table", { window = c(-Inf, Inf), name = "cohort2") - expect_true(cdm$cohort2 |> dplyr::pull("subject_id") |> length() == 0) + expect_true(cdm$cohort2 |> dplyr::pull("subject_id") |> length() == 1) expect_equal(omopgenerics::attrition(cdm$cohort2)$reason, c("Initial qualifying events", "Not in table table between -Inf & Inf days relative to cohort_start_date", @@ -133,8 +133,9 @@ test_that("requiring absence in another table", { targetEndDate = "date_end", window = c(0, Inf), name = "cohort3") - expect_true(all(cdm$cohort3 |> dplyr::pull("subject_id") == 1)) - expect_true(all(cdm$cohort3 |> dplyr::pull("cohort_start_date") == "2003-06-15")) + expect_true(all(cdm$cohort3 |> dplyr::pull("subject_id") == c(1,1,2,1,1))) + expect_true(all(cdm$cohort3 |> dplyr::pull("cohort_start_date") |> sort() == + c("1999-05-03", "2002-01-30", "2002-06-13", "2003-05-17", "2004-03-11"))) expect_equal(omopgenerics::attrition(cdm$cohort3)$reason, c("Initial qualifying events", "Not in table table between 0 & Inf days relative to cohort_start_date", @@ -150,9 +151,9 @@ test_that("requiring absence in another table", { window = c(-Inf, 0), censorDate = "cohort_end_date", name = "cohort4") - expect_true(all(cdm$cohort4 |> dplyr::pull("subject_id") == c(1, 3, 4, 1, 1, 1, 3))) + expect_true(all(cdm$cohort4 |> dplyr::pull("subject_id") == c(2,3,1,1))) expect_true(all((cdm$cohort4 |> dplyr::pull("cohort_start_date") == - c("2001-03-30", "2015-03-25", "1997-10-22", "2000-06-23", "2001-07-16", "2001-12-04", "2015-03-05")))) + c("1999-05-03", "2015-02-25", "2001-03-24", "2001-11-28")))) expect_equal(omopgenerics::attrition(cdm$cohort4)$reason, c("Initial qualifying events", "Not in table table between -Inf & 0 days relative to cohort_start_date, censoring at cohort_end_date", @@ -168,9 +169,10 @@ test_that("requiring absence in another table", { targetEndDate = "date_end", window = c(0, Inf), censorDate = NULL) - expect_true(all(cdm$cohort1 |> dplyr::pull("subject_id") |> sort() == c(1, 1, 1, 1, 3))) + expect_true(all(cdm$cohort1 |> dplyr::pull("subject_id") |> sort() == c(1, 1, 1, 1, 1, 1, 2))) expect_true(all((cdm$cohort1 |> dplyr::pull("cohort_start_date") |> sort() == - c("2000-06-23", "2001-07-16", "2001-12-04", "2003-06-15", "2015-03-05")))) + c("1999-05-03", "2001-03-24", "2001-11-28", "2002-01-30", "2002-06-13", + "2003-05-17", "2004-03-11")))) expect_equal(omopgenerics::attrition(cdm$cohort1)$reason, c("Initial qualifying events", "Not in table table between 0 & Inf days relative to cohort_start_date", diff --git a/tests/testthat/test-sampleCohorts.R b/tests/testthat/test-sampleCohorts.R index efe2be7a..72581cc2 100644 --- a/tests/testthat/test-sampleCohorts.R +++ b/tests/testthat/test-sampleCohorts.R @@ -1,7 +1,7 @@ test_that("sampleCohort subsetting one cohort", { cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> + omock::mockPerson(n = 4, seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> omock::mockCohort(name = c("cohort1"), numberCohorts = 5, seed = 2) cdm <- CDMConnector::copyCdmTo(con = DBI::dbConnect(duckdb::duckdb()), cdm = cdm_local, @@ -29,19 +29,19 @@ test_that("sampleCohort subsetting one cohort", { test_that("sampleCohort subsetting multiple cohorts", { cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 10) |> - omock::mockObservationPeriod() |> + omock::mockPerson(n = 10,seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> omock::mockCohort(name = c("cohort1"), numberCohorts = 3, seed = 2) cdm <- CDMConnector::copyCdmTo(con = DBI::dbConnect(duckdb::duckdb()), cdm = cdm_local, schema = "main", overwrite = TRUE) - cdm$cohort1 <- sampleCohorts(cdm$cohort1, n = 5) + cdm$cohort1 <- sampleCohorts(cdm$cohort1, n = 4) expect_true(all(attrition(cdm$cohort1) |> - dplyr::filter(reason == "Sample 5 individuals") |> + dplyr::filter(reason == "Sample 4 individuals") |> dplyr::arrange(cohort_definition_id) |> - dplyr::pull("number_subjects") == c(5,5,4))) + dplyr::pull("number_subjects") == c(4,4,4))) # Subset it again but only cohorts 1 and 3 cdm$cohort2 <- sampleCohorts(cdm$cohort1, c(1,3), 4, name = "cohort2") @@ -53,15 +53,15 @@ test_that("sampleCohort subsetting multiple cohorts", { expect_true(all(attrition(cdm$cohort2) |> dplyr::filter(reason == "Sample 4 individuals") |> dplyr::arrange(cohort_definition_id) |> - dplyr::pull("excluded_subjects") == c(1,0))) + dplyr::pull("excluded_subjects") == c(1,0,1,2,0))) PatientProfiles::mockDisconnect(cdm) }) test_that("sampleCohort subsetting all cohorts", { cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> + omock::mockPerson(n = 4,seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> omock::mockCohort(name = c("cohort1"), numberCohorts = 3, seed = 2) cdm <- CDMConnector::copyCdmTo(con = DBI::dbConnect(duckdb::duckdb()), cdm = cdm_local, diff --git a/tests/testthat/test-subsetCohorts.R b/tests/testthat/test-subsetCohorts.R index 46460ccf..a04222f8 100644 --- a/tests/testthat/test-subsetCohorts.R +++ b/tests/testthat/test-subsetCohorts.R @@ -1,7 +1,7 @@ test_that("subsetCohort works", { cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> + omock::mockPerson(n = 4, seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> omock::mockCohort(name = c("cohort1"), numberCohorts = 5, seed = 2) cdm <- cdm_local |> copyCdm() @@ -48,9 +48,9 @@ test_that("subsetCohort works", { test_that("codelist works", { testthat::skip_on_cran() cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> - omock::mockCohort() + omock::mockPerson(n = 4,seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(seed = 1) cdm_local$concept <- dplyr::tibble( "concept_id" = c(1, 2, 3), "concept_name" = c("my concept 1", "my concept 2", "my concept 3"), @@ -97,8 +97,8 @@ test_that("codelist works", { test_that("Expected behaviour", { testthat::skip_on_cran() cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> + omock::mockPerson(n = 4,seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> omock::mockCohort(name = c("cohort1"), numberCohorts = 5, seed = 2) cdm <- cdm_local |> copyCdm() diff --git a/tests/testthat/test-trimDemographics.R b/tests/testthat/test-trimDemographics.R index 880c0230..cd9ce56c 100644 --- a/tests/testthat/test-trimDemographics.R +++ b/tests/testthat/test-trimDemographics.R @@ -328,7 +328,8 @@ test_that("cohort Id, name, additional columns", { testthat::skip_on_cran() cdm <- mockCohortConstructor(nPerson = 5, con = connection(), - writeSchema = writeSchema()) + writeSchema = writeSchema(), + seed = 1) cdm$cohort2 <- cdm$cohort2 |> dplyr::mutate( col_extra1 = as.numeric(subject_id) + 1, @@ -352,9 +353,9 @@ test_that("cohort Id, name, additional columns", { expect_equal( x1, dplyr::tibble( - subject_id = c(1, 1, 2, 3, 5) |> as.integer(), - cohort_start_date = as.Date(c("2001-04-03", "2002-05-07", "1999-07-26", "2015-02-19", "2012-06-12")), - cohort_end_date = as.Date(c("2002-05-06", "2005-11-07", "2002-09-17", "2015-06-27", "2012-09-09")) + subject_id = c(1, 1, 2, 3) |> as.integer(), + cohort_start_date = as.Date(c("2001-04-03", "2002-05-07", "1999-07-26", "2015-02-19")), + cohort_end_date = as.Date(c("2002-05-06", "2005-11-07", "2002-09-17", "2015-06-27")) ) ) expect_equal( diff --git a/tests/testthat/test-unionCohorts.R b/tests/testthat/test-unionCohorts.R index 597ac0d8..1d92588b 100644 --- a/tests/testthat/test-unionCohorts.R +++ b/tests/testthat/test-unionCohorts.R @@ -1,27 +1,27 @@ test_that("unionCohorts works", { cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> - omock::mockCohort(name = c("cohort1"), numberCohorts = 4) + omock::mockPerson(n = 4, seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(name = c("cohort1"), numberCohorts = 4, seed = 1) cdm <- cdm_local |> copyCdm() # simple example cdm$cohort2 <- unionCohorts(cdm$cohort1, name = "cohort2") expect_true(all( cdm$cohort2 %>% dplyr::pull("cohort_start_date") %>% sort() == - c("1994-02-19", "1999-08-24", "2000-06-23", "2015-01-19") + c("1999-05-03", "2001-03-24", "2015-01-22") )) expect_true(all( cdm$cohort2 %>% dplyr::pull("cohort_end_date") %>% sort() == - c("2001-08-26", "2006-09-27", "2007-08-06", "2015-09-14") + c("2002-06-07", "2009-06-12", "2015-06-22") )) expect_true(all( - cdm$cohort2 %>% dplyr::pull("subject_id") %>% sort() == 1:4 + cdm$cohort2 %>% dplyr::pull("subject_id") %>% sort() == 1:3 )) expect_true(all(attrition(cdm$cohort2) == dplyr::tibble( cohort_definition_id = 1, - number_records = 4, - number_subjects = 4, + number_records = 3, + number_subjects = 3, reason_id = 1, reason = "Initial qualifying events", excluded_records = 0, @@ -33,20 +33,20 @@ test_that("unionCohorts works", { cdm$cohort3 <- unionCohorts(cdm$cohort1, cohortId = 1:2, name = "cohort3") expect_true(all( cdm$cohort3 %>% dplyr::pull("cohort_start_date") %>% sort() == - c("1997-10-22", "2000-06-23", "2015-03-05") + c("1999-05-03", "2001-03-24", "2001-11-28", "2002-01-30", "2002-06-13", "2015-02-25") )) expect_true(all( cdm$cohort3 %>% dplyr::pull("cohort_end_date") %>% sort() == - c("1999-05-28", "2006-09-27", "2015-07-06") + c("2001-06-15", "2001-11-27", "2002-01-29", "2002-06-12", "2005-07-19", "2015-04-30") )) expect_true(all( - cdm$cohort3 %>% dplyr::pull("subject_id") %>% sort() == c(1, 3, 4) + cdm$cohort3 %>% dplyr::pull("subject_id") %>% sort() == c(1, 1, 1, 1, 2, 3) )) expect_true(all( attrition(cdm$cohort3) == dplyr::tibble( cohort_definition_id = 1, - number_records = 3, + number_records = 6, number_subjects = 3, reason_id = 1, reason = "Initial qualifying events", @@ -73,8 +73,8 @@ test_that("unionCohorts works", { test_that("gap and name works", { cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> + omock::mockPerson(n = 4, seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> omock::mockCohort(name = c("cohort"), numberCohorts = 4, seed = 11, recordPerson = 2) cdm_local$cohort <- cdm_local$cohort |> dplyr::arrange(.data$subject_id, .data$cohort_start_date) |> @@ -120,21 +120,21 @@ test_that("gap and name works", { cdm$cohort <- unionCohorts(cdm$cohort, gap = 2, cohortName = "test") expect_true(all( cdm$cohort %>% dplyr::pull("cohort_start_date") %>% sort() == - c("1991-10-17", "1991-12-11", "1993-02-17", "1999-04-12", "1999-08-23", - "1999-09-19", "2001-12-27", "2015-03-02", "2015-03-17") + c("1991-07-14", "1991-10-21", "1995-01-27", "1999-07-26", "1999-08-22", + "2002-08-29", "2003-02-07", "2015-02-04", "2015-02-16", "2015-03-07") )) expect_true(all( cdm$cohort %>% dplyr::pull("cohort_end_date") %>% sort() == - c("1991-10-19", "1992-10-12", "1999-07-18", "1999-08-24", "2001-03-19", - "2001-06-05", "2008-08-28", "2015-03-09", "2015-03-25") + c("1991-08-14", "1994-05-05", "1995-09-11", "1999-08-15", "2001-07-23", + "2002-12-29", "2007-06-11", "2015-02-12", "2015-02-26", "2015-07-28") )) expect_true(all( - cdm$cohort %>% dplyr::pull("subject_id") %>% sort() == c(1, rep(2, 3), rep(3, 2), rep(4, 3)) + cdm$cohort %>% dplyr::pull("subject_id") %>% sort() == c(1, 1, rep(2, 2), rep(3, 3), rep(4, 3)) )) expect_true(all(attrition(cdm$cohort) == dplyr::tibble( cohort_definition_id = 1, - number_records = 9, + number_records = 10, number_subjects = 4, reason_id = 1, reason = "Initial qualifying events", @@ -149,8 +149,8 @@ test_that("gap and name works", { test_that("Expected behaviour", { testthat::skip_on_cran() cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> + omock::mockPerson(n = 4, seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> omock::mockCohort(name = c("cohort"), numberCohorts = 4, seed = 8, recordPerson = 2) cdm <- cdm_local |> copyCdm() expect_error( @@ -202,9 +202,9 @@ test_that("Expected behaviour", { test_that("test codelist", { testthat::skip_on_cran() cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> - omock::mockCohort() + omock::mockPerson(n = 4,seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(seed = 1) cdm_local$concept <- dplyr::tibble( "concept_id" = c(1, 2, 3), "concept_name" = c("my concept 1", "my concept 2", "my concept 3"), @@ -258,14 +258,14 @@ test_that("test codelist", { cdm$cohort4 <- unionCohorts(cdm$cohort3, name = "cohort4") expect_true(all( cdm$cohort4 %>% dplyr::pull("cohort_start_date") %>% sort() == - c("1997-10-22", "2001-03-30", "2003-06-15", "2009-12-22", "2010-01-01", "2010-01-11", "2010-05-31", "2012-09-27", "2014-12-06", "2015-03-25") + c("1999-05-03", "2003-05-17", "2004-03-11", "2009-12-22", "2010-01-01", "2010-01-11", "2010-05-31", "2012-09-27", "2014-12-06", "2015-02-25") )) expect_true(all( cdm$cohort4 %>% dplyr::pull("cohort_end_date") %>% sort() == - c("1999-05-28", "2003-06-14", "2005-11-23", "2010-05-04", "2011-08-24", "2014-02-09", "2014-05-20", "2014-12-10", "2015-04-14", "2015-06-24") + c("2001-06-15", "2004-03-10", "2005-07-19", "2010-05-04", "2011-08-24", "2014-02-09", "2014-05-20", "2014-12-10", "2015-04-30", "2015-06-24") )) expect_true(all( - cdm$cohort4 %>% dplyr::pull("subject_id") %>% sort() == c(1, 1, 1, 1, 2, 3, 3, 3, 4, 4) + cdm$cohort4 %>% dplyr::pull("subject_id") %>% sort() == c(1, 1, 1, 1, 2, 2, 3, 3, 3, 4) )) codes <- attr(cdm$cohort4, "cohort_codelist") expect_true(all(codes |> dplyr::pull("codelist_name") |> sort() == c(rep("c1", 2), "c2"))) @@ -278,9 +278,9 @@ test_that("test codelist", { test_that("keep original cohorts", { cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> - omock::mockCohort(name = c("cohort1"), numberCohorts = 4) + omock::mockPerson(n = 4, seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(name = c("cohort1"), numberCohorts = 4,seed = 1) cdm <- cdm_local |> copyCdm() start_settings <- settings(cdm$cohort1) diff --git a/tests/testthat/test-yearCohorts.R b/tests/testthat/test-yearCohorts.R index 4170df30..9c5a07b6 100644 --- a/tests/testthat/test-yearCohorts.R +++ b/tests/testthat/test-yearCohorts.R @@ -1,9 +1,9 @@ test_that("yearCohorts - change name", { testthat::skip_on_cran() cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> - omock::mockCohort(name = c("cohort")) + omock::mockPerson(n = 4,seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(name = c("cohort"),seed = 1) cdm <- cdm_local |> copyCdm() # simple example @@ -20,11 +20,11 @@ test_that("yearCohorts - change name", { target_cohort_name = "cohort_1" )) expect_true(all(cdm$cohort1 |> dplyr::pull("cohort_start_date") |> sort() == - c("1997-10-22", "1998-01-01", "1999-01-01", "2001-03-30", "2002-01-01"))) + c("1999-05-03", "2000-01-01", "2001-01-01"))) expect_true(all(cdm$cohort1 |> dplyr::pull("cohort_end_date") |> sort() == - c("1997-12-31", "1998-12-31", "1999-05-28", "2001-12-31", "2002-12-31"))) - expect_true(all(cdm$cohort1 |> dplyr::pull("subject_id") |> sort() == c(1, 1, 4, 4, 4))) - expect_true(all(cdm$cohort1 |> dplyr::pull("cohort_definition_id") |> sort() == c(1:3, 5:6))) + c("1999-12-31", "2000-12-31", "2001-06-15"))) + expect_true(all(cdm$cohort1 |> dplyr::pull("subject_id") |> sort() == c(2,2,2))) + expect_true(all(cdm$cohort1 |> dplyr::pull("cohort_definition_id") |> sort() == c(3,4,5))) expect_true(all(attrition(cdm$cohort1)$reason == c( 'Initial qualifying events', 'Restrict to observations between: 1997-01-01 and 1997-12-31', 'Initial qualifying events', 'Restrict to observations between: 1998-01-01 and 1998-12-31', @@ -36,9 +36,9 @@ test_that("yearCohorts - change name", { # more than 1 cohort cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> - omock::mockCohort(name = c("cohort"), numberCohorts = 3) + omock::mockPerson(n = 4,seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(name = c("cohort"), numberCohorts = 3, seed = 1) cdm <- cdm_local |> copyCdm() # all cohorts cdm$cohort1 <- yearCohorts(cohort = cdm$cohort, @@ -58,11 +58,11 @@ test_that("yearCohorts - change name", { target_cohort_name = rep(paste0("cohort_", 1:3), 4) )) expect_true(all(cdm$cohort1 |> dplyr::pull("cohort_start_date") |> sort() == - c("2005-01-01", "2005-01-01", "2005-01-01", "2006-01-01", "2006-01-01", "2007-01-01"))) + c("2005-01-01", "2005-01-01", "2005-01-01", "2006-01-01", "2007-01-01"))) expect_true(all(cdm$cohort1 |> dplyr::pull("cohort_end_date") |> sort() == - c("2005-11-23", "2005-12-31", "2005-12-31", "2006-09-27", "2006-12-31", "2007-08-06"))) - expect_true(all(cdm$cohort1 |> dplyr::pull("subject_id") |> sort() == c(1, 1, 1, 4, 4, 4))) - expect_true(all(cdm$cohort1 |> dplyr::pull("cohort_definition_id") |> sort() == c(1:3, 5:6, 9))) + c("2005-01-15", "2005-07-19", "2005-12-31", "2006-12-31", "2007-01-17"))) + expect_true(all(cdm$cohort1 |> dplyr::pull("subject_id") |> sort() == c(1, 1, 1, 1, 1))) + expect_true(all(cdm$cohort1 |> dplyr::pull("cohort_definition_id") |> sort() == c(1, 2, 3, 6, 9))) expect_true(all(attrition(cdm$cohort1)$reason == c( 'Initial qualifying events', 'Restrict to observations between: 2005-01-01 and 2005-12-31', 'Initial qualifying events', 'Restrict to observations between: 2005-01-01 and 2005-12-31', @@ -77,7 +77,7 @@ test_that("yearCohorts - change name", { 'Initial qualifying events', 'Restrict to observations between: 2008-01-01 and 2008-12-31', 'Initial qualifying events', 'Restrict to observations between: 2008-01-01 and 2008-12-31' ))) - expect_true(all(cohortCount(cdm$cohort1)$number_records == c(1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0))) + expect_true(all(cohortCount(cdm$cohort1)$number_records == c(1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0))) # just 1 cohort cdm$cohort1 <- yearCohorts(cohort = cdm$cohort, @@ -97,7 +97,7 @@ test_that("yearCohorts - change name", { expect_true(all(cdm$cohort1 |> dplyr::pull("cohort_start_date") |> sort() == c("2005-01-01"))) expect_true(all(cdm$cohort1 |> dplyr::pull("cohort_end_date") |> sort() == - c("2005-11-23"))) + c("2005-07-19"))) expect_true(all(cdm$cohort1 |> dplyr::pull("subject_id") |> sort() == 1)) expect_true(all(cdm$cohort1 |> dplyr::pull("cohort_definition_id") |> sort() == 1)) expect_true(all(attrition(cdm$cohort1)$reason == c( @@ -121,9 +121,9 @@ test_that("yearCohorts - change name", { test_that("yearCohorts - keep name", { testthat::skip_on_cran() cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> - omock::mockCohort(name = c("cohort")) + omock::mockPerson(n = 4, seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(name = c("cohort"), seed = 1) cdm <- cdm_local |> copyCdm() # simple example @@ -139,11 +139,11 @@ test_that("yearCohorts - keep name", { target_cohort_name = "cohort_1" )) expect_true(all(cdm$cohort |> dplyr::pull("cohort_start_date") |> sort() == - c("1997-10-22", "1998-01-01", "1999-01-01", "2001-03-30", "2002-01-01"))) + c("1999-05-03", "2000-01-01", "2001-01-01"))) expect_true(all(cdm$cohort |> dplyr::pull("cohort_end_date") |> sort() == - c("1997-12-31", "1998-12-31", "1999-05-28", "2001-12-31", "2002-12-31"))) - expect_true(all(cdm$cohort |> dplyr::pull("subject_id") |> sort() == c(1, 1, 4, 4, 4))) - expect_true(all(cdm$cohort |> dplyr::pull("cohort_definition_id") |> sort() == c(1:3, 5:6))) + c("1999-12-31", "2000-12-31", "2001-06-15"))) + expect_true(all(cdm$cohort |> dplyr::pull("subject_id") |> sort() == c(2, 2, 2))) + expect_true(all(cdm$cohort |> dplyr::pull("cohort_definition_id") |> sort() == c(3, 4, 5))) expect_true(all(attrition(cdm$cohort)$reason == c( 'Initial qualifying events', 'Restrict to observations between: 1997-01-01 and 1997-12-31', 'Initial qualifying events', 'Restrict to observations between: 1998-01-01 and 1998-12-31', @@ -155,9 +155,9 @@ test_that("yearCohorts - keep name", { # more than 1 cohort cdm_local <- omock::mockCdmReference() |> - omock::mockPerson(n = 4) |> - omock::mockObservationPeriod() |> - omock::mockCohort(name = c("cohort"), numberCohorts = 3) + omock::mockPerson(n = 4, seed = 1) |> + omock::mockObservationPeriod(seed = 1) |> + omock::mockCohort(name = c("cohort"), numberCohorts = 3, seed = 1) cdm <- cdm_local |> copyCdm() # just 1 cohort @@ -177,7 +177,7 @@ test_that("yearCohorts - keep name", { expect_true(all(cdm$cohort |> dplyr::pull("cohort_start_date") |> sort() == c("2005-01-01"))) expect_true(all(cdm$cohort |> dplyr::pull("cohort_end_date") |> sort() == - c("2005-11-23"))) + c("2005-07-19"))) expect_true(all(cdm$cohort |> dplyr::pull("subject_id") |> sort() == c(1))) expect_true(all(cdm$cohort |> dplyr::pull("cohort_definition_id") |> sort() == c(1))) expect_true(all(attrition(cdm$cohort)$reason == c( diff --git a/vignettes/a07_filter_cohorts.Rmd b/vignettes/a07_filter_cohorts.Rmd index f2fc2d18..db30b2f0 100644 --- a/vignettes/a07_filter_cohorts.Rmd +++ b/vignettes/a07_filter_cohorts.Rmd @@ -1,8 +1,8 @@ --- -title: "a06_filter_cohorts" +title: "Filter Cohorts" output: rmarkdown::html_vignette vignette: > - %\VignetteIndexEntry{a06_filter_cohorts} + %\VignetteIndexEntry{a07_filter_cohorts} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- @@ -16,4 +16,47 @@ knitr::opts_chunk$set( ```{r setup} library(CohortConstructor) +library(CohortCharacteristics) +library(ggplot2) +``` + +```{r, include = FALSE} +knitr::opts_chunk$set( + collapse = TRUE, + eval = TRUE, message = FALSE, warning = FALSE, + comment = "#>" +) + +library(CDMConnector) +library(dplyr, warn.conflicts = FALSE) + +if (Sys.getenv("EUNOMIA_DATA_FOLDER") == ""){ + Sys.setenv("EUNOMIA_DATA_FOLDER" = file.path(tempdir(), "eunomia"))} +if (!dir.exists(Sys.getenv("EUNOMIA_DATA_FOLDER"))){ dir.create(Sys.getenv("EUNOMIA_DATA_FOLDER")) + downloadEunomiaData() +} +``` + +For this example we'll use the Eunomia synthetic data from the CDMConnector package. + +```{r} +con <- DBI::dbConnect(duckdb::duckdb(), dbdir = eunomia_dir()) +cdm <- cdm_from_con(con, cdm_schema = "main", + write_schema = c(prefix = "my_study_", schema = "main")) +``` + +Let's start by creating two drug cohorts, one for users of diclofenac and another for users of acetaminophen. + +```{r} +cdm$medications <- conceptCohort(cdm = cdm, + conceptSet = list("diclofenac" = 1124300, + "acetaminophen" = 1127433), + name = "medications") +cohortCount(cdm$medications) +``` + +```{r} +cdm$medications_sample <- sampleCohorts(cdm$medications,cohortId = 1, n = 100, name = "medications_sample") + +cohortCount(cdm$medications_sample) ```