OHDSI / FeatureExtraction

An R package for generating features (covariates) for a cohort using data in the Common Data Model.
http://ohdsi.github.io/FeatureExtraction/
61 stars 60 forks source link

Add covariate builder that uses cohorts to build (binary) features #96

Closed schuemie closed 1 year ago

schuemie commented 4 years ago

The default covariate builder is mostly based on the occurrence of concepts (or their ancestors), We have a covariate builder based on cohort attributes, but not one based on cohorts.

The builder could create binary features based on the occurrence of a user-defined set of cohorts in some user-specified time window,

gowthamrao commented 4 years ago

@schuemie in this case, we will have four date combinations

Features may then be constructed based on

Plus - we may want to support observation_period in relation to f.cohort dates, or c.cohort dates.

Do you anticipate the covariate builder to support all these scenarios?

schuemie commented 4 years ago

No, I was thinking of adhering to the current pattern used in FeatureExtraction, so allowing the user to specify 1 start and 1 end date relative to the index date (= start of the cohort of interest), and maybe an option to choose if the feature cohort start should be in the lookback window, or whether there needs to be overlap with the lookback window.

jreps commented 4 years ago

I have code in the PLP skeletons - we do this for the simple models: https://github.com/OHDSI/StudyProtocolSandbox/blob/master/SkeletonPredictionStudy/R/CohortCovariateCode.R (it even ables counts rather than binary) in the recent studied I also included age interaction: https://github.com/ohdsi-studies/Covid19PredictionStudies/blob/master/CovidVulnerabilityIndex/R/CohortCovariateCode.R

This is an example of running it: https://github.com/OHDSI/PredictionComparison/blob/andromeda/R/atriaModel.R

javier-gracia-tabuenca-tuni commented 3 years ago

I think I solved this by duplicating DomainConcept.sql, and replacing domain_table with cohort_table. Then, I created a temporal table with the list of cohort_ids and cohort_names that is used to calculate the overlap and name the covariates.

Modified DomainConcept.sql, named to CohortOverlap.sql CohortOverlap.sql.txt

Can be used like

# cohortSetReference  as the table used in CohortDiagnostics 
# cohorts defined in atlas-demo.ohdsi.org
cohortSetReference <- tibble(
  atlasId = c( 1776012, 1776013, 1776018  ) , 
  atlasName =c( "Asthma", "Last condition", "First condition" ),
  cohortId = c( 1776012, 1776013, 1776018  ),
  name = c("Asthma",  "Last condition", "First condition" )
)

# create temp table in server 
overlapCohortsTable_name <- "tmp_table_cohorts_overlap"
overlapCohortsTable <- cohortSetReference %>% filter(cohortId %in% c("1776013", "1776018"))

DatabaseConnector::insertTable(
  connection,
  tableName = overlapCohortsTable_name,
  data = as.data.frame(overlapCohortsTable),
  dropTableIfExists = TRUE,
  createTable = TRUE,
  tempTable = FALSE,
  oracleTempSchema = oracleTempSchema)

cohortOverlap <- createAnalysisDetails(
  analysisId = 51,
  sqlFileName = "CohortOverlap.sql",
  parameters = list(
    analysisId = 51,
    analysisName = "Cohort overlap", 
    domainId = "Cohort overlap", 
    #domainTable = cohortTable, 
    #domainConceptId = "cohort_definition_id",
    domain_start_date = "cohort_start_date",
    domain_end_date = "cohort_end_date", 
    #
    cohort_ids = "1776013, 1776018", 
    overlap_cohorts_table = overlapCohortsTable_name
  ),
  includedCovariateConceptIds = c(),
  addDescendantsToInclude = FALSE,
  excludedCovariateConceptIds = c(),
  addDescendantsToExclude = FALSE,
  includedCovariateIds = c()
)

detTempCovSet <- createDetailedTemporalCovariateSettings(
  analyses = list(
    cohortOverlap
  ), 
  temporalStartDays = c(-365,   0,  365*5,   365*10,  365*20),
  temporalEndDays = c(  -1, 365*5, 365*10,   365*20,   365*50)
)

I can write a more reproducible example if you need it.

schuemie commented 3 years ago

@jreps 's code has a very nice implementation that allows you to create different kinds of features of the same cohort, like binary, counts, etc. We should reuse that here.

schuemie commented 2 years ago

I'll start working on this, as I need it for a project.

anthonysena commented 2 years ago

OK thanks for the head's up here @schuemie. Do you plan to use the implementation from PLP as you suggested in the earlier comment? I am just curious about the design at a high level.

schuemie commented 2 years ago

Here's my initial version: https://github.com/OHDSI/FeatureExtraction/commit/fddbdd6741aa0a952a4ae081e0fa5d40b30a746f

Some of the thinking so far: