-
Notifications
You must be signed in to change notification settings - Fork 0
/
regmodels.r
322 lines (256 loc) · 8.46 KB
/
regmodels.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
library(rpart)
library(randomForest)
library(gbm)
library(kernlab)
source("monitor.r")
GetMethodInfo <- function(methodID) {
# Performs Sobol sampling of parameters of the specified regression method
#
# Args:
# methodID: ID of the regression method to be sampled
# Generate Sobol sequence for the specified regression method
sobolSeq <- randtoolbox::sobol(n = methodsParams[[methodID]]$sobolLen,
dim = length(methodsParams[[methodID]]$names),
init = TRUE,
scrambling = 1,
seed = 1)
paramSeq <- NULL
# Convert values in Sobol sequence to actual parameter values
for (j in 1:ncol(sobolSeq)) {
# Extract parameter properties
minVal <- methodsParams[[methodID]]$minVals[j]
maxVal <- methodsParams[[methodID]]$maxVals[j]
dgts <- methodsParams[[methodID]]$dgts[j]
# Convert Sobol sequence:
# 1) Select all Sobol numbers for the parameter
# 2) Convert Sobol numbers to parameter range
# 3) Add converted numbers to final parameter sequence
paramCol <- sobolSeq[, j]
paramCol <- sapply(paramCol, ConvertSobol, minVal = minVal,
maxVal = maxVal, dgts = dgts)
paramSeq <- cbind(paramSeq, paramCol)
colnames(paramSeq)[ncol(paramSeq)] <-
methodsParams[[methodID]]$names[ncol(paramSeq)]
}
# Populate methodInfo data structure
methodInfo <- list()
methodInfo$methodID <- methodID
methodInfo$methodName <- methodNames[[methodID]]
methodInfo$sobolSeq <- sobolSeq
Monitor(paste("Method ID:", methodInfo$methodID))
Monitor(paste("Method Name:", methodInfo$methodName))
Monitor(paste("Sobol sequence:"))
Monitor(methodInfo$sobolSeq)
return(methodInfo)
}
ConvertSobol <- function(minVal, maxVal, sobol, dgts) {
# Converts number from sobol hypercube to a number from the given parameter
# range
#
# Args:
# minVal: minimal value of the target value range
# maxVal: maximal value of the target value range
# sobol: number from Sobol hypercube to be converted
# dgts: number of digits after decimal point
return(minVal + round((maxVal - minVal) * sobol, dgts))
}
GetMethodParams <- function(methodInfo, sobolIter, sysDataInfo, sampleSizeID, sampleID) {
# Sobol parameters to convert into method parameters
sobolParams <- methodInfo$sobolSeq[sobolIter, ]
# Data properties
obsNum <- sysDataInfo$sampleSizes[sampleSizeID]
ftrNum <- sysDataInfo$featureNum
# Output method parameters
methodParams <- NULL
if (methodInfo$methodName == "CART") {
# minSplit <- ConvertSobol(2, floor(obsNum / 2), sobolParams[1], 0)
# minBucket <- ConvertSobol(1, floor(obsNum / 2), sobolParams[2], 0)
minSplit <- ConvertSobol(2, min(floor(obsNum / 2), 10), sobolParams[1], 0)
minBucket <- ConvertSobol(1, min(floor(obsNum / 2), 10), sobolParams[2], 0)
maxDepth <- ConvertSobol(2, 10, sobolParams[3], 0)
complexity <- ConvertSobol(0, 0.01, sobolParams[4], 4)
methodParams <- c(minSplit, minBucket, maxDepth, complexity)
}
else if (methodInfo$methodName == "BAGGING") {
ntree <- ConvertSobol(2, 20, sobolParams[1], 0)
# minBucket <- ConvertSobol(1, floor(obsNum / 2), sobolParams[2], 0)
minBucket <- ConvertSobol(1, min(floor(obsNum / 2), 10), sobolParams[2], 0)
methodParams <- c(ntree, minBucket)
}
else if (methodInfo$methodName == "FOREST") {
ntree <- ConvertSobol(2, 20, sobolParams[1], 0)
# minBucket <- ConvertSobol(1, floor(obsNum / 2), sobolParams[2], 0)
minBucket <- ConvertSobol(1, min(floor(obsNum / 2), 10), sobolParams[2], 0)
mtry <- ConvertSobol(floor(ftrNum / 2), ftrNum - 1, sobolParams[3], 0)
methodParams <- c(ntree, minBucket, mtry)
}
else if (methodInfo$methodName == "ESVR") {
epsilon <- ConvertSobol(0.01, 1, sobolParams[1], 2)
sigma <- ConvertSobol(0.01, 1, sobolParams[2], 2)
cParam <- ConvertSobol(1, 100, sobolParams[3], 0)
methodParams <- c(epsilon, sigma, cParam)
}
return(methodParams)
}
# Regression models training methods ##########################################
Trainer <- function(methodName, params, data) {
#
#
# Args:
# methodName:
# params:
# data:
model <- NULL
# Select regression method implementation
regMethod <<- switch(methodName,
"CART" = TrainCart,
"BAGGING" = TrainBag,
"FOREST" = TrainFrst,
# "4" = TrainBst,
"ESVR" = TrainEsvr)
# "6" = TrainNusvr)
# Train regression model
model <- regMethod(params, data)
return(model)
}
TrainCart <- function(params, data) {
#
#
# Args:
# methodID:
# params:
# data:
Monitor()
Monitor("CART Training params:", 2)
Monitor(params, 2)
minSplit <- params[1]
minBucket <- params[2]
maxDepth <- params[3]
complexity <- params[4]
Monitor(paste("CART. ",
"minsplit: ", minSplit, "; ",
"minBucket: ", minBucket, "; ",
"maxDepth: ", maxDepth, "; ",
"complexity: ", complexity, "; ",
sep = ""))
# Train regression model
Monitor("Starting CART model training...", 3)
require(rpart, quietly = TRUE)
set.seed(1)
model <-
rpart(PERF ~ .,
data = data,
method = "anova",
parms = list(split = "information"),
control = rpart.control(minsplit = minSplit,
minbucket = minBucket,
maxdepth = maxDepth,
cp = complexity,
usesurrogate = 0,
maxsurrogate = 0))
Monitor("Finished CART model training", 3)
return(model)
}
TrainBag <- function(params, data) {
#
Monitor()
ntree <- params[1]
nodesize <- params[2]
mtry <- ncol(data) - 1
Monitor(paste("BAGGING. ",
"ntree: ", ntree, "; ",
"nodesize: ", nodesize, "; ",
"mtry: ", mtry, "; ",
sep = ""))
# Train regression model
Monitor("Starting BAGGING model training...")
require(randomForest, quietly = TRUE)
set.seed(1)
model <-
randomForest(PERF ~ .,
data = data,
ntree = ntree,
nodesize = nodesize,
mtry = mtry,
importance = TRUE,
na.action = na.omit,
replace = FALSE)
Monitor("Finished BAGGING model training")
return(model)
}
TrainFrst <- function(params, data) {
#
Monitor()
ntree <- params[1]
nodesize <- params[2]
mtry <- params[3]
Monitor(paste("FOREST. ",
"ntree: ", ntree, "; ",
"nodesize: ", nodesize, "; ",
"mtry: ", mtry, "; ",
sep = ""))
# Train regression model
Monitor("Starting FOREST model training...")
require(randomForest, quietly = TRUE)
set.seed(1)
model <-
randomForest(PERF ~ .,
data = data,
ntree = ntree,
nodesize = nodesize,
mtry = mtry,
importance = TRUE,
na.action = na.omit,
replace = FALSE)
Monitor("Finished FOREST model training")
return(model)
}
TrainEsvr <- function(params, data) {
#
Monitor()
# Initialize parameters
epsilon <- params[1]
sigma <- params[2]
C <- params[3]
Monitor(paste("SVM. ",
"epsilon: ", epsilon, "; ",
"sigma: ", sigma, "; ",
"C: ", C, "; ",
sep = ""))
# Train regression model
Monitor("Starting ESVR model training...")
set.seed(1)
model <-
ksvm(PERF~.,
data = data,
type = "eps-svr",
kernel = "rbfdot",
epsilon = epsilon,
kpar = list(sigma = sigma),
C = C,
cross = 10)
Monitor("Finished ESVR model training")
return(model)
}
TrainNusvr <- function(params, data) {
#
Monitor()
# Initialize parameters
nu <- params[1]
sigma <- params[2]
C <- params[3]
# Train regression model
Monitor("Starting NUSVR model training...")
set.seed(seedIter)
model <-
ksvm(PERF~.,
data = data,
type = "nu-svr",
kernel = "rbfdot",
nu = nu,
kpar = list(sigma = sigma),
C = C,
cross = 10)
Monitor("Finished NUSVR model training")
return(model)
}