Skip to content

Commit

Permalink
location estimates
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsemharb committed Feb 14, 2023
1 parent 45252d9 commit 7ef757b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 61 deletions.
125 changes: 64 additions & 61 deletions R/RunStudy.R
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,21 @@ runStudy <- function(connectionDetails = NULL,
death_cohort_id = deathCohortId
)
data <- DatabaseConnector::querySql(connection, sql, snakeCaseToCamelCase = T)

#calculate locality metrics for Time to Treatment Switch
browser()
metrics <- data %>%
dplyr::filter(event == 1) %>%
dplyr::select(cohortDefinitionId, timeToEvent) %>%
dplyr::group_by(cohortDefinitionId) %>%
dplyr::summarise(minimum = min(timeToEvent),
q1 = quantile(timeToEvent, 0.25),
median = median(timeToEvent),
q3 = quantile(timeToEvent, 0.75),
maximum = max(timeToEvent)) %>%
dplyr::mutate(iqr = q3 - q1, analysis_name = "Time to Treatment Switch") %>%
dplyr::relocate(iqr, .before = minimum)

timeToTreatmentSwitch <- purrr::map_df(targetIds, function(targetId){
data <- data %>% dplyr::filter(cohortDefinitionId == targetId) %>% dplyr::select(id, timeToEvent, event)
if (nrow(data) < 30 | length(data$event[data$event == 1]) < 1) {return(NULL)}
Expand Down Expand Up @@ -284,75 +299,63 @@ runStudy <- function(connectionDetails = NULL,
sankeyData$databaseId = databaseId
andrData$treatment_sankey <- sankeyData

delta <- Sys.time() - start
ParallelLogger::logInfo(paste("Generating time to treatment switch data took",
signif(delta, 3),
attr(delta, "units")))

# Locality estimation of some time periods ------------------------------------------------
# median follow-up time
ParallelLogger::logInfo("Time periods locality estimation")
start <- Sys.time()
browser()

sqlAggreg <- SqlRender::loadRenderTranslateSql(dbms = connection@dbms,
sqlFilename = file.path("quartiles", "QuartilesAggregation.sql"),
packageName = getThisPackageName(),
warnOnMissingParameters = TRUE,
analysis_name = "Follow up Time")

sql <- SqlRender::loadRenderTranslateSql(dbms = connection@dbms,
sqlFilename = file.path("quartiles", "MedianFollowUp.sql"),
packageName = getThisPackageName(),
warnOnMissingParameters = TRUE,
cohort_database_schema = cohortDatabaseSchema,
cohort_table = cohortTable,
target_ids = paste(targetIds, collapse = ', '))

sql <- paste0(sql, sqlAggreg)
metrics <- rbind(metrics, DatabaseConnector::querySql(connection, sql))

sqlAggreg <- SqlRender::loadRenderTranslateSql(dbms = connection@dbms,
sqlFilename = file.path("quartiles", "QuartilesAggregation.sql"),
packageName = getThisPackageName(),
warnOnMissingParameters = TRUE,
analysis_name = "Time to Treatment")

sql <- SqlRender::loadRenderTranslateSql(dbms = connection@dbms,
sqlFilename = file.path("quartiles", "MedianTimeToTreatment.sql"),
packageName = getThisPackageName(),
warnOnMissingParameters = TRUE,
cdm_database_schema = cdmDatabaseSchema,
cohort_database_schema = cohortDatabaseSchema,
cohort_table = cohortTable,
target_ids = paste(targetIds, collapse = ', '))

sql <- paste0(sql, sqlAggreg)
metrics <- rbind(metrics, DatabaseConnector::querySql(connection, sql))

andrData$metrics_distribution <- metrics

# drop treatment complementary tables
sql <- SqlRender::loadRenderTranslateSql(dbms = connection@dbms,
sqlFilename = "TreatmentTablesDrop.sql",
packageName = getThisPackageName(),
warnOnMissingParameters = TRUE,
cohort_database_schema = cohortDatabaseSchema
)
)
DatabaseConnector::executeSql(connection, sql)

delta <- Sys.time() - start
ParallelLogger::logInfo(paste("Generating time to treatment switch data took",
signif(delta, 3),
attr(delta, "units")))


# # Generate Metrics Distribution info -----------------------------------------------------
# ParallelLogger::logInfo("Generating metrics distribution")
# ParallelLogger::logInfo("Creating auxiliary tables")
#
# start <- Sys.time()
# sql <- SqlRender::loadRenderTranslateSql(dbms = connection@dbms,
# sqlFilename = file.path("quartiles", "IQRComplementaryTables.sql"),
# packageName = getThisPackageName(),
# warnOnMissingParameters = TRUE,
# cdm_database_schema = cdmDatabaseSchema,
# cohort_database_schema = cohortDatabaseSchema,
# cohort_table = cohortTable,
# target_ids = paste(targetIds, collapse = ', '))
# DatabaseConnector::executeSql(connection, sql)
# outcomeBasedAnalyses <- c('TimeToDeath', 'TimeToSymptomaticProgression', 'TimeToTreatmentInitiation')
# distribAnalyses <- c('AgeAtDiagnosis', 'YearOfDiagnosis', 'CharlsonAtDiagnosis', 'PsaAtDiagnosis', outcomeBasedAnalyses)
# outcomes <- getFeatures()
#
# metricsDistribution <- data.table::data.table()
#
# for (analysis in distribAnalyses) {
# outcome <- gsub("TimeTo", "", analysis)
# outcome <- substring(SqlRender::camelCaseToTitleCase(outcome), 2)
# outcomeId <- outcomes[tolower(outcomes$name) == tolower(outcome), "cohortId"][[1]]
#
#
# if (length(outcomeId) == 0 & analysis %in% outcomeBasedAnalyses) {
# next
# }
# result <- getAtEventDistribution(connection,
# cohortDatabaseSchema,
# cdmDatabaseSchema,
# cohortTable = cohortTable,
# targetIds = targetIds,
# outcomeId = outcomeId,
# databaseId = databaseId,
# analysisName = analysis)
# metricsDistribution <- rbind(metricsDistribution, result)
# }
#
# andrData$metrics_distribution <- metricsDistribution
#
#
# sql <- SqlRender::loadRenderTranslateSql(dbms = connection@dbms,
# sqlFilename = file.path("quartiles", "RemoveComplementaryTables.sql"),
# packageName = getThisPackageName(),
# cohort_database_schema = cohortDatabaseSchema)
# DatabaseConnector::executeSql(connection, sql)
#
# delta <- Sys.time() - start
# ParallelLogger::logInfo(paste("Generating metrics distribution took",
# signif(delta, 3),
# attr(delta, "units")))

# Counting cohorts -----------------------------------------------------------------------
ParallelLogger::logInfo("Counting cohorts")
counts <- getCohortCounts(connection = connection,
Expand Down
6 changes: 6 additions & 0 deletions inst/sql/sql_server/quartiles/MedianFollowUp.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
WITH init_data AS (
SELECT cohort_definition_id,
DATEDIFF(day, cohort_start_date, cohort_end_date) AS value
FROM @cohort_database_schema.@cohort_table
WHERE cohort_definition_id IN (@target_ids)
),
19 changes: 19 additions & 0 deletions inst/sql/sql_server/quartiles/MedianTimeToTreatment.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
WITH tab as (
SELECT cohort_definition_id, cohort_start_date, min(drug_exposure_start_date) as drug_exposure_date
FROM @cohort_database_schema.@cohort_table coh
JOIN @cdm_database_schema.drug_exposure de
ON coh.subject_id = de.person_id
AND de.drug_concept_id IN (
SELECT concept_id
FROM @cohort_database_schema.drug_codesets
)
AND de.drug_exposure_start_date >= coh.cohort_start_date
AND de.drug_exposure_start_date <= coh.cohort_end_date
WHERE coh.cohort_definition_id IN (@target_ids)
GROUP BY cohort_definition_id, subject_id, cohort_start_date
ORDER BY cohort_definition_id),
init_data AS(
SELECT cohort_definition_id,
DATEDIFF(day, cohort_start_date, drug_exposure_date) AS value
FROM tab
),

0 comments on commit 7ef757b

Please sign in to comment.