-
Notifications
You must be signed in to change notification settings - Fork 1
/
classification.R
190 lines (162 loc) · 6.14 KB
/
classification.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
## get the path of this very script
args <- commandArgs(trailingOnly = F)
scriptPath <- normalizePath(dirname(sub("^--file=", "", args[grep("^--file=", args)])))
source(paste(scriptPath, 'ml_util.R', sep='/'))
for(label in outcome.col) {
if(opt$verbose)
cat("=========", label, ":\n")
outcome <- as.character(meta[[label]])
## the space in the class names cause problem for some models
outcome <- as.factor(gsub("[[:space:]]+", "_", outcome))
## remove NA values
outcome.na <- is.na(outcome)
not.na <- (! outcome.na) & complete.cases(otus)
## if more than half of the samples are not numeric
## if(sum(not.na) < 0.5 * length(outcome)) {
## if(opt$verbose)
## cat("outcome has less than half of numeric values. skip it.\n")
## next
## }
if (! is.null(opt$replicate)) {
replicate <- replicate[not.na]
uniq_rep <- unique(replicate)
## create 5-repeat 10 folds
idx_rep <- createMultiFolds(uniq_rep)
idx <- lapply(idx_rep,
function (i) {
reps <- uniq_rep[i]
which(replicate %in% reps)
})
} else {
idx <- NULL
}
outcome <- outcome[not.na]
if (opt$verbose) {
cat("---- a glimpse of outcome:\n")
print(table(outcome))
}
if (opt$debug) save.image('debug.Rdata')
if (length(outcome) < min.sample.size)
stop("There should be more than ", min.sample.size, " samples.")
## if there less than 2 classes in this category
if (nlevels(outcome) < 2) {
if(opt$verbose)
cat("outcome has less than 2 classes. skip it.\n")
next
}
train.set <- otus[not.na, ]
if (opt$split < 1) {
set.seed(1)
training.rows <- createDataPartition(outcome, p=opt$split, list=F)
} else {
training.rows <- 1:length(outcome)
}
train.full <- train.set[training.rows, ]
test.full <- train.set[-training.rows, ]
train.outcome <- outcome[training.rows]
test.outcome <- outcome[-training.rows]
if (opt$debug) save.image('debug.Rdata')
nzv <- nearZeroVar(train.full)
if (length(nzv) > 0) {
train.full <- train.full[, -nzv]
test.full <- test.full[, -nzv]
}
tooHigh <- findCorrelation(cor(train.full), .9)
if (length(tooHigh) > 0) {
train.full <- train.full[, -tooHigh]
test.full <- test.full[, -tooHigh]
}
## save the test set results in a data.frame
if (length(test.outcome) > 0)
testResults <- data.frame(obs=test.outcome)
if (opt$debug) save.image('debug.Rdata')
## benchmark the specified models
tuned.list <- list()
accu <- data.frame()
top.f <- data.frame()
for (model in models) {
if (opt$feature_selection) {
fiveStats <- function(...) c(twoClassSummary(...), defaultSummary(...))
ctrl <- rfeControl(method = "repeatedcv",
repeats = 5, number=10,
index = idx,
saveDetails = TRUE)
## random forest
ctrl$functions <- rfFuncs
if (nlevels(train.outcome) == 2) {
ctrl$functions$summary <- fiveStats
} else {
ctrl$functions$summary <- defaultSummary
}
set.seed(721)
tuned <- rfe(train.full,
train.outcome,
sizes = seq(10, ncol(train.full)-10, by=10),
metric = "Kappa",
ntree = 1000,
rfeControl = ctrl)
tuned$method = 'rf'
} else {
save.image('debug.Rdata')
tuned <- classification.tune(train.full, train.outcome, model, ctrl=opt$cv, idx=idx)
save.image('debug.Rdata')
cm.plot <- cm.result(caret::confusionMatrix(tuned), title=paste(label, model))
print(cm.plot)
## if(is.na(tuned) | is.null(tuned)) next
if (class(tuned) != 'train') {
cat("Warning message:\nModel ", model, " failed.\n")
next
}
}
tuned.list[[model]] <- tuned
if (opt$verbose) {
print(tuned)
}
save.image(sprintf("%s.Rdata", output))
## add a new column - Model
accu <- rbind(accu, cbind(tuned$resample, Model=tuned$method))
if (length(test.outcome) > 0)
testResults[model] <- predict(tuned, test.full)
imp <- varImp(tuned)
top.f <- rbind(top.f,
data.frame(imp$importance[order(imp$importance,
decreasing=T),,drop=FALSE],
Model=model))
## plot top features
pimp <- plot.imp(imp, tax.16s, main=model)
print(pimp, position=c(0, 0, 0.56, 1))
save.image(sprintf("%s.Rdata", output))
}
accu$Field <- label
accuracies <- rbind(accuracies, accu)
top.f$Field <- label
top.features <- rbind(top.features, top.f)
## if (opt$verbose) print(accuracies)
big.tuned.list[[label]] <- tuned.list
if (length(tuned.list) > 1) {
## compare the model performances
resamp <- resamples(tuned.list)
m.diff <- diff(resamp)
if (opt$verbose) print(summary(m.diff))
print(dotplot(m.diff))
}
## plot yhat vs. obs
if (length(test.outcome) > 0) {
method.names <- names(testResults)
obs <- testResults[,1]
for(i in 2:length(testResults)) {
pred <- testResults[,i]
plot(pred ~ obs,
xlab=method.names[1], ylab=method.names[i])
abline(0, 1, col="red")
## mtext(paste(c("RMSE=", "R^2="),
## c(RMSE())))
rmse <- format(round(caret::RMSE(pred, obs), 2), nsmall=2)
rsq <- format(round(caret::R2(pred, obs), 2), nsmall=2)
legend("topleft", text.col="blue", "ab",
paste(c("RMSE","R^2 "), c(rmse, rsq), sep='=', collapse='\n'))
}
}
}
dev.off()
save.image(sprintf("%s.Rdata", output))