-
Notifications
You must be signed in to change notification settings - Fork 1
/
FP_example_workflow_Eunomia.Rmd
216 lines (173 loc) · 7.22 KB
/
FP_example_workflow_Eunomia.Rmd
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
---
title: "An example for Frequent Pattern Mining using the Eunomia package"
date: "11/12/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(DatabaseConnector)
library(SqlRender)
library(Eunomia)
library(FeatureExtraction)
library(AssociationRuleMining)
devtools::load_all()
```
### Connect to the database
```{r}
### Define database parameters
cdmdatabaseschema = "main"
resultsdatabaseschema = "main"
fpm_inputFile = "fpm_testing.txt"
fpm_outputFile_SPAM = "fpm_testingResults_SPAM.txt"
fpm_outputFile_SPADE = "fpm_testingResults_SPADE.txt"
fpm_outputFile_prefixSpan = "fpm_testingResults_prefixSpan.txt"
fpm_outputFile_Clasp = "fpm_testingResults_Clasp.txt"
fpm_outputFile_CMClasp = "fpm_testingResults_CMClasp.txt"
fpm_outputFile_MaxSP = "fpm_testingResults_MaxSP.txt"
fpm_outputFile_VMSP = "fpm_testingResults_VMSP.txt"
fpm_outputFile_VGEN = "fpm_testingResults_VGEN.txt"
fpm_outputFile_RuleGrowth = "fpm_testingResults_RuleGrowth.txt"
fpm_outputFile_ERMiner = "fpm_testingResults_ERMiner.txt"
connectionDetails <- Eunomia::getEunomiaConnectionDetails()
connection <- connect(connectionDetails)
#on.exit(DatabaseConnector::disconnect(connection)) #Close db connection on error or exit
```
### Define cohort
```{r}
# Define cohort
cohort <- "SELECT person_id AS subject_id,
condition_start_date AS cohort_start_date
INTO #diagnoses
FROM @cdm.condition_occurrence
WHERE condition_concept_id IN (
SELECT descendant_concept_id
FROM @cdm.concept_ancestor
WHERE ancestor_concept_id = 4329847 -- Myocardial infarction
)
AND condition_concept_id NOT IN (
SELECT descendant_concept_id
FROM @cdm.concept_ancestor
WHERE ancestor_concept_id = 314666 -- Old myocardial infarction
);
INSERT INTO @cdm.cohort (subject_id, cohort_start_date, cohort_definition_id)
SELECT subject_id,
cohort_start_date,
CAST (1 AS INT) AS cohort_definition_id
FROM #diagnoses
INNER JOIN @cdm.visit_occurrence
ON subject_id = person_id
AND cohort_start_date >= visit_start_date
AND cohort_start_date <= visit_end_date
WHERE visit_concept_id IN (9201, 9203, 262); -- Inpatient or ER;"
renderTranslateExecuteSql(connection, cohort, cdm = cdmdatabaseschema)
sql <- "ALTER TABLE #diagnoses ADD cohort_definition_id INT NOT NULL DEFAULT(1)"
# Execute the script to receive the data
renderTranslateExecuteSql(connection, sql)
querySql(connection, "SELECT count(*) FROM diagnoses;")
```
### Get the data and close the connection
```{r}
# Define covariate settings
TemporalcovariateSettings_eunomia <- createTemporalCovariateSettings(useConditionOccurrence = TRUE,
temporalStartDays = seq(-(60*365), -1, by = 1) ,
temporalEndDays = seq(-(60*365)+1, 0, by = 1))
# Extract covariates
TemporalcovariateData_eunomia <- getDbCovariateData(connection = connection,
cdmDatabaseSchema = cdmdatabaseschema,
cohortDatabaseSchema = resultsdatabaseschema,
cohortTable = "diagnoses",
rowIdField = "subject_id",
covariateSettings = TemporalcovariateSettings_eunomia,
cohortTableIsTemp = TRUE)
disconnect(connection)
```
#### Frequent pattern mining ####
## Prepare the data
```{r}
testData <- getInputFileForFrequentPatterns(covariateDataObject = TemporalcovariateData_eunomia, fileToSave = fpm_inputFile)
```
## Run SPAM
```{r}
spam_frequentPatterns <- runFrequentPatterns(algorithm = "SPAM",
inputFile = fpm_inputFile,
outputFile = fpm_outputFile_SPAM,
minsup = 0.5,
showID = TRUE)
head(spam_frequentPatterns)
```
## Run SPADE
```{r}
spade_frequentPatterns <- runFrequentPatterns(algorithm = "SPADE",
inputFile = fpm_inputFile,
outputFile = fpm_outputFile_SPADE,
minsup = 0.5,
showID = TRUE)
head(spade_frequentPatterns)
```
## Run prefixSpan
```{r}
pS_frequentPatterns <- runFrequentPatterns(algorithm = "prefixSpan",
inputFile = fpm_inputFile,
outputFile = fpm_outputFile_prefixSpan,
minsup = 0.5,
showID = TRUE)
head(pS_frequentPatterns)
```
## Run Clasp
```{r}
clasp_frequentPatterns <- runFrequentPatterns(algorithm = "Clasp",
inputFile = fpm_inputFile,
outputFile = fpm_outputFile_Clasp,
minsup = 0.50,
showID = TRUE )
head(clasp_frequentPatterns)
```
## Run CM-Clasp
```{r}
cmclasp_frequentPatterns <- runFrequentPatterns(algorithm = "CM-Clasp",
inputFile = fpm_inputFile,
outputFile = fpm_outputFile_CMClasp,
minsup = 0.50,
showID = TRUE )
head(cmclasp_frequentPatterns)
```
## Run VMSP
```{r}
vmsp_frequentPatterns <- runFrequentPatterns(algorithm = "VMSP",
inputFile = fpm_inputFile,
outputFile = fpm_outputFile_VMSP,
minsup = 0.50,
showID = TRUE )
head(vmsp_frequentPatterns)
```
## Run VGEN
```{r}
vgen_frequentPatterns <- runFrequentPatterns(algorithm = "VGEN",
inputFile = fpm_inputFile,
outputFile = fpm_outputFile_VGEN,
minsup = 0.50,
showID = TRUE )
head(vgen_frequentPatterns)
```
## Run RuleGrowth
```{r}
ruleGrowth_frequentPatterns <- runFrequentPatterns(algorithm = "RuleGrowth",
inputFile = fpm_inputFile,
outputFile = fpm_outputFile_RuleGrowth,
minsup = 0.50,
minconf = 0.50,
showID = FALSE #Does not retrieve IDs
)
head(ruleGrowth_frequentPatterns)
```
## Run RuleGrowth
```{r}
erminer_frequentPatterns <- runFrequentPatterns(algorithm = "ERMiner",
inputFile = fpm_inputFile,
outputFile = fpm_outputFile_ERMiner,
minsup = 0.50,
minconf = 0.5,
showID = TRUE #Does not retrieve IDs
)
head(erminer_frequentPatterns)
```