Skip to content

Commit f6309c4

Browse files
committed
CHADS2-VASc statistics
1 parent 82bdcea commit f6309c4

2 files changed

Lines changed: 92 additions & 1 deletion

File tree

‎inst/sql/sql_server/Chads2Vasc.sql‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ SELECT o.cohort_definition_id,
280280
END AS p90_value
281281
INTO @covariate_table
282282
FROM #chads2Vasc_prep2 p
283-
CROSS JOIN #chads2Vasc_stats o
283+
INNER JOIN #chads2Vasc_stats o
284+
ON p.cohort_definition_id = o.cohort_definition_id
284285
{@included_cov_table != ''} ? {WHERE 1000 + @analysis_id IN (SELECT id FROM @included_cov_table)}
285286
GROUP BY o.count_value,
286287
o.count_no_value,
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
library(testthat)
2+
library(FeatureExtraction)
3+
4+
test_that("CHADS2-VASc quartiles are calculated within each cohort", {
5+
skip_if_not(dbms == "sqlite" && exists("eunomiaConnection"))
6+
7+
syntheticPersonIds <- c(-334001L, -334002L)
8+
DatabaseConnector::executeSql(
9+
connection = eunomiaConnection,
10+
sql = paste0(
11+
"DELETE FROM ", eunomiaCdmDatabaseSchema,
12+
".person WHERE person_id IN (", paste(syntheticPersonIds, collapse = ", "), ")"
13+
)
14+
)
15+
on.exit(
16+
DatabaseConnector::executeSql(
17+
connection = eunomiaConnection,
18+
sql = paste0(
19+
"DELETE FROM ", eunomiaCdmDatabaseSchema,
20+
".person WHERE person_id IN (", paste(syntheticPersonIds, collapse = ", "), ")"
21+
)
22+
),
23+
add = TRUE
24+
)
25+
DatabaseConnector::insertTable(
26+
connection = eunomiaConnection,
27+
tableName = "person",
28+
databaseSchema = eunomiaCdmDatabaseSchema,
29+
data = data.frame(
30+
personId = syntheticPersonIds,
31+
genderConceptId = c(8507L, 8507L),
32+
yearOfBirth = c(1900L, 2000L),
33+
raceConceptId = c(0L, 0L),
34+
ethnicityConceptId = c(0L, 0L)
35+
),
36+
dropTableIfExists = FALSE,
37+
tempTable = FALSE,
38+
createTable = FALSE,
39+
progressBar = FALSE,
40+
camelCaseToSnakeCase = TRUE
41+
)
42+
43+
cohort <- data.frame(
44+
cohortDefinitionId = c(1, 2),
45+
subjectId = syntheticPersonIds,
46+
cohortStartDate = as.Date("2020-01-01"),
47+
cohortEndDate = as.Date("2020-01-01")
48+
)
49+
cohortTableName <- paste0("#chads2vasc_", tableSuffix)
50+
DatabaseConnector::insertTable(
51+
connection = eunomiaConnection,
52+
tableName = cohortTableName,
53+
data = cohort,
54+
dropTableIfExists = TRUE,
55+
tempTable = TRUE,
56+
createTable = TRUE,
57+
progressBar = FALSE,
58+
camelCaseToSnakeCase = TRUE
59+
)
60+
61+
covariateData <- getDbCovariateData(
62+
connection = eunomiaConnection,
63+
cdmDatabaseSchema = eunomiaCdmDatabaseSchema,
64+
cohortTable = cohortTableName,
65+
cohortTableIsTemp = TRUE,
66+
cohortIds = c(1, 2),
67+
covariateSettings = createCovariateSettings(useChads2Vasc = TRUE),
68+
aggregated = TRUE
69+
)
70+
on.exit(Andromeda::close(covariateData), add = TRUE)
71+
72+
summaries <- dplyr::collect(covariateData$covariatesContinuous)
73+
summaries <- summaries[summaries$covariateId == 1904, ]
74+
75+
expect_equal(nrow(summaries), 2)
76+
elderlySummary <- summaries[summaries$cohortDefinitionId == 1, ]
77+
youngSummary <- summaries[summaries$cohortDefinitionId == 2, ]
78+
79+
# The elderly person receives two age points. The young male cohort has
80+
# neither age, sex, nor condition-derived points. Before the cohort-keyed
81+
# join, its zero-score distribution could set the elderly cohort's quartiles
82+
# to zero.
83+
expect_equal(youngSummary$maxValue, 0)
84+
expect_true(all(c(
85+
elderlySummary$minValue,
86+
elderlySummary$p25Value,
87+
elderlySummary$medianValue,
88+
elderlySummary$p75Value
89+
) >= 2))
90+
})

0 commit comments

Comments
 (0)