knitr::opts_chunk$set(echo = FALSE)
library(dplyr)
library(DT)

Extracting needles from covariate_ref table

Sys.setenv(DATABASECONNECTOR_JAR_FOLDER = keyring::key_get("legendT2dmDriverPath")) # NOTE to Jin: you need to set these key-chain variables for your computer

legendT2dmConnectionDetails <- DatabaseConnector::createConnectionDetails(
  dbms = "postgresql",
  server = paste(keyring::key_get("legendt2dmServer"),
                 keyring::key_get("legendt2dmDatabase"),
                 sep = "/"),
  user = keyring::key_get("legendt2dmUser"),
  password = keyring::key_get("legendt2dmPassword"))
con <- DatabaseConnector::connect(legendT2dmConnectionDetails)
covariates <- DatabaseConnector::querySql(con, sql = "SELECT DISTINCT covariate_id, covariate_name, concept_id FROM legendt2dm_class_diagnostics.covariate_ref;", snakeCaseToCamelCase = TRUE)
DatabaseConnector::disconnect(con)

covariates <- covariates %>% mutate(covariateName = toupper(covariateName))
matchNeedle <- grep(pattern = "NEEDLE", covariates$covariateName)
matchDevice <- grep(pattern = "DEVICE_EXPOSURE", covariates$covariateName)
matches <- intersect(matchNeedle, matchDevice)
needles <- covariates[matches,]

Table contains r nrow(covariates) distinct covariateIds.

Of these, r length(matches) contain DEVICE_EXPOSURE and NEEDLE.

And, r length(unique(needles$conceptId)) unique concept_id codes.

Needle covariateIds from covariate_ref table`

datatable(needles, rownames = FALSE,
          options = list(order = list(2, 'desc')))

Limiting to PEN NEEDLE

matchNeedle <- grep(pattern = "PEN NEEDLE", covariates$covariateName)
matchDevice <- grep(pattern = "DEVICE_EXPOSURE", covariates$covariateName)
matches <- intersect(matchNeedle, matchDevice)
penNeedles <- covariates[matches,]

datatable(penNeedles, rownames = FALSE,
          options = list(order = list(2, 'desc')))

Limiting to PEN NEEDLE conceptIds

sort(unique(penNeedles$conceptId))

Limiting to NOVO NEEDLE

matchNovo <- grep(pattern = "NOVO", covariates$covariateName)
matchNeedle <- grep(pattern = "NEEDLE", covariates$covariateName)
matchDevice <- grep(pattern = "DEVICE_EXPOSURE", covariates$covariateName)
matches <- intersect(matchNovo, intersect(matchNeedle, matchDevice))
novoNeedles <- covariates[matches,]

datatable(novoNeedles, rownames = FALSE,
          options = list(order = list(2, 'desc')))

Limiting to NOVO NEEDLE

sort(unique(novoNeedles$conceptId))

Extracting needles from fitted PS models

con <- DatabaseConnector::connect(legendT2dmConnectionDetails)
psCovariates <- DatabaseConnector::querySql(con, sql = "
  SELECT DISTINCT ps_covariate_assessment.covariate_id, ps_covariate_assessment.covariate_name, concept_id 
  FROM legendt2dm_class_diagnostics.ps_covariate_assessment
  INNER JOIN legendt2dm_class_diagnostics.covariate_ref
  ON covariate_ref.covariate_id = ps_covariate_assessment.covariate_id;", snakeCaseToCamelCase = TRUE)
DatabaseConnector::disconnect(con)

psCovariates <- psCovariates %>% mutate(covariateName = toupper(covariateName))
psMatchNeedle <- grep(pattern = "NEEDLE", psCovariates$covariateName)
psNeedles <- psCovariates[psMatchNeedle,]

Fitted PS models contain r nrow(psCovariates) distinct covariateIds.

Of these, r length(psMatchNeedle) contain NEEDLE.

And, r length(unique(psNeedles$conceptId)) unique concept_id codes.

Needle covariateId from fitted PS models

datatable(psNeedles, rownames = FALSE,
          options = list(order = list(2, 'desc')))

Current fitted PS model conceptIds to exclude

sort(unique(psNeedles$conceptId))

Current conceptIds to exclude

exclude <- sort(unique(c(penNeedles$conceptId,novoNeedles$conceptId)))
exclude
paste(exclude, collapse = ";")

Limitations: would like to include VA results before modifying package



ohdsi-studies/LegendT2dm documentation built on July 4, 2025, 8:25 p.m.