diff --git a/NAMESPACE b/NAMESPACE index 3659d2d4..0681df36 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -17,7 +17,7 @@ export(createTable1) export(createTable1CovariateSettings) export(createTemporalCovariateSettings) export(createTemporalSequenceCovariateSettings) -export(filterByCohortDefinitionId) +export(filterByCohortDefinitionIds) export(filterByRowId) export(getDbCohortAttrCovariatesData) export(getDbCohortBasedCovariatesData) diff --git a/R/Aggregation.R b/R/Aggregation.R index fcde5b25..6d282680 100644 --- a/R/Aggregation.R +++ b/R/Aggregation.R @@ -26,7 +26,7 @@ #' @examples #' \dontrun{ #' covariateData <- FeatureExtraction:::createEmptyCovariateData( -#' cohortId = 1, +#' cohortIds = 1, #' aggregated = FALSE, #' temporal = FALSE #' ) diff --git a/R/CovariateData.R b/R/CovariateData.R index 5b9ca7ec..cc83cc47 100644 --- a/R/CovariateData.R +++ b/R/CovariateData.R @@ -55,7 +55,7 @@ setClass("CovariateData", contains = "Andromeda") #' @examples #' \dontrun{ #' covariateData <- FeatureExtraction:::createEmptyCovariateData( -#' cohortId = 1, +#' cohortIds = 1, #' aggregated = FALSE, #' temporal = FALSE #' ) @@ -128,13 +128,15 @@ loadCovariateData <- function(file, readOnly) { setMethod("show", "CovariateData", function(object) { cli::cat_line(pillar::style_subtle("# CovariateData object")) cli::cat_line("") - cohortId <- attr(object, "metaData")$cohortId - if (length(cohortId) > 1) { - cli::cat_line(paste("Cohorts of interest IDs:", paste(cohortId, collapse = ", "))) - } else if (cohortId == -1) { - cli::cat_line("All cohorts") - } else { - cli::cat_line(paste("Cohort of interest ID:", cohortId)) + cohortIds <- attr(object, "metaData")$cohortIds + if (!is.null(cohortIds)) { + if (length(cohortIds) > 1) { + cli::cat_line(paste("Cohorts of interest IDs:", paste(cohortIds, collapse = ", "))) + } else if (cohortIds == -1) { + cli::cat_line("All cohorts") + } else { + cli::cat_line(paste("Cohort of interest ID:", cohortIds)) + } } cli::cat_line("") cli::cat_line(pillar::style_subtle("Inherits from Andromeda:")) @@ -204,7 +206,7 @@ isCovariateData <- function(x) { #' @examples #' \dontrun{ #' covariateData <- FeatureExtraction:::createEmptyCovariateData( -#' cohortId = 1, +#' cohortIds = 1, #' aggregated = FALSE, #' temporal = FALSE #' ) @@ -232,7 +234,7 @@ isAggregatedCovariateData <- function(x) { #' @examples #' \dontrun{ #' covariateData <- FeatureExtraction:::createEmptyCovariateData( -#' cohortId = 1, +#' cohortIds = 1, #' aggregated = FALSE, #' temporal = FALSE #' ) @@ -252,20 +254,21 @@ isTemporalCovariateData <- function(x) { #' Creates an empty covariate data object #' -#' @param cohortId cohort number +#' @param cohortIds For which cohort IDs should the covariate data be created? #' @param aggregated if the data should be aggregated #' @param temporal if the data is temporary #' #' @examples #' \dontrun{ #' covariateData <- FeatureExtraction:::createEmptyCovariateData( -#' cohortId = 1, +#' cohortIds = 1, #' aggregated = FALSE, #' temporal = FALSE #' ) #' } +#' @return the empty CovariateData object #' -createEmptyCovariateData <- function(cohortId, aggregated, temporal) { +createEmptyCovariateData <- function(cohortIds, aggregated, temporal) { dummy <- tibble( covariateId = 1, covariateValue = 1 @@ -276,28 +279,20 @@ createEmptyCovariateData <- function(cohortId, aggregated, temporal) { if (!is.null(temporal) && temporal) { dummy$timeId <- 1 } - covariateData <- Andromeda::andromeda( - covariates = dummy[!1, ], - covariateRef = tibble( - covariateId = 1, - covariateName = "", - analysisId = 1, - conceptId = 1 - )[!1, ], - analysisRef = tibble( - analysisId = 1, - analysisName = "", - domainId = "", - startDay = 1, - endDay = 1, - isBinary = "", - missingMeansZero = "" - )[!1, ] - ) - attr(covariateData, "metaData") <- list( - populationSize = 0, - cohortId = cohortId - ) + covariateData <- Andromeda::andromeda(covariates = dummy[!1, ], + covariateRef = tibble(covariateId = 1, + covariateName = "", + analysisId = 1, + conceptId = 1)[!1, ], + analysisRef = tibble(analysisId = 1, + analysisName = "", + domainId = "", + startDay = 1, + endDay = 1, + isBinary = "", + missingMeansZero = "")[!1, ]) + attr(covariateData, "metaData") <- list(populationSize = 0, + cohortIds = cohortIds) class(covariateData) <- "CovariateData" return(covariateData) } diff --git a/R/GetCovariates.R b/R/GetCovariates.R index f23c47a9..379b3959 100644 --- a/R/GetCovariates.R +++ b/R/GetCovariates.R @@ -46,7 +46,10 @@ #' specify both the database and the schema, so for example #' 'cdm_instance.dbo'. #' @param cohortTableIsTemp Is the cohort table a temp table? -#' @param cohortId For which cohort ID(s) should covariates be constructed? If set to -1, +#' @param cohortId DEPRECATED:For which cohort ID(s) should covariates be constructed? If set to -1, +#' covariates will be constructed for all cohorts in the specified cohort +#' table. +#' @param cohortIds For which cohort ID(s) should covariates be constructed? If set to c(-1), #' covariates will be constructed for all cohorts in the specified cohort #' table. #' @param rowIdField The name of the field in the cohort table that is to be used as the @@ -78,7 +81,7 @@ #' cohortTable = "cohort", #' cohortDatabaseSchema = "main", #' cohortTableIsTemp = FALSE, -#' cohortId = -1, +#' cohortIds = -1, #' rowIdField = "subject_id", #' covariateSettings = covSettings, #' aggregated = FALSE @@ -95,6 +98,7 @@ getDbCovariateData <- function(connectionDetails = NULL, cohortDatabaseSchema = cdmDatabaseSchema, cohortTableIsTemp = FALSE, cohortId = -1, + cohortIds = c(-1), rowIdField = "subject_id", covariateSettings, aggregated = FALSE) { @@ -107,6 +111,10 @@ getDbCovariateData <- function(connectionDetails = NULL, if (cdmVersion == "4") { stop("CDM version 4 is not supported any more") } + if (!missing(cohortId)) { + warning("cohortId argument has been deprecated, please use cohortIds") + cohortIds <- cohortId + } if (!is.null(connectionDetails)) { connection <- DatabaseConnector::connect(connectionDetails) on.exit(DatabaseConnector::disconnect(connection)) @@ -120,17 +128,13 @@ getDbCovariateData <- function(connectionDetails = NULL, } else { cohortDatabaseSchemaTable <- paste(cohortDatabaseSchema, cohortTable, sep = ".") } - sql <- "SELECT cohort_definition_id, COUNT_BIG(*) AS population_size FROM @cohort_database_schema_table {@cohort_id != -1} ? {WHERE cohort_definition_id IN (@cohort_id)} GROUP BY cohort_definition_id;" - sql <- SqlRender::render( - sql = sql, - cohort_database_schema_table = cohortDatabaseSchemaTable, - cohort_id = cohortId - ) - sql <- SqlRender::translate( - sql = sql, - targetDialect = attr(connection, "dbms"), - oracleTempSchema = oracleTempSchema - ) + sql <- "SELECT cohort_definition_id, COUNT_BIG(*) AS population_size FROM @cohort_database_schema_table {@cohort_ids != -1} ? {WHERE cohort_definition_id IN (@cohort_ids)} GROUP BY cohort_definition_id;" + sql <- SqlRender::render(sql = sql, + cohort_database_schema_table = cohortDatabaseSchemaTable, + cohort_ids = cohortIds) + sql <- SqlRender::translate(sql = sql, + targetDialect = attr(connection, "dbms"), + oracleTempSchema = oracleTempSchema) temp <- DatabaseConnector::querySql(connection, sql, snakeCaseToCamelCase = TRUE) if (aggregated) { populationSize <- temp$populationSize @@ -139,7 +143,7 @@ getDbCovariateData <- function(connectionDetails = NULL, populationSize <- sum(temp$populationSize) } if (sum(populationSize) == 0) { - covariateData <- createEmptyCovariateData(cohortId, aggregated, covariateSettings$temporal) + covariateData <- createEmptyCovariateData(cohortIds, aggregated, covariateSettings$temporal) warning("Population is empty. No covariates were constructed") } else { if (inherits(covariateSettings, "covariateSettings")) { @@ -152,17 +156,15 @@ getDbCovariateData <- function(connectionDetails = NULL, } for (i in 1:length(covariateSettings)) { fun <- attr(covariateSettings[[i]], "fun") - args <- list( - connection = connection, - oracleTempSchema = oracleTempSchema, - cdmDatabaseSchema = cdmDatabaseSchema, - cohortTable = cohortDatabaseSchemaTable, - cohortId = cohortId, - cdmVersion = cdmVersion, - rowIdField = rowIdField, - covariateSettings = covariateSettings[[i]], - aggregated = aggregated - ) + args <- list(connection = connection, + oracleTempSchema = oracleTempSchema, + cdmDatabaseSchema = cdmDatabaseSchema, + cohortTable = cohortDatabaseSchemaTable, + cohortIds = cohortIds, + cdmVersion = cdmVersion, + rowIdField = rowIdField, + covariateSettings = covariateSettings[[i]], + aggregated = aggregated) tempCovariateData <- do.call(eval(parse(text = fun)), args) if (is.null(covariateData)) { covariateData <- tempCovariateData @@ -197,7 +199,7 @@ getDbCovariateData <- function(connectionDetails = NULL, } } attr(covariateData, "metaData")$populationSize <- populationSize - attr(covariateData, "metaData")$cohortId <- cohortId + attr(covariateData, "metaData")$cohortIds <- cohortIds } return(covariateData) } diff --git a/R/GetCovariatesFromCohortAttributes.R b/R/GetCovariatesFromCohortAttributes.R index e4a3b61f..2489e56b 100644 --- a/R/GetCovariatesFromCohortAttributes.R +++ b/R/GetCovariatesFromCohortAttributes.R @@ -61,7 +61,7 @@ #' cdmDatabaseSchema = "main", #' cdmVersion = "5", #' cohortTable = "cohort", -#' cohortId = 1, +#' cohortIds = 1, #' rowIdField = "subject_id", #' covariateSettings = covariateSettings, #' aggregated = FALSE @@ -74,6 +74,7 @@ getDbCohortAttrCovariatesData <- function(connection, cdmDatabaseSchema, cohortTable = "#cohort_person", cohortId = -1, + cohortIds = c(-1), cdmVersion = "5", rowIdField = "subject_id", covariateSettings, @@ -84,6 +85,10 @@ getDbCohortAttrCovariatesData <- function(connection, if (cdmVersion == "4") { stop("Common Data Model version 4 is not supported") } + if (!missing(cohortId)) { + warning("cohortId argument has been deprecated, please use cohortIds") + cohortIds <- cohortId + } start <- Sys.time() writeLines("Constructing covariates from cohort attributes table") @@ -154,11 +159,11 @@ getDbCohortAttrCovariatesData <- function(connection, delta <- Sys.time() - start writeLines(paste("Loading took", signif(delta, 3), attr(delta, "units"))) - result <- createEmptyCovariateData(cohortId, aggregated, covariateSettings$temporal) - result$covariates <- covariates - result$covariateRef <- covariateRef - result$analysisRef <- analysisRef - + result <- createEmptyCovariateData(cohortIds, aggregated, covariateSettings$temporal) + result$covariates = covariates + result$covariateRef = covariateRef + result$analysisRef = analysisRef + return(result) } diff --git a/R/GetCovariatesFromOtherCohorts.R b/R/GetCovariatesFromOtherCohorts.R index a92a3a2b..f7f8e68c 100644 --- a/R/GetCovariatesFromOtherCohorts.R +++ b/R/GetCovariatesFromOtherCohorts.R @@ -31,6 +31,7 @@ getDbCohortBasedCovariatesData <- function(connection, cdmDatabaseSchema, cohortTable = "#cohort_person", cohortId = -1, + cohortIds = c(-1), cdmVersion = "5", rowIdField = "subject_id", covariateSettings, @@ -46,6 +47,10 @@ getDbCohortBasedCovariatesData <- function(connection, checkmate::assertClass(covariateSettings, "covariateSettings", add = errorMessages) checkmate::assertLogical(aggregated, len = 1, add = errorMessages) checkmate::reportAssertions(collection = errorMessages) + if (!missing(cohortId)) { + warning("cohortId argument has been deprecated, please use cohortIds") + cohortIds <- cohortId + } start <- Sys.time() message("Constructing covariates from other cohorts") @@ -130,10 +135,10 @@ getDbCohortBasedCovariatesData <- function(connection, oracleTempSchema = oracleTempSchema, cdmDatabaseSchema = cdmDatabaseSchema, cohortTable = cohortTable, - cohortId = cohortId, + cohortIds = cohortIds, cdmVersion = cdmVersion, rowIdField = rowIdField, - detailledSettings, + covariateSettings = detailledSettings, aggregated = aggregated ) diff --git a/R/GetDefaultCovariates.R b/R/GetDefaultCovariates.R index a136788d..706fd89b 100644 --- a/R/GetDefaultCovariates.R +++ b/R/GetDefaultCovariates.R @@ -57,6 +57,7 @@ getDbDefaultCovariateData <- function(connection, cdmDatabaseSchema, cohortTable = "#cohort_person", cohortId = -1, + cohortIds = c(-1), cdmVersion = "5", rowIdField = "subject_id", covariateSettings, @@ -74,10 +75,13 @@ getDbDefaultCovariateData <- function(connection, if (!missing(targetCovariateTable) && !is.null(targetCovariateTable) && aggregated) { stop("Writing aggregated results to database is currently not supported") } - + if (!missing(cohortId)) { + warning("cohortId argument has been deprecated, please use cohortIds") + cohortIds <- cohortId + } settings <- .toJson(covariateSettings) rJava::J("org.ohdsi.featureExtraction.FeatureExtraction")$init(system.file("", package = "FeatureExtraction")) - json <- rJava::J("org.ohdsi.featureExtraction.FeatureExtraction")$createSql(settings, aggregated, cohortTable, rowIdField, rJava::.jarray(as.character(cohortId)), cdmDatabaseSchema) + json <- rJava::J("org.ohdsi.featureExtraction.FeatureExtraction")$createSql(settings, aggregated, cohortTable, rowIdField, rJava::.jarray(as.character(cohortIds)), cdmDatabaseSchema) todo <- .fromJson(json) if (length(todo$tempTables) != 0) { ParallelLogger::logInfo("Sending temp tables to server") @@ -260,11 +264,9 @@ getDbDefaultCovariateData <- function(connection, attr(covariateData, "metaData") <- list() if (is.null(covariateData$covariates) && is.null(covariateData$covariatesContinuous)) { warning("No data found, probably because no covariates were specified.") - covariateData <- createEmptyCovariateData( - cohortId = cohortId, - aggregated = aggregated, - temporal = covariateSettings$temporal - ) + covariateData <- createEmptyCovariateData(cohortIds = cohortIds, + aggregated = aggregated, + temporal = covariateSettings$temporal) } class(covariateData) <- "CovariateData" attr(class(covariateData), "package") <- "FeatureExtraction" diff --git a/R/HelperFunctions.R b/R/HelperFunctions.R index 73c697df..e66842d6 100644 --- a/R/HelperFunctions.R +++ b/R/HelperFunctions.R @@ -25,7 +25,7 @@ #' @examples #' \dontrun{ #' covariateData <- FeatureExtraction:::createEmptyCovariateData( -#' cohortId = 1, +#' cohortIds = 1, #' aggregated = FALSE, #' temporal = FALSE #' ) @@ -62,10 +62,10 @@ filterByRowId <- function(covariateData, rowIds) { return(result) } -#' Filter covariates by cohort definition ID +#' Filter covariates by cohort definition IDs #' #' @param covariateData An object of type \code{CovariateData} -#' @param cohortId The cohort definition ID to keep. +#' @param cohortIds The cohort definition IDs to keep. #' #' @return #' An object of type \code{covariateData}. @@ -73,19 +73,19 @@ filterByRowId <- function(covariateData, rowIds) { #' @examples #' \dontrun{ #' covariateData <- FeatureExtraction:::createEmptyCovariateData( -#' cohortId = 1, +#' cohortIds = 1, #' aggregated = FALSE, #' temporal = FALSE #' ) #' -#' covData <- filterByCohortDefinitionId( +#' covData <- filterByCohortDefinitionIds( #' covariateData = covariateData, -#' cohortId = 1 +#' cohortIds = c(1) #' ) #' } #' #' @export -filterByCohortDefinitionId <- function(covariateData, cohortId) { +filterByCohortDefinitionIds <- function(covariateData, cohortIds) { if (!isCovariateData(covariateData)) { stop("Data not of class CovariateData") } @@ -93,19 +93,19 @@ filterByCohortDefinitionId <- function(covariateData, cohortId) { stop("CovariateData object is closed") } if (!isAggregatedCovariateData(covariateData)) { - stop("Can only filter aggregated data by cohortId") + stop("Can only filter aggregated data by cohortIds") } if (is.null(covariateData$covariates)) { covariates <- NULL } else { covariates <- covariateData$covariates %>% - filter(.data$cohortDefinitionId %in% cohortId) + filter(.data$cohortDefinitionId %in% cohortIds) } if (is.null(covariateData$covariatesContinuous)) { covariatesContinuous <- NULL } else { covariatesContinuous <- covariateData$covariatesContinuous %>% - filter(.data$cohortDefinitionId %in% cohortId) + filter(.data$cohortDefinitionId %in% cohortIds) } result <- Andromeda::andromeda( covariates = covariates, @@ -114,7 +114,7 @@ filterByCohortDefinitionId <- function(covariateData, cohortId) { analysisRef = covariateData$analysisRef ) metaData <- attr(covariateData, "metaData") - metaData$populationSize <- metaData$populationSize[as.numeric(names(metaData$populationSize)) %in% cohortId] + metaData$populationSize <- metaData$populationSize[as.numeric(names(metaData$populationSize)) %in% cohortIds] attr(result, "metaData") <- metaData class(result) <- "CovariateData" attr(class(result), "package") <- "FeatureExtraction" diff --git a/R/Normalization.R b/R/Normalization.R index 2c59ab8c..4289b11e 100644 --- a/R/Normalization.R +++ b/R/Normalization.R @@ -31,7 +31,7 @@ #' @examples #' \dontrun{ #' covariateData <- FeatureExtraction:::createEmptyCovariateData( -#' cohortId = 1, +#' cohortIds = 1, #' aggregated = FALSE, #' temporal = FALSE #' ) diff --git a/R/UnitTestHelperFunctions.R b/R/UnitTestHelperFunctions.R index a31560aa..cd3c3117 100644 --- a/R/UnitTestHelperFunctions.R +++ b/R/UnitTestHelperFunctions.R @@ -48,7 +48,7 @@ #' 'cdm_instance.dbo'. #' @param cohortTable Name of the (temp) table holding the cohort for which we want to #' construct covariates -#' @param cohortId For which cohort ID(s) should covariates be constructed? If set to -1, +#' @param cohortIds For which cohort ID(s) should covariates be constructed? If set to -1, #' covariates will be constructed for all cohorts in the specified cohort #' table. #' @param cdmVersion Define the OMOP CDM version used: currently supported is "5". @@ -90,7 +90,7 @@ oracleTempSchema = NULL, cdmDatabaseSchema, cohortTable = "#cohort_person", - cohortId = -1, + cohortIds = c(-1), cdmVersion = "5", rowIdField = "subject_id", covariateSettings, @@ -104,23 +104,20 @@ } # Some SQL to construct the covariate: - sql <- paste( - "SELECT @row_id_field AS row_id, 1 AS covariate_id,", - "DATEDIFF(DAY, observation_period_start_date, cohort_start_date)", - "AS covariate_value", - "FROM @cohort_table c", - "INNER JOIN @cdm_database_schema.observation_period op", - "ON op.person_id = c.subject_id", - "WHERE cohort_start_date >= observation_period_start_date", - "AND cohort_start_date <= observation_period_end_date", - "{@cohort_id != -1} ? {AND cohort_definition_id = @cohort_id}" - ) + sql <- paste("SELECT @row_id_field AS row_id, 1 AS covariate_id,", + "DATEDIFF(DAY, observation_period_start_date, cohort_start_date)", + "AS covariate_value", + "FROM @cohort_table c", + "INNER JOIN @cdm_database_schema.observation_period op", + "ON op.person_id = c.subject_id", + "WHERE cohort_start_date >= observation_period_start_date", + "AND cohort_start_date <= observation_period_end_date", + "{@cohort_ids != -1} ? {AND cohort_definition_id IN @cohort_ids}") sql <- SqlRender::render(sql, - cohort_table = cohortTable, - cohort_id = cohortId, - row_id_field = rowIdField, - cdm_database_schema = cdmDatabaseSchema - ) + cohort_table = cohortTable, + cohort_ids = cohortIds, + row_id_field = rowIdField, + cdm_database_schema = cdmDatabaseSchema) sql <- SqlRender::translate(sql, targetDialect = attr(connection, "dbms")) # Retrieve the covariate: covariates <- DatabaseConnector::querySql(connection, sql, snakeCaseToCamelCase = TRUE) diff --git a/extras/CohortBasedCovariatesVignetteDataFetch.R b/extras/CohortBasedCovariatesVignetteDataFetch.R index 7c02e6ea..ea9b06de 100644 --- a/extras/CohortBasedCovariatesVignetteDataFetch.R +++ b/extras/CohortBasedCovariatesVignetteDataFetch.R @@ -65,7 +65,7 @@ covariateData <- getDbCovariateData(connectionDetails = connectionDetails, cdmDatabaseSchema = cdmDatabaseSchema, cohortDatabaseSchema = cohortDatabaseSchema, cohortTable = cohortTable, - cohortId = 1, + cohortIds = c(1), rowIdField = "subject_id", covariateSettings = covariateSettings) @@ -97,7 +97,7 @@ covariateData <- getDbCovariateData(connectionDetails = connectionDetails, cdmDatabaseSchema = cdmDatabaseSchema, cohortDatabaseSchema = cohortDatabaseSchema, cohortTable = cohortTable, - cohortId = 1, + cohortIds = c(1), rowIdField = "subject_id", covariateSettings = covariateSettingsList, aggregated = TRUE) diff --git a/extras/GetHdpsCovariates.R b/extras/GetHdpsCovariates.R index 36214128..2656dd10 100644 --- a/extras/GetHdpsCovariates.R +++ b/extras/GetHdpsCovariates.R @@ -37,11 +37,16 @@ getDbHdpsCovariateData <- function(connection, cdmDatabaseSchema, cohortTable = "cohort_person", cohortId = -1, + cohortIds = c(-1), cdmVersion = "5", rowIdField = "subject_id", covariateSettings, aggregated = FALSE) { - if (cohortId != -1) + if (!missing(cohortId)) { + warning("cohortId argument has been deprecated, please use cohortIds") + cohortIds <- cohortId + } + if (cohortIds != -1) stop("Haven't implemented restricting to cohort ID yet.") if (aggregated) stop("Aggregation not implemented yet") diff --git a/extras/TestCode.R b/extras/TestCode.R index 9a49ee81..a9ba4e9b 100644 --- a/extras/TestCode.R +++ b/extras/TestCode.R @@ -188,7 +188,7 @@ covs <- getDbCovariateData(connectionDetails = connectionDetails, cdmDatabaseSchema = cdmDatabaseSchema, cohortDatabaseSchema = cohortDatabaseSchema, cohortTable = cohortTable, - cohortId = c(740910, 945286), + cohortIds = c(740910, 945286), rowIdField = "row_id", cohortTableIsTemp = FALSE, covariateSettings = settings, @@ -222,7 +222,7 @@ covs <- getDbCovariateData(connectionDetails = connectionDetails, cdmDatabaseSchema = cdmDatabaseSchema, cohortDatabaseSchema = cohortDatabaseSchema, cohortTable = cohortTable, - cohortId = 1, + cohortIds = c(1), rowIdField = "row_id", cohortTableIsTemp = FALSE, covariateSettings = list(settings, settings2), @@ -238,7 +238,7 @@ covs <- getDbCovariateData(connectionDetails = connectionDetails, cdmDatabaseSchema = cdmDatabaseSchema, cohortDatabaseSchema = cohortDatabaseSchema, cohortTable = cohortTable, - cohortId = 1, + cohortIds = c(1), rowIdField = "row_id", cohortTableIsTemp = FALSE, covariateSettings = covariateSettings, @@ -261,7 +261,7 @@ covs <- getDbCovariateData(connectionDetails = connectionDetails, cdmDatabaseSchema = cdmDatabaseSchema, cohortDatabaseSchema = cohortDatabaseSchema, cohortTable = cohortTable, - cohortId = 1, + cohortIds = c(1), rowIdField = "row_id", cohortTableIsTemp = FALSE, covariateSettings = covariateSettings, @@ -274,7 +274,7 @@ aggCovs <- getDbCovariateData(connectionDetails = connectionDetails, cdmDatabaseSchema = cdmDatabaseSchema, cohortDatabaseSchema = cohortDatabaseSchema, cohortTable = cohortTable, - cohortId = 1, + cohortIds = c(1), rowIdField = "row_id", cohortTableIsTemp = FALSE, covariateSettings = covariateSettings, @@ -304,7 +304,7 @@ getDbDefaultCovariateData(connection = conn, oracleTempSchema = oracleTempSchema, cdmDatabaseSchema = cdmDatabaseSchema, cohortTable = paste(cohortDatabaseSchema, cohortTable, sep = "."), - cohortId = -1, + cohortIds = c(-1), rowIdField = "row_id", covariateSettings = settings, targetCovariateTable = "#my_covs", @@ -402,7 +402,7 @@ covs <- getDbCovariateData(connectionDetails = connectionDetails, cdmDatabaseSchema = cdmDatabaseSchema, cohortDatabaseSchema = cohortDatabaseSchema, cohortTable = cohortTable, - cohortId = 1, + cohortIds = c(1), rowIdField = "row_id", cohortTableIsTemp = FALSE, covariateSettings = settings, @@ -541,7 +541,7 @@ covs <- getDbCovariateData(connectionDetails = connectionDetails, cdmDatabaseSchema = "main", cohortDatabaseSchema = "main", cohortTable = "cohort", - cohortId = c(2^34), + cohortIds = c(2^34), covariateSettings = settings, aggregated = TRUE) @@ -595,7 +595,7 @@ covs <- getDbCovariateData(connectionDetails = connectionDetails, cdmDatabaseSchema = "main", cohortDatabaseSchema = "main", cohortTable = "cohort", - cohortId = c(1,2), + cohortIds = c(1,2), covariateSettings = settings, aggregated = TRUE) diff --git a/extras/VignetteDataFetch.R b/extras/VignetteDataFetch.R index f39e3156..e1f6b206 100644 --- a/extras/VignetteDataFetch.R +++ b/extras/VignetteDataFetch.R @@ -72,7 +72,7 @@ covariateData <- getDbCovariateData(connection = connection, cohortDatabaseSchema = resultsDatabaseSchema, cohortTable = cohortsTable, cohortTableIsTemp = TRUE, - cohortId = 1118084, + cohortIds = c(1118084), rowIdField = "subject_id", covariateSettings = covariateSettings) @@ -98,7 +98,7 @@ covariateData2 <- getDbCovariateData(connection = connection, cohortDatabaseSchema = resultsDatabaseSchema, cohortTable = cohortsTable, cohortTableIsTemp = TRUE, - cohortId = 1118084, + cohortIds = c(1118084), covariateSettings = covariateSettings, aggregated = TRUE) @@ -115,7 +115,7 @@ covariateData2b <- getDbCovariateData(connection = connection, cohortDatabaseSchema = resultsDatabaseSchema, cohortTable = cohortsTable, cohortTableIsTemp = TRUE, - cohortId = 1118084, + cohortIds = c(1118084), covariateSettings = covariateSettings, aggregated = TRUE) @@ -132,7 +132,7 @@ covDiclofenac <- getDbCovariateData(connection = connection, cohortDatabaseSchema = resultsDatabaseSchema, cohortTable = cohortsTable, cohortTableIsTemp = TRUE, - cohortId = 1124300, + cohortIds = c(1124300), covariateSettings = covariateSettings, aggregated = TRUE) @@ -143,10 +143,10 @@ covCelecoxib <- getDbCovariateData(connection = connection, cohortDatabaseSchema = resultsDatabaseSchema, cohortTable = cohortsTable, cohortTableIsTemp = TRUE, - cohortId = 1118084, + cohortIds = c(1118084), covariateSettings = covariateSettings, aggregated = TRUE) saveCovariateData(covCelecoxib, file.path(vignetteFolder, "covCelecoxib")) -DatabaseConnector::disconnect(connection) +DatabaseConnector::disconnect(connection) \ No newline at end of file diff --git a/man-roxygen/GetCovarParams.R b/man-roxygen/GetCovarParams.R index ff44ad26..5feb35b8 100644 --- a/man-roxygen/GetCovarParams.R +++ b/man-roxygen/GetCovarParams.R @@ -16,7 +16,10 @@ #' covariates. If it is a temp table, the name should have a hash prefix, #' e.g. '#temp_table'. If it is a non-temp table, it should include the #' database schema, e.g. 'cdm_database.cohort'. -#' @param cohortId For which cohort ID should covariates be constructed? If set to -1, +#' @param cohortId DEPRECATED:For which cohort ID should covariates be constructed? If set to -1, +#' covariates will be constructed for all cohorts in the specified cohort +#' table. +#' @param cohortIds For which cohort ID(s) should covariates be constructed? If set to c(-1), #' covariates will be constructed for all cohorts in the specified cohort #' table. #' @param cdmVersion The version of the Common Data Model used. Currently only diff --git a/man/aggregateCovariates.Rd b/man/aggregateCovariates.Rd index 30a5ea24..911c557e 100644 --- a/man/aggregateCovariates.Rd +++ b/man/aggregateCovariates.Rd @@ -19,7 +19,7 @@ Aggregate covariate data \examples{ \dontrun{ covariateData <- FeatureExtraction:::createEmptyCovariateData( - cohortId = 1, + cohortIds = 1, aggregated = FALSE, temporal = FALSE ) diff --git a/man/createEmptyCovariateData.Rd b/man/createEmptyCovariateData.Rd index 0dc2e7e4..dc2a2566 100644 --- a/man/createEmptyCovariateData.Rd +++ b/man/createEmptyCovariateData.Rd @@ -4,25 +4,27 @@ \alias{createEmptyCovariateData} \title{Creates an empty covariate data object} \usage{ -createEmptyCovariateData(cohortId, aggregated, temporal) +createEmptyCovariateData(cohortIds, aggregated, temporal) } \arguments{ -\item{cohortId}{cohort number} +\item{cohortIds}{For which cohort IDs should the covariate data be created?} \item{aggregated}{if the data should be aggregated} \item{temporal}{if the data is temporary} } +\value{ +the empty CovariateData object +} \description{ Creates an empty covariate data object } \examples{ \dontrun{ covariateData <- FeatureExtraction:::createEmptyCovariateData( - cohortId = 1, + cohortIds = 1, aggregated = FALSE, temporal = FALSE ) } - } diff --git a/man/dot-getDbLooCovariateData.Rd b/man/dot-getDbLooCovariateData.Rd index 87b78d96..eafd3be7 100644 --- a/man/dot-getDbLooCovariateData.Rd +++ b/man/dot-getDbLooCovariateData.Rd @@ -9,7 +9,7 @@ oracleTempSchema = NULL, cdmDatabaseSchema, cohortTable = "#cohort_person", - cohortId = -1, + cohortIds = c(-1), cdmVersion = "5", rowIdField = "subject_id", covariateSettings, @@ -32,7 +32,7 @@ specify both the database and the schema, so for example \item{cohortTable}{Name of the (temp) table holding the cohort for which we want to construct covariates} -\item{cohortId}{For which cohort ID(s) should covariates be constructed? If set to -1, +\item{cohortIds}{For which cohort ID(s) should covariates be constructed? If set to -1, covariates will be constructed for all cohorts in the specified cohort table.} diff --git a/man/filterByCohortDefinitionId.Rd b/man/filterByCohortDefinitionIds.Rd similarity index 54% rename from man/filterByCohortDefinitionId.Rd rename to man/filterByCohortDefinitionIds.Rd index 792f2cf1..09eeb556 100644 --- a/man/filterByCohortDefinitionId.Rd +++ b/man/filterByCohortDefinitionIds.Rd @@ -1,33 +1,33 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/HelperFunctions.R -\name{filterByCohortDefinitionId} -\alias{filterByCohortDefinitionId} -\title{Filter covariates by cohort definition ID} +\name{filterByCohortDefinitionIds} +\alias{filterByCohortDefinitionIds} +\title{Filter covariates by cohort definition IDs} \usage{ -filterByCohortDefinitionId(covariateData, cohortId) +filterByCohortDefinitionIds(covariateData, cohortIds) } \arguments{ \item{covariateData}{An object of type \code{CovariateData}} -\item{cohortId}{The cohort definition ID to keep.} +\item{cohortIds}{The cohort definition IDs to keep.} } \value{ An object of type \code{covariateData}. } \description{ -Filter covariates by cohort definition ID +Filter covariates by cohort definition IDs } \examples{ \dontrun{ covariateData <- FeatureExtraction:::createEmptyCovariateData( - cohortId = 1, + cohortIds = 1, aggregated = FALSE, temporal = FALSE ) -covData <- filterByCohortDefinitionId( +covData <- filterByCohortDefinitionIds( covariateData = covariateData, - cohortId = 1 + cohortIds = c(1) ) } diff --git a/man/filterByRowId.Rd b/man/filterByRowId.Rd index 8c8a6c4c..bf673e5f 100644 --- a/man/filterByRowId.Rd +++ b/man/filterByRowId.Rd @@ -20,7 +20,7 @@ Filter covariates by row ID \examples{ \dontrun{ covariateData <- FeatureExtraction:::createEmptyCovariateData( - cohortId = 1, + cohortIds = 1, aggregated = FALSE, temporal = FALSE ) diff --git a/man/getDbCohortAttrCovariatesData.Rd b/man/getDbCohortAttrCovariatesData.Rd index 35d79a5f..aa2a0efb 100644 --- a/man/getDbCohortAttrCovariatesData.Rd +++ b/man/getDbCohortAttrCovariatesData.Rd @@ -10,6 +10,7 @@ getDbCohortAttrCovariatesData( cdmDatabaseSchema, cohortTable = "#cohort_person", cohortId = -1, + cohortIds = c(-1), cdmVersion = "5", rowIdField = "subject_id", covariateSettings, @@ -32,7 +33,11 @@ covariates. If it is a temp table, the name should have a hash prefix, e.g. '#temp_table'. If it is a non-temp table, it should include the database schema, e.g. 'cdm_database.cohort'.} -\item{cohortId}{For which cohort ID should covariates be constructed? If set to -1, +\item{cohortId}{DEPRECATED:For which cohort ID should covariates be constructed? If set to -1, +covariates will be constructed for all cohorts in the specified cohort +table.} + +\item{cohortIds}{For which cohort ID(s) should covariates be constructed? If set to c(-1), covariates will be constructed for all cohorts in the specified cohort table.} @@ -107,7 +112,7 @@ covData <- getDbCohortAttrCovariatesData( cdmDatabaseSchema = "main", cdmVersion = "5", cohortTable = "cohort", - cohortId = 1, + cohortIds = 1, rowIdField = "subject_id", covariateSettings = covariateSettings, aggregated = FALSE diff --git a/man/getDbCohortBasedCovariatesData.Rd b/man/getDbCohortBasedCovariatesData.Rd index 51303afa..624cb50e 100644 --- a/man/getDbCohortBasedCovariatesData.Rd +++ b/man/getDbCohortBasedCovariatesData.Rd @@ -10,6 +10,7 @@ getDbCohortBasedCovariatesData( cdmDatabaseSchema, cohortTable = "#cohort_person", cohortId = -1, + cohortIds = c(-1), cdmVersion = "5", rowIdField = "subject_id", covariateSettings, @@ -32,7 +33,11 @@ covariates. If it is a temp table, the name should have a hash prefix, e.g. '#temp_table'. If it is a non-temp table, it should include the database schema, e.g. 'cdm_database.cohort'.} -\item{cohortId}{For which cohort ID should covariates be constructed? If set to -1, +\item{cohortId}{DEPRECATED:For which cohort ID should covariates be constructed? If set to -1, +covariates will be constructed for all cohorts in the specified cohort +table.} + +\item{cohortIds}{For which cohort ID(s) should covariates be constructed? If set to c(-1), covariates will be constructed for all cohorts in the specified cohort table.} diff --git a/man/getDbCovariateData.Rd b/man/getDbCovariateData.Rd index 5d8573ac..9a8dfd43 100644 --- a/man/getDbCovariateData.Rd +++ b/man/getDbCovariateData.Rd @@ -14,6 +14,7 @@ getDbCovariateData( cohortDatabaseSchema = cdmDatabaseSchema, cohortTableIsTemp = FALSE, cohortId = -1, + cohortIds = c(-1), rowIdField = "subject_id", covariateSettings, aggregated = FALSE @@ -49,7 +50,11 @@ specify both the database and the schema, so for example \item{cohortTableIsTemp}{Is the cohort table a temp table?} -\item{cohortId}{For which cohort ID(s) should covariates be constructed? If set to -1, +\item{cohortId}{DEPRECATED:For which cohort ID(s) should covariates be constructed? If set to -1, +covariates will be constructed for all cohorts in the specified cohort +table.} + +\item{cohortIds}{For which cohort ID(s) should covariates be constructed? If set to c(-1), covariates will be constructed for all cohorts in the specified cohort table.} @@ -93,7 +98,7 @@ covData <- getDbCovariateData( cohortTable = "cohort", cohortDatabaseSchema = "main", cohortTableIsTemp = FALSE, - cohortId = -1, + cohortIds = -1, rowIdField = "subject_id", covariateSettings = covSettings, aggregated = FALSE diff --git a/man/getDbDefaultCovariateData.Rd b/man/getDbDefaultCovariateData.Rd index a4ab46f4..594ca8b6 100644 --- a/man/getDbDefaultCovariateData.Rd +++ b/man/getDbDefaultCovariateData.Rd @@ -10,6 +10,7 @@ getDbDefaultCovariateData( cdmDatabaseSchema, cohortTable = "#cohort_person", cohortId = -1, + cohortIds = c(-1), cdmVersion = "5", rowIdField = "subject_id", covariateSettings, @@ -36,7 +37,11 @@ covariates. If it is a temp table, the name should have a hash prefix, e.g. '#temp_table'. If it is a non-temp table, it should include the database schema, e.g. 'cdm_database.cohort'.} -\item{cohortId}{For which cohort ID should covariates be constructed? If set to -1, +\item{cohortId}{DEPRECATED:For which cohort ID should covariates be constructed? If set to -1, +covariates will be constructed for all cohorts in the specified cohort +table.} + +\item{cohortIds}{For which cohort ID(s) should covariates be constructed? If set to c(-1), covariates will be constructed for all cohorts in the specified cohort table.} diff --git a/man/isAggregatedCovariateData.Rd b/man/isAggregatedCovariateData.Rd index 9a85de09..a24b40ab 100644 --- a/man/isAggregatedCovariateData.Rd +++ b/man/isAggregatedCovariateData.Rd @@ -18,7 +18,7 @@ Check whether covariate data is aggregated \examples{ \dontrun{ covariateData <- FeatureExtraction:::createEmptyCovariateData( - cohortId = 1, + cohortIds = 1, aggregated = FALSE, temporal = FALSE ) diff --git a/man/isTemporalCovariateData.Rd b/man/isTemporalCovariateData.Rd index facb726c..a0847f7d 100644 --- a/man/isTemporalCovariateData.Rd +++ b/man/isTemporalCovariateData.Rd @@ -18,7 +18,7 @@ Check whether covariate data is temporal \examples{ \dontrun{ covariateData <- FeatureExtraction:::createEmptyCovariateData( - cohortId = 1, + cohortIds = 1, aggregated = FALSE, temporal = FALSE ) diff --git a/man/saveCovariateData.Rd b/man/saveCovariateData.Rd index 1e16d59b..1fa0453a 100644 --- a/man/saveCovariateData.Rd +++ b/man/saveCovariateData.Rd @@ -22,7 +22,7 @@ The data will be written to a set of files in the folder specified by the user. \examples{ \dontrun{ covariateData <- FeatureExtraction:::createEmptyCovariateData( - cohortId = 1, + cohortIds = 1, aggregated = FALSE, temporal = FALSE ) diff --git a/man/tidyCovariateData.Rd b/man/tidyCovariateData.Rd index a044a59f..59e99088 100644 --- a/man/tidyCovariateData.Rd +++ b/man/tidyCovariateData.Rd @@ -33,7 +33,7 @@ infrequent covariates. For temporal covariates, redundancy is evaluated per time \examples{ \dontrun{ covariateData <- FeatureExtraction:::createEmptyCovariateData( - cohortId = 1, + cohortIds = 1, aggregated = FALSE, temporal = FALSE ) diff --git a/tests/testthat/test-Aggregation.R b/tests/testthat/test-Aggregation.R index 2f667ff9..24059a99 100644 --- a/tests/testthat/test-Aggregation.R +++ b/tests/testthat/test-Aggregation.R @@ -5,16 +5,13 @@ test_that("aggregateCovariates works", { skip_if_not(runTestsOnEunomia) settings <- createCovariateSettings(useDemographicsAgeGroup = TRUE, useChads2Vasc = TRUE) - - covariateData <- getDbCovariateData( - connectionDetails = eunomiaConnectionDetails, - cdmDatabaseSchema = eunomiaCdmDatabaseSchema, - cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, - cohortId = 1, - covariateSettings = settings, - aggregated = FALSE - ) - + covariateData <- getDbCovariateData(connectionDetails = eunomiaConnectionDetails, + cdmDatabaseSchema = eunomiaCdmDatabaseSchema, + cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, + cohortIds = c(1), + covariateSettings = settings, + aggregated = FALSE) + aggregatedCovariateData <- aggregateCovariates(covariateData) expect_true(isAggregatedCovariateData(aggregatedCovariateData)) expect_error(aggregateCovariates("blah"), "not of class CovariateData") @@ -32,12 +29,10 @@ test_that("aggregateCovariates works", { test_that("aggregateCovariates handles temporalCovariates", { skip_if_not(runTestsOnEunomia) settings <- createTemporalCovariateSettings(useDemographicsGender = TRUE) - covariateData <- getDbCovariateData( - connectionDetails = eunomiaConnectionDetails, - cdmDatabaseSchema = eunomiaCdmDatabaseSchema, - cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, - cohortId = 1, - covariateSettings = settings - ) + covariateData <- getDbCovariateData(connectionDetails = eunomiaConnectionDetails, + cdmDatabaseSchema = eunomiaCdmDatabaseSchema, + cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, + cohortIds = c(1), + covariateSettings = settings) expect_error(aggregateCovariates(covariateData), "temporal covariates") }) diff --git a/tests/testthat/test-CovariateData.R b/tests/testthat/test-CovariateData.R index 647e44cf..46ca1608 100644 --- a/tests/testthat/test-CovariateData.R +++ b/tests/testthat/test-CovariateData.R @@ -10,22 +10,16 @@ test_that("test CovariateData Class on Empty", { errCovData <- list() - covData <- FeatureExtraction:::createEmptyCovariateData( - cohortId = 9999, - aggregated = FALSE, - temporal = FALSE - ) - aggCovData <- FeatureExtraction:::createEmptyCovariateData( - cohortId = 9999, - aggregated = TRUE, - temporal = FALSE - ) + covData <- FeatureExtraction:::createEmptyCovariateData(cohortIds = 9999, + aggregated = FALSE, + temporal = FALSE) + aggCovData <- FeatureExtraction:::createEmptyCovariateData(cohortIds = 9999, + aggregated = TRUE, + temporal = FALSE) - tempCovData <- FeatureExtraction:::createEmptyCovariateData( - cohortId = 9999, - aggregated = FALSE, - temporal = TRUE - ) + tempCovData <- FeatureExtraction:::createEmptyCovariateData(cohortIds = 9999, + aggregated = FALSE, + temporal = TRUE) # check that objects are covariate Data class expect_false(isCovariateData(errCovData)) @@ -55,15 +49,13 @@ test_that("test saveCovariateData error cases", { skip_if_not(runTestsOnEunomia) saveFileTest <- tempfile("covDatSave") settings <- createDefaultCovariateSettings() - covariateData <- getDbCovariateData( - connectionDetails = eunomiaConnectionDetails, - cdmDatabaseSchema = eunomiaCdmDatabaseSchema, - cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, - cohortId = 1, - covariateSettings = settings, - aggregated = FALSE - ) - # create error for test + covariateData <- getDbCovariateData(connectionDetails = eunomiaConnectionDetails, + cdmDatabaseSchema = eunomiaCdmDatabaseSchema, + cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, + cohortIds = c(1), + covariateSettings = settings, + aggregated = FALSE) + #create error for test errCovData <- list() expect_error(saveCovariateData()) # empty call error @@ -80,18 +72,16 @@ test_that("test saveCovariateData error cases", { test_that("test summary call for covariateData class", { skip_if_not(runTestsOnEunomia) settings <- createDefaultCovariateSettings() - covariateData <- getDbCovariateData( - connectionDetails = eunomiaConnectionDetails, - cdmDatabaseSchema = eunomiaCdmDatabaseSchema, - cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, - cohortId = 1, - covariateSettings = settings, - aggregated = FALSE - ) + covariateData <- getDbCovariateData(connectionDetails = eunomiaConnectionDetails, + cdmDatabaseSchema = eunomiaCdmDatabaseSchema, + cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, + cohortIds = c(1), + covariateSettings = settings, + aggregated = FALSE) sumOut <- summary(covariateData) Andromeda::close(covariateData) - expect_equal(sumOut$metaData$cohortId, 1L) + expect_equal(sumOut$metaData$cohortIds, 1L) }) test_that("test loadCovariateData", { @@ -104,8 +94,8 @@ test_that("Test exit/warning conditions", { expect_error(loadCovariateData(file = tempDir)) on.exit(unlink(tempDir)) - # ReadOnly parameter depreciated - cvData <- FeatureExtraction:::createEmptyCovariateData(cohortId = 1, aggregated = FALSE, temporal = FALSE) + # ReadOnly parameter depreciated + cvData <- FeatureExtraction:::createEmptyCovariateData(cohortIds = 1, aggregated = FALSE, temporal = FALSE) tempFile <- tempfile() tempFileName <- paste0(tempFile, ".zip") saveCovariateData(cvData, tempFileName) @@ -115,7 +105,18 @@ test_that("Test exit/warning conditions", { }) test_that("Test show method", { - cvData <- FeatureExtraction:::createEmptyCovariateData(cohortId = 1, aggregated = FALSE, temporal = FALSE) + cvData <- FeatureExtraction:::createEmptyCovariateData(cohortIds = c(1,2), aggregated = FALSE, temporal = FALSE) expect_invisible(show(cvData)) on.exit(rm(cvData)) }) +test_that("getDbCovariateData cohortId warning", { + skip_if_not(runTestsOnEunomia) + settings <- createDefaultCovariateSettings() + expect_warning(getDbCovariateData(connectionDetails = eunomiaConnectionDetails, + cdmDatabaseSchema = eunomiaCdmDatabaseSchema, + cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, + cohortId = c(1), + covariateSettings = settings, + aggregated = FALSE), "cohortId argument has been deprecated, please use cohortIds") +}) + diff --git a/tests/testthat/test-GetCohortBasedCovariates.R b/tests/testthat/test-GetCohortBasedCovariates.R index d30e08c1..e9a6cad6 100644 --- a/tests/testthat/test-GetCohortBasedCovariates.R +++ b/tests/testthat/test-GetCohortBasedCovariates.R @@ -95,7 +95,7 @@ runCohortBasedBinaryNonAggTest <- function(connection, cdmDatabaseSchema, ohdsiD cohortDatabaseSchema = ohdsiDatabaseSchema, cohortTableIsTemp = tempTable, cohortTable = cohortTable, - cohortId = 1, + cohortIds = c(1), cdmVersion = "5", rowIdField = "subject_id", covariateSettings = settings, @@ -137,7 +137,7 @@ runCohortBasedBinaryAggTest <- function(connection, cdmDatabaseSchema, ohdsiData cohortDatabaseSchema = ohdsiDatabaseSchema, cohortTableIsTemp = tempTable, cohortTable = cohortTable, - cohortId = 1, + cohortIds = c(1), cdmVersion = "5", rowIdField = "subject_id", covariateSettings = settings, @@ -180,7 +180,7 @@ runCohortBasedBinaryNonAggTemporalTest <- function(connection, cdmDatabaseSchema cohortDatabaseSchema = ohdsiDatabaseSchema, cohortTableIsTemp = tempTable, cohortTable = cohortTable, - cohortId = 1, + cohortIds = c(1), cdmVersion = "5", rowIdField = "subject_id", covariateSettings = settings, @@ -224,7 +224,7 @@ runCohortBasedBinaryAggTemporalTest <- function(connection, cdmDatabaseSchema, o cohortDatabaseSchema = ohdsiDatabaseSchema, cohortTableIsTemp = tempTable, cohortTable = cohortTable, - cohortId = 1, + cohortIds = c(1), cdmVersion = "5", rowIdField = "subject_id", covariateSettings = settings, @@ -271,7 +271,7 @@ runCohortBasedCountsNonAggTest <- function(connection, cdmDatabaseSchema, ohdsiD cohortDatabaseSchema = ohdsiDatabaseSchema, cohortTable = cohortTable, cohortTableIsTemp = tempTable, - cohortId = 1, + cohortIds = c(1), cdmVersion = "5", rowIdField = "subject_id", covariateSettings = settings, @@ -315,7 +315,7 @@ runCohortBasedCountsAggTest <- function(connection, cdmDatabaseSchema, ohdsiData cohortDatabaseSchema = ohdsiDatabaseSchema, cohortTableIsTemp = tempTable, cohortTable = cohortTable, - cohortId = 1, + cohortIds = c(1), cdmVersion = "5", rowIdField = "subject_id", covariateSettings = settings, @@ -361,7 +361,7 @@ runCohortBasedCountsNonAggTemporalTest <- function(connection, cdmDatabaseSchema cohortDatabaseSchema = ohdsiDatabaseSchema, cohortTableIsTemp = tempTable, cohortTable = cohortTable, - cohortId = 1, + cohortIds = c(1), cdmVersion = "5", rowIdField = "subject_id", covariateSettings = settings, @@ -406,7 +406,7 @@ runCohortBasedCountsAggTemporalTest <- function(connection, cdmDatabaseSchema, o cohortDatabaseSchema = ohdsiDatabaseSchema, cohortTable = cohortTable, cohortTableIsTemp = tempTable, - cohortId = 1, + cohortIds = c(1), cdmVersion = "5", rowIdField = "subject_id", covariateSettings = settings, @@ -455,7 +455,7 @@ runCohortBasedCountsAggMultiCohortTest <- function(connection, cdmDatabaseSchema cohortDatabaseSchema = ohdsiDatabaseSchema, cohortTableIsTemp = tempTable, cohortTable = cohortTable, - cohortId = c(1, 101), + cohortIds = c(1, 101), cdmVersion = "5", rowIdField = "subject_id", covariateSettings = settings, diff --git a/tests/testthat/test-GetCovariates.R b/tests/testthat/test-GetCovariates.R index f12aba99..b37f2274 100644 --- a/tests/testthat/test-GetCovariates.R +++ b/tests/testthat/test-GetCovariates.R @@ -96,14 +96,12 @@ test_that("getDbCovariateData cohortTableIsTemp tests when table name contains # test_that("getDbCovariateData populationSize == 0 tests", { skip_if_not(runTestsOnEunomia) - expect_warning(getDbCovariateData( - connection = eunomiaConnection, - cdmDatabaseSchema = eunomiaCdmDatabaseSchema, - cohortTableIsTemp = FALSE, - cohortTable = "cohort", - cohortId = 0, # This is a cohort that is not created in Eunomia - covariateSettings = getCovariateSettings() - )) + expect_warning(getDbCovariateData(connection = eunomiaConnection, + cdmDatabaseSchema = eunomiaCdmDatabaseSchema, + cohortTableIsTemp = FALSE, + cohortTable = "cohort", + cohortIds = c(0), # This is a cohort that is not created in Eunomia + covariateSettings = getCovariateSettings())) }) test_that("Custom covariate builder", { @@ -119,13 +117,11 @@ test_that("Custom covariate builder", { ) looCovSet <- FeatureExtraction:::.createLooCovariateSettings(useLengthOfObs = TRUE) covariateSettingsList <- list(covariateSettings, looCovSet) - covariates <- getDbCovariateData( - connection = eunomiaConnection, - cdmDatabaseSchema = eunomiaCdmDatabaseSchema, - cohortTable = "cohort", - cohortId = -1, - covariateSettings = covariateSettingsList - ) + covariates <- getDbCovariateData(connection = eunomiaConnection, + cdmDatabaseSchema = eunomiaCdmDatabaseSchema, + cohortTable = "cohort", + cohortIds = c(-1), + covariateSettings = covariateSettingsList) }) test_that("getDbCovariateData care site from person tests", { @@ -146,27 +142,23 @@ test_that("getDbCovariateData care site from person tests", { ) covariateSettings <- createCovariateSettings(useCareSiteId = TRUE) - covariateData <- getDbCovariateData( - connection = eunomiaConnection, - cdmDatabaseSchema = eunomiaCdmDatabaseSchema, - cohortTableIsTemp = FALSE, - cohortTable = "cohort", - cohortId = 1, - covariateSettings = covariateSettings - ) + covariateData <- getDbCovariateData(connection = eunomiaConnection, + cdmDatabaseSchema = eunomiaCdmDatabaseSchema, + cohortTableIsTemp = FALSE, + cohortTable = "cohort", + cohortIds = c(1), + covariateSettings = covariateSettings) expect_gt(pull(count(covariateData$covariates)), 0) joined <- inner_join(collect(covariateData$covariates), person, by = c("rowId" = "personId")) expect_true(all(joined$careSiteId * 1000 + 12 == joined$covariateId)) - covariateData <- getDbCovariateData( - connection = eunomiaConnection, - cdmDatabaseSchema = eunomiaCdmDatabaseSchema, - cohortTableIsTemp = FALSE, - cohortTable = "cohort", - cohortId = 1, - covariateSettings = covariateSettings, - aggregated = TRUE - ) + covariateData <- getDbCovariateData(connection = eunomiaConnection, + cdmDatabaseSchema = eunomiaCdmDatabaseSchema, + cohortTableIsTemp = FALSE, + cohortTable = "cohort", + cohortIds = c(1), + covariateSettings = covariateSettings, + aggregated = TRUE) expect_gt(pull(count(covariateData$covariates)), 0) }) @@ -213,13 +205,12 @@ test_that("getDbCovariateData care site from visit_occurrence tests", { ) covariateSettings <- createCovariateSettings(useCareSiteId = TRUE) - covariateData <- getDbCovariateData( - connection = eunomiaConnection, - cdmDatabaseSchema = "main", - cohortTableIsTemp = FALSE, - cohortTable = "cohort", - cohortId = 1, - covariateSettings = covariateSettings - ) - expect_equal(pull(count(filter(covariateData$covariates, covariateId > 4012))), sum(cohort$cohortDefinitionId == 1)) + covariateData <- getDbCovariateData(connection = eunomiaConnection, + cdmDatabaseSchema = "main", + cohortTableIsTemp = FALSE, + cohortTable = "cohort", + cohortIds = c(1, 2), + covariateSettings = covariateSettings) + expect_equal(pull(count(filter(covariateData$covariates, covariateId > 4012))), + sum(cohort$cohortDefinitionId == 1) + sum(cohort$cohortDefinitionId == 2)) }) diff --git a/tests/testthat/test-GetCovariatesFromCohortAttributes.R b/tests/testthat/test-GetCovariatesFromCohortAttributes.R index e8c21e46..6c0f8bfc 100644 --- a/tests/testthat/test-GetCovariatesFromCohortAttributes.R +++ b/tests/testthat/test-GetCovariatesFromCohortAttributes.R @@ -61,7 +61,8 @@ test_that("getDbCohortAttrCovariatesData hasIncludedAttributes > 0", { connection = eunomiaConnection, cdmDatabaseSchema = eunomiaCdmDatabaseSchema, cohortTable = cohortTable, - covariateSettings = covariateSettings + covariateSettings = covariateSettings, + cohortIds = c(1, 2) ) expect_equal(class(result), "CovariateData") }) @@ -71,3 +72,23 @@ test_that("createCohortAttrCovariateSettings check", { result <- createCohortAttrCovariateSettings(attrDatabaseSchema = "main") expect_equal(class(result), "covariateSettings") }) + +test_that("getDbCohortAttrCovariatesData cohortId warning", { + skip_if_not(runTestsOnEunomia) + covariateSettings <- createCohortAttrCovariateSettings( + attrDatabaseSchema = eunomiaOhdsiDatabaseSchema, + cohortAttrTable = cohortAttributeTable, + attrDefinitionTable = attributeDefinitionTable, + includeAttrIds = c(1), + isBinary = FALSE, + missingMeansZero = TRUE + ) + # cohortId argument + expect_warning(getDbCohortAttrCovariatesData( + connection = eunomiaConnection, + cdmDatabaseSchema = eunomiaCdmDatabaseSchema, + cohortTable = cohortTable, + covariateSettings = covariateSettings, + cohortId = 1 + ), "cohortId argument has been deprecated, please use cohortIds") +}) \ No newline at end of file diff --git a/tests/testthat/test-GetCovariatesTemporalSequence.R b/tests/testthat/test-GetCovariatesTemporalSequence.R index 91cd1c1e..33a1de47 100644 --- a/tests/testthat/test-GetCovariatesTemporalSequence.R +++ b/tests/testthat/test-GetCovariatesTemporalSequence.R @@ -47,28 +47,25 @@ test_that("createTemporalSequenceCovariateSettings correctly sets function", { # check extraction test_that("getDbCovariateData works with createTemporalSequenceCovariateSettings", { skip_if_not(runTestsOnEunomia) - covSet <- createTemporalSequenceCovariateSettings( - useDemographicsGender = T, - useDemographicsAge = T, - useDemographicsRace = T, - useDemographicsEthnicity = T, - useDemographicsAgeGroup = T, - useConditionEraGroupStart = T, - useDrugEraStart = T, - timePart = "month", - timeInterval = 1, - sequenceEndDay = -1, - sequenceStartDay = -365 * 5 - ) - + covSet <- createTemporalSequenceCovariateSettings(useDemographicsGender = T, + useDemographicsAge = T, + useDemographicsRace = T, + useDemographicsEthnicity = T, + useDemographicsAgeGroup = T, + useConditionEraGroupStart = T, + useDrugEraStart = T, + timePart = 'month', + timeInterval = 1, + sequenceEndDay = -1, + sequenceStartDay = -365*5) + + + result <- getDbCovariateData(connection = eunomiaConnection, + cdmDatabaseSchema = "main", + cohortTable = "cohort", + cohortIds = c(1), + covariateSettings = covSet) - result <- getDbCovariateData( - connection = eunomiaConnection, - cdmDatabaseSchema = "main", - cohortTable = "cohort", - cohortId = 1, - covariateSettings = covSet - ) expect_true(is(result, "CovariateData")) # check timeId is 59 or less @@ -82,14 +79,12 @@ test_that("Temporal Covariate Settings are backwards compatible", { # Temporal covariate settings created previously will not have # the temporalSequence property covSet <- FeatureExtraction::createDefaultTemporalCovariateSettings() - covSet$temporalSequence <- NULL - - result <- getDbCovariateData( - connection = eunomiaConnection, - cdmDatabaseSchema = "main", - cohortTable = "cohort", - cohortId = 1, - covariateSettings = covSet - ) + covSet$temporalSequence <- NULL + + result <- getDbCovariateData(connection = eunomiaConnection, + cdmDatabaseSchema = "main", + cohortTable = "cohort", + cohortIds = c(1), + covariateSettings = covSet) expect_true(is(result, "CovariateData")) }) diff --git a/tests/testthat/test-GetDefaultCovariates.R b/tests/testthat/test-GetDefaultCovariates.R index cce99225..ab149785 100644 --- a/tests/testthat/test-GetDefaultCovariates.R +++ b/tests/testthat/test-GetDefaultCovariates.R @@ -31,6 +31,7 @@ test_that("Test exit conditions", { expect_error(getDbDefaultCovariateData( connection = eunomiaConnection, cdmDatabaseSchema = "main", + cohortId = -1, covariateSettings = createDefaultCovariateSettings(), targetDatabaseSchema = "main", targetCovariateTable = "cov", diff --git a/tests/testthat/test-HelperFunctions.R b/tests/testthat/test-HelperFunctions.R index 27d69b87..c53f7c5a 100644 --- a/tests/testthat/test-HelperFunctions.R +++ b/tests/testthat/test-HelperFunctions.R @@ -5,44 +5,40 @@ test_that("Test helper functions for non-aggregated covariate data", { skip_if_not(runTestsOnEunomia) expect_error(filterByRowId("blah", 1), "not of class CovariateData") - - covariateData <- getDbCovariateData( - connection = eunomiaConnection, - cdmDatabaseSchema = eunomiaCdmDatabaseSchema, - cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, - cohortId = 1:2, - covariateSettings = createCovariateSettings(useDemographicsAgeGroup = TRUE, useChads2Vasc = TRUE), - aggregated = F - ) - + + covariateData <- getDbCovariateData(connection = eunomiaConnection, + cdmDatabaseSchema = eunomiaCdmDatabaseSchema, + cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, + cohortIds = 1:2, + covariateSettings = createCovariateSettings(useDemographicsAgeGroup = TRUE, useChads2Vasc = TRUE), + aggregated = F) + covariateDataFiltered <- filterByRowId(covariateData, rowIds = 1) expect_equal(unique(pull(covariateDataFiltered$covariates, rowId)), 1) locallyAggregated <- aggregateCovariates(covariateData) - expect_error(filterByCohortDefinitionId(locallyAggregated, 1), "no such column") - - expect_error(filterByCohortDefinitionId(covariateData, 1), "Can only filter aggregated") + expect_error(filterByCohortDefinitionIds(locallyAggregated, cohortIds = c(1)), "no such column") + expect_error(filterByCohortDefinitionIds(covariateData, cohortIds = c(1)), "Can only filter aggregated") + Andromeda::close(covariateData) expect_error(filterByRowId(covariateData, 1), "closed") }) test_that("Test helper functions for aggregated covariate data", { skip_if_not(runTestsOnEunomia) - expect_error(filterByCohortDefinitionId("blah", 1), "not of class CovariateData") - - aggregatedCovariateData <- getDbCovariateData( - connection = eunomiaConnection, - cdmDatabaseSchema = eunomiaCdmDatabaseSchema, - cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, - cohortId = 1:2, - covariateSettings = createCovariateSettings(useDemographicsAgeGroup = TRUE, useChads2Vasc = TRUE), - aggregated = TRUE - ) - - aggCovariateDataFiltered <- filterByCohortDefinitionId(aggregatedCovariateData, 1) + expect_error(filterByCohortDefinitionIds("blah", cohortIds = c(1)), "not of class CovariateData") + + aggregatedCovariateData <- getDbCovariateData(connection = eunomiaConnection, + cdmDatabaseSchema = eunomiaCdmDatabaseSchema, + cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, + cohortIds = 1:2, + covariateSettings = createCovariateSettings(useDemographicsAgeGroup = TRUE, useChads2Vasc = TRUE), + aggregated = TRUE) + + aggCovariateDataFiltered <- filterByCohortDefinitionIds(aggregatedCovariateData, cohortIds = c(1)) expect_equal(unique(pull(aggCovariateDataFiltered$covariates, cohortDefinitionId)), 1) expect_error(filterByRowId(aggregatedCovariateData, 1), "Cannot filter aggregated") Andromeda::close(aggregatedCovariateData) - expect_error(filterByCohortDefinitionId(aggregatedCovariateData, 1), "closed") + expect_error(filterByCohortDefinitionIds(aggregatedCovariateData, cohortIds = c(1)), "closed") }) diff --git a/tests/testthat/test-Table1.R b/tests/testthat/test-Table1.R index 189bb03b..f1eb5204 100644 --- a/tests/testthat/test-Table1.R +++ b/tests/testthat/test-Table1.R @@ -12,21 +12,17 @@ test_that("getDefaultTable1Specifications works", { test_that("createTable1 works with categorical covariates", { skip_if_not(runTestsOnEunomia) - settings <- createCovariateSettings( - useDemographicsAgeGroup = TRUE, - useDemographicsGender = TRUE, - useChads2Vasc = F - ) - - covariateData1 <- getDbCovariateData( - connection = eunomiaConnection, - cdmDatabaseSchema = eunomiaCdmDatabaseSchema, - cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, - cohortId = 1, - covariateSettings = settings, - aggregated = TRUE - ) - + settings <- createCovariateSettings(useDemographicsAgeGroup = TRUE, + useDemographicsGender = TRUE, + useChads2Vasc = F) + + covariateData1 <- getDbCovariateData(connection = eunomiaConnection, + cdmDatabaseSchema = eunomiaCdmDatabaseSchema, + cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, + cohortIds = c(1), + covariateSettings = settings, + aggregated = TRUE) + expect_error(createTable1("blah"), "not of type 'covariateData'") expect_error(createTable1(covariateData1, output = "blah"), "The `output` argument must be") expect_error(createTable1(covariateData1, showCounts = F, showPercent = F), "counts or percent") @@ -40,17 +36,13 @@ test_that("createTable1 works with categorical covariates", { expect_s3_class(table1, "data.frame") expect_equal(ncol(table1), 2) - - - covariateData2 <- getDbCovariateData( - connection = eunomiaConnection, - cdmDatabaseSchema = eunomiaCdmDatabaseSchema, - cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, - cohortId = 2, - covariateSettings = settings, - aggregated = TRUE - ) - + covariateData2 <- getDbCovariateData(connection = eunomiaConnection, + cdmDatabaseSchema = eunomiaCdmDatabaseSchema, + cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, + cohortIds = c(2), + covariateSettings = settings, + aggregated = TRUE) + expect_error(createTable1(covariateData1, "blah"), "not of type 'covariateData'") table1 <- createTable1(covariateData1, covariateData2) expect_s3_class(table1, "data.frame") @@ -60,16 +52,13 @@ test_that("createTable1 works with categorical covariates", { expect_s3_class(table1, "data.frame") expect_equal(ncol(table1), 4) - - rawCovariateData <- getDbCovariateData( - connection = eunomiaConnection, - cdmDatabaseSchema = eunomiaCdmDatabaseSchema, - cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, - cohortId = 1, - covariateSettings = settings, - aggregated = FALSE - ) - + rawCovariateData <- getDbCovariateData(connection = eunomiaConnection, + cdmDatabaseSchema = eunomiaCdmDatabaseSchema, + cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, + cohortIds = c(1), + covariateSettings = settings, + aggregated = FALSE) + expect_error(createTable1(rawCovariateData), "data is not aggregated") expect_error(createTable1(covariateData1, rawCovariateData), "data is not aggregated") }) @@ -79,21 +68,17 @@ test_that("createTable1 works with categorical covariates", { test_that("createTable1 works with continuous covariates", { skip_if_not(runTestsOnEunomia) - settings <- createCovariateSettings( - useDemographicsAgeGroup = TRUE, - useDemographicsGender = TRUE, - useChads2Vasc = TRUE - ) - - covariateData1 <- getDbCovariateData( - connection = eunomiaConnection, - cdmDatabaseSchema = eunomiaCdmDatabaseSchema, - cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, - cohortId = 1, - covariateSettings = settings, - aggregated = TRUE - ) - + settings <- createCovariateSettings(useDemographicsAgeGroup = TRUE, + useDemographicsGender = TRUE, + useChads2Vasc = TRUE) + + covariateData1 <- getDbCovariateData(connection = eunomiaConnection, + cdmDatabaseSchema = eunomiaCdmDatabaseSchema, + cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, + cohortIds = c(1), + covariateSettings = settings, + aggregated = TRUE) + # Does not fail? # expect_error(createTable1(covariateData1)) @@ -104,34 +89,27 @@ test_that("createTable1 works with continuous covariates", { table1 <- createTable1(covariateData1, output = "one column") expect_s3_class(table1, "data.frame") - - covariateData2 <- getDbCovariateData( - connection = eunomiaConnection, - cdmDatabaseSchema = eunomiaCdmDatabaseSchema, - cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, - cohortId = 2, - covariateSettings = settings, - aggregated = TRUE - ) - - table1 <- createTable1(covariateData1, covariateData2, - output = "one column", - cohortId1 = 1, cohortId2 = 2, - showCounts = TRUE, showPercent = TRUE - ) + covariateData2 <- getDbCovariateData(connection = eunomiaConnection, + cdmDatabaseSchema = eunomiaCdmDatabaseSchema, + cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, + cohortIds = c(2), + covariateSettings = settings, + aggregated = TRUE) + + table1 <- createTable1(covariateData1, covariateData2, output = "one column", + cohortId1 = 1, cohortId2 = 2, + showCounts = TRUE, showPercent = TRUE) expect_s3_class(table1, "data.frame") settings <- createCovariateSettings(useChads2Vasc = TRUE) - covariateData3 <- getDbCovariateData( - connection = eunomiaConnection, - cdmDatabaseSchema = eunomiaCdmDatabaseSchema, - cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, - cohortId = 1, - covariateSettings = settings, - aggregated = TRUE - ) + covariateData3 <- getDbCovariateData(connection = eunomiaConnection, + cdmDatabaseSchema = eunomiaCdmDatabaseSchema, + cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, + cohortIds = c(1), + covariateSettings = settings, + aggregated = TRUE) table1 <- createTable1(covariateData3, output = "one column", showCounts = T, showPercent = T) expect_s3_class(table1, "data.frame") }) @@ -148,15 +126,13 @@ test_that("createTable1 works with other covariates", { spec[1, "analysisId"] <- NA_integer_ spec[2, "covariateIds"] <- NA_character_ - covariateData1 <- getDbCovariateData( - connection = eunomiaConnection, - cdmDatabaseSchema = eunomiaCdmDatabaseSchema, - cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, - cohortId = 1, - covariateSettings = settings, - aggregated = TRUE - ) - + covariateData1 <- getDbCovariateData(connection = eunomiaConnection, + cdmDatabaseSchema = eunomiaCdmDatabaseSchema, + cohortDatabaseSchema = eunomiaOhdsiDatabaseSchema, + cohortIds = c(1), + covariateSettings = settings, + aggregated = TRUE) + table1 <- createTable1(covariateData1, specifications = spec, output = "list") expect_type(table1, "list") }) diff --git a/tests/testthat/test-query-no-fail.R b/tests/testthat/test-query-no-fail.R index 8fe4ee5c..115ed358 100644 --- a/tests/testthat/test-query-no-fail.R +++ b/tests/testthat/test-query-no-fail.R @@ -120,17 +120,15 @@ runExtractionPerPerson <- function(connection, cdmDatabaseSchema, ohdsiDatabaseS includedCovariateIds = c() ) - suppressWarnings(covariateData <- getDbCovariateData( - connection = connection, - cdmDatabaseSchema = cdmDatabaseSchema, - oracleTempSchema = ohdsiDatabaseSchema, - cohortDatabaseSchema = ohdsiDatabaseSchema, - cohortTable = cohortTable, - cohortTableIsTemp = TRUE, - cohortId = 1124300, - rowIdField = "subject_id", - covariateSettings = settings - )) + suppressWarnings(covariateData <- getDbCovariateData(connection = connection, + cdmDatabaseSchema = cdmDatabaseSchema, + oracleTempSchema = ohdsiDatabaseSchema, + cohortDatabaseSchema = ohdsiDatabaseSchema, + cohortTable = cohortTable, + cohortTableIsTemp = TRUE, + cohortIds = c(1124300), + rowIdField = "subject_id", + covariateSettings = settings)) return(covariateData) } @@ -300,18 +298,16 @@ runExtractionAggregated <- function(connection, cdmDatabaseSchema, ohdsiDatabase includedCovariateIds = c() ) - suppressWarnings(covariateData <- getDbCovariateData( - connection = connection, - cdmDatabaseSchema = cdmDatabaseSchema, - oracleTempSchema = ohdsiDatabaseSchema, - cohortDatabaseSchema = ohdsiDatabaseSchema, - cohortTable = cohortTable, - cohortTableIsTemp = TRUE, - cohortId = 1124300, - rowIdField = "subject_id", - covariateSettings = settings, - aggregated = TRUE - )) + suppressWarnings(covariateData <- getDbCovariateData(connection = connection, + cdmDatabaseSchema = cdmDatabaseSchema, + oracleTempSchema = ohdsiDatabaseSchema, + cohortDatabaseSchema = ohdsiDatabaseSchema, + cohortTable = cohortTable, + cohortTableIsTemp = TRUE, + cohortIds = c(1124300), + rowIdField = "subject_id", + covariateSettings = settings, + aggregated = TRUE)) return(covariateData) } @@ -363,67 +359,63 @@ test_that("Run all analysis at aggregated level on Eunomia", { # runExtractionTemporalPerPerson ----------- runExtractionTemporalPerPerson <- function(connection, cdmDatabaseSchema, ohdsiDatabaseSchema, cohortTable) { - settings <- createTemporalCovariateSettings( - useDemographicsGender = TRUE, - useDemographicsAge = TRUE, - useDemographicsAgeGroup = TRUE, - useDemographicsRace = TRUE, - useDemographicsEthnicity = TRUE, - useDemographicsIndexYear = TRUE, - useDemographicsIndexMonth = TRUE, - useDemographicsIndexYearMonth = TRUE, - useDemographicsPriorObservationTime = TRUE, - useDemographicsPostObservationTime = TRUE, - useDemographicsTimeInCohort = TRUE, - useCareSiteId = TRUE, - useConditionOccurrence = TRUE, - useConditionOccurrencePrimaryInpatient = TRUE, - useConditionEraStart = TRUE, - useConditionEraOverlap = TRUE, - useConditionEraGroupStart = FALSE, - useConditionEraGroupOverlap = FALSE, - useDrugExposure = TRUE, - useDrugEraStart = TRUE, - useDrugEraOverlap = TRUE, - useDrugEraGroupStart = FALSE, - useDrugEraGroupOverlap = FALSE, - useProcedureOccurrence = TRUE, - useDeviceExposure = TRUE, - useMeasurement = TRUE, - useMeasurementValue = TRUE, - useMeasurementRangeGroup = TRUE, - useObservation = TRUE, - useCharlsonIndex = TRUE, - useDcsi = TRUE, - useChads2 = TRUE, - useChads2Vasc = TRUE, - useHfrs = TRUE, - useDistinctConditionCount = TRUE, - useDistinctIngredientCount = TRUE, - useDistinctProcedureCount = TRUE, - useDistinctMeasurementCount = TRUE, - useDistinctObservationCount = TRUE, - useVisitCount = TRUE, - useVisitConceptCount = TRUE, - temporalStartDays = -365:-1, - temporalEndDays = -365:-1, - includedCovariateConceptIds = c(), - addDescendantsToInclude = FALSE, - excludedCovariateConceptIds = c(), - addDescendantsToExclude = FALSE, - includedCovariateIds = c() - ) - suppressWarnings(covariateData <- getDbCovariateData( - connection = connection, - cdmDatabaseSchema = cdmDatabaseSchema, - oracleTempSchema = ohdsiDatabaseSchema, - cohortDatabaseSchema = ohdsiDatabaseSchema, - cohortTable = cohortTable, - cohortTableIsTemp = TRUE, - cohortId = 1124300, - rowIdField = "subject_id", - covariateSettings = settings - )) + settings <- createTemporalCovariateSettings(useDemographicsGender = TRUE, + useDemographicsAge = TRUE, + useDemographicsAgeGroup = TRUE, + useDemographicsRace = TRUE, + useDemographicsEthnicity = TRUE, + useDemographicsIndexYear = TRUE, + useDemographicsIndexMonth = TRUE, + useDemographicsIndexYearMonth = TRUE, + useDemographicsPriorObservationTime = TRUE, + useDemographicsPostObservationTime = TRUE, + useDemographicsTimeInCohort = TRUE, + useCareSiteId = TRUE, + useConditionOccurrence = TRUE, + useConditionOccurrencePrimaryInpatient = TRUE, + useConditionEraStart = TRUE, + useConditionEraOverlap = TRUE, + useConditionEraGroupStart = FALSE, + useConditionEraGroupOverlap = FALSE, + useDrugExposure = TRUE, + useDrugEraStart = TRUE, + useDrugEraOverlap = TRUE, + useDrugEraGroupStart = FALSE, + useDrugEraGroupOverlap = FALSE, + useProcedureOccurrence = TRUE, + useDeviceExposure = TRUE, + useMeasurement = TRUE, + useMeasurementValue = TRUE, + useMeasurementRangeGroup = TRUE, + useObservation = TRUE, + useCharlsonIndex = TRUE, + useDcsi = TRUE, + useChads2 = TRUE, + useChads2Vasc = TRUE, + useHfrs = TRUE, + useDistinctConditionCount = TRUE, + useDistinctIngredientCount = TRUE, + useDistinctProcedureCount = TRUE, + useDistinctMeasurementCount = TRUE, + useDistinctObservationCount = TRUE, + useVisitCount = TRUE, + useVisitConceptCount = TRUE, + temporalStartDays = -365:-1, + temporalEndDays = -365:-1, + includedCovariateConceptIds = c(), + addDescendantsToInclude = FALSE, + excludedCovariateConceptIds = c(), + addDescendantsToExclude = FALSE, + includedCovariateIds = c()) + suppressWarnings(covariateData <- getDbCovariateData(connection = connection, + cdmDatabaseSchema = cdmDatabaseSchema, + oracleTempSchema = ohdsiDatabaseSchema, + cohortDatabaseSchema = ohdsiDatabaseSchema, + cohortTable = cohortTable, + cohortTableIsTemp = TRUE, + cohortIds = c(1124300), + rowIdField = "subject_id", + covariateSettings = settings)) return(covariateData) } @@ -475,68 +467,64 @@ test_that("Run all temporalanalysis at per-person level on Eunomia", { # runExtractionTemporalPerPerson ----------- runExtractionTemporalAggregated <- function(connection, cdmDatabaseSchema, ohdsiDatabaseSchema, cohortTable) { - settings <- createTemporalCovariateSettings( - useDemographicsGender = TRUE, - useDemographicsAge = TRUE, - useDemographicsAgeGroup = TRUE, - useDemographicsRace = TRUE, - useDemographicsEthnicity = TRUE, - useDemographicsIndexYear = TRUE, - useDemographicsIndexMonth = TRUE, - useDemographicsIndexYearMonth = TRUE, - useDemographicsPriorObservationTime = TRUE, - useDemographicsPostObservationTime = TRUE, - useDemographicsTimeInCohort = TRUE, - useCareSiteId = TRUE, - useConditionOccurrence = TRUE, - useConditionOccurrencePrimaryInpatient = TRUE, - useConditionEraStart = TRUE, - useConditionEraOverlap = TRUE, - useConditionEraGroupStart = FALSE, - useConditionEraGroupOverlap = FALSE, - useDrugExposure = TRUE, - useDrugEraStart = TRUE, - useDrugEraOverlap = TRUE, - useDrugEraGroupStart = FALSE, - useDrugEraGroupOverlap = FALSE, - useProcedureOccurrence = TRUE, - useDeviceExposure = TRUE, - useMeasurement = TRUE, - useMeasurementValue = TRUE, - useMeasurementRangeGroup = TRUE, - useObservation = TRUE, - useCharlsonIndex = TRUE, - useDcsi = TRUE, - useChads2 = TRUE, - useChads2Vasc = TRUE, - useHfrs = TRUE, - useDistinctConditionCount = TRUE, - useDistinctIngredientCount = TRUE, - useDistinctProcedureCount = TRUE, - useDistinctMeasurementCount = TRUE, - useDistinctObservationCount = TRUE, - useVisitCount = TRUE, - useVisitConceptCount = TRUE, - temporalStartDays = -365:-1, - temporalEndDays = -365:-1, - includedCovariateConceptIds = c(), - addDescendantsToInclude = FALSE, - excludedCovariateConceptIds = c(), - addDescendantsToExclude = FALSE, - includedCovariateIds = c() - ) - suppressWarnings(covariateData <- getDbCovariateData( - connection = connection, - cdmDatabaseSchema = cdmDatabaseSchema, - oracleTempSchema = ohdsiDatabaseSchema, - cohortDatabaseSchema = ohdsiDatabaseSchema, - cohortTable = cohortTable, - cohortTableIsTemp = TRUE, - cohortId = 1124300, - rowIdField = "subject_id", - covariateSettings = settings, - aggregated = TRUE - )) + settings <- createTemporalCovariateSettings(useDemographicsGender = TRUE, + useDemographicsAge = TRUE, + useDemographicsAgeGroup = TRUE, + useDemographicsRace = TRUE, + useDemographicsEthnicity = TRUE, + useDemographicsIndexYear = TRUE, + useDemographicsIndexMonth = TRUE, + useDemographicsIndexYearMonth = TRUE, + useDemographicsPriorObservationTime = TRUE, + useDemographicsPostObservationTime = TRUE, + useDemographicsTimeInCohort = TRUE, + useCareSiteId = TRUE, + useConditionOccurrence = TRUE, + useConditionOccurrencePrimaryInpatient = TRUE, + useConditionEraStart = TRUE, + useConditionEraOverlap = TRUE, + useConditionEraGroupStart = FALSE, + useConditionEraGroupOverlap = FALSE, + useDrugExposure = TRUE, + useDrugEraStart = TRUE, + useDrugEraOverlap = TRUE, + useDrugEraGroupStart = FALSE, + useDrugEraGroupOverlap = FALSE, + useProcedureOccurrence = TRUE, + useDeviceExposure = TRUE, + useMeasurement = TRUE, + useMeasurementValue = TRUE, + useMeasurementRangeGroup = TRUE, + useObservation = TRUE, + useCharlsonIndex = TRUE, + useDcsi = TRUE, + useChads2 = TRUE, + useChads2Vasc = TRUE, + useHfrs = TRUE, + useDistinctConditionCount = TRUE, + useDistinctIngredientCount = TRUE, + useDistinctProcedureCount = TRUE, + useDistinctMeasurementCount = TRUE, + useDistinctObservationCount = TRUE, + useVisitCount = TRUE, + useVisitConceptCount = TRUE, + temporalStartDays = -365:-1, + temporalEndDays = -365:-1, + includedCovariateConceptIds = c(), + addDescendantsToInclude = FALSE, + excludedCovariateConceptIds = c(), + addDescendantsToExclude = FALSE, + includedCovariateIds = c()) + suppressWarnings(covariateData <- getDbCovariateData(connection = connection, + cdmDatabaseSchema = cdmDatabaseSchema, + oracleTempSchema = ohdsiDatabaseSchema, + cohortDatabaseSchema = ohdsiDatabaseSchema, + cohortTable = cohortTable, + cohortTableIsTemp = TRUE, + cohortIds = c(1124300), + rowIdField = "subject_id", + covariateSettings = settings, + aggregated = TRUE)) return(covariateData) } diff --git a/tests/testthat/test-spot-checks.R b/tests/testthat/test-spot-checks.R index b7f47000..d120988a 100644 --- a/tests/testthat/test-spot-checks.R +++ b/tests/testthat/test-spot-checks.R @@ -2,45 +2,39 @@ library(testthat) library(Andromeda) runSpotChecks <- function(connection, cdmDatabaseSchema, ohdsiDatabaseSchema, cohortTable) { - settings <- createCovariateSettings( - useDemographicsGender = TRUE, - useDemographicsAge = TRUE, - useConditionOccurrenceLongTerm = TRUE, - useDrugEraShortTerm = TRUE, - useVisitConceptCountLongTerm = TRUE, - longTermStartDays = -365, - mediumTermStartDays = -180, - shortTermStartDays = -30, - endDays = 0, - includedCovariateConceptIds = c(), - addDescendantsToInclude = FALSE, - excludedCovariateConceptIds = c(21603933), - addDescendantsToExclude = TRUE, - includedCovariateIds = c() - ) - suppressWarnings(covariateData <- getDbCovariateData( - connection = connection, - cdmDatabaseSchema = cdmDatabaseSchema, - oracleTempSchema = ohdsiDatabaseSchema, - cohortDatabaseSchema = ohdsiDatabaseSchema, - cohortTable = cohortTable, - cohortTableIsTemp = TRUE, - cohortId = 1124300, - rowIdField = "subject_id", - covariateSettings = settings - )) - suppressWarnings(covariateDataAgg <- getDbCovariateData( - connection = connection, - cdmDatabaseSchema = cdmDatabaseSchema, - oracleTempSchema = ohdsiDatabaseSchema, - cohortDatabaseSchema = ohdsiDatabaseSchema, - cohortTable = cohortTable, - cohortTableIsTemp = TRUE, - cohortId = 1124300, - rowIdField = "subject_id", - covariateSettings = settings, - aggregated = TRUE - )) + settings <- createCovariateSettings(useDemographicsGender = TRUE, + useDemographicsAge = TRUE, + useConditionOccurrenceLongTerm = TRUE, + useDrugEraShortTerm = TRUE, + useVisitConceptCountLongTerm = TRUE, + longTermStartDays = -365, + mediumTermStartDays = -180, + shortTermStartDays = -30, + endDays = 0, + includedCovariateConceptIds = c(), + addDescendantsToInclude = FALSE, + excludedCovariateConceptIds = c(21603933), + addDescendantsToExclude = TRUE, + includedCovariateIds = c()) + suppressWarnings(covariateData <- getDbCovariateData(connection = connection, + cdmDatabaseSchema = cdmDatabaseSchema, + oracleTempSchema = ohdsiDatabaseSchema, + cohortDatabaseSchema = ohdsiDatabaseSchema, + cohortTable = cohortTable, + cohortTableIsTemp = TRUE, + cohortIds = c(1124300), + rowIdField = "subject_id", + covariateSettings = settings)) + suppressWarnings(covariateDataAgg <- getDbCovariateData(connection = connection, + cdmDatabaseSchema = cdmDatabaseSchema, + oracleTempSchema = ohdsiDatabaseSchema, + cohortDatabaseSchema = ohdsiDatabaseSchema, + cohortTable = cohortTable, + cohortTableIsTemp = TRUE, + cohortIds = c(1124300), + rowIdField = "subject_id", + covariateSettings = settings, + aggregated = TRUE)) if (covariateData$covariates %>% count() %>% pull() == 0) { return(TRUE) } diff --git a/tests/testthat/test-tidyCovariates.R b/tests/testthat/test-tidyCovariates.R index b0917fcc..f76c6ab2 100644 --- a/tests/testthat/test-tidyCovariates.R +++ b/tests/testthat/test-tidyCovariates.R @@ -6,28 +6,22 @@ test_that("Test exit conditions ", { # Covariate Data object check expect_error(tidyCovariateData(covariateData = list())) # CovariateData object closed - cvData <- FeatureExtraction:::createEmptyCovariateData( - cohortId = 1, - aggregated = FALSE, - temporal = FALSE - ) + cvData <- FeatureExtraction:::createEmptyCovariateData(cohortIds = 1, + aggregated = FALSE, + temporal = FALSE) Andromeda::close(cvData) expect_error(tidyCovariateData(covariateData = cvData)) # CovariateData aggregated - cvData <- FeatureExtraction:::createEmptyCovariateData( - cohortId = 1, - aggregated = TRUE, - temporal = FALSE - ) + cvData <- FeatureExtraction:::createEmptyCovariateData(cohortIds = 1, + aggregated = TRUE, + temporal = FALSE) expect_error(tidyCovariateData(covariateData = cvData)) }) test_that("Test empty covariateData", { - cvData <- FeatureExtraction:::createEmptyCovariateData( - cohortId = 1, - aggregated = FALSE, - temporal = FALSE - ) + cvData <- FeatureExtraction:::createEmptyCovariateData(cohortIds = 1, + aggregated = FALSE, + temporal = FALSE) result <- tidyCovariateData(covariateData = cvData) expect_equal(length(pull(result$covariates, covariateId)), length(pull(cvData$covariates, covariateId))) }) @@ -79,17 +73,13 @@ test_that("tidyCovariates works", { test_that("tidyCovariateData on Temporal Data", { skip_if_not(runTestsOnEunomia) - covariateSettings <- createTemporalCovariateSettings( - useDrugExposure = TRUE, - temporalStartDays = -2:-1, - temporalEndDays = -2:-1 - ) - covariateData <- getDbCovariateData( - connection = eunomiaConnection, - cdmDatabaseSchema = eunomiaCdmDatabaseSchema, - cohortId = 1, - covariateSettings = covariateSettings - ) + covariateSettings <- createTemporalCovariateSettings(useDrugExposure = TRUE, + temporalStartDays = -2:-1, + temporalEndDays = -2:-1) + covariateData <- getDbCovariateData(connection = eunomiaConnection, + cdmDatabaseSchema = eunomiaCdmDatabaseSchema, + cohortIds = c(1), + covariateSettings = covariateSettings) tidy <- tidyCovariateData(covariateData) expect_equal(length(pull(tidy$analysisRef, analysisId)), length(pull(covariateData$analysisRef, analysisId))) }) diff --git a/vignettes/CreatingCovariatesUsingCohortAttributes.Rmd b/vignettes/CreatingCovariatesUsingCohortAttributes.Rmd index 2ab46998..61bc1142 100644 --- a/vignettes/CreatingCovariatesUsingCohortAttributes.Rmd +++ b/vignettes/CreatingCovariatesUsingCohortAttributes.Rmd @@ -129,14 +129,12 @@ looCovSet <- createCohortAttrCovariateSettings( Setting `isBinary` and `missingMeansZero` is only necessary if we want to aggregate the covariates. We can then use these settings to fetch the `covariates` object: ```{r eval=FALSE} -covariates <- getDbCovariateData( - connectionDetails = connectionDetails, - cdmDatabaseSchema = cdmDatabaseSchema, - cohortDatabaseSchema = cohortDatabaseSchema, - cohortTable = "rehospitalization", - cohortId = 1, - covariateSettings = looCovSet -) +covariates <- getDbCovariateData(connectionDetails = connectionDetails, + cdmDatabaseSchema = cdmDatabaseSchema, + cohortDatabaseSchema = cohortDatabaseSchema, + cohortTable = "rehospitalization", + cohortIds = c(1), + covariateSettings = looCovSet) ``` In this case we will have only one covariate for our predictive model, the length of observation. In most cases, we will want our custom covariates in addition to the default covariates. We can do this by creating a list of covariate settings: @@ -160,14 +158,12 @@ looCovSet <- createCohortAttrCovariateSettings( covariateSettingsList <- list(covariateSettings, looCovSet) -covariates <- getDbCovariateData( - connectionDetails = connectionDetails, - cdmDatabaseSchema = cdmDatabaseSchema, - cohortDatabaseSchema = resultsDatabaseSchema, - cohortTable = "rehospitalization", - cohortId = 1, - covariateSettings = covariateSettingsList -) +covariates <- getDbCovariateData(connectionDetails = connectionDetails, + cdmDatabaseSchema = cdmDatabaseSchema, + cohortDatabaseSchema = resultsDatabaseSchema, + cohortTable = "rehospitalization", + cohortIds = c(1), + covariateSettings = covariateSettingsList) ``` In this example both demographic covariates and our length of observation covariate will be generated and can be used in our predictive model. diff --git a/vignettes/CreatingCustomCovariateBuilders.Rmd b/vignettes/CreatingCustomCovariateBuilders.Rmd index 43e11b96..07263de9 100644 --- a/vignettes/CreatingCustomCovariateBuilders.Rmd +++ b/vignettes/CreatingCustomCovariateBuilders.Rmd @@ -68,7 +68,7 @@ The covariate construction function has to accept the following arguments: * `cdmDatabaseSchema`: The name of the database schema that contains the OMOP CDM instance. On SQL Server, this will specify both the database and the schema, so for example 'cdm_instance.dbo'. * `cdmVersion`: Defines the OMOP CDM version used: currently supports "4" and "5". * `cohortTable`: Name of the table holding the cohort for which we want to construct covariates. This is a fully specified name, so either the name of a temp table (e.g. '#cohort_table'), or a permanent table including its database schema (e.g. 'cdm_schema.dbo.cohort'). -* `cohortId`: The cohort definition ID of the cohort. If set to -1, use all entries in the cohort table. +* `cohortIds`: The cohort definition IDs of the cohort. If set to -1, use all entries in the cohort table. * `rowIdField`: The name of the field in the cohort temp table that is to be used as the row_id field in the output table. This can be especially usefull if there is more than one period per person. * `covariateSettings`: The object created in your covariate settings function. * `aggregated`: Should covariates be constructed per-person, or aggregated across the cohort? @@ -93,7 +93,7 @@ getDbLooCovariateData <- function(connection, cdmDatabaseSchema, cdmVersion = "5", cohortTable = "#cohort_person", - cohortId = -1, + cohortIds = c(-1), rowIdField = "subject_id", covariateSettings, aggregated = FALSE) { @@ -106,23 +106,20 @@ getDbLooCovariateData <- function(connection, } # Some SQL to construct the covariate: - sql <- paste( - "SELECT @row_id_field AS row_id, 1 AS covariate_id,", - "DATEDIFF(DAY, observation_period_start_date, cohort_start_date)", - "AS covariate_value", - "FROM @cohort_table c", - "INNER JOIN @cdm_database_schema.observation_period op", - "ON op.person_id = c.subject_id", - "WHERE cohort_start_date >= observation_period_start_date", - "AND cohort_start_date <= observation_period_end_date", - "{@cohort_id != -1} ? {AND cohort_definition_id = @cohort_id}" - ) + sql <- paste("SELECT @row_id_field AS row_id, 1 AS covariate_id,", + "DATEDIFF(DAY, observation_period_start_date, cohort_start_date)", + "AS covariate_value", + "FROM @cohort_table c", + "INNER JOIN @cdm_database_schema.observation_period op", + "ON op.person_id = c.subject_id", + "WHERE cohort_start_date >= observation_period_start_date", + "AND cohort_start_date <= observation_period_end_date", + "{@cohort_ids != -1} ? {AND cohort_definition_id IN @cohort_ids}") sql <- SqlRender::render(sql, - cohort_table = cohortTable, - cohort_id = cohortId, - row_id_field = rowIdField, - cdm_database_schema = cdmDatabaseSchema - ) + cohort_table = cohortTable, + cohort_ids = cohortIds, + row_id_field = rowIdField, + cdm_database_schema = cdmDatabaseSchema) sql <- SqlRender::translate(sql, targetDialect = attr(connection, "dbms")) # Retrieve the covariate: @@ -169,14 +166,12 @@ We can use our custom covariate builder in the `PatientLevelPrediction` package, ```{r eval=FALSE} looCovSet <- createLooCovariateSettings(useLengthOfObs = TRUE) -covariates <- getDbCovariateData( - connectionDetails = connectionDetails, - cdmDatabaseSchema = cdmDatabaseSchema, - cohortDatabaseSchema = resultsDatabaseSchema, - cohortTable = "rehospitalization", - cohortId = 1, - covariateSettings = looCovSet -) +covariates <- getDbCovariateData(connectionDetails = connectionDetails, + cdmDatabaseSchema = cdmDatabaseSchema, + cohortDatabaseSchema = resultsDatabaseSchema, + cohortTable = "rehospitalization", + cohortIds = c(1), + covariateSettings = looCovSet) ``` @@ -196,14 +191,12 @@ looCovSet <- createLooCovariateSettings(useLengthOfObs = TRUE) covariateSettingsList <- list(covariateSettings, looCovSet) -covariates <- getDbCovariateData( - connectionDetails = connectionDetails, - cdmDatabaseSchema = cdmDatabaseSchema, - cohortDatabaseSchema = resultsDatabaseSchema, - cohortTable = "rehospitalization", - cohortId = 1, - covariateSettings = covariateSettingsList -) +covariates <- getDbCovariateData(connectionDetails = connectionDetails, + cdmDatabaseSchema = cdmDatabaseSchema, + cohortDatabaseSchema = resultsDatabaseSchema, + cohortTable = "rehospitalization", + cohortIds = c(1), + covariateSettings = covariateSettingsList) ``` In this example both demographic covariates and our length of observation covariate will be generated and can be used in our predictive model. diff --git a/vignettes/CreatingCustomCovariateBuildersKorean.Rmd b/vignettes/CreatingCustomCovariateBuildersKorean.Rmd index dae69d70..b85c115c 100644 --- a/vignettes/CreatingCustomCovariateBuildersKorean.Rmd +++ b/vignettes/CreatingCustomCovariateBuildersKorean.Rmd @@ -71,7 +71,7 @@ createLooCovariateSettings <- function(useLengthOfObs = TRUE) { - cdmDatabaseSchema : OMOP CDM 인스턴스를 포함하는 데이터베이스 스키마의 이름. SQL SQL 서버에서 이것은 데이터베이스와 스키마를 모두 지정한다(예: cdm_instance.dbo) - cdmVersion : 사용되는 OMOP CDM 버전 정의: 현재 "4" 및 "5" 지원 - cohortTable : 우리가 공변량을 구성하고자 하는 코호트를 보유하고 있는 테이블의 이름. 이것은 완전히 구체적인 이름이므로 임시테이블의 이름(예: '#cohort_table') 또는 데이터베이스 스키마가 포함된 영구 테이블(예: 'cdm_schema.dbo.cohort)이 될 수 있다. -- cohortId : 코호트의 코호트 정의 ID. -1로 설정된 경우 코호트 테이블의 모든 항목을 사용하면 된다. +- cohortIds : 코호트의 코호트 정의 ID. -1로 설정된 경우 코호트 테이블의 모든 항목을 사용하면 된다. - cdmVersion : 공통 데이터 모델 버전 - rowIdField : 결과 테이블에서 row_id 필드로 사용될 코호트 임시 테이블의 필드 이름이다. 1인당 1개이상의 기간이 있는 경우 특히 유용하다. - covariateSettings : 공변량 설정 함수에서 생성된 객체 @@ -95,7 +95,7 @@ getDbLooCovariateData <- function(connection, oracleTempSchema = NULL, cdmDatabaseSchema, cohortTable = "#cohort_person", - cohortId = -1, + cohortIds = c(-1), cdmVersion = "5", rowIdField = "subject_id", covariateSettings, @@ -109,23 +109,20 @@ getDbLooCovariateData <- function(connection, } # Some SQL to construct the covariate: - sql <- paste( - "SELECT @row_id_field AS row_id, 1 AS covariate_id,", - "DATEDIFF(DAY, observation_period_start_date, cohort_start_date)", - "AS covariate_value", - "FROM @cohort_table c", - "INNER JOIN @cdm_database_schema.observation_period op", - "ON op.person_id = c.subject_id", - "WHERE cohort_start_date >= observation_period_start_date", - "AND cohort_start_date <= observation_period_end_date", - "{@cohort_id != -1} ? {AND cohort_definition_id = @cohort_id}" - ) + sql <- paste("SELECT @row_id_field AS row_id, 1 AS covariate_id,", + "DATEDIFF(DAY, observation_period_start_date, cohort_start_date)", + "AS covariate_value", + "FROM @cohort_table c", + "INNER JOIN @cdm_database_schema.observation_period op", + "ON op.person_id = c.subject_id", + "WHERE cohort_start_date >= observation_period_start_date", + "AND cohort_start_date <= observation_period_end_date", + "{@cohort_ids != -1} ? {AND cohort_definition_id IN @cohort_ids}") sql <- SqlRender::render(sql, - cohort_table = cohortTable, - cohort_id = cohortId, - row_id_field = rowIdField, - cdm_database_schema = cdmDatabaseSchema - ) + cohort_table = cohortTable, + cohort_ids = cohortIds, + row_id_field = rowIdField, + cdm_database_schema = cdmDatabaseSchema) sql <- SqlRender::translate(sql, targetDialect = attr(connection, "dbms")) # Retrieve the covariate: @@ -179,14 +176,12 @@ PatientLevelPrediction 패키지의 사용자 정의 공변량 빌더와 cohortM ```{r eval=FALSE} looCovSet <- createLooCovariateSettings(useLengthOfObs = TRUE) -covariates <- getDbCovariateData( - connectionDetails = connectionDetails, - cdmDatabaseSchema = cdmDatabaseSchema, - cohortDatabaseSchema = resultsDatabaseSchema, - cohortTable = "rehospitalization", - cohortId = 1, - covariateSettings = looCovSet -) +covariates <- getDbCovariateData(connectionDetails = connectionDetails, + cdmDatabaseSchema = cdmDatabaseSchema, + cohortDatabaseSchema = resultsDatabaseSchema, + cohortTable = "rehospitalization", + cohortIds = c(1), + covariateSettings = looCovSet) ``` 이 경우 우리는 예측 모델인 관측 길이에 대한 공변량만 가질 것이다. 대부분의 경우, 우리는 기본 공변량 외에 우리의 맞춤 공변량을 원할 것이다. 공변량 설정 목록을 생성하여 이 작업을 수행할 수 있다. @@ -205,14 +200,12 @@ looCovSet <- createLooCovariateSettings(useLengthOfObs = TRUE) covariateSettingsList <- list(covariateSettings, looCovSet) -covariates <- getDbCovariateData( - connectionDetails = connectionDetails, - cdmDatabaseSchema = cdmDatabaseSchema, - cohortDatabaseSchema = resultsDatabaseSchema, - cohortTable = "rehospitalization", - cohortId = 1, - covariateSettings = covariateSettingsList -) +covariates <- getDbCovariateData(connectionDetails = connectionDetails, + cdmDatabaseSchema = cdmDatabaseSchema, + cohortDatabaseSchema = resultsDatabaseSchema, + cohortTable = "rehospitalization", + cohortIds = c(1), + covariateSettings = covariateSettingsList) ``` 이 예에서 인구 통계학적 공변량과 관찰 공변량의 길이가 모두 생성되어 예측 모델에 사용될 수 있다. diff --git a/vignettes/UsingFeatureExtraction.Rmd b/vignettes/UsingFeatureExtraction.Rmd index 87e1b11c..237eee1b 100644 --- a/vignettes/UsingFeatureExtraction.Rmd +++ b/vignettes/UsingFeatureExtraction.Rmd @@ -298,16 +298,13 @@ We can create per-person covariates for one of the cohorts of interest, for exam ```{r eval=FALSE} covariateSettings <- createDefaultCovariateSettings() -covariateData <- getDbCovariateData( - connectionDetails = connectionDetails, - cdmDatabaseSchema = cdmDatabaseSchema, - cohortDatabaseSchema = resultsDatabaseSchema, - cohortTable = "cohorts_of_interest", - cohortId = 1118084, - rowIdField = "subject_id", - covariateSettings = covariateSettings -) - +covariateData <- getDbCovariateData(connectionDetails = connectionDetails, + cdmDatabaseSchema = cdmDatabaseSchema, + cohortDatabaseSchema = resultsDatabaseSchema, + cohortTable = "cohorts_of_interest", + cohortIds = c(1118084), + rowIdField = "subject_id", + covariateSettings = covariateSettings) summary(covariateData) ``` ```{r echo=FALSE,message=FALSE} @@ -423,15 +420,13 @@ Of course, if all we wanted was aggregated statistics it would have been more ef ```{r eval=FALSE} covariateSettings <- createDefaultCovariateSettings() -covariateData2 <- getDbCovariateData( - connectionDetails = connectionDetails, - cdmDatabaseSchema = cdmDatabaseSchema, - cohortDatabaseSchema = resultsDatabaseSchema, - cohortTable = "cohorts_of_interest", - cohortId = 1118084, - covariateSettings = covariateSettings, - aggregated = TRUE -) +covariateData2 <- getDbCovariateData(connectionDetails = connectionDetails, + cdmDatabaseSchema = cdmDatabaseSchema, + cohortDatabaseSchema = resultsDatabaseSchema, + cohortTable = "cohorts_of_interest", + cohortIds = c(1118084), + covariateSettings = covariateSettings, + aggregated = TRUE) summary(covariateData2) ``` ```{r echo=FALSE,message=FALSE} @@ -506,15 +501,13 @@ Here we based table 1 on a `covariateData` object containing all default covaria ```{r eval=FALSE} covariateSettings <- createTable1CovariateSettings() -covariateData2b <- getDbCovariateData( - connectionDetails = connectionDetails, - cdmDatabaseSchema = cdmDatabaseSchema, - cohortDatabaseSchema = resultsDatabaseSchema, - cohortTable = "cohorts_of_interest", - cohortId = 1118084, - covariateSettings = covariateSettings, - aggregated = TRUE -) +covariateData2b <- getDbCovariateData(connectionDetails = connectionDetails, + cdmDatabaseSchema = cdmDatabaseSchema, + cohortDatabaseSchema = resultsDatabaseSchema, + cohortTable = "cohorts_of_interest", + cohortIds = c(1118084), + covariateSettings = covariateSettings, + aggregated = TRUE) summary(covariateData2b) ``` ```{r echo=FALSE,message=FALSE} @@ -534,26 +527,21 @@ settings <- createTable1CovariateSettings( addDescendantsToExclude = TRUE ) -covCelecoxib <- getDbCovariateData( - connectionDetails = connectionDetails, - cdmDatabaseSchema = cdmDatabaseSchema, - cohortDatabaseSchema = resultsDatabaseSchema, - cohortTable = "cohorts_of_interest", - cohortId = 1118084, - covariateSettings = settings, - aggregated = TRUE -) - -covDiclofenac <- getDbCovariateData( - connectionDetails = connectionDetails, - cdmDatabaseSchema = cdmDatabaseSchema, - cohortDatabaseSchema = resultsDatabaseSchema, - cohortTable = "cohorts_of_interest", - cohortId = 1124300, - covariateSettings = settings, - aggregated = TRUE -) - +covCelecoxib <- getDbCovariateData(connectionDetails = connectionDetails, + cdmDatabaseSchema = cdmDatabaseSchema, + cohortDatabaseSchema = resultsDatabaseSchema, + cohortTable = "cohorts_of_interest", + cohortIds = c(1118084), + covariateSettings = settings, + aggregated = TRUE) + +covDiclofenac <- getDbCovariateData(connectionDetails = connectionDetails, + cdmDatabaseSchema = cdmDatabaseSchema, + cohortDatabaseSchema = resultsDatabaseSchema, + cohortTable = "cohorts_of_interest", + cohortIds = c(1124300), + covariateSettings = settings, + aggregated = TRUE) std <- computeStandardizedDifference(covCelecoxib, covDiclofenac) ``` diff --git a/vignettes/UsingFeatureExtractionKorean.Rmd b/vignettes/UsingFeatureExtractionKorean.Rmd index 561cc21e..884d002d 100644 --- a/vignettes/UsingFeatureExtractionKorean.Rmd +++ b/vignettes/UsingFeatureExtractionKorean.Rmd @@ -308,15 +308,13 @@ data.frame(cohort_concept_id = c(1124300, 1118084), count = c(240761, 47293)) ```{r eval=FALSE} covariateSettings <- createDefaultCovariateSettings() -covariateData <- getDbCovariateData( - connectionDetails = connectionDetails, - cdmDatabaseSchema = cdmDatabaseSchema, - cohortDatabaseSchema = resultsDatabaseSchema, - cohortTable = "cohorts_of_interest", - cohortId = 1118084, - rowIdField = "subject_id", - covariateSettings = covariateSettings -) +covariateData <- getDbCovariateData(connectionDetails = connectionDetails, + cdmDatabaseSchema = cdmDatabaseSchema, + cohortDatabaseSchema = resultsDatabaseSchema, + cohortTable = "cohorts_of_interest", + cohortIds = c(1118084), + rowIdField = "subject_id", + covariateSettings = covariateSettings) summary(covariateData) ``` @@ -416,15 +414,13 @@ covariateData2 <- aggregateCovariates(covariateData) ```{r eval=FALSE} covariateSettings <- createDefaultCovariateSettings() -covariateData2 <- getDbCovariateData( - connectionDetails = connectionDetails, - cdmDatabaseSchema = cdmDatabaseSchema, - cohortDatabaseSchema = resultsDatabaseSchema, - cohortTable = "cohorts_of_interest", - cohortId = 1118084, - covariateSettings = covariateSettings, - aggregated = TRUE -) +covariateData2 <- getDbCovariateData(connectionDetails = connectionDetails, + cdmDatabaseSchema = cdmDatabaseSchema, + cohortDatabaseSchema = resultsDatabaseSchema, + cohortTable = "cohorts_of_interest", + cohortIds = c(1118084), + covariateSettings = covariateSettings, + aggregated = TRUE) summary(covariateData2) ``` ```{r echo=FALSE,message=FALSE} @@ -502,15 +498,13 @@ if (file.exists(file.path(vignetteFolder, "aggregatedCovariates"))) { ```{r eval=FALSE} covariateSettings <- createTable1CovariateSettings() -covariateData2b <- getDbCovariateData( - connectionDetails = connectionDetails, - cdmDatabaseSchema = cdmDatabaseSchema, - cohortDatabaseSchema = resultsDatabaseSchema, - cohortTable = "cohorts_of_interest", - cohortId = 1118084, - covariateSettings = covariateSettings, - aggregated = TRUE -) +covariateData2b <- getDbCovariateData(connectionDetails = connectionDetails, + cdmDatabaseSchema = cdmDatabaseSchema, + cohortDatabaseSchema = resultsDatabaseSchema, + cohortTable = "cohorts_of_interest", + cohortIds = c(1118084), + covariateSettings = covariateSettings, + aggregated = TRUE) summary(covariateData2b) ``` ```{r echo=FALSE,message=FALSE} @@ -532,26 +526,21 @@ settings <- createTable1CovariateSettings( addDescendantsToExclude = TRUE ) -covCelecoxib <- getDbCovariateData( - connectionDetails = connectionDetails, - cdmDatabaseSchema = cdmDatabaseSchema, - cohortDatabaseSchema = resultsDatabaseSchema, - cohortTable = "cohorts_of_interest", - cohortId = 1118084, - covariateSettings = settings, - aggregated = TRUE -) - -covDiclofenac <- getDbCovariateData( - connectionDetails = connectionDetails, - cdmDatabaseSchema = cdmDatabaseSchema, - cohortDatabaseSchema = resultsDatabaseSchema, - cohortTable = "cohorts_of_interest", - cohortId = 1124300, - covariateSettings = settings, - aggregated = TRUE -) - +covCelecoxib <- getDbCovariateData(connectionDetails = connectionDetails, + cdmDatabaseSchema = cdmDatabaseSchema, + cohortDatabaseSchema = resultsDatabaseSchema, + cohortTable = "cohorts_of_interest", + cohortIds = c(1118084), + covariateSettings = settings, + aggregated = TRUE) + +covDiclofenac <- getDbCovariateData(connectionDetails = connectionDetails, + cdmDatabaseSchema = cdmDatabaseSchema, + cohortDatabaseSchema = resultsDatabaseSchema, + cohortTable = "cohorts_of_interest", + cohortIds = c(1124300), + covariateSettings = settings, + aggregated = TRUE) std <- computeStandardizedDifference(covCelecoxib, covDiclofenac) ```