-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_AssociationRuleMining.R
125 lines (92 loc) · 5.38 KB
/
run_AssociationRuleMining.R
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
library(DatabaseConnector)
library(SqlRender)
library(Eunomia)
library(FeatureExtraction)
library(AssociationRuleMining)
devtools::load_all()
#### Connect to the database ####
connectionDetails <- createConnectionDetails(
dbms = "",
server = "",
user ="",
password = "",
port = 0)
#### Define database parameters ####
cdmDatabaseSchema = ""
resultsDatabaseSchema = ""
cohortTable <- ""
#cohortId <- 1
rowIdField <- "subject_id"
#### Define location and names of input/output files ####
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
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.
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
getInputFileForAssociationRules(covariateDataObject = covariateData, fileToSave = arm_inputFile)
## Run Apriori
apriori_associationSets <- runAssociationRules(algorithm = "Apriori",
inputFile = arm_inputFile,
outputFile = arm_outputFile,
minsup = 0.5 )
## Run Eclat
eclat_associationSets <- runAssociationRules(algorithm = "Eclat",
inputFile = arm_inputFile,
outputFile = arm_outputFile,
minsup = 0.5 )
## Run FP-Growth
fpgrowth_associationSets <- runAssociationRules(algorithm = "FP-Growth",
inputFile = arm_inputFile,
outputFile = arm_outputFile,
minsup = 0.5 )
## Run Relim
relim_associationSets <- runAssociationRules(algorithm = "Relim",
inputFile = arm_inputFile,
outputFile = arm_outputFile,
minsup = 0.5 )
#### Frequent pattern mining ####
## Prepare the data
getInputFileForFrequentPatterns(covariateDataObject = TemporalcovariateData, fileToSave = fpm_inputFile)
## Run SPAM
spam_frequentPatterns <- runFrequentPatterns(algorithm = "SPAM",
inputFile = fpm_inputFile,
outputFile = fpm_outputFile,
minsup = 0.5,
showID = TRUE)
## Run SPADE
spade_frequentPatterns <- runFrequentPatterns(algorithm = "SPADE",
inputFile = fpm_inputFile,
outputFile = fpm_outputFile,
minsup = 0.5,
showID = TRUE)
## Run prefixSpan
pS_frequentPatterns <- runFrequentPatterns(algorithm = "prefixSpan",
inputFile = fpm_inputFile,
outputFile = fpm_outputFile,
minsup = 0.5,
showID = TRUE)