diff --git a/.Rhistory b/.Rhistory deleted file mode 100644 index 0de058a3..00000000 --- a/.Rhistory +++ /dev/null @@ -1,512 +0,0 @@ -# Matched cohort -CDMConnector::cohort_attrition(cdm$matched_cohort1) %>% filter(cohort_definition_id == 4) -cdm <- generateMatchedCohortSet(cdm = cdm, -name = "matched_cohort1", -targetCohortName = "cohort1", -targetCohortId = 1, -ratio = 0) -CDMConnector::cohort_count(cdm$matched_cohort1) -cdm$matched_cohort1 -cohort_attrition(cdm$matched_cohort1) -CDMConnector::cohort_attrition(cdm$matched_cohort1) -cdm <- generateMatchedCohortSet(cdm = cdm, -name = "matched_cohort1", -targetCohortName = "cohort1", -targetCohortId = 1, -ratio = -1) -CDMConnector::cohort_attrition(cdm$matched_cohort1) -cdm <- generateMatchedCohortSet(cdm = cdm, -name = "matched_cohort2", -targetCohortName = "cohort1", -targetCohortId = c(1,3), -ratio = 10) -CDMConnector::cohort_count(cdm$matched_cohort2) -CDMConnector::cohort_count(cdm$matched_cohort2) -CDMConnector::cohort_set(cdm$matched_cohort2) -cdm <- generateMatchedCohortSet(cdm = cdm, -name = "matched_cohort2", -targetCohortName = "cohort1", -targetCohortId = c(1,3), -ratio = 10) -CDMConnector::cohort_set(cdm$matched_cohort2) %>% filter(cohort_definition_id %in% c(1,4)) -CDMConnector::cohort_set(cdm$matched_cohort2) %>% filter(cohort_definition_id %in% c(3,6)) -cdm <- generateMatchedCohortSet(cdm = cdm, -name = "matched_cohort2", -targetCohortName = "cohort1", -targetCohortId = 1, -ratio = Inf) -devtools::load_all() -devtools::check() -cdm -cdm %>% generateMatchedCohortSet(name = "matched_cohort_nosequant",targetCohortName = "cohort1",targetCohortId = c(1)) -cdm <- cdm %>% generateMatchedCohortSet(name = "matched_cohort_nosequant",targetCohortName = "cohort1",targetCohortId = c(1)) -cdm$matched_cohort_nosequant -name <- "matched_cohort_nosequant" -cdm[[name]] %>% -CDMConnector::cohort_set() -cdm <- cdm %>% generateMatchedCohortSet(name = "matched_cohort_nosequant",targetCohortName = "cohort1",targetCohortId = c(1,3)) -cdm[[name]] %>% -CDMConnector::cohort_set() -cdm[[name]] %>% -CDMConnector::cohort_set() %>% -dplyr::arrange("cohort_name") -cdm[[name]] %>% -CDMConnector::cohort_set() %>% -dplyr::cohort_definition_id -cdm[[name]] %>% -CDMConnector::cohort_set() %>% -dplyr::mutate(cohort_definition_id = target_cohort_definition_id) -cdm[[name]] %>% -CDMConnector::cohort_set() %>% -dplyr::mutate(cohort_definition_id = target_cohort__id) -cdm[[name]] %>% -CDMConnector::cohort_set() %>% -dplyr::mutate(cohort_definition_id = target_cohort_id) -cdm[[name]] %>% -CDMConnector::cohort_set() %>% -dplyr::mutate(cohort_definition_id = target_cohort_id) %>% -arrange(cohort_definition_id) -cdm[[name]] %>% -CDMConnector::cohort_set() %>% -dplyr::mutate(cohort_definition_id = target_cohort_id) %>% -arrange(cohort_definition_id) %>% -dplyr::mutate(cohort_definition_id = dplyr::n()) -cdm[[name]] %>% -CDMConnector::cohort_set() %>% -dplyr::mutate(cohort_definition_id = target_cohort_id) %>% -arrange(cohort_definition_id) %>% -dplyr::mutate(cohort_definition_id = dplyr::row_n()) -cdm[[name]] %>% -CDMConnector::cohort_set() %>% -dplyr::mutate(cohort_definition_id = target_cohort_id) %>% -arrange(cohort_definition_id) %>% -dplyr::mutate(cohort_definition_id = dplyr::row_number()) -cdm[[name]] %>% -CDMConnector::cohort_set() %>% -dplyr::mutate(cohort_definition_id_new = target_cohort_id) %>% -arrange(cohort_definition_id_new) %>% -dplyr::mutate(cohort_definition_id_new = dplyr::row_number()) -cdm[[name]] %>% -CDMConnector::cohort_set() %>% -dplyr::mutate(cohort_definition_id_new = target_cohort_id) %>% -arrange(cohort_definition_id_new, cohort_name) %>% -dplyr::mutate(cohort_definition_id_new = dplyr::row_number()) -cdm[[name]] %>% -CDMConnector::cohort_set() %>% -dplyr::mutate(cohort_definition_id_new = target_cohort_id) -cdm[[name]] %>% -CDMConnector::cohort_set() %>% -dplyr::mutate(cohort_definition_id_new = target_cohort_id) %>% -arrange(cohort_definition_id_new) %>% -dplyr::mutate(cohort_definition_id_new = dplyr::row_number()) -cdm[[name]] %>% -CDMConnector::cohort_attrition() -cdm[[name]] %>% -CDMConnector::cohort_attrition() %>% -inner_join( -new_cohort_set %>% dplyr::select("cohort_definition_id","cohort_definition_id_new") -) -new_cohort_set <- cdm[[name]] %>% -CDMConnector::cohort_set() %>% -dplyr::mutate(cohort_definition_id_new = target_cohort_id) %>% -arrange(cohort_definition_id_new) %>% -dplyr::mutate(cohort_definition_id_new = dplyr::row_number()) -cdm[[name]] %>% -CDMConnector::cohort_attrition() %>% -inner_join( -new_cohort_set %>% dplyr::select("cohort_definition_id","cohort_definition_id_new") -) -CDMConnector::cohort_count() -CDMConnector::cohort_count(cdm$matched_cohort_nosequant) -cdm[[name]] %>% -CDMConnector::cohort_count() %>% -inner_join( -new_cohort_set %>% dplyr::select("cohort_definition_id","cohort_definition_id_new") -) %>% -dplyr::select(-"cohort_definition_id") %>% -dplyr::rename("cohort_definition_id" = "cohort_definition_id_new") -cdm[[name]] %>% -CDMConnector::cohort_count() %>% -inner_join( -new_cohort_set %>% dplyr::select("cohort_definition_id","cohort_definition_id_new") -) %>% -dplyr::select(-"cohort_definition_id") %>% -dplyr::rename("cohort_definition_id" = "cohort_definition_id_new") %>% -dplyr::relocate(cohort_definition_id) -cdm[[name]] -cdm[[name]] %>% -inner_join( -new_cohort_set %>% dplyr::select("cohort_definition_id","cohort_definition_id_new") -) %>% -dplyr::select(-"cohort_definition_id") %>% -dplyr::rename("cohort_definition_id" = "cohort_definition_id_new") %>% -dplyr::relocate(cohort_definition_id) -cdm[[name]] %>% -inner_join( -new_cohort_set %>% dplyr::select("cohort_definition_id","cohort_definition_id_new") -) -new_cohort_set -new_cohort_set %>% dplyr::select("cohort_definition_id","cohort_definition_id_new") -cdm[[name]] %>% -inner_join( -new_cohort_set %>% dplyr::select("cohort_definition_id","cohort_definition_id_new"), -by = "cohort_definition_id" -) %>% -dplyr::select(-"cohort_definition_id") %>% -dplyr::rename("cohort_definition_id" = "cohort_definition_id_new") %>% -dplyr::relocate(cohort_definition_id) -new_cohort_set %>% dplyr::select("cohort_definition_id","cohort_definition_id_new") -cdm[[name]] -cdm[[name]] %>% -inner_join( -new_cohort_set %>% dplyr::select("cohort_definition_id","cohort_definition_id_new"), -by = "cohort_definition_id", -copy = TRUE -) %>% -dplyr::select(-"cohort_definition_id") %>% -dplyr::rename("cohort_definition_id" = "cohort_definition_id_new") %>% -dplyr::relocate(cohort_definition_id) -cdm[[name]] <- cdm[[name]] %>% -inner_join( -new_cohort_set %>% dplyr::select("cohort_definition_id","cohort_definition_id_new"), -by = "cohort_definition_id", -copy = TRUE -) %>% -dplyr::select(-"cohort_definition_id") %>% -dplyr::rename("cohort_definition_id" = "cohort_definition_id_new") %>% -dplyr::relocate(cohort_definition_id) -new_cohort_set -cdm[[name]] <- CDMConnector::new_generated_cohort_set( -cohort_ref = new_cohort, -cohort_attrition_ref = new_cohort_attrition , -cohort_set_ref = new_cohort_set, -cohort_count_ref = new_cohort_count, -overwrite = TRUE) -new_cohort_set <- cdm[[name]] %>% -CDMConnector::cohort_set() %>% -dplyr::mutate(cohort_definition_id_new = target_cohort_id) %>% -arrange(cohort_definition_id_new) %>% -dplyr::mutate(cohort_definition_id_new = dplyr::row_number()) -new_cohort_attrition <- cdm[[name]] %>% -CDMConnector::cohort_attrition() %>% -inner_join( -new_cohort_set %>% dplyr::select("cohort_definition_id","cohort_definition_id_new"), -by = "cohort_definition_id" -) %>% -dplyr::select(-"cohort_definition_id") %>% -dplyr::rename("cohort_definition_id" = "cohort_definition_id_new") %>% -dplyr::relocate(cohort_definition_id) -new_cohort_count <- cdm[[name]] %>% -CDMConnector::cohort_count() %>% -inner_join( -new_cohort_set %>% dplyr::select("cohort_definition_id","cohort_definition_id_new"), -by = "cohort_definition_id" -) %>% -dplyr::select(-"cohort_definition_id") %>% -dplyr::rename("cohort_definition_id" = "cohort_definition_id_new") %>% -dplyr::relocate(cohort_definition_id) -new_cohort <- cdm[[name]] %>% -inner_join( -new_cohort_set %>% dplyr::select("cohort_definition_id","cohort_definition_id_new"), -by = "cohort_definition_id", -copy = TRUE -) %>% -dplyr::select(-"cohort_definition_id") %>% -dplyr::rename("cohort_definition_id" = "cohort_definition_id_new") %>% -dplyr::relocate(cohort_definition_id) -new_cohort_set <- new_cohort_set %>% -dplyr::select(-"cohort_definition_id") %>% -dplyr::rename("cohort_definition_id" = "cohort_definition_id_new") %>% -dplyr::relocate(cohort_definition_id) -cdm[[name]] <- CDMConnector::new_generated_cohort_set( -cohort_ref = new_cohort, -cohort_attrition_ref = new_cohort_attrition , -cohort_set_ref = new_cohort_set, -cohort_count_ref = new_cohort_count, -overwrite = TRUE) -new_cohort_set <- cdm[[name]] %>% -CDMConnector::cohort_set() %>% -dplyr::mutate(cohort_definition_id_new = target_cohort_id) %>% -arrange(cohort_definition_id_new) %>% -dplyr::mutate(cohort_definition_id_new = dplyr::row_number()) -new_cohort_attrition <- cdm[[name]] %>% -CDMConnector::cohort_attrition() %>% -inner_join( -new_cohort_set %>% dplyr::select("cohort_definition_id","cohort_definition_id_new"), -by = "cohort_definition_id" -) %>% -dplyr::select(-"cohort_definition_id") %>% -dplyr::rename("cohort_definition_id" = "cohort_definition_id_new") %>% -dplyr::relocate(cohort_definition_id) -new_cohort_count <- cdm[[name]] %>% -CDMConnector::cohort_count() %>% -inner_join( -new_cohort_set %>% dplyr::select("cohort_definition_id","cohort_definition_id_new"), -by = "cohort_definition_id" -) %>% -dplyr::select(-"cohort_definition_id") %>% -dplyr::rename("cohort_definition_id" = "cohort_definition_id_new") %>% -dplyr::relocate(cohort_definition_id) -new_cohort <- cdm[[name]] %>% -inner_join( -new_cohort_set %>% dplyr::select("cohort_definition_id","cohort_definition_id_new"), -by = "cohort_definition_id", -copy = TRUE -) %>% -dplyr::select(-"cohort_definition_id") %>% -dplyr::rename("cohort_definition_id" = "cohort_definition_id_new") %>% -dplyr::relocate(cohort_definition_id) %>% -CDMConnector::compute_query() %>% -CDMConnector::compute_query(name = name, temporary = FALSE, schema = attr(cdm, "write_schema"), overwrite = TRUE) -new_cohort_set <- new_cohort_set %>% -dplyr::select(-"cohort_definition_id") %>% -dplyr::rename("cohort_definition_id" = "cohort_definition_id_new") %>% -dplyr::relocate(cohort_definition_id) -cdm[[name]] <- CDMConnector::new_generated_cohort_set( -cohort_ref = new_cohort, -cohort_attrition_ref = new_cohort_attrition , -cohort_set_ref = new_cohort_set, -cohort_count_ref = new_cohort_count, -overwrite = TRUE) -cdm[[name]] -CDMConnector::cohort_set(cdm[[name]]) -devtools::load_all() -cdm$cohort1 %>% -generateMatchedCohortSet -cdm$cohort1 %>% -generateMatchedCohortSet(name = "new_matched_cohort", -targetCohortName = "cohort1", -targetCohortId = c(1,2), -matchSex = TRUE, -matchYearOfBirth = TRUE, -ratio = 2) -cdm$cohort1 -cdm$cohort1 %>% -generateMatchedCohortSet(name = "new_matched_cohort", -targetCohortName = "cohort1", -targetCohortId = c(1,2), -matchSex = TRUE, -matchYearOfBirth = TRUE, -ratio = 2) -cdm$cohort1 %>% -generateMatchedCohortSet(name = "new_matched_cohort", -targetCohortName = "cohort_1", -targetCohortId = c(1,2), -matchSex = TRUE, -matchYearOfBirth = TRUE, -ratio = 2) -cdm <- mockDrugUtilisation(numberIndividuals = 100) -cdm$cohort1 %>% -generateMatchedCohortSet(name = "new_matched_cohort", -targetCohortName = "cohort_1", -targetCohortId = c(1,2), -matchSex = TRUE, -matchYearOfBirth = TRUE, -ratio = 2) -cdm -cdm$cohort1 %>% -generateMatchedCohortSet(name = "new_matched_cohort", -targetCohortName = "cohort1", -targetCohortId = c(1,2), -matchSex = TRUE, -matchYearOfBirth = TRUE, -ratio = 2) -generateMatchedCohortSet(name = "new_matched_cohort", -targetCohortName = "cohort1", -targetCohortId = c(1,2), -matchSex = TRUE, -matchYearOfBirth = TRUE, -ratio = 2) -cdm %>% generateMatchedCohortSet(name = "new_matched_cohort", -targetCohortName = "cohort1", -targetCohortId = c(1,2), -matchSex = TRUE, -matchYearOfBirth = TRUE, -ratio = 2) -cdm -name <- "new_matched_cohort" -targetCohortName <- "cohort1" -targetCohortId = c(1,2) -targetCohortId <- c(1,2) -matchSex = TRUE -matchYearOfBirth = TRUE -ratio = 2 -cdm -cdm %>% generateMatchedCohortSet(name = name, targetCohortName = targetCohortName, targetCohortId = targetCohortId, matchSex = matchSex, matchYearOfBirth = matchYearOfBirth, ratio = ratio, ) -cdm %>% generateMatchedCohortSet(name = name, targetCohortName = targetCohortName, targetCohortId = targetCohortId, matchSex = matchSex, matchYearOfBirth = matchYearOfBirth, ratio = ratio) -# validate initial input -validateInput( -cdm = cdm, name = name, targetCohortName = targetCohortName, -targetCohortId = targetCohortId, matchSex = matchSex, -matchYearOfBirth = matchYearOfBirth, ratio = ratio -) -# get the number of cohorts -n <- getNumberOfCohorts(cdm, targetCohortName) -# get target cohort id -targetCohortId <- getTargetCohortId(cdm, targetCohortId, targetCohortName) -# Create the cohort name with cases and controls of the targetCohortId -cdm <- getNewCohort(cdm, name, targetCohortName, targetCohortId, n) -# Exclude cases from controls -cdm <- excludeCases(cdm, name, targetCohortId, n) -# get matched tables -matchCols <- getMatchCols(matchSex, matchYearOfBirth) -if(!is.null(matchCols)){ -# Exclude individuals without any match -cdm <- excludeNoMatchedIndividuals(cdm, name, matchCols, n) -# Match as ratio was infinite -cdm <- infiniteMatching(cdm, name, targetCohortId) -# Delete controls that are not in observation -cdm <- checkObservationPeriod(cdm, name, targetCohortId, n) -# Check ratio -cdm <- checkRatio(cdm, name, ratio, targetCohortId, n) -# Check cohort set ref -cdm <- checkCohortSetRef(cdm, name, targetCohortName, matchSex, matchYearOfBirth, targetCohortId, n) -# Rename cohort definition ids -cdm <- renameCohortDefinitionIds(cdm) -} -# Rename cohort definition ids -cdm <- renameCohortDefinitionIds(cdm, name) -devtools::load_all() -devtools::check() -library(CohortConstructor) -library(dplyr) -library(DrugUtilisation) -cdm <- mockDrugUtilisation(numberIndividual = 1000) -library(CohortConstructor) -library(dplyr) -library(DrugUtilisation) -cdm <- mockDrugUtilisation(numberIndividual = 1000) -cdm <- generateMatchedCohortSet(cdm = cdm, -name = "matched_cohort2", -targetCohortName = "cohort1", -targetCohortId = 1, -ratio = Inf) -CDMConnector::cohort_count(cdm$matched_cohort2) -library(CohortConstructor) -library(dplyr) -library(DrugUtilisation) -cdm <- mockDrugUtilisation(numberIndividual = 1000) -library(CohortConstructor) -library(dplyr) -library(DrugUtilisation) -cdm <- mockDrugUtilisation(numberIndividual = 1000) -CDMConnector::cohort_set(cdm$cohort1) -cdm <- generateMatchedCohortSet(cdm = cdm, -name = "matched_cohort1", -targetCohortName = "cohort1", -targetCohortId = 1) -CDMConnector::cohort_set(cdm$matched_cohort1) -cdm -devtools::load_all() -devtools::load_all() -devtools::check() -library(dplyr) -devtools::load_all() -devtools::check() -devtools::load_all() -devtools::check() -# Create cdm object -cdm <- DrugUtilisation::generateConceptCohortSet( -cdm = DrugUtilisation::mockDrugUtilisation(numberIndividuals = 200), -conceptSet = list(asthma = 317009), -name = "cases", -end = "observation_period_end_date", -requiredObservation = c(180, 180), -overwrite = TRUE) -generateMatchedCohortSet(cdm, -name = "new_cohort", -targetCohortName = "cases", -ratio = 2) -expect_no_error(a <- generateMatchedCohortSet(cdm, -name = "new_cohort", -targetCohortName = "cases", -ratio = 2)) -cdm <- DrugUtilisation::generateConceptCohortSet( -cdm = DrugUtilisation::mockDrugUtilisation(numberIndividuals = 200), -conceptSet = list(asthma = 317009, other = 4141052, other1 = 432526), -name = "cases", -end = "observation_period_end_date", -requiredObservation = c(10,10), -overwrite = TRUE) -cdm -expect_no_error(generateMatchedCohortSet(cdm, -name = "new_cohort", -targetCohortName = "cases")) -expect_no_error(generateMatchedCohortSet(cdm, -name = "new_cohort", -targetCohortName = "cases", -ratio = 3)) -expect_no_error(generateMatchedCohortSet(cdm, -name = "new_cohort", -targetCohortName = "cases", -ratio = Inf)) -expect_no_error(generateMatchedCohortSet(cdm, -name = "new_cohort", -matchSex = FALSE, -matchYearOfBirth = TRUE, -targetCohortName = "cases")) -expect_no_error(generateMatchedCohortSet(cdm, -name = "new_cohort", -matchSex = TRUE, -matchYearOfBirth = FALSE, -targetCohortName = "cases")) -expect_no_error(b <- generateMatchedCohortSet(cdm, -name = "new_cohort", -matchSex = FALSE, -matchYearOfBirth = FALSE, -targetCohortName = "cases")) -help(arrange) -devtools::load_all() -devtools::check() -# Generate mock data -cdmMock <- DrugUtilisation::mockDrugUtilisation( -numberIndividuals = 10, -person = tibble::tibble("person_id" = seq(1,10,1), -"gender_concept_id" = rep(8532,10), -"year_of_birth" = rep(1980, 10), -"day_of_birth" = rep(1, 10), -"birth_date_time" = as.Date(rep("1980-04-01",10)), -"month_of_birth" = rep(4, 10)), -condition_occurrence = tibble::tibble("condition_ocurrence_id" = seq(1,10,1), -"person_id" = seq(1,10,1), -"condition_concept_id" = c(317009,317009,4266367,4266367,rep(1,6)), -"condition_start_date" = as.Date(c("2017-10-30","2003-01-04","2014-12-15","2010-09-09","2004-08-26","1985-03-31","1985-03-13","1985-07-11","1983-11-07","2020-01-13")), -"condition_end_date" = as.Date(c("2017-11-01","2003-01-05","2014-12-16","2010-09-10","2004-08-27","1985-04-01","1985-03-14","1985-07-12","1983-11-08","2020-01-14")), -"condition_type_concept_id" = rep(32020,10)), -observation_period = tibble::tibble("observation_period_id" = seq(1,10,1), -"person_id" = seq(1,10,1), -"observation_period_start_date" = as.Date(rep("1984-01-01",10)), -"observation_period_end_date" = as.Date(rep("2021-01-01",10)), -"period_type_concept_id" = 44814724) -) -cdm <- DrugUtilisation::generateConceptCohortSet( -cdm = cdmMock, -conceptSet = list(c1 = 317009, c2 = 4266367), -name = "cases", -end = "observation_period_end_date", -requiredObservation = c(0,0), -overwrite = TRUE -) -a <- generateMatchedCohortSet(cdm, -name = "new_cohort", -targetCohortName = "cases", -targetCohortId = NULL, -matchSex = TRUE, -matchYearOfBirth = TRUE, -ratio = 4) -a$new_cohort -a[["new_cohort"]] %>% -dplyr::filter(cohort_definition_id %in% c(1,3)) %>% -dplyr::summarise(subject_id) %>% -dplyr::distinct() %>% dplyr::pull() %>% length() == 10) -a[["new_cohort"]] -a[["new_cohort"]] %>% -dplyr::filter(cohort_definition_id %in% c(1,3)) -a[["new_cohort"]] %>% -dplyr::filter(cohort_definition_id %in% c(1,3)) %>% -dplyr::summarise(subject_id) -devtools::load_all() -devtools::check() -devtools::load_all() -devtools::check() -library(CohortConstructor) diff --git a/NAMESPACE b/NAMESPACE index 0a6fd942..b46beaf5 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,22 +5,29 @@ export(cohortCodelist) export(cohortCount) export(collapseCohort) export(conceptCohort) +export(endDateColumn) export(getIdentifier) export(intersectCohort) export(joinOverlap) export(matchCohort) export(requireAge) export(requireCohortIntersectFlag) +export(requireConceptIntersectFlag) +export(requireDeathFlag) export(requireDemographics) export(requireFutureObservation) export(requireInDateRange) export(requirePriorObservation) export(requireSex) +export(requireTableIntersectFlag) export(restrictToFirstEntry) export(settings) export(splitOverlap) +export(startDateColumn) export(tableName) export(trimToDateRange) +importFrom(PatientProfiles,endDateColumn) +importFrom(PatientProfiles,startDateColumn) importFrom(magrittr,"%>%") importFrom(omopgenerics,attrition) importFrom(omopgenerics,cohortCodelist) diff --git a/R/reexports-omopgenerics.R b/R/reexports.R similarity index 67% rename from R/reexports-omopgenerics.R rename to R/reexports.R index 656637f3..889be3a7 100644 --- a/R/reexports-omopgenerics.R +++ b/R/reexports.R @@ -17,3 +17,11 @@ omopgenerics::cohortCodelist #' @importFrom omopgenerics tableName #' @export omopgenerics::tableName + +#' @importFrom PatientProfiles startDateColumn +#' @export +PatientProfiles::startDateColumn + +#' @importFrom PatientProfiles endDateColumn +#' @export +PatientProfiles::endDateColumn diff --git a/R/requireCohortIntersectFlag.R b/R/requireCohortIntersectFlag.R index 51806a1d..ddc3e594 100644 --- a/R/requireCohortIntersectFlag.R +++ b/R/requireCohortIntersectFlag.R @@ -1,7 +1,8 @@ #' Require cohort subjects are present in another cohort #' #' @param x Cohort table. -#' @param targetCohortTable Name of the cohort that we want to check for overlap. +#' @param targetCohortTable Name of the cohort that we want to check for +#' intersect. #' @param targetCohortId Vector of cohort definition ids to include. #' @param indexDate Variable in x that contains the date to compute the #' intersection. @@ -9,11 +10,16 @@ #' (in overlap) or on its own (for incidence). #' @param targetEndDate Date of reference in cohort table, either for end #' (overlap) or NULL (if incidence). +#' @param censorDate Whether to censor overlap events at a specific date or a +#' column date of x. #' @param window Window to consider events over. #' @param negate If set as TRUE, criteria will be applied as exclusion #' rather than inclusion (i.e. require absence in another cohort). +#' @param name Name of the new cohort with the future observation restriction. +#' +#' @return Cohort table with only those in the other cohort kept (or those that +#' are not in the cohort if negate = TRUE) #' -#' @return Cohort table with only those in the other cohort kept #' @export #' #' @examples @@ -31,72 +37,90 @@ requireCohortIntersectFlag <- function(x, indexDate = "cohort_start_date", targetStartDate = "cohort_start_date", targetEndDate = "cohort_end_date", + censorDate = NULL, window = list(c(0, Inf)), - negate = FALSE){ + negate = FALSE, + name = omopgenerics::tableName(x)){ + # checks + assertCharacter(name, length = 1) + assertLogical(negate, length = 1) + validateCohortTable(x) + cdm <- omopgenerics::cdmReference(x) + validateCDM(cdm) + validateIndexDate(indexDate, x) -cols <- unique(c("cohort_definition_id", "subject_id", - "cohort_start_date", "cohort_end_date", - indexDate)) + cols <- unique(c("cohort_definition_id", "subject_id", + "cohort_start_date", "cohort_end_date", + indexDate)) -if(is.list(window)){ -window_start <- window[[1]][1] -window_end <- window[[1]][2] -} else { - window_start <- window[1] - window_end <- window[2] -} + if(is.list(window)){ + window_start <- window[[1]][1] + window_end <- window[[1]][2] + } else { + window_start <- window[1] + window_end <- window[2] + } -cdm <- attr(x, "cdm_reference") + cdm <- omopgenerics::cdmReference(x) -if(is.null(cdm[[targetCohortTable]])){ - cli::cli_abort("targetCohortTable not found in cdm reference") -} + if(is.null(cdm[[targetCohortTable]])){ + cli::cli_abort("targetCohortTable not found in cdm reference") + } -if(is.null(targetCohortId)){ -targetCohortId <- CDMConnector::settings(cdm[[targetCohortTable]]) %>% - dplyr::pull("cohort_definition_id") -} + if(is.null(targetCohortId)){ + targetCohortId <- CDMConnector::settings(cdm[[targetCohortTable]]) %>% + dplyr::pull("cohort_definition_id") + } -if(length(targetCohortId) > 1){ - cli::cli_abort("Only one target cohort is currently supported") -} + if(length(targetCohortId) > 1){ + cli::cli_abort("Only one target cohort is currently supported") + } -target_name <- cdm[[targetCohortTable]] %>% - omopgenerics::settings() %>% - dplyr::filter(.data$cohort_definition_id == .env$targetCohortId) %>% - dplyr::pull("cohort_name") + target_name <- cdm[[targetCohortTable]] %>% + omopgenerics::settings() %>% + dplyr::filter(.data$cohort_definition_id == .env$targetCohortId) %>% + dplyr::pull("cohort_name") -subsetCohort <- x %>% - dplyr::select(dplyr::all_of(.env$cols)) %>% - PatientProfiles::addCohortIntersectFlag( - targetCohortTable = targetCohortTable, - targetCohortId = targetCohortId, - indexDate = indexDate, - targetStartDate = targetStartDate, - targetEndDate = targetEndDate, - window = window, - nameStyle = "intersect_cohort" - ) + subsetCohort <- x %>% + dplyr::select(dplyr::all_of(.env$cols)) %>% + PatientProfiles::addCohortIntersectFlag( + targetCohortTable = targetCohortTable, + targetCohortId = targetCohortId, + indexDate = indexDate, + targetStartDate = targetStartDate, + targetEndDate = targetEndDate, + window = window, + censorDate = censorDate, + nameStyle = "intersect_cohort" + ) -if(isFALSE(negate)){ - subsetCohort <- subsetCohort %>% - dplyr::filter(.data$intersect_cohort == 1) %>% - dplyr::select(!"intersect_cohort") -} else { - # ie require absence instead of presence - subsetCohort <- subsetCohort %>% - dplyr::filter(.data$intersect_cohort != 1) %>% - dplyr::select(!"intersect_cohort") -} + if(isFALSE(negate)){ + subsetCohort <- subsetCohort %>% + dplyr::filter(.data$intersect_cohort == 1) %>% + dplyr::select(!"intersect_cohort") + # attrition reason + reason <- glue::glue("In cohort {target_name} between {window_start} & ", + "{window_end} days relative to {indexDate}") + } else { + # ie require absence instead of presence + subsetCohort <- subsetCohort %>% + dplyr::filter(.data$intersect_cohort != 1) %>% + dplyr::select(!"intersect_cohort") + # attrition reason + reason <- glue::glue("Not in cohort {target_name} between {window_start} & ", + "{window_end} days relative to {indexDate}") + } + if (!is.null(censorDate)) { + reason <- glue::glue("{reason}, censoring at {censorDate}") + } -x %>% - dplyr::inner_join(subsetCohort, - by = c(cols)) %>% - CDMConnector::recordCohortAttrition(reason = - glue::glue("In cohort {target_name} between ", - "{window_start} & ", - "{window_end} days relative to ", - "{indexDate}")) + x <- x %>% + dplyr::inner_join(subsetCohort, + by = c(cols)) %>% + dplyr::compute(name = name, temporary = FALSE) %>% + CDMConnector::recordCohortAttrition(reason = reason) + return(x) } + diff --git a/R/requireConceptIntersectFlag.R b/R/requireConceptIntersectFlag.R new file mode 100644 index 00000000..150a196e --- /dev/null +++ b/R/requireConceptIntersectFlag.R @@ -0,0 +1,119 @@ +#' Require cohort subjects to have events of a concept list +#' +#' @param x Cohort table. +#' @param conceptSet Concept set list. +#' @param indexDate Variable in x that contains the date to compute the +#' intersection. +#' @param targetStartDate Date of reference in cohort table, either for start +#' (in overlap) or on its own (for incidence). +#' @param targetEndDate Date of reference in cohort table, either for end +#' (overlap) or NULL (if incidence). +#' @param censorDate Whether to censor overlap events at a specific date or a +#' column date of x. +#' @param window Window to consider events over. +#' @param negate If set as TRUE, criteria will be applied as exclusion +#' rather than inclusion (i.e. require absence in another cohort). +#' @param name Name of the new cohort with the future observation restriction. +#' +#' @return Cohort table with only those with the events in the concept list +#' kept (or those without the event if nagate = TRUE) +#' +#' @export +#' +#' @examples +#' library(PatientProfiles) +#' library(CohortConstructor) +#' cdm <- mockPatientProfiles() +#' cdm <- CDMConnector::insertTable(cdm, name = "concept", +#' table = dplyr::tibble( +#' "concept_id" = 1, +#' "concept_name" = "my concept", +#' "domain_id" = "Drug", +#' "vocabulary_id" = NA, +#' "concept_class_id" = NA, +#' "concept_code" = NA, +#' "valid_start_date" = NA, +#' "valid_end_date" = NA +#' )) +#' cdm$cohort2 <- requireConceptIntersectFlag( +#' x = cdm$cohort1, +#' conceptSet = list(a = 1), +#' window = c(-Inf, 0), +#' name = "cohort2") +requireConceptIntersectFlag <- function(x, + conceptSet, + indexDate = "cohort_start_date", + targetStartDate = "event_start_date", + targetEndDate = "event_end_date", + censorDate = NULL, + window = list(c(0, Inf)), + negate = FALSE, + name = omopgenerics::tableName(x)){ + # checks + assertCharacter(name, length = 1) + assertLogical(negate, length = 1) + validateCohortTable(x) + cdm <- omopgenerics::cdmReference(x) + validateCDM(cdm) + validateIndexDate(indexDate, x) + assertList(conceptSet) + + cols <- unique(c("cohort_definition_id", "subject_id", + "cohort_start_date", "cohort_end_date", + indexDate)) + + if(is.list(window)){ + window_start <- window[[1]][1] + window_end <- window[[1]][2] + } else { + window_start <- window[1] + window_end <- window[2] + } + + if (length(conceptSet) > 1) { + cli::cli_abort("We currently suport 1 concept set.") + } + + cdm <- omopgenerics::cdmReference(x) + + subsetCohort <- x %>% + dplyr::select(dplyr::all_of(.env$cols)) %>% + PatientProfiles::addConceptIntersectFlag( + conceptSet = conceptSet, + indexDate = indexDate, + targetStartDate = targetStartDate, + targetEndDate = targetEndDate, + window = window, + censorDate = censorDate, + nameStyle = "intersect_concept" + ) + + if(isFALSE(negate)){ + subsetCohort <- subsetCohort %>% + dplyr::filter(.data$intersect_concept == 1) %>% + dplyr::select(!"intersect_concept") + # attrition reason + reason <- glue::glue("Concept {names(conceptSet)} between {window_start} & ", + "{window_end} days relative to {indexDate}") + } else { + # ie require absence instead of presence + subsetCohort <- subsetCohort %>% + dplyr::filter(.data$intersect_concept != 1) %>% + dplyr::select(!"intersect_concept") + # attrition reason + reason <- glue::glue("Not in concept {names(conceptSet)} between {window_start} & ", + "{window_end} days relative to {indexDate}") + } + + if (!is.null(censorDate)) { + reason <- glue::glue("{reason}, censoring at {censorDate}") + } + + x <- x %>% + dplyr::inner_join(subsetCohort, + by = c(cols)) %>% + dplyr::compute(name = name, temporary = FALSE) %>% + CDMConnector::recordCohortAttrition(reason = reason) + + return(x) +} diff --git a/R/requireDeathFlag.R b/R/requireDeathFlag.R new file mode 100644 index 00000000..f0f6d7f0 --- /dev/null +++ b/R/requireDeathFlag.R @@ -0,0 +1,97 @@ +#' Require cohort subjects' death at a certain time window +#' +#' @param x Cohort table. +#' @param indexDate Variable in x that contains the date to compute the +#' intersection. +#' @param censorDate Whether to censor overlap events at a specific date or a +#' column date of x. +#' @param window Window to consider events over. +#' @param negate If set as TRUE, criteria will be applied as exclusion +#' rather than inclusion (i.e. require absence in another cohort). +#' @param name Name of the new cohort with the future observation restriction. +#' +#' @return Cohort table with only those with a death event kept (or without +#' if negate = TRUE) +#' +#' @export +#' +#' @examples +#' library(PatientProfiles) +#' library(CDMConnector) +#' library(CohortConstructor) +#' cdm <- mockPatientProfiles() +#' cdm <- insertTable(cdm, "death", +#' table = dplyr::tibble( +#' person_id = 1, +#' death_date = as.Date("2020-05-01"), +#' death_type_concept_id = NA)) +#' cdm$cohort1 <- cdm$cohort1 %>% requireDeathFlag() +#' attrition(cdm$cohort1) + + +requireDeathFlag <- function(x, + indexDate = "cohort_start_date", + censorDate = NULL, + window = list(c(0, Inf)), + negate = FALSE, + name = omopgenerics::tableName(x)) { + # checks + assertCharacter(name, length = 1) + assertLogical(negate, length = 1) + validateCohortTable(x) + cdm <- omopgenerics::cdmReference(x) + validateCDM(cdm) + validateIndexDate(indexDate, x) + + cols <- unique(c("cohort_definition_id", "subject_id", + "cohort_start_date", "cohort_end_date", + indexDate)) + + if(is.list(window)){ + window_start <- window[[1]][1] + window_end <- window[[1]][2] + } else { + window_start <- window[1] + window_end <- window[2] + } + + cdm <- omopgenerics::cdmReference(x) + + subsetCohort <- x %>% + dplyr::select(dplyr::all_of(.env$cols)) %>% + PatientProfiles::addDeathFlag( + indexDate = indexDate, + censorDate = censorDate, + window = window, + deathFlagName = "death" + ) + + if(isFALSE(negate)){ + subsetCohort <- subsetCohort %>% + dplyr::filter(.data$death == 1) %>% + dplyr::select(!"death") + # attrition reason + reason <- glue::glue("Death between {window_start} & ", + "{window_end} days relative to {indexDate}") + } else { + # ie require absence instead of presence + subsetCohort <- subsetCohort %>% + dplyr::filter(.data$death != 1) %>% + dplyr::select(!"death") + # attrition reason + reason <- glue::glue("Alive between {window_start} & ", + "{window_end} days relative to {indexDate}") + } + + if (!is.null(censorDate)) { + reason <- glue::glue("{reason}, censoring at {censorDate}") + } + + x <- x %>% + dplyr::inner_join(subsetCohort, + by = c(cols)) %>% + dplyr::compute(name = name, temporary = FALSE) %>% + CDMConnector::recordCohortAttrition(reason = reason) + + return(x) +} diff --git a/R/requireTableIntersectFlag.R b/R/requireTableIntersectFlag.R new file mode 100644 index 00000000..df8d9640 --- /dev/null +++ b/R/requireTableIntersectFlag.R @@ -0,0 +1,112 @@ +#' Require cohort subjects are present in another table +#' +#' @param x Cohort table. +#' @param tableName Name of the table to check for intersect. +#' @param indexDate Variable in x that contains the date to compute the +#' intersection. +#' @param targetStartDate Date of reference in cohort table, either for start +#' (in overlap) or on its own (for incidence). +#' @param targetEndDate Date of reference in cohort table, either for end +#' (overlap) or NULL (if incidence). +#' @param censorDate Whether to censor overlap events at a specific date or a +#' column date of x. +#' @param window Window to consider events over. +#' @param negate If set as TRUE, criteria will be applied as exclusion +#' rather than inclusion (i.e. require absence in another cohort). +#' @param name Name of the new cohort with the future observation restriction. +#' +#' @return Cohort table with only those in the other table kept (or those that +#' are not in the table if negate = TRUE) +#' +#' @export +#' +#' @examples +#' library(PatientProfiles) +#' library(CohortConstructor) +#' cdm <- mockPatientProfiles() +#' cdm$cohort1 %>% +#' requireTableIntersectFlag(tableName = "drug_exposure", +#' indexDate = "cohort_start_date", +#' window = c(-Inf, 0)) +requireTableIntersectFlag <- function(x, + tableName, + indexDate = "cohort_start_date", + targetStartDate = startDateColumn(tableName), + targetEndDate = endDateColumn(tableName), + censorDate = NULL, + window = list(c(0, Inf)), + negate = FALSE, + name = omopgenerics::tableName(x)){ + # checks + assertCharacter(name, length = 1) + assertLogical(negate, length = 1) + assertCharacter(tableName) + validateCohortTable(x) + cdm <- omopgenerics::cdmReference(x) + validateCDM(cdm) + validateIndexDate(indexDate, x) + + cols <- unique(c("cohort_definition_id", "subject_id", + "cohort_start_date", "cohort_end_date", + indexDate)) + + if (is.list(window)) { + window_start <- window[[1]][1] + window_end <- window[[1]][2] + } else { + window_start <- window[1] + window_end <- window[2] + } + + cdm <- omopgenerics::cdmReference(x) + + if (is.null(cdm[[tableName]])) { + cli::cli_abort("{tableName} not found in cdm reference") + } + + if (length(tableName) > 1) { + cli::cli_abort("Currently just one table supported.") + } + + subsetCohort <- x %>% + dplyr::select(dplyr::all_of(.env$cols)) %>% + PatientProfiles::addTableIntersectFlag( + tableName = tableName, + indexDate = indexDate, + targetStartDate = targetStartDate, + targetEndDate = targetEndDate, + window = window, + censorDate = censorDate, + nameStyle = "intersect_table" + ) + + if (isFALSE(negate)) { + subsetCohort <- subsetCohort %>% + dplyr::filter(.data$intersect_table == 1) %>% + dplyr::select(!"intersect_table") + # attrition reason + reason <- glue::glue("In table {tableName} between {window_start} & ", + "{window_end} days relative to {indexDate}") + } else { + # ie require absence instead of presence + subsetCohort <- subsetCohort %>% + dplyr::filter(.data$intersect_table != 1) %>% + dplyr::select(!"intersect_table") + # attrition reason + reason <- glue::glue("Not in table {tableName} between {window_start} & ", + "{window_end} days relative to {indexDate}") + } + + + if (!is.null(censorDate)) { + reason <- glue::glue("{reason}, censoring at {censorDate}") + } + + x <- x %>% + dplyr::inner_join(subsetCohort, + by = c(cols)) %>% + dplyr::compute(name = name, temporary = FALSE) %>% + CDMConnector::recordCohortAttrition(reason = reason) + + return(x) +} diff --git a/man/reexports.Rd b/man/reexports.Rd index af92e22c..6d6b66ab 100644 --- a/man/reexports.Rd +++ b/man/reexports.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/reexports-omopgenerics.R +% Please edit documentation in R/reexports.R \docType{import} \name{reexports} \alias{reexports} @@ -8,6 +8,8 @@ \alias{attrition} \alias{cohortCodelist} \alias{tableName} +\alias{startDateColumn} +\alias{endDateColumn} \title{Objects exported from other packages} \keyword{internal} \description{ @@ -16,5 +18,7 @@ below to see their documentation. \describe{ \item{omopgenerics}{\code{\link[omopgenerics]{attrition}}, \code{\link[omopgenerics]{cohortCodelist}}, \code{\link[omopgenerics]{cohortCount}}, \code{\link[omopgenerics]{settings}}, \code{\link[omopgenerics]{tableName}}} + + \item{PatientProfiles}{\code{\link[PatientProfiles]{endDateColumn}}, \code{\link[PatientProfiles]{startDateColumn}}} }} diff --git a/man/requireCohortIntersectFlag.Rd b/man/requireCohortIntersectFlag.Rd index c39efe41..6bc350c0 100644 --- a/man/requireCohortIntersectFlag.Rd +++ b/man/requireCohortIntersectFlag.Rd @@ -11,14 +11,17 @@ requireCohortIntersectFlag( indexDate = "cohort_start_date", targetStartDate = "cohort_start_date", targetEndDate = "cohort_end_date", + censorDate = NULL, window = list(c(0, Inf)), - negate = FALSE + negate = FALSE, + name = omopgenerics::tableName(x) ) } \arguments{ \item{x}{Cohort table.} -\item{targetCohortTable}{Name of the cohort that we want to check for overlap.} +\item{targetCohortTable}{Name of the cohort that we want to check for +intersect.} \item{targetCohortId}{Vector of cohort definition ids to include.} @@ -31,13 +34,19 @@ intersection.} \item{targetEndDate}{Date of reference in cohort table, either for end (overlap) or NULL (if incidence).} +\item{censorDate}{Whether to censor overlap events at a specific date or a +column date of x.} + \item{window}{Window to consider events over.} \item{negate}{If set as TRUE, criteria will be applied as exclusion rather than inclusion (i.e. require absence in another cohort).} + +\item{name}{Name of the new cohort with the future observation restriction.} } \value{ -Cohort table with only those in the other cohort kept +Cohort table with only those in the other cohort kept (or those that +are not in the cohort if negate = TRUE) } \description{ Require cohort subjects are present in another cohort diff --git a/man/requireConceptIntersectFlag.Rd b/man/requireConceptIntersectFlag.Rd new file mode 100644 index 00000000..520fc437 --- /dev/null +++ b/man/requireConceptIntersectFlag.Rd @@ -0,0 +1,70 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/requireConceptIntersectFlag.R +\name{requireConceptIntersectFlag} +\alias{requireConceptIntersectFlag} +\title{Require cohort subjects to have events of a concept list} +\usage{ +requireConceptIntersectFlag( + x, + conceptSet, + indexDate = "cohort_start_date", + targetStartDate = "event_start_date", + targetEndDate = "event_end_date", + censorDate = NULL, + window = list(c(0, Inf)), + negate = FALSE, + name = omopgenerics::tableName(x) +) +} +\arguments{ +\item{x}{Cohort table.} + +\item{conceptSet}{Concept set list.} + +\item{indexDate}{Variable in x that contains the date to compute the +intersection.} + +\item{targetStartDate}{Date of reference in cohort table, either for start +(in overlap) or on its own (for incidence).} + +\item{targetEndDate}{Date of reference in cohort table, either for end +(overlap) or NULL (if incidence).} + +\item{censorDate}{Whether to censor overlap events at a specific date or a +column date of x.} + +\item{window}{Window to consider events over.} + +\item{negate}{If set as TRUE, criteria will be applied as exclusion +rather than inclusion (i.e. require absence in another cohort).} + +\item{name}{Name of the new cohort with the future observation restriction.} +} +\value{ +Cohort table with only those with the events in the concept list +kept (or those without the event if nagate = TRUE) +} +\description{ +Require cohort subjects to have events of a concept list +} +\examples{ +library(PatientProfiles) +library(CohortConstructor) +cdm <- mockPatientProfiles() +cdm <- CDMConnector::insertTable(cdm, name = "concept", + table = dplyr::tibble( + "concept_id" = 1, + "concept_name" = "my concept", + "domain_id" = "Drug", + "vocabulary_id" = NA, + "concept_class_id" = NA, + "concept_code" = NA, + "valid_start_date" = NA, + "valid_end_date" = NA + )) +cdm$cohort2 <- requireConceptIntersectFlag( + x = cdm$cohort1, + conceptSet = list(a = 1), + window = c(-Inf, 0), + name = "cohort2") +} diff --git a/man/requireDeathFlag.Rd b/man/requireDeathFlag.Rd new file mode 100644 index 00000000..e3e3360b --- /dev/null +++ b/man/requireDeathFlag.Rd @@ -0,0 +1,51 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/requireDeathFlag.R +\name{requireDeathFlag} +\alias{requireDeathFlag} +\title{Require cohort subjects' death at a certain time window} +\usage{ +requireDeathFlag( + x, + indexDate = "cohort_start_date", + censorDate = NULL, + window = list(c(0, Inf)), + negate = FALSE, + name = omopgenerics::tableName(x) +) +} +\arguments{ +\item{x}{Cohort table.} + +\item{indexDate}{Variable in x that contains the date to compute the +intersection.} + +\item{censorDate}{Whether to censor overlap events at a specific date or a +column date of x.} + +\item{window}{Window to consider events over.} + +\item{negate}{If set as TRUE, criteria will be applied as exclusion +rather than inclusion (i.e. require absence in another cohort).} + +\item{name}{Name of the new cohort with the future observation restriction.} +} +\value{ +Cohort table with only those with a death event kept (or without +if negate = TRUE) +} +\description{ +Require cohort subjects' death at a certain time window +} +\examples{ +library(PatientProfiles) +library(CDMConnector) +library(CohortConstructor) +cdm <- mockPatientProfiles() +cdm <- insertTable(cdm, "death", + table = dplyr::tibble( + person_id = 1, + death_date = as.Date("2020-05-01"), + death_type_concept_id = NA)) +cdm$cohort1 <- cdm$cohort1 \%>\% requireDeathFlag() +attrition(cdm$cohort1) +} diff --git a/man/requireTableIntersectFlag.Rd b/man/requireTableIntersectFlag.Rd new file mode 100644 index 00000000..c9e3f73a --- /dev/null +++ b/man/requireTableIntersectFlag.Rd @@ -0,0 +1,58 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/requireTableIntersectFlag.R +\name{requireTableIntersectFlag} +\alias{requireTableIntersectFlag} +\title{Require cohort subjects are present in another table} +\usage{ +requireTableIntersectFlag( + x, + tableName, + indexDate = "cohort_start_date", + targetStartDate = startDateColumn(tableName), + targetEndDate = endDateColumn(tableName), + censorDate = NULL, + window = list(c(0, Inf)), + negate = FALSE, + name = omopgenerics::tableName(x) +) +} +\arguments{ +\item{x}{Cohort table.} + +\item{tableName}{Name of the table to check for intersect.} + +\item{indexDate}{Variable in x that contains the date to compute the +intersection.} + +\item{targetStartDate}{Date of reference in cohort table, either for start +(in overlap) or on its own (for incidence).} + +\item{targetEndDate}{Date of reference in cohort table, either for end +(overlap) or NULL (if incidence).} + +\item{censorDate}{Whether to censor overlap events at a specific date or a +column date of x.} + +\item{window}{Window to consider events over.} + +\item{negate}{If set as TRUE, criteria will be applied as exclusion +rather than inclusion (i.e. require absence in another cohort).} + +\item{name}{Name of the new cohort with the future observation restriction.} +} +\value{ +Cohort table with only those in the other table kept (or those that +are not in the table if negate = TRUE) +} +\description{ +Require cohort subjects are present in another table +} +\examples{ +library(PatientProfiles) +library(CohortConstructor) +cdm <- mockPatientProfiles() +cdm$cohort1 \%>\% + requireTableIntersectFlag(tableName = "drug_exposure", + indexDate = "cohort_start_date", + window = c(-Inf, 0)) +} diff --git a/tests/testthat/test-requireCohortIntersectFlag.R b/tests/testthat/test-requireCohortIntersectFlag.R index 7cffccb9..9a5b6f52 100644 --- a/tests/testthat/test-requireCohortIntersectFlag.R +++ b/tests/testthat/test-requireCohortIntersectFlag.R @@ -11,8 +11,8 @@ test_that("requiring presence in another cohort", { cdm$cohort3 <- requireCohortIntersectFlag(x = cdm$cohort1, targetCohortTable = "cohort2", targetCohortId = 1, - window = c(-Inf, Inf)) |> - dplyr::compute(name = "cohort3", temporary = FALSE) + window = c(-Inf, Inf), + name = "cohort3") expect_true(all(cdm$cohort3 %>% dplyr::distinct(subject_id) %>% @@ -24,12 +24,17 @@ test_that("requiring presence in another cohort", { dplyr::filter(cohort_definition_id == 1) %>% dplyr::distinct(subject_id) %>% dplyr::pull()))) + expect_true(all(omopgenerics::attrition(cdm$cohort3)$reason == + c("Initial qualifying events", + "In cohort cohort_1 between -Inf & Inf days relative to cohort_start_date", + "Initial qualifying events", + "In cohort cohort_1 between -Inf & Inf days relative to cohort_start_date"))) cdm$cohort4 <- requireCohortIntersectFlag(x = cdm$cohort1, targetCohortTable = "cohort2", targetCohortId = 2, - window = c(-Inf, Inf)) |> - dplyr::compute(name = "cohort4", temporary = FALSE) + window = c(-Inf, Inf), + name = "cohort4") expect_true(all(cdm$cohort4 %>% dplyr::distinct(subject_id) %>% dplyr::pull() %in% @@ -40,7 +45,38 @@ test_that("requiring presence in another cohort", { dplyr::filter(cohort_definition_id == 2) %>% dplyr::distinct(subject_id) %>% dplyr::pull()))) + expect_true(all(omopgenerics::attrition(cdm$cohort4)$reason == + c("Initial qualifying events", + "In cohort cohort_2 between -Inf & Inf days relative to cohort_start_date", + "Initial qualifying events", + "In cohort cohort_2 between -Inf & Inf days relative to cohort_start_date"))) + # name + cdm$cohort1 <- requireCohortIntersectFlag(x = cdm$cohort1, + targetCohortTable = "cohort2", + targetCohortId = 2, + window = c(-Inf, Inf)) + expect_true(all(omopgenerics::attrition(cdm$cohort1)$reason == + c("Initial qualifying events", + "In cohort cohort_2 between -Inf & Inf days relative to cohort_start_date", + "Initial qualifying events", + "In cohort cohort_2 between -Inf & Inf days relative to cohort_start_date"))) + + # censor date + cdm$cohort5 <- requireCohortIntersectFlag(x = cdm$cohort2, + targetCohortTable = "cohort1", + targetCohortId = 2, + 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(omopgenerics::attrition(cdm$cohort5)$reason == + c("Initial qualifying events", + "In cohort cohort_2 between 0 & Inf days relative to cohort_start_date, censoring at cohort_end_date", + "Initial qualifying events", + "In cohort cohort_2 between 0 & Inf days relative to cohort_start_date, censoring at cohort_end_date"))) # expected errors # only support one target id at the moment @@ -63,7 +99,6 @@ test_that("requiring presence in another cohort", { }) test_that("requiring absence in another cohort", { - cdm_local <- omock::mockCdmReference() |> omock::mockPerson(n = 4) |> omock::mockObservationPeriod() |> @@ -76,15 +111,14 @@ test_that("requiring absence in another cohort", { cdm$cohort3_inclusion <- requireCohortIntersectFlag(x = cdm$cohort1, targetCohortTable = "cohort2", targetCohortId = 1, - window = c(-Inf, Inf)) |> - dplyr::compute(name = "cohort3_inclusion", temporary = FALSE) + window = c(-Inf, Inf), + name = "cohort3_inclusion") cdm$cohort3_exclusion <- requireCohortIntersectFlag(x = cdm$cohort1, targetCohortTable = "cohort2", targetCohortId = 1, window = c(-Inf, Inf), - negate = TRUE) |> - dplyr::compute(name = "cohort3_exclusion", temporary = FALSE) - + negate = TRUE, + name = "cohort3_exclusion") in_both <- intersect(cdm$cohort3_inclusion %>% dplyr::pull("subject_id") %>% unique(), @@ -92,6 +126,11 @@ test_that("requiring absence in another cohort", { dplyr::pull("subject_id") %>% unique()) expect_true(length(in_both) == 0) + expect_true(all(omopgenerics::attrition(cdm$cohort3_exclusion)$reason == + c("Initial qualifying events", + "Not in cohort cohort_1 between -Inf & Inf days relative to cohort_start_date", + "Initial qualifying events", + "Not in cohort cohort_1 between -Inf & Inf days relative to cohort_start_date"))) CDMConnector::cdm_disconnect(cdm) }) diff --git a/tests/testthat/test-requireConceptIntersectFlag.R b/tests/testthat/test-requireConceptIntersectFlag.R new file mode 100644 index 00000000..7f691b3d --- /dev/null +++ b/tests/testthat/test-requireConceptIntersectFlag.R @@ -0,0 +1,142 @@ +test_that("require flag in concept", { + cdm_local <- omock::mockCdmReference() |> + omock::mockPerson(n = 4) |> + omock::mockObservationPeriod() |> + omock::mockCohort(tableName = c("cohort1"), numberCohorts = 2) + cdm_local$concept <- dplyr::tibble( + "concept_id" = 1, + "concept_name" = "my concept", + "domain_id" = "Drug", + "vocabulary_id" = NA, + "concept_class_id" = NA, + "concept_code" = NA, + "valid_start_date" = NA, + "valid_end_date" = NA + ) + cdm_local$drug_exposure <- dplyr::tibble( + "drug_exposure_id" = 1:11, + "person_id" = c(1, 1, 1, 1, 2, 2, 3, 1, 1, 1, 1), + "drug_concept_id" = c(1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1), + "drug_exposure_start_date" = c(0, 300, 1500, 750, 10, 800, 150, 1800, 1801, 1802, 1803), + "drug_exposure_end_date" = c(400, 800, 1600, 1550, 2000, 1000, 600, 1801, 1802, 1803, 1804), + "drug_type_concept_id" = 1 + ) |> + dplyr::mutate( + "drug_exposure_start_date" = as.Date(.data$drug_exposure_start_date, origin = "2010-01-01"), + "drug_exposure_end_date" = as.Date(.data$drug_exposure_end_date, origin = "2010-01-01") + ) + cdm <- CDMConnector::copy_cdm_to(con = DBI::dbConnect(duckdb::duckdb(), ":memory:"), + cdm = cdm_local, + schema = "main") + cdm$cohort3 <- requireConceptIntersectFlag(x = cdm$cohort1, + conceptSet = list(a = 1), + window = c(-Inf, Inf), + name = "cohort3") + expect_true(all(cdm$cohort1 |> dplyr::pull("subject_id") == + c(1, 1, 3, 4, 1, 1, 1, 3))) + expect_true(all(cdm$cohort1 |> dplyr::pull("cohort_start_date") == + c("2001-03-30", "2003-06-15", "2015-03-25", "1997-10-22", "2000-06-23", + "2001-07-16", "2001-12-04", "2015-03-05"))) + + expect_true(all(omopgenerics::attrition(cdm$cohort3)$reason == + c("Initial qualifying events", + "Concept a between -Inf & Inf days relative to cohort_start_date", + "Initial qualifying events", + "Concept a between -Inf & Inf days relative to cohort_start_date"))) + + # censor date + cdm$cohort5 <- requireConceptIntersectFlag(x = cdm$cohort1, + conceptSet = list(a = 1), + window = c(-Inf, Inf), + censorDate = "cohort_end_date", + name = "cohort5") + expect_true(cdm$cohort5 |> dplyr::pull("subject_id") |> length() == 0) + expect_true(all(omopgenerics::attrition(cdm$cohort5)$reason == + c("Initial qualifying events", + "Concept a between -Inf & Inf days relative to cohort_start_date, censoring at cohort_end_date", + "Initial qualifying events", + "Concept a between -Inf & Inf days relative to cohort_start_date, censoring at cohort_end_date"))) + + # name + cdm$cohort1 <- requireConceptIntersectFlag(x = cdm$cohort1, + conceptSet = list(a = 1), + window = c(-Inf, Inf)) + expect_true(all(omopgenerics::attrition(cdm$cohort1)$reason == + c("Initial qualifying events", + "Concept a between -Inf & Inf days relative to cohort_start_date", + "Initial qualifying events", + "Concept a between -Inf & Inf days relative to cohort_start_date"))) + + + + # expected errors + # only support one concept at the moment + expect_error( + requireConceptIntersectFlag(x = cdm$cohort1, + conceptSet = list(a = 1, b = 2), + window = c(-Inf, Inf)) + ) + expect_error( + requireConceptIntersectFlag(x = cdm$cohort1, + conceptSet = NULL, + window = c(-Inf, Inf)) + ) + + CDMConnector::cdm_disconnect(cdm) +}) + +test_that("requiring absence in another cohort", { + cdm_local <- omock::mockCdmReference() |> + omock::mockPerson(n = 4) |> + omock::mockObservationPeriod() |> + omock::mockCohort(tableName = c("cohort1"), numberCohorts = 2) + cdm_local$concept <- dplyr::tibble( + "concept_id" = 1, + "concept_name" = "my concept", + "domain_id" = "Drug", + "vocabulary_id" = NA, + "concept_class_id" = NA, + "concept_code" = NA, + "valid_start_date" = NA, + "valid_end_date" = NA + ) + cdm_local$drug_exposure <- dplyr::tibble( + "drug_exposure_id" = 1:11, + "person_id" = c(1, 1, 1, 1, 2, 2, 3, 1, 1, 1, 1), + "drug_concept_id" = c(1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1), + "drug_exposure_start_date" = c(0, 300, 1500, 750, 10, 800, 150, 1800, 1801, 1802, 1803), + "drug_exposure_end_date" = c(400, 800, 1600, 1550, 2000, 1000, 600, 1801, 1802, 1803, 1804), + "drug_type_concept_id" = 1 + ) |> + dplyr::mutate( + "drug_exposure_start_date" = as.Date(.data$drug_exposure_start_date, origin = "2010-01-01"), + "drug_exposure_end_date" = as.Date(.data$drug_exposure_end_date, origin = "2010-01-01") + ) + cdm <- CDMConnector::copy_cdm_to(con = DBI::dbConnect(duckdb::duckdb(), ":memory:"), + cdm = cdm_local, + schema = "main") + + cdm$cohort3_inclusion <- requireConceptIntersectFlag(x = cdm$cohort1, + conceptSet = list(a = 1), + window = c(-Inf, Inf), + name = "cohort3_inclusion") + cdm$cohort3_exclusion <- requireConceptIntersectFlag(x = cdm$cohort1, + conceptSet = list(a = 1), + window = c(-Inf, Inf), + negate = TRUE, + name = "cohort3_exclusion") + in_both <- intersect(cdm$cohort3_inclusion %>% + dplyr::pull("subject_id") %>% + unique(), + cdm$cohort3_exclusion %>% + dplyr::pull("subject_id") %>% + unique()) + expect_true(length(in_both) == 0) + expect_true(all(omopgenerics::attrition(cdm$cohort3_exclusion)$reason == + c("Initial qualifying events", + "Not in concept a between -Inf & Inf days relative to cohort_start_date", + "Initial qualifying events", + "Not in concept a between -Inf & Inf days relative to cohort_start_date"))) + + CDMConnector::cdm_disconnect(cdm) +}) diff --git a/tests/testthat/test-requireDeathFlag.R b/tests/testthat/test-requireDeathFlag.R new file mode 100644 index 00000000..e0c8a8e0 --- /dev/null +++ b/tests/testthat/test-requireDeathFlag.R @@ -0,0 +1,91 @@ +test_that("requiring death", { + cdm_local <- omock::mockCdmReference() |> + omock::mockPerson(n = 4) |> + omock::mockObservationPeriod() |> + omock::mockCohort(tableName = c("cohort1"), numberCohorts = 2) + cdm_local$death <- dplyr::tibble( + person_id = c(1,3), + death_date = as.Date(c("2013-06-29", "2015-10-11")), + death_type_concept_id = NA + ) + cdm <- CDMConnector::copy_cdm_to(con = DBI::dbConnect(duckdb::duckdb(), ":memory:"), + cdm = cdm_local, + schema = "main") + + cdm$cohort3 <- requireDeathFlag(x = cdm$cohort1, + window = c(0, Inf), + name = "cohort3") + expect_true(all(cdm$cohort3 |> dplyr::pull("subject_id") %in% c(1,3))) + expect_true(all( + cdm$cohort1 |> dplyr::filter(subject_id %in% c(1,3)) |> dplyr::pull("cohort_start_date") |> sort() == + cdm$cohort3 |> dplyr::pull("cohort_start_date") |> sort() + )) + expect_true(all(omopgenerics::attrition(cdm$cohort3)$reason == + c("Initial qualifying events", "Death between 0 & Inf days relative to cohort_start_date", + "Initial qualifying events", "Death between 0 & Inf days relative to cohort_start_date"))) + +# censor + cdm$cohort4 <- requireDeathFlag(x = cdm$cohort1, + window = c(0, Inf), + censorDate = "cohort_end_date", + name = "cohort4") + expect_true(cdm$cohort4 |> dplyr::tally() |> dplyr::pull() == 0) + expect_true(all(omopgenerics::attrition(cdm$cohort4)$reason == + c("Initial qualifying events", "Death between 0 & Inf days relative to cohort_start_date, censoring at cohort_end_date", + "Initial qualifying events", "Death between 0 & Inf days relative to cohort_start_date, censoring at cohort_end_date"))) + + + # index date + cdm$cohort5 <- requireDeathFlag(x = cdm$cohort1, + window = c(0, 365), + indexDate = "cohort_end_date", + name = "cohort5") + expect_true(all(cdm$cohort5 |> dplyr::pull("subject_id") %in% 3)) + expect_true(all( + cdm$cohort5 |> dplyr::pull("cohort_start_date") |> sort() == + c("2015-03-05", "2015-03-25") + )) + expect_true(all(omopgenerics::attrition(cdm$cohort5)$reason == + c("Initial qualifying events", "Death between 0 & 365 days relative to cohort_end_date", + "Initial qualifying events", "Death between 0 & 365 days relative to cohort_end_date"))) + # name + cdm$cohort1 <- requireDeathFlag(x = cdm$cohort1) + expect_true(all(omopgenerics::attrition(cdm$cohort1)$reason == + c("Initial qualifying events", + "Death between 0 & Inf days relative to cohort_start_date", + "Initial qualifying events", + "Death between 0 & Inf days relative to cohort_start_date"))) + + CDMConnector::cdm_disconnect(cdm) +}) + +test_that("not death", { + cdm_local <- omock::mockCdmReference() |> + omock::mockPerson(n = 4) |> + omock::mockObservationPeriod() |> + omock::mockCohort(tableName = c("cohort1"), numberCohorts = 2, seed = 3) + cdm_local$death <- dplyr::tibble( + person_id = c(1,3), + death_date = as.Date(c("2013-06-29", "2015-10-11")), + death_type_concept_id = NA + ) + cdm <- CDMConnector::copy_cdm_to(con = DBI::dbConnect(duckdb::duckdb(), ":memory:"), + cdm = cdm_local, + schema = "main") + + cdm$cohort3 <- requireDeathFlag(x = cdm$cohort1, + window = c(0, Inf), + name = "cohort3", + negate = TRUE) + + expect_true(all(cdm$cohort3 |> dplyr::pull("subject_id") %in% c(2,4))) + expect_true(all( + cdm$cohort1 |> dplyr::filter(subject_id %in% c(2,4)) |> dplyr::pull("cohort_start_date") |> sort() == + cdm$cohort3 |> dplyr::pull("cohort_start_date") |> sort() + )) + expect_true(all(omopgenerics::attrition(cdm$cohort3)$reason == + c("Initial qualifying events", "Alive between 0 & Inf days relative to cohort_start_date", + "Initial qualifying events", "Alive between 0 & Inf days relative to cohort_start_date"))) + + CDMConnector::cdm_disconnect(cdm) +}) diff --git a/tests/testthat/test-requireTableIntersectFlag.R b/tests/testthat/test-requireTableIntersectFlag.R new file mode 100644 index 00000000..83c06cc3 --- /dev/null +++ b/tests/testthat/test-requireTableIntersectFlag.R @@ -0,0 +1,138 @@ +test_that("requiring presence in another table", { + cdm_local <- omock::mockCdmReference() |> + omock::mockPerson(n = 4) |> + omock::mockObservationPeriod() |> + omock::mockCohort(tableName = c("cohort1"), numberCohorts = 2) + 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")), + date_end = as.Date(c("2002-01-01", "2015-10-01", "2000-01-01")) + ) + cdm <- CDMConnector::copy_cdm_to(con = DBI::dbConnect(duckdb::duckdb(), ":memory:"), + cdm = cdm_local, + schema = "main") + + cdm$cohort2 <- requireTableIntersectFlag(x = cdm$cohort1, + tableName = "table", + targetStartDate = "date_start", + targetEndDate = "date_end", + window = c(-Inf, Inf), + name = "cohort2") + + expect_equal(cdm$cohort2 |> dplyr::pull("subject_id") |> sort(), + cdm$cohort1 |> dplyr::pull("subject_id") |> sort()) + expect_equal(omopgenerics::attrition(cdm$cohort2)$reason, + c("Initial qualifying events", + "In table table between -Inf & Inf days relative to cohort_start_date", + "Initial qualifying events", + "In table table between -Inf & Inf days relative to cohort_start_date")) + + cdm$cohort3 <- requireTableIntersectFlag(x = cdm$cohort1, + tableName = "table", + targetStartDate = "date_start", + 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("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"))) + expect_equal(omopgenerics::attrition(cdm$cohort3)$reason, + c("Initial qualifying events", + "In table table between 0 & Inf days relative to cohort_start_date", + "Initial qualifying events", + "In table table between 0 & Inf days relative to cohort_start_date")) + + cdm$cohort4 <- requireTableIntersectFlag(x = cdm$cohort1, + tableName = "table", + targetStartDate = "date_start", + targetEndDate = "date_end", + 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_equal(omopgenerics::attrition(cdm$cohort4)$reason, + c("Initial qualifying events", + "In table table between -Inf & 0 days relative to cohort_start_date, censoring at cohort_end_date", + "Initial qualifying events", + "In table table between -Inf & 0 days relative to cohort_start_date, censoring at cohort_end_date")) + + # expected errors + # currently just 1 table suported´ + expect_error( + requireTableIntersectFlag(x = cdm$cohort1, + tableName = c("table", "observation_period"), + window = c(-Inf, Inf)) + ) + expect_error( + requireTableIntersectFlag(x = cdm$cohort1, + tableName = cdm$table, + window = c(-Inf, Inf)) + ) + + CDMConnector::cdm_disconnect(cdm) +}) + +test_that("requiring absence in another table", { + cdm_local <- omock::mockCdmReference() |> + omock::mockPerson(n = 4) |> + omock::mockObservationPeriod() |> + omock::mockCohort(tableName = c("cohort1"), numberCohorts = 2) + 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")), + date_end = as.Date(c("2002-01-01", "2015-10-01", "2000-01-01")) + ) + cdm <- CDMConnector::copy_cdm_to(con = DBI::dbConnect(duckdb::duckdb(), ":memory:"), + cdm = cdm_local, + schema = "main") + + cdm$cohort2 <- requireTableIntersectFlag(x = cdm$cohort1, + tableName = "table", + targetStartDate = "date_start", + targetEndDate = "date_end", + window = c(-Inf, Inf), + name = "cohort2", + negate = TRUE) + + expect_true(cdm$cohort2 |> dplyr::pull("subject_id") |> length() == 0) + expect_equal(omopgenerics::attrition(cdm$cohort2)$reason, + c("Initial qualifying events", + "Not in table table between -Inf & Inf days relative to cohort_start_date", + "Initial qualifying events", + "Not in table table between -Inf & Inf days relative to cohort_start_date")) + + cdm$cohort3 <- requireTableIntersectFlag(x = cdm$cohort1, + tableName = "table", + targetStartDate = "date_start", + targetEndDate = "date_end", + window = c(0, Inf), + name = "cohort3", + negate = TRUE) + expect_true(all(cdm$cohort3 |> dplyr::pull("subject_id") == 1)) + expect_true(all(cdm$cohort3 |> dplyr::pull("cohort_start_date") == "2003-06-15")) + expect_equal(omopgenerics::attrition(cdm$cohort3)$reason, + c("Initial qualifying events", + "Not in table table between 0 & Inf days relative to cohort_start_date", + "Initial qualifying events", + "Not in table table between 0 & Inf days relative to cohort_start_date")) + + cdm$cohort4 <- requireTableIntersectFlag(x = cdm$cohort1, + tableName = "table", + targetStartDate = "date_start", + targetEndDate = "date_end", + window = c(-Inf, 0), + censorDate = "cohort_end_date", + name = "cohort4", + negate = TRUE) + expect_true(all(cdm$cohort4 |> dplyr::pull("subject_id") == c(1, 3, 4, 1, 1, 1, 3))) + 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")))) + 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", + "Initial qualifying events", + "Not in table table between -Inf & 0 days relative to cohort_start_date, censoring at cohort_end_date")) + + CDMConnector::cdm_disconnect(cdm) +})