-
Notifications
You must be signed in to change notification settings - Fork 1
/
AR_example_workflow_Eunomia.Rmd
132 lines (105 loc) · 4.02 KB
/
AR_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
---
title: "An example for Association Rule Mining using the Eunomia package"
date: "11/12/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(results = 'hide')
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"
arm_inputFile = "testing.txt"
arm_outputFile = "testingResults.txt"
connectionDetails <- Eunomia::getEunomiaConnectionDetails()
connection <- connect(connectionDetails)
#on.exit(DatabaseConnector::disconnect(connection)) #Close db connection on error or exit
```
#### Define cohort ####
There are two options for defining a cohort:
- 1) Define the cohort in ATLAS and export the SQL file
- 2) define it locally within R
```{r}
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}
#### Feature Extraction ####
covariateSettings <- createCovariateSettings(useConditionOccurrenceAnyTimePrior = TRUE,
#includedCovariateIds = c(),
#includedCovariateConceptIds = c()
)
covariateData_eunomia <- getDbCovariateData(connection = connection, cdmDatabaseSchema = cdmdatabaseschema, cohortDatabaseSchema = resultsDatabaseSchema, cohortTable = "diagnoses", rowIdField = "subject_id", covariateSettings = covariateSettings, cohortTableIsTemp = TRUE)
disconnect(connection)
```
```{r}
getInputFileForAssociationRules(covariateDataObject = covariateData_eunomia, fileToSave = arm_inputFile)
```
## Run Apriori
```{r}
apriori_associationSets <- runAssociationRules(algorithm = "Apriori",
inputFile = arm_inputFile,
outputFile = arm_outputFile,
minsup = 0.5 )
head(apriori_associationSets)
```
## Run Eclat
```{r}
eclat_associationSets <- runAssociationRules(algorithm = "Eclat",
inputFile = arm_inputFile,
outputFile = arm_outputFile,
minsup = 0.5 )
head(eclat_associationSets)
```
## Run FP-Growth
```{r}
fpgrowth_associationSets <- runAssociationRules(algorithm = "FP-Growth",
inputFile = arm_inputFile,
outputFile = arm_outputFile,
minsup = 0.5 )
head(fpgrowth_associationSets)
```
## Run Relim
```{r}
relim_associationSets <- runAssociationRules(algorithm = "Relim",
inputFile = arm_inputFile,
outputFile = arm_outputFile,
minsup = 0.5 )
head(relim_associationSets)
```