-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_AssociationRuleMining.Rmd
168 lines (131 loc) · 5.74 KB
/
run_AssociationRuleMining.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
---
title: "Code to run the AssociationRuleMining package"
author: "Solon Ioannou"
date: "11/12/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(DatabaseConnector)
library(SqlRender)
library(Eunomia)
library(FeatureExtraction)
library(AssociationRuleMining)
devtools::load_all()
```
#### Connect to the database ####
```{r}
connectionDetails <- createConnectionDetails(
dbms = "",
server = "",
user ="",
password = "",
port = 0)
```
#### Define database parameters ####
```{r}
cdmDatabaseSchema = ""
resultsDatabaseSchema = ""
cohortTable <- ""
#cohortId <-
rowIdField <- "subject_id"
```
#### Define location and names of input/output files ####
```{r}
arm_inputFile <- ".txt" #This is the input file containing structured data for the algorithms. Should be a .txt file.
arm_outputFile <- ".txt" # This is where the results output will be saved. Should be a .txt file
fpm_inputFile <- ".txt" #This is the input file containing structured data for the algorithms. Should be a .txt file.
fpm_outputFile <- ".txt" # This is where the results output will be saved. Should be a .txt file
```
#### Define covariate settings and Construct covariate data ####
##### For Assoiation rules
```{r}
covariateSettings <- FeatureExtraction::createCovariateSettings(useConditionOccurrenceAnyTimePrior = TRUE,
useDrugExposureAnyTimePrior = TRUE)
covariateData <- FeatureExtraction::getDbCovariateData(connectionDetails = connectionDetails,
cdmDatabaseSchema = cdmDatabaseSchema,
cohortDatabaseSchema = cohortDatabaseSchema,
cohortTable = cohortTable,
cohortId = cohortId,
covariateSettings = covariateSettings,
...)
```
##### For Frequent pattern mining covariates need to have a timeId
###### NOTE: For the following covariate settings as they are defined below, the construction time might be quite long.
```{r}
temporalCovariateSettings <- FeatureExtraction::createTemporalCovariateSettings(useConditionOccurrence = TRUE,
useDrugExposure = TRUE,
temporalStartDays = seq(-(99*365), -1, by = 1) ,
temporalEndDays = seq(-(99*365)+1, 0, by = 1))
temporalCovariateData <- FeatureExtraction::getDbCovariateData(connection = connection,
cdmDatabaseSchema = cdmdatabaseschema,
cohortDatabaseSchema = resultsdatabaseschema,
cohortTable = cohortTable,
rowIdField = "subject_id",
covariateSettings = temporalCovariateSettings,
...)
```
#### Association rule mining ####
## Prepare the data
```{r}
getInputFileForAssociationRules(covariateDataObject = covariateData, fileToSave = arm_inputFile)
```
## Run Apriori
```{r}
apriori_associationSets <- runAssociationRules(algorithm = "Apriori",
inputFile = arm_inputFile,
outputFile = arm_outputFile,
minsup = 0.5 )
```
## Run Eclat
```{r}
eclat_associationSets <- runAssociationRules(algorithm = "Eclat",
inputFile = arm_inputFile,
outputFile = arm_outputFile,
minsup = 0.5 )
```
## Run FP-Growth
```{r}
fpgrowth_associationSets <- runAssociationRules(algorithm = "FP-Growth",
inputFile = arm_inputFile,
outputFile = arm_outputFile,
minsup = 0.5 )
```
## Run Relim
```{r}
relim_associationSets <- runAssociationRules(algorithm = "Relim",
inputFile = arm_inputFile,
outputFile = arm_outputFile,
minsup = 0.5 )
```
#### Frequent pattern mining ####
## Prepare the data
```{r}
getInputFileForFrequentPatterns(covariateDataObject = TemporalcovariateData, fileToSave = fpm_inputFile)
```
## Run SPAM
```{r}
spam_frequentPatterns <- runFrequentPatterns(algorithm = "SPAM",
inputFile = fpm_inputFile,
outputFile = fpm_outputFile,
minsup = 0.5,
showID = TRUE)
```
## Run SPADE
```{r}
spade_frequentPatterns <- runFrequentPatterns(algorithm = "SPADE",
inputFile = fpm_inputFile,
outputFile = fpm_outputFile,
minsup = 0.5,
showID = TRUE)
```
## Run prefixSpan
```{r}
pS_frequentPatterns <- runFrequentPatterns(algorithm = "prefixSpan",
inputFile = fpm_inputFile,
outputFile = fpm_outputFile,
minsup = 0.5,
showID = TRUE)
```