Skip to content

Commit

Permalink
createPs() now checks if filtering of the covariate data is necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
schuemie committed Oct 24, 2024
1 parent 6637f10 commit d91d554
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 21 deletions.
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
CohortMethod 5.4.1
==================

Changes:

1. `createPs()` now checks if filtering of the covariate data is necessary (either because subject have been removed from the study population or because `excludeCovariateIds` or `includeCovariateIds` was specified). If no filtering is required, no extra copy of the covariate data data is created, saving IO time.

CohortMethod 5.4.0
==================

Expand Down
52 changes: 31 additions & 21 deletions R/PsFunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,29 +126,39 @@ createPs <- function(cohortMethodData,
sampled <- FALSE
ref <- NULL
} else {
covariates <- cohortMethodData$covariates %>%
filter(.data$rowId %in% local(population$rowId))
rowIds <- cohortMethodData$covariates %>%
distinct(.data$rowId) %>%
pull()
if (all(rowIds %in% population$rowId) &&
length(includeCovariateIds) == 0 &&
length(excludeCovariateIds) == 0) {
# No filtering necessary, send to tidyCovariateData:
covariateData <- FeatureExtraction::tidyCovariateData(cohortMethodData)
} else {
# Need filtering here before sending it to tidyCovariateData:
covariates <- cohortMethodData$covariates %>%
filter(.data$rowId %in% local(population$rowId))

if (length(includeCovariateIds) != 0) {
covariates <- covariates %>%
filter(.data$covariateId %in% includeCovariateIds)
}
if (length(excludeCovariateIds) != 0) {
covariates <- covariates %>%
filter(!.data$covariateId %in% excludeCovariateIds)
if (length(includeCovariateIds) != 0) {
covariates <- covariates %>%
filter(.data$covariateId %in% includeCovariateIds)
}
if (length(excludeCovariateIds) != 0) {
covariates <- covariates %>%
filter(!.data$covariateId %in% excludeCovariateIds)
}
filteredCovariateData <- Andromeda::andromeda(
covariates = covariates,
covariateRef = cohortMethodData$covariateRef,
analysisRef = cohortMethodData$analysisRef
)
metaData <- attr(cohortMethodData, "metaData")
metaData$populationSize <- nrow(population)
attr(filteredCovariateData, "metaData") <- metaData
class(filteredCovariateData) <- "CovariateData"
covariateData <- FeatureExtraction::tidyCovariateData(filteredCovariateData)
close(filteredCovariateData)
}
filteredCovariateData <- Andromeda::andromeda(
covariates = covariates,
covariateRef = cohortMethodData$covariateRef,
analysisRef = cohortMethodData$analysisRef
)
metaData <- attr(cohortMethodData, "metaData")
metaData$populationSize <- nrow(population)
attr(filteredCovariateData, "metaData") <- metaData
class(filteredCovariateData) <- "CovariateData"

covariateData <- FeatureExtraction::tidyCovariateData(filteredCovariateData)
close(filteredCovariateData)
on.exit(close(covariateData))
covariates <- covariateData$covariates
attr(population, "metaData")$deletedInfrequentCovariateIds <- attr(covariateData, "metaData")$deletedInfrequentCovariateIds
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-parameterSweep.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ test_that("Create study population functions", {
})

test_that("Propensity score functions", {
# No filtering required:
studyPop <- cohortMethodData$cohorts |>
collect()
ps <- createPs(cohortMethodData, studyPop)

studyPop <- createStudyPopulation(cohortMethodData,
outcomeId = 194133,
removeSubjectsWithPriorOutcome = TRUE,
Expand All @@ -85,6 +90,7 @@ test_that("Propensity score functions", {
# Cross-validation:
ps <- createPs(cohortMethodData, studyPop)


ps <- createPs(cohortMethodData, studyPop, prior = createPrior("laplace", 0.1, exclude = 0))
expect_lt(0.65, computePsAuc(ps)[1])

Expand Down

0 comments on commit d91d554

Please sign in to comment.