Repository navigation
Expand file tree
/
Copy pathdatamatrixModeling_binary.R
More file actions
executable file
·221 lines (166 loc) · 7.67 KB
/
Copy pathdatamatrixModeling_binary.R
File metadata and controls
executable file
·221 lines (166 loc) · 7.67 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
datamatrixModeling_binary <- function(csvPath,target,inputs,
rescale = T,
removeCorrelated = T,
semisupervised = T,
kClusters = 9,
genetic = F,
boruta = T,
univariate = F,
makeplots = T
){
# Version: BINARY CLASSIFIER
# target and inputs are column headings in csv file,
# everything else is ignored
#
# Current version: plots not saved,
# use makeplots = F to disable plotting
cat(paste("\nTarget Variable: ",target, "\n"))
#packages
cat("Loading packages...\n\n")
libs <- c("caret", "e1071", "magrittr", "rpart", "nnet", "parallel", "randomForest", "xgboost", "Boruta", "leaps", "MASS", "ranger", "cluster", "subselect", "corrplot", "gridExtra")
lapply(libs, require,character.only=T)
#Load data
datamatrix <- read.csv(csvPath)
# Set model parameters
modelparams <- list(tree = list(method = "rpart",
#tuneGrid = data.frame(.cp = 0.01),
parms = list(split="information"),
control = rpart.control(minsplit=20, minbucket=7,
usesurrogate=0,
maxsurrogate=0)),
#method = "anova" removed since "method" is used in caret,
forest = list(method = "rf",
ntree = 500,
#tuneGrid = data.frame(.mtry = mtry), #default n/3 for regression!
#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")
)
# Trim dataset for convenience
dsraw <- datamatrix[ c(target, inputs) ]
# Pre-process data
# methods: zv removes zero-variance columns
# corr removes highly correlated columns
# center/scale recenters variables
#
ppMethods <- "zv"
if(rescale) ppMethods <- c(ppMethods,"center","scale")
if(removeCorrelated) ppMethods <- c(ppMethods, "corr")
pp <-preProcess(dsraw[inputs], method = ppMethods, cutoff=0.9)
print(pp)
dsinputs <- predict(pp, newdata <- dsraw[inputs])
#get reduced input set/dataset
ds <- cbind(dsinputs,dsraw[target])
inputs <- setdiff(names(ds), target)
# Clusterin for semi-supervised analysis with kClusters
if(semisupervised){
cat(paste("Clustering into ", kClusters, " clusters... By unsupervised random forest\n\n"))
rfUL <- randomForest(x=ds[,inputs],
ntree = 500,
replace=FALSE)
ds <- cbind(ds, clusters.conv = pam(1-rfUL$proximity, k = kClusters, diss = TRUE, cluster.only = TRUE))
ds$clusters.conv <- as.factor(ds$clusters.conv) #change to faactor not int
inputs <- setdiff(names(ds),target)
}
# Variable selection
variableSelections <- list(all=inputs)
#genetic
if(genetic){
gen <- genetic(cor(ds[,inputs]), 4) #manually selected 4 outputs
variableSelections$genetic <- names(ds[,inputs])[gen$bestsets]
print("Finished genetic variable selection")
}
#boruta
if(boruta){
bor <- Boruta(x=ds[,inputs], y=ds[,target]) #default values
variableSelections$boruta <- names(ds[,inputs])[which(bor$finalDecision == "Confirmed")]
print("Finished Boruta variable selection")
}
#univariate
if(univariate){
nums <- sapply(ds[inputs],is.numeric)
nums <- names(nums)[nums] #get names not T/F
pvals <- lapply(nums,
function(var) {
formula <- as.formula(paste(var, "~", target))
test <- wilcox.test(formula, ds[, c(var, target)])
test$p.value #could use 1-pchisq(test$statistic, df= test$parameter)
})
variableSelections$univariate <- setdiff(inputs, nums[pvals > 0.05/length(nums)]) #discard variables above threshold
print("Finished univariate selection")
}
#WIP: use metric=roc for binary classification
#Modeling, dataparams are arguements to caret::train
modelformula <- as.formula(paste(target,"~."))
dataparams <- list(form = modelformula,
# data = ds[,c(target,inputs)],
metric="Accuracy", #other option: AUC
trControl=trainControl(allowParallel = T,
method = "repeatedcv",
number = 10,
repeats= 5,
verboseIter = F) # use method="none" to disable grid tuning for speed
)
caretparams <- lapply(modelparams,function(x) c(dataparams,x))
modelList <- list()
models <- list()
for(jjj in 1:length(variableSelections)){
modeldata <- ds[,c(target,variableSelections[[jjj]])]
print(paste("Training models using ", names(variableSelections)[jjj], " variables"))
# 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
models[[model_name]] <- do.call(caret::train, c(caretparams[[iii]], list(data=modeldata)))
}
modelList[[names(variableSelections)[jjj]]] = models
}
cat("...Done training models\n\n")
# Get model accuracies
#WIP: why is accuracy only quoted to 2 decimals in summary?
rs <- resamples(models)
summary(object = rs)
#get best accuracy
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]
cat(paste("best model(s): ", maxmodels, "\n"))
cat(sprintf("%s: %.4f \n", rs$metrics[1], maxacc))
#print(paste(rs$metrics[1], maxacc, sep=" : "))
#WIP save models/variable selections
if(makeplots){
#boxplot metrics over CV
# +1 to col arg keeps one box from being black
dev.new()
boxplot(acc,col=(as.numeric(as.factor(rs$methods))+1), las=2,
ylab=paste("cross-validation",rs$metrics[1]))
legend("bottomright", legend=unique(rs$methods),
fill=(as.numeric(as.factor(rs$methods))+1) )
# correlation plot if < 30 variables
dev.new()
par(mfrow=c(length(variableSelections),1))
for(jjj in 1:length(variableSelections)){
subsetName <- names( variableSelections)[[jjj]]
correlations <- cor(Filter(is.numeric,ds[variableSelections[[jjj]]]), use="pairwise")
corrord <- order(correlations[1,])
correlations <- correlations[corrord,corrord]
corrplot(correlations,
title = paste("Correlations for",subsetName) )
} #end for corrplots
} #end if makeplots
# WIP: ROC curve for binary
# for best model call pROC::roc(rf$y, as.numeric(rf$predicted))
}