library(CaseCrossover)

Introduction

This vignette describes how you can use the CaseCrossover package to perform a single case-crossover study. We will walk through all the steps needed to perform an exemplar study, and we have selected the well-studied topic of the effect of NSAIDs on gastrointestinal (GI) bleeding-related hospitalization. For simplicity, we focus on one NSAID: diclofenac.

Installation instructions

Before installing the CaseCrossover package make sure you have Java available. Java can be downloaded from www.java.com. For Windows users, RTools is also necessary. RTools can be downloaded from CRAN. See these instructions for properly configuring your R environment.

The CaseCrossover package is currently maintained in a Github repository, and has dependencies on other packages in Github. All of these packages can be downloaded and installed from within R using the drat package:

install.packages("drat")
drat::addRepo("OHDSI")
install.packages("CaseCrossover")

Once installed, you can type library(CaseCrossover) to load the package.

Overview

In the CaseCrossover package a study requires four steps:

  1. Loading data on the cases (and potential controls when performing a case-time-control analysis) from the database needed for matching.
  2. Selecting subjects to include in the study.
  3. Determining exposure status for cases (and controls) based on a definition of the risk windows.
  4. Fitting the model using conditional logistic regression.

In the following sections these steps will be demonstrated.

Configuring the connection to the server

We need to tell R how to connect to the server where the data are. CaseCrossover uses the DatabaseConnector package, which provides the createConnectionDetails function. Type ?createConnectionDetails for the specific settings required for the various database management systems (DBMS). For example, one might connect to a PostgreSQL database using this code:

connectionDetails <- createConnectionDetails(dbms = "postgresql", 
                                             server = "localhost/ohdsi", 
                                             user = "joe", 
                                             password = "supersecret")

cdmDatabaseSchema <- "my_cdm_data"
cohortDatabaseSchema <- "my_results"
cohortTable <- "my_cohorts"
cdmVersion <- "5"

The last three lines define the cdmDatabaseSchema and cohortDatabaseSchema variables,as well as the CDM version. We'll use these later to tell R where the data in CDM format live, where we have stored our cohorts of interest, and what version CDM is used. Note that for Microsoft SQL Server, databaseschemas need to specify both the database and the schema, so for example cdmDatabaseSchema <- "my_cdm_data.dbo".

Preparing the health outcome of interest and nesting cohort

We need to define the exposures and outcomes for our study. Additionally, we can specify a cohort in which to nest the study. The CDM also already contains standard cohorts in the drug_era and condition_era table that could be used if those meet the requirement of the study, but often we require custom cohort definitions. One way to define cohorts is by writing SQL statements against the OMOP CDM that populate a table of events in which we are interested. The resulting table should have the same structure as the cohort table in the CDM, meaning it should have the fields cohort_definition_id, cohort_start_date, cohort_end_date,and subject_id.

For our example study, we will rely on drug_era to define exposures, and we have created a file called vignette.sql with the following contents to define the outcome and the nesting cohort:

/***********************************
File vignette.sql 
***********************************/

IF OBJECT_ID('@cohortDatabaseSchema.@cohortTable', 'U') IS NOT NULL
  DROP TABLE @cohortDatabaseSchema.@cohortTable;

SELECT 1 AS cohort_definition_id,
    condition_start_date AS cohort_start_date,
    condition_end_date AS cohort_end_date,
    condition_occurrence.person_id AS subject_id
INTO @cohortDatabaseSchema.@cohortTable
FROM @cdmDatabaseSchema.condition_occurrence
INNER JOIN @cdmDatabaseSchema.visit_occurrence
    ON condition_occurrence.visit_occurrence_id = visit_occurrence.visit_occurrence_id
WHERE condition_concept_id IN (
        SELECT descendant_concept_id
        FROM @cdmDatabaseSchema.concept_ancestor
        WHERE ancestor_concept_id = 192671 -- GI - Gastrointestinal haemorrhage
        )
    AND visit_occurrence.visit_concept_id IN (9201, 9203);

INSERT INTO @cohortDatabaseSchema.@cohortTable 
(cohort_definition_id, cohort_start_date, cohort_end_date, subject_id)
SELECT 2 AS cohort_definition_id,
    MIN(condition_start_date) AS cohort_start_date,
    NULL AS cohort_end_date,
    person_id AS subject_id
FROM @cdmDatabaseSchema.condition_occurrence
WHERE condition_concept_id IN (
        SELECT descendant_concept_id
        FROM @cdmDatabaseSchema.concept_ancestor
        WHERE ancestor_concept_id = 80809 -- rheumatoid arthritis
        )
GROUP BY person_id;

