-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCreateStrategusAnalysisSpecification.R
More file actions
185 lines (156 loc) · 6.17 KB
/
CreateStrategusAnalysisSpecification.R
File metadata and controls
185 lines (156 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
library(dplyr)
library(Strategus)
# Time-at-risks (TARs) for the outcomes of interest in your study
timeAtRisks <- tibble(
label = c("Year 1", "Year 2", "Year 3", "Year 4"),
riskWindowStart = c(0, 365, 730, 1095),
startAnchor = c("cohort start"),
riskWindowEnd = c(365, 730, 1095, 1460),
endAnchor = c("cohort end")
)
studyStartDate <- "20210101" # YYYYMMDD
studyEndDate <- "20241231" # YYYYMMDD
# Some of the settings require study dates with hyphens
studyStartDateWithHyphens <- gsub("(\\d{4})(\\d{2})(\\d{2})", "\\1-\\2-\\3", studyStartDate)
studyEndDateWithHyphens <- gsub("(\\d{4})(\\d{2})(\\d{2})", "\\1-\\2-\\3", studyEndDate)
# Shared Resources -------------------------------------------------------------
# NOTE: Generated by DownloadCohorts.R
cohortDefinitionSet <- CohortGenerator::getCohortDefinitionSet(
settingsFileName = "inst/cohorts.csv",
jsonFolder = "inst/cohorts",
sqlFolder = "inst/sql/sql_server"
)
if (any(duplicated(cohortDefinitionSet$cohortId))) {
stop("*** Error: duplicate cohort IDs found ***")
}
# Create some data frames to hold the cohorts we'll use in each analysis ---------------
# Outcomes: The outcomes for this study take values >= 100 and < 200
oList <- cohortDefinitionSet |>
filter(cohortId >= 100 & cohortId < 200) |>
mutate(outcomeCohortId = cohortId, outcomeCohortName = cohortName) |>
select(outcomeCohortId, outcomeCohortName) |>
mutate(cleanWindow = 0)
# CohortGeneratorModule --------------------------------------------------------
cgModuleSettingsCreator <- CohortGeneratorModule$new()
cohortDefinitionShared <- cgModuleSettingsCreator$createCohortSharedResourceSpecifications(cohortDefinitionSet)
cohortGeneratorModuleSpecifications <- cgModuleSettingsCreator$createModuleSpecifications(
generateStats = TRUE
)
# CohortDiagnosticsModule Settings ---------------------------------------------
cdModuleSettingsCreator <- CohortDiagnosticsModule$new()
cohortDiagnosticsModuleSpecifications <- cdModuleSettingsCreator$createModuleSpecifications(
cohortIds = cohortDefinitionSet$cohortId,
runInclusionStatistics = TRUE,
runIncludedSourceConcepts = TRUE,
runOrphanConcepts = TRUE,
runTimeSeries = FALSE,
runVisitContext = TRUE,
runBreakdownIndexEvents = TRUE,
runIncidenceRate = TRUE,
runCohortRelationship = TRUE,
runTemporalCohortCharacterization = TRUE,
minCharacterizationMean = 0.01
)
# CharacterizationModule Settings ---------------------------------------------
cModuleSettingsCreator <- CharacterizationModule$new()
# Custom covariates
customCovariateCohorts <- tibble(
cohortId = 200,
cohortName = "Treatment-requiring Diabetic Retinopathy or Macular Edema including vitrectomy"
)
customCovariateSettings <- FeatureExtraction::createCohortBasedCovariateSettings(
analysisId = 999,
covariateCohorts = customCovariateCohorts,
valueType = "binary",
startDay = -365,
endDay = 0,
)
covariateSettings <- FeatureExtraction::createDefaultCovariateSettings()
characterizationModuleSpecifications <- cModuleSettingsCreator$createModuleSpecifications(
targetIds = cohortDefinitionSet$cohortId, # NOTE: This is all T/C/I/O
outcomeIds = oList$outcomeCohortId,
minPriorObservation = 180,
dechallengeStopInterval = 0,
dechallengeEvaluationWindow = 0,
riskWindowStart = timeAtRisks$riskWindowStart,
startAnchor = timeAtRisks$startAnchor,
riskWindowEnd = timeAtRisks$riskWindowEnd,
endAnchor = timeAtRisks$endAnchor,
minCharacterizationMean = 0,
outcomeWashoutDays = rep(0, nrow(oList)),
covariateSettings = list(covariateSettings, customCovariateSettings)
)
# CohortIncidenceModule --------------------------------------------------------
ciModuleSettingsCreator <- CohortIncidenceModule$new()
tIds <- cohortDefinitionSet |>
filter(!cohortId %in% oList$outcomeCohortId) |>
pull(cohortId)
targetList <- lapply(
tIds,
function(cohortId) {
CohortIncidence::createCohortRef(
id = cohortId,
name = cohortDefinitionSet$cohortName[cohortDefinitionSet$cohortId == cohortId]
)
}
)
outcomeList <- lapply(
seq_len(nrow(oList)),
function(i) {
CohortIncidence::createOutcomeDef(
id = i,
name = cohortDefinitionSet$cohortName[cohortDefinitionSet$cohortId == oList$outcomeCohortId[i]],
cohortId = oList$outcomeCohortId[i],
cleanWindow = oList$cleanWindow[i]
)
}
)
tars <- list()
for (i in seq_len(nrow(timeAtRisks))) {
tars[[i]] <- CohortIncidence::createTimeAtRiskDef(
id = i,
startWith = gsub("cohort ", "", timeAtRisks$startAnchor[i]),
endWith = gsub("cohort ", "", timeAtRisks$endAnchor[i]),
startOffset = timeAtRisks$riskWindowStart[i],
endOffset = timeAtRisks$riskWindowEnd[i]
)
}
analysis1 <- CohortIncidence::createIncidenceAnalysis(
targets = tIds,
outcomes = seq_len(nrow(oList)),
tars = seq_along(tars)
)
irStudyWindow <- CohortIncidence::createDateRange(
startDate = studyStartDateWithHyphens,
endDate = studyEndDateWithHyphens
)
irDesign <- CohortIncidence::createIncidenceDesign(
targetDefs = targetList,
outcomeDefs = outcomeList,
tars = tars,
analysisList = list(analysis1),
studyWindow = irStudyWindow,
strataSettings = CohortIncidence::createStrataSettings(
byYear = TRUE,
byGender = TRUE,
byAge = TRUE,
ageBreaks = seq(0, 110, by = 5)
)
)
cohortIncidenceModuleSpecifications <- ciModuleSettingsCreator$createModuleSpecifications(
irDesign = irDesign$toList()
)
# NOTE: TreatmentPatterns module has been moved to a separate analysis
# See CreateStrategusAnalysisSpecificationTreatmentPatterns.R for the
# properly configured TreatmentPatterns analyses (4 separate versions)
# Create the analysis specifications ------------------------------------------
analysisSpecifications <- Strategus::createEmptyAnalysisSpecificiations() |>
Strategus::addSharedResources(cohortDefinitionShared) |>
Strategus::addModuleSpecifications(cohortGeneratorModuleSpecifications) |>
Strategus::addModuleSpecifications(cohortDiagnosticsModuleSpecifications) |>
Strategus::addModuleSpecifications(characterizationModuleSpecifications) |>
Strategus::addModuleSpecifications(cohortIncidenceModuleSpecifications)
ParallelLogger::saveSettingsToJson(
analysisSpecifications,
file.path("inst", "drScreeningStudyAnalysisSpecification.json")
)