knitr::opts_chunk$set(echo = TRUE) withr::local_envvar( R_USER_CACHE_DIR = tempfile(), EUNOMIA_DATA_FOLDER = Sys.getenv("EUNOMIA_DATA_FOLDER", unset = tempfile()) )
For this example we take the case of Viral Sinusitis and several treatments as events. We set our minEraDuration = 7
, minCombinationDuration = 7
and combinationWindow = 7
. We treat multiple events of Viral Sinusitis as separate cases by setting concatTargets = FALSE
. When set to TRUE
it would append multiple cases, which might be useful for time invariant target cohorts like chronic conditions.
library(CDMConnector) library(dplyr) library(TreatmentPatterns) cohortSet <- readCohortSet( path = system.file(package = "TreatmentPatterns", "exampleCohorts") ) con <- DBI::dbConnect( drv = duckdb::duckdb(), dbdir = eunomiaDir() ) cdm <- cdmFromCon( con = con, cdmSchema = "main", writeSchema = "main" ) cdm <- generateCohortSet( cdm = cdm, cohortSet = cohortSet, name = "cohort_table", overwrite = TRUE ) cohorts <- cohortSet %>% # Remove 'cohort' and 'json' columns select(-"cohort", -"json", -"cohort_name_snakecase") %>% mutate(type = c("event", "event", "event", "event", "exit", "event", "event", "target")) %>% rename( cohortId = "cohort_definition_id", cohortName = "cohort_name", ) outputEnv <- computePathways( cohorts = cohorts, cohortTableName = "cohort_table", cdm = cdm, minEraDuration = 7, combinationWindow = 7, minPostCombinationDuration = 7, concatTargets = FALSE ) results <- export( andromeda = outputEnv, minCellCount = 1, nonePaths = TRUE, outputPath = tempdir() )
Now that we ran our TreatmentPatterns analysis and have exported our results, we can evaluate the output. The export()
function in TreatmentPatterns returns an R6 class of TreatmentPatternsResults
. All results are query-able from this object. Additionally the files are written to the specified outputPath
. If no outputPath
is set, only the result object is returned, and no files are written.
If you would like to save the results to csv-, or zip-file after the fact you can still do this. Or upload it to a database:
# Save to csv-, zip-file results$saveAsCsv(path = tempdir()) results$saveAsZip(path = tempdir(), name = "tp-results.zip") # Upload to database connectionDetails <- DatabaseConnector::createConnectionDetails( dbms = "sqlite", server = file.path(tempdir(), "db.sqlite") ) results$uploadResultsToDb( connectionDetails = connectionDetails, schema = "main", prefix = "tp_", overwrite = TRUE, purgeSiteDataBeforeUploading = FALSE )
The treatmentPathways file contains all the pathways found, with a frequency, pairwise stratified by age group, sex and index year.
head(results$treatment_pathways)
We can see the pathways contain the treatment names we provided in our event cohorts. Besides that we also see the paths are annoted with a +
or -
. The +
indicates two treatments are a combination therapy, i.e. amoxicillin+clavulanate
is a combination of amoxicillin and clavulanate. The -
indicates a switch between treatments, i.e. acetaminophen-penicillinv
is a switch from acetaminophen to penicillin v. Note that these combinations and switches can occur in the same pathway, i.e. acetaminophen-amoxicillin+clavulanate
. The first treatment is acetaminophen that switches to a combination of amoxicillin and clavulanate.
The countsAge, countsSex, and countsYear contain counts per age, sex, and index year.
head(results$counts_age) head(results$counts_sex) head(results$counts_year)
The summaryEventDuration contains summary statistics from different events, across all found "lines". A "line" is equal to the level in the Sunburst or Sankey diagrams. The summary statistics allow for plotting of boxplots with the plotEventDuration()
function.
results$plotEventDuration()
Not that besides our events there are two extra rows: mono-event, and combination-event. These are both types of events on average.
We see that most events last between 0 and 100 days. We can see that for combination-events and amoxicillin+clavulanate there is a tendency for events to last longer than that. amoxicillin+clavulanate most likely skews the duration in the combination-events group.
We can alter the x-axis to get a clearer view of the durations of the events:
results$plotEventDuration() + ggplot2::xlim(0, 100)
Now we can more clearly investigate particular treatments. We can see that penicilin v tends to last quite short across all treatment lines, while aspirin and acetaminophen seem to skew to a longer duration.
Additionally we can also set a minCellCount
for the individual events.
results$plotEventDuration(minCellCount = 10) + ggplot2::xlim(0, 100)
The metadata file is a file that contains information about the circumstances the analysis was performed in, and information about R, and the CDM.
results$metadata
From the filtered treatmentPathways file we are able to create a sunburst plot.
The inner most layer is the first event that occurs, going outwards. This aligns with the event duration plot we looked at earlier.
results$plotSunburst()
We can also create a Sankey Diagram, which in theory displays the same data. Additionally you see the Stopped node in the Sankey diagram. This indicates the end of the pathway. It is mostly a practical addition so that single layer Sankey diagrams can still be plotted.
results$plotSankey()
# Close Andromeda objects Andromeda::close(outputEnv) # Close connection to CDM Reference DBI::dbDisconnect(conn = con) rm(defaultSettings, minEra60, splitAcuteTherapy, includeEndDate, con, cdm)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.