Repository navigation
Expand file tree
/
Copy pathdatamatrixModeling_binary.RMD
More file actions
415 lines (301 loc) · 13.2 KB
/
Copy pathdatamatrixModeling_binary.RMD
File metadata and controls
415 lines (301 loc) · 13.2 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
---
title: "Data Matrix Modeling"
output: pdf_document
params:
csvPath: !r NULL
target: !r NULL
inputs: !r NULL
leaveOneOut: !r FALSE
rescale: !r FALSE
removeCorrelated: !r TRUE
semisupervised: !r FALSE
kClusters: !r as.numeric(9)
genetic: !r FALSE
boruta: !r TRUE
univariate: !r TRUE
---
## Version: BINARY CLASSIFIER
Updated 10/30/2017 by E. Gates. See readme on github
WIP: semi-supervised feature and genetic variable selection
WIP: custom model selection
Compiled: `r format(Sys.time(), "%Y-%b-%d %H:%M:%S")`
Target Variable: `r params$target`
Input File: `r params$csvPath`
Target and inputs are column headings in csv file,
everything else is ignored
```{r Loading Packages + Data, echo=FALSE, warning=FALSE, message=FALSE}
#cat(paste("Target Variable: ",params$target, "\n", "Input File: ", params$csvPath))
# WIP reduce number of packages
libs <- c("caret", "kernlab", "knitr", "e1071", "magrittr", "rpart", "nnet", "parallel",
"randomForest", "xgboost", "Boruta", "leaps", "MASS",
"ranger", "cluster", "subselect", "corrplot", "gridExtra","pROC")
invisible(lapply(libs, require,character.only=T))
set.seed(25)
#Load data
datamatrix <- read.csv(params$csvPath)
if(is.null(params$inputs)){
print("No input columns specified, using all non-target columns")
params$inputs <- setdiff(names(datamatrix),params$target)
}
#error check
if(is.null(params$target)) stop("No target specified")
# Set model parameters
modelparams <- list(tree = list(method = "rpart",
# #tuneGrid = data.frame(.cp = 0.01),
parms = list(split="information")
),
forest = list(method = "rf",
ntree = 500,
#tuneGrid = data.frame(.mtry = mtry),
#replace = TRUE,
#na.action = randomForest::na.roughfix,
importance = FALSE,
predict.all = FALSE
),
#na.action removed since "na.action" is used in caret
xgboost = list(method = "xgbLinear"),
nnet = list(method = "nnet",
#tuneGrid=data.frame(.size = 10, .decay = 0),
#linout = TRUE,
skip = TRUE,
MaxNWts = 10000,
trace = FALSE,
maxit = 100),
svm = list(method = "svmRadial"),
logit = list(method = "glm")
)
# Partition + Trim dataset for convenience
dsraw <- datamatrix[ c(params$target, params$inputs) ]
#set.seed(5)
#inTrain <- createDataPartition(as.factor(datamatrix[,params$target]), 1, p=0.7)
# testing <- dsraw[-inTrain[[1]], ]
# dsraw <- dsraw[inTrain[[1]], ]
```
## Pre-processing data: By default removes columns with zero variance and discards variables correlated >0.8
```{r Pre-Processing Data, echo=FALSE}
# methods: zv removes zero-variance columns
# corr removes highly correlated columns
# center/scale recenters variables
#coerce to factor if needed
if(!is.factor(dsraw[params$target])){
dsraw[params$target]<- as.factor(dsraw[,params$target])
levels(dsraw[,params$target])<- c("control","case")
}
#check if binary
if(dsraw[,params$target] %>% levels %>% length !=2){
stop("Target must have two levels")
}
#remove columns where all values are NA
dsraw <- dsraw[,colSums(is.na(dsraw))<nrow(dsraw)]
#reduced input set
inputs <- setdiff(names(dsraw),params$target)
ppMethods <- "zv"
if(params$rescale) {ppMethods <- c(ppMethods,"center","scale")}
if(params$removeCorrelated) {ppMethods <- c(ppMethods, "corr")}
pp <- preProcess(dsraw[inputs], method = ppMethods, cutoff=0.8)
#print(pp)
#get reduced input set/dataset
ds <- cbind(predict(pp, newdata = dsraw[inputs]),dsraw[params$target])
Filteredinputs <- setdiff(names(ds), params$target)
# WIP: Clustering for semi-supervised analysis with kClusters
if(params$semisupervised){
cat(paste("Clustering into ", params$kClusters, " clusters... By unsupervised random forest\n\n"))
rfUL <- randomForest(x=ds[,Filteredinputs],
ntree = 500,
replace=FALSE)
ds <- cbind(ds, clusters.conv = pam(1-rfUL$proximity, k = params$kClusters, diss = TRUE, cluster.only = TRUE))
ds$clusters.conv <- as.factor(ds$clusters.conv) #change to factor not int
Filteredinputs <- setdiff(names(ds),params$target)
}
```
## Pre-Processing results:
Started with `r length(inputs)` non-NA variables.
```{r,echo=FALSE}
pp
```
`r length(Filteredinputs)` remained after pre-processing
## Variable Selection:
Default is Boruta and Wilcoxon test (P value cutoff 0.20/ `r length(Filteredinputs)`). Wilcoxon currently only tests numeric input variables
```{r Performing Variable Selection, echo=FALSE, fig.width=7.5, fig.height=8}
# Variable selection
variableSelections <- list() #list(all=Filteredinputs)
#if <30 variables use all of them
if(Filteredinputs %>% length <= 30){
variableSelections[["all"]] <- Filteredinputs
correlations <- cor(Filter(is.numeric,ds[variableSelections$all]), use="pairwise")
corrord <- order(correlations[1,])
correlations <- correlations[corrord,corrord]
corrplot(correlations,
title = "Correlations for all variables",
mar = c(1,2,2,0) )
}
#genetic
if(params$genetic){
gen <- genetic(cor(ds[,Filteredinputs]), 4) #manually selected 4 outputs
variableSelections$genetic <- names(ds[,Filteredinputs])[gen$bestsets]
print("Genetic variable selections:")
print(variableSelections$genetic)
correlations <- cor(Filter(is.numeric,ds[variableSelections$genetic]), use="pairwise")
corrord <- order(correlations[1,])
correlations <- correlations[corrord,corrord]
corrplot(correlations,
title = "Correlations for Genetic method",
mar = c(1,2,2,0) )
}
#boruta
if(params$boruta){
bor <- Boruta(x=ds[,Filteredinputs], y=ds[,params$target], maxRuns = 500)
variableSelections$boruta <- names(ds[,Filteredinputs])[which(bor$finalDecision == "Confirmed")]
print("Finished Boruta variable selection")
print(bor)
if(length(variableSelections$boruta) > 0){
correlations <- cor(Filter(is.numeric,ds[variableSelections$boruta]), use="pairwise")
corrord <- order(correlations[1,])
correlations <- correlations[corrord,corrord]
corrplot(correlations,
title = "Correlations for Boruta method",
mar = c(1,2,2,0) )
#par(mfrow=c(length(variableSelections$boruta),1))
for(iii in 1:length(variableSelections$boruta)){
boxplot( as.formula( paste(variableSelections$boruta[[iii]], "~", params$target) ),
data=ds,
ylab=paste(variableSelections$boruta[[iii]]),
mar=c(12,1,0,0)
)
}# end boxplots
} #end if >0 variables
} #end if params$boruta
#univariate (currently only works for numeric variables)
if(params$univariate){
nums <- sapply(ds[Filteredinputs],is.numeric)
nums <- names(nums)[nums] #get names not T/F
pvals <- lapply(nums,
function(var) {
formula <- as.formula(paste(var, "~", params$target))
test <- wilcox.test(formula, ds[, c(var, params$target)])
test$p.value #could use 1-pchisq(test$statistic, df= test$parameter)
})
#pvals <- lapply(nums,
# function(var) {
# formula <- as.formula(paste(params$target, "~", var))
# logit <- glm(formula, data=ds[, c(var,params$target)], family=binomial )
# coef(summary(logit))[2,4]
# })
variableSelections$univariate <- setdiff(Filteredinputs, nums[pvals > 0.2/length(nums)]) #discard variables above threshold
if(variableSelections$univariate %>% length > 1){
correlations <- cor(Filter(is.numeric,ds[variableSelections$univariate]), use="pairwise")
corrord <- order(correlations[1,])
correlations <- correlations[corrord,corrord]
corrplot(correlations,
title = "Correlations for Univariate method",
mar = c(1,2,2,0) )
#par(mfrow=c(length(variableSelections$univariate),1))
for(iii in 1:length(variableSelections$univariate)){
boxplot( as.formula( paste(variableSelections$univariate[[iii]], "~", params$target) ),
data=ds,
ylab=paste(variableSelections$univariate[[iii]]),
mar=c(12,1,0,0)
)
}# end boxplots
}
}
```
```{r Univariate selection, echo=FALSE, results='asis', fig.width=7.5, fig.height=8}
#print results and error handle if no significant variables
if(params$univariate){
if(length(variableSelections$univariate) == 0){
sprintf("No P values < %.5f", 0.2/length(nums))
variableSelections$univariate <- NULL
} else {
pvaltable <- data.frame(Variable = Filteredinputs[pvals< (0.2/length(nums)) ],
P_value = unlist(pvals[pvals<(0.2/length(nums))] )
)
kable(pvaltable, format = "markdown")
}
}
```
# Modeling using `r names(modelparams)`.
Use Leave-one-out cross validation: `r params$leaveOneOut`
```{r Modeling (may take some time), echo=FALSE, warning=FALSE, message=FALSE}
#WIP: consider using metric=roc for binary classification
#Modeling, dataparams are arguements to caret::train
modelformula <- as.formula(paste(params$target,"~."))
dataparams <- list(form = modelformula,
# data = ds[,c(params$target,Filteredinputs)],
metric="Accuracy", #other option: AUC?
trControl=trainControl(allowParallel = T,
method = ifelse(params$leaveOneOut,"LOOCV", "repeatedcv"),
classProbs=TRUE,
#returnResamp = "final",
#number = 10,
#repeats= 5,
verboseIter = F) # use method="none" to disable grid tuning for speed
)
caretparams <- lapply(modelparams,function(x) c(dataparams,x))
#initialize outputs
models <- list()
acc <- list()
for(jjj in 1:length(variableSelections)){
modeldata <- ds[,c(params$target,variableSelections[[jjj]])]
# RE seeding: Hawthorn et al, "The design and analysis of benchmark experiments" (2005)
for (iii in 1:length(modelparams)){
model_name <-paste(names(variableSelections)[jjj], names(modelparams)[iii],sep="_")
#print(paste("Training model", iii, "of", (length(modelparams)*length(variableSelections)), ":", model_name, sep=" "))
set.seed(3141) #seed before train to get same subsamples
invisible( models[[model_name]] <- do.call(caret::train, c(caretparams[[iii]], list(data=modeldata))) )
metric <- models[[model_name]]$metric
#get best acuracy manually for LOOCV
acc[[model_name]] <- max(models[[model_name]]$results[metric])
}
}
```
```{r Finding best model and plotting, echo = FALSE, fig.width=8, fig.height=10}
if(params$leaveOneOut){
maxacc <- max(unlist(acc))
maxmodels <- names(which(acc==maxacc))
#plot
par(mar=c(8,5,1,1))
barplot(unlist(acc),las=2, ylim=c(0,1),
ylab=paste("training set LOOCV",metric))
} else {
rs <- resamples(models)
print(summary(object = rs))
acc <- rs$values[,grepl(rs$metrics[1], names(rs$values))]
names(acc) <- rs$models
maxacc <- max(apply(acc,2,mean))
maxmodels <- rs$models[apply(acc,2,mean)==maxacc]
# plot +1 to col arg keeps one box from being black
par(mar=c(8,5,1,1))
boxplot(acc,col=(as.numeric(as.factor(rs$methods))+1), las=2,
ylab=paste("training set cross-validation",metric) )
legend("bottomright", legend=unique(rs$methods),
fill=(as.numeric(as.factor(rs$methods))+1) )
}
#get best accuracy
#cat(paste("best model(s): ", maxmodels, "\n"))
#cat(sprintf("%s: %.4f \n", metric, maxacc))
#lapply(maxmodels, function(x) models[[x]])
```
Best model(s): `r maxmodels`
`r sprintf("%s: %.4f", metric, maxacc)`
```{r Output model,echo = FALSE}
lapply(maxmodels, function(x) models[[x]])
```
```{r if LOOCV CV plot ROC,echo = FALSE, fig.width=8, fig.height=10}
if(params$leaveOneOut){
print(paste("Building ROC Curve for model", maxmodels[[1]]))
#recursiely subset
bestmod <- models[[maxmodels[[1]]]] #pick first by default
modpars <- bestmod$bestTune
results <- bestmod$pred
#filter predictions on best model
for(kkk in 1:length(modpars)){
results <- subset(results, results[,names(modpars)[kkk]]==modpars[,kkk])
}
print(caret::confusionMatrix(results$pred, reference = results$obs, positive="case"))
ROC1 <- roc(controls=results$control, cases = results$case)
print(ROC1)
plot(ROC1, print.auc=T, print.auc.y = 0.2, print.auc.x = 0.5)
# results is a frame with LOOCV data
}
```