-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
98198bb
commit e591642
Showing
10 changed files
with
165 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
.httr-oauth | ||
.DS_Store | ||
inst/doc | ||
.Rhistory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#' Summarise cohort overlap | ||
#' | ||
#' @param cohort A cohort table in a cdm reference | ||
#' @param restrictToFirstEntry If TRUE only an individual's first entry per | ||
#' cohort will be considered. If FALSE all entries per individual will be | ||
#' considered | ||
#' @param timing Summary statistics for timing. If NULL, timings between cohort | ||
#' entries will not be considered | ||
#' | ||
#' @return A summarised result | ||
#' @export | ||
#' | ||
#' @examples | ||
summariseCohortOverlap <- function(cohort, | ||
restrictToFirstEntry = TRUE, | ||
timing = c("min", "q25", | ||
"median","q75", | ||
"max")){ | ||
|
||
# validate inputs | ||
|
||
|
||
# add cohort names | ||
cdm <- omopgenerics::cdmReference(cohort) | ||
name <- omopgenerics:::getTableName(cohort) # change to tableName when og is released | ||
|
||
cdm[[name]] <- PatientProfiles::addCohortName(cdm[[name]]) | ||
|
||
if(isTRUE(restrictToFirstEntry)){ | ||
cdm[[name]] <- cdm[[name]] %>% | ||
restrictToFirstEntry() | ||
} | ||
|
||
# should we use addCohortIntersectDate instead to avoid potentially large number of rows? | ||
cdm[[name]] <- cdm[[name]] %>% | ||
dplyr::inner_join(cdm[[name]], | ||
by = "subject_id") %>% | ||
dplyr::rename("cohort_start_date" = "cohort_start_date.x", | ||
"cohort_end_date" = "cohort_end_date.x", | ||
"cohort_name" = "cohort_name.x", | ||
"cohort_definition_id" = "cohort_definition_id.x", | ||
"cohort_start_date_comparator" = "cohort_start_date.y", | ||
"cohort_end_date_comparator" = "cohort_end_date.y", | ||
"cohort_name_comparator" = "cohort_name.y", | ||
"cohort_definition_id_comparator" = "cohort_definition_id.y") %>% | ||
dplyr::mutate(comparison = as.character(paste0(as.character(.data$cohort_name), | ||
as.character(" &&& "), | ||
as.character(.data$cohort_name_comparator)))) | ||
|
||
name_overlap <- paste0(omopgenerics::uniqueTableName(), "_", name, "_overlap") | ||
|
||
cdm[[name_overlap]] <- cdm[[name]] %>% | ||
dplyr::compute(temporary = FALSE, | ||
name = name_overlap) %>% | ||
omopgenerics::newCohortTable(.softValidation = TRUE) | ||
|
||
if(is.null(timing)){ | ||
cohort_timings <- cdm[[name_overlap]] %>% | ||
PatientProfiles::summariseCharacteristics( | ||
strata = list("comparison")) %>% # can we only get number subject and records? | ||
dplyr::filter(.data$variable_name %in% c("Number subjects", | ||
"Number records")) %>% | ||
dplyr::mutate(result_type = "cohort_overlap") | ||
|
||
return(cohort_timings) | ||
|
||
} | ||
|
||
cohort_timings <- cdm[[name_overlap]] %>% | ||
dplyr::mutate(diff_days = !!CDMConnector::datediff("cohort_start_date", | ||
"cohort_start_date_comparator", | ||
interval = "day")) %>% | ||
dplyr::collect() %>% | ||
PatientProfiles::summariseResult(group=list("comparison"), | ||
variables = list(diff_days = "diff_days"), | ||
functions = list(diff_days = timing))%>% | ||
dplyr::mutate(result_type = "cohort_overlap") | ||
|
||
cohort_timings | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
test_that("expected output", { | ||
cdm <- DrugUtilisation::generateConceptCohortSet( | ||
cdm = DrugUtilisation::mockDrugUtilisation(numberIndividuals = 200), | ||
conceptSet = list(c_1 = 317009, c_2 = 432526, c_3 = 4141052), | ||
name = "cohort", | ||
end = "observation_period_end_date" | ||
) | ||
|
||
overlap1 <- summariseCohortOverlap(cdm$cohort, | ||
restrictToFirstEntry = TRUE, | ||
timing = c("min", "q25", | ||
"median","q75", | ||
"max")) | ||
expect_equal(colnames(omopgenerics::emptySummarisedResult()), | ||
colnames(overlap1)) | ||
|
||
overlap2 <- summariseCohortOverlap(cdm$cohort, | ||
restrictToFirstEntry = FALSE, | ||
timing = c("min", "q25", | ||
"median","q75", | ||
"max")) | ||
expect_equal(colnames(omopgenerics::emptySummarisedResult()), | ||
colnames(overlap2)) | ||
|
||
|
||
overlap3 <- summariseCohortOverlap(cdm$cohort, | ||
restrictToFirstEntry = TRUE, | ||
timing = c("min", | ||
"max")) | ||
expect_equal(colnames(omopgenerics::emptySummarisedResult()), | ||
colnames(overlap3)) | ||
|
||
overlap4 <- summariseCohortOverlap(cdm$cohort, | ||
restrictToFirstEntry = TRUE, | ||
timing = NULL) | ||
expect_equal(colnames(omopgenerics::emptySummarisedResult()), | ||
colnames(overlap4)) | ||
|
||
CDMConnector::cdm_disconnect(cdm) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.