This is parameterized SQL which can be used by the SqlRender package. We use parameterized SQL so we do not have to pre-specify the names of the CDM and cohort schemas. That way, if we want to run the SQL on a different schema, we only need to change the parameter values; we do not have to change the SQL code. By also making use of translation functionality in SqlRender, we can make sure the SQL code can be run in many different environments.

library(SqlRender)
sql <- readSql("vignette.sql")
sql <- render(sql,
              cdmDatabaseSchema = cdmDatabaseSchema, 
              cohortDatabaseSchema = cohortDatabaseSchema
              cohortTable = cohortTable)
sql <- translate(sql, targetDialect = connectionDetails$dbms)

connection <- connect(connectionDetails)
executeSql(connection, sql)

In this code, we first read the SQL from the file into memory. In the next line, we replace the three parameter names with the actual values. We then translate the SQL into the dialect appropriate for the DBMS we already specified in the connectionDetails. Next, we connect to the server, and submit the rendered and translated SQL.

If all went well, we now have a table with the outcome of interest and the nesting cohort. We can see how many events:

sql <- paste("SELECT cohort_definition_id, COUNT(*) AS count",
             "FROM @cohortDatabaseSchema.@cohortTable",
             "GROUP BY cohort_definition_id")
sql <- renderSql(sql, 
                 cohortDatabaseSchema = cohortDatabaseSchema, 
                 cohortTable = cohortTable)$sql
sql <- translateSql(sql, targetDialect = connectionDetails$dbms)$sql

querySql(connection, sql)
data.frame(cohort_definition_id = c(1,2),count=c(422274, 118430))

Extracting the data from the server

Now we can tell CaseCrossover to extract the necessary data on the cases:

caseCrossoverData <- getDbCaseCrossoverData(connectionDetails = connectionDetails,
                                            cdmDatabaseSchema = cdmDatabaseSchema,
                                            oracleTempSchema = oracleTempSchema,
                                            outcomeDatabaseSchema = cohortDatabaseSchema,
                                            outcomeTable = cohortTable,
                                            outcomeId = 1,
                                            exposureDatabaseSchema = cdmDatabaseSchema,
                                            exposureTable = "drug_era",
                                            exposureIds = 1124300,
                                            useNestingCohort = TRUE,
                                            nestingCohortDatabaseSchema = cohortDatabaseSchema,
                                            nestingCohortTable = cohortTable,
                                            nestingCohortId = 2,
                                            useObservationEndAsNestingEndDate = TRUE,
                                            getTimeControlData = TRUE)


caseCrossoverData
if (file.exists("s:/temp/vignetteCaseCrossover")){
  caseCrossoverData <- loadCaseCrossoverData("s:/temp/vignetteCaseCrossover/caseCrossoverData")
} 
if (file.exists("s:/temp/vignetteCaseCrossover")){
  caseCrossoverData
}

There are many parameters, but they are all documented in the CaseCrossover manual. In short, we are pointing the function to the table created earlier and indicating which concept ID in that table identifies the outcome. Note that it is possible to fetch the data for multiple outcomes at once for efficiency. We specify that we will use the drug_era table to identify exposures, and will only retrieve data on exposure to Diclofenac (concept ID 1124300). We furthermore specify a nesting cohort in the same table, meaning that people will be eligible to be cases if and when they fall inside the specified cohort. In this case, the nesting cohort starts when people have their first diagnosis of rheumatoid arthritis. We use the useObservationEndAsNestingEndDate argument to indicate people will stay eligible until the end of their observation period. We furthermore specify we want to retrieve data on time controls, which will be used later to adjust for time-trends in exposures, effectively turning the case-crossover study into a case-time-control study.

Data about the cases (and potential time controls) are extracted from the server and stored in the caseCrossoverData object. This object uses the package Andromeda to store information in a way that ensures R does not run out of memory, even when the data are large.

We can use the generic summary() function to view some more information of the data we extracted:

summary(caseCrossoverData)
if (file.exists("s:/temp/vignetteCaseCrossover")){
  summary(caseCrossoverData)
}

Saving the data to file

Creating the caseCrossoverData object can take considerable computing time, and it is probably a good idea to save it for future sessions. Because caseCrossoverData uses Andromeda, we cannot use R's regular save function. Instead, we'll have to use the saveCaseCrossoverData() function:

saveCaseCrossoverData(caseCrossoverData, "GiBleed")

We can use the loadCaseCrossoverData() function to load the data in a future session.

Selecting subjects

Next, we can use the data to select matched controls per case:

subjects <- selectSubjectsToInclude(caseCrossoverData = caseCrossoverData,
                                    outcomeId = 1,
                                    firstOutcomeOnly = TRUE,
                                    washoutPeriod = 183)

In this example, we specify a washout period of 180 days, meaning that cases (and controls) are required to have a minimum of 180 days of observation prior to the index date. We also specify we will only consider the first outcome per person. If a person's first outcome is within the washout period, that person will be removed from the analysis.

The subjects object is a data frame with five columns:

head(subjects)
data.frame(personId = c(3,123,345,6,234,567),
           indexDate = c("2009-10-10", "2009-10-11", "2009-10-09", "2010-05-04", "2010-05-04", "2010-05-05"), 
           isCase = c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE), 
           stratumId = c(1,2,3,4,5,6),
           observationPeriodStartDate = c("2001-10-12", "2002-01-11", "2001-05-03", "2003-02-01", "2007-01-01", "2006-03-01"))

We can show the attrition to see why cases and events were filtered:

getAttritionTable(subjects)
if (file.exists("s:/temp/vignetteCaseCrossover")){
  subjects <- readRDS("s:/temp/vignetteCaseCrossover/subjects.rds")
} 
if (file.exists("s:/temp/vignetteCaseCrossover")){
  getAttritionTable(subjects)
}

Determining exposure status

We can now evaluate the exposure status of the cases in various time windows relative to the index date:

exposureStatus <- getExposureStatus(subjects = subjects,
                                    caseCrossoverData = caseCrossoverData,
                                    exposureId = 1124300,
                                    firstExposureOnly = FALSE,
                                    riskWindowStart = -30,
                                    riskWindowEnd = 0,
                                    controlWindowOffsets = c(-60))

Here we specify we are interested in all exposures, not just the first one, and that we will use two windows per subject: a case window defined as the 30 days preceding (and including) the index date, and a control window which has the same length as the case window but is shifted 60 days backwards, so from 90 days to (and including) 60 days prior to the index date. Note that multiple control windows can be specified by specifying more control window offsets.

Exposure status is then determined based on whether an exposure overlaps with one of the windows. The resulting exposureStatus object is a data frame with six columns:

head(exposureStatus)
data.frame(personId = c(3,123,345,6,234,567),
           indexDate = c("2009-10-10", "2009-10-11", "2009-10-09", "2010-05-04", "2010-05-04", "2010-05-05"), 
           isCase = c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE), 
           stratumId = c(1,2,3,4,5,6),
           isCaseWindow = c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE), 
           exposed = c(0, 0, 0, 0, 0, 0))

Fitting the model

We can now fit the model, which is a logistic regression conditioned on the matched sets:

fit <- fitCaseCrossoverModel(exposureStatus)

fit
if (file.exists("s:/temp/vignetteCaseCrossover")){
  fit <- readRDS("s:/temp/vignetteCaseCrossover/fit.rds")
  fit
}

The generic functions print, coef, and confint are implemented for the fit object:

fit
if (file.exists("s:/temp/vignetteCaseCrossover")){
  fit <- readRDS("s:/temp/vignetteCaseCrossover/fit.rds")
  fit
}
coef(fit)
if (file.exists("s:/temp/vignetteCaseCrossover")){
  coef(fit)
}
confint(fit)
if (file.exists("s:/temp/vignetteCaseCrossover")){
  confint(fit)
}

Case-time-control

A variant of the case-crossover design is the case-time-control design. This design adjusts for time-trends in exposure by using a set of control subjects. To use this design in the CaseCrossover package, one needs to simply provide matching criteria to the selectSubjectsToInclude function:

matchingCriteria <- createMatchingCriteria(controlsPerCase = 1,
                                           matchOnAge = TRUE,
                                           ageCaliper = 2,
                                           matchOnGender = TRUE)

subjectsCtc <- selectSubjectsToInclude(caseCrossoverData = caseCrossoverData,
                                       outcomeId = 1,
                                       firstOutcomeOnly = TRUE,
                                       washoutPeriod = 183,
                                       matchingCriteria = matchingCriteria)

The other steps remain the same:

exposureStatusCtc <- getExposureStatus(subjects = subjectsCtc,
                                       caseCrossoverData = caseCrossoverData,
                                       exposureId = 1124300,
                                       firstExposureOnly = FALSE,
                                       riskWindowStart = -30,
                                       riskWindowEnd = 0,
                                       controlWindowOffsets = c(-60))

fitCtc <- fitCaseCrossoverModel(exposureStatusCtc)

fitCtc
if (file.exists("s:/temp/vignetteCaseCrossover")){
  fitCtc <- readRDS("s:/temp/vignetteCaseCrossover/fitCtc.rds")
  fitCtc
}

Acknowledgments

Considerable work has been dedicated to provide the CaseCrossover package.

citation("CaseCrossover")

Furthermore, CaseCrossover makes extensive use of the Cyclops package.

citation("Cyclops")


OHDSI/CaseCrossover documentation built on Nov. 21, 2020, 7:03 a.m.