-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspep_functions.R
226 lines (193 loc) · 7.88 KB
/
spep_functions.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
char2sparse = function(charvector) {
MyCorpus <- VCorpus(VectorSource(charvector),
readerControl = list(language = "en"))
MyCorpus <- tm_map(MyCorpus, content_transformer(tolower))
DTM <- DocumentTermMatrix(MyCorpus,
control = list(bounds = list(global = c(0, Inf))))
sparse_DTM <- sparseMatrix(i = DTM$i, j = DTM$j, x = DTM$v,
dims = dim(DTM),
dimnames = list(rownames(DTM), colnames(DTM)))
return(sparse_DTM)
}
xy2plot = function(sparse_DTM, annotations, title, do_roc = FALSE) {
stopifnot(dim(sparse_DTM)[1] == length(annotations))
# Don't need to reshuffle because I already shuffled to decide which to annotate.
xtrain = sparse_DTM[1:(dim(sparse_DTM)[1]/2), ]
ytrain = annotations[1:(length(annotations)/2)]
xtest = sparse_DTM[(dim(sparse_DTM)[1]/2 + 1) : (dim(sparse_DTM)[1]), ]
ytest = annotations[(length(annotations)/2 + 1):(length(annotations))]
# svm.model <- svm(xtrain, ytrain, cost = 10, gamma = 1)
ST = tune.svm(xtrain, ytrain, gamma=10^(-4:2), cost=10^(0:4))
svm.model = ST$best.model
g = as.numeric(ST$best.parameters['gamma'])
c = as.numeric(ST$best.parameters['cost'])
p = ggplot(ST$performances, aes(x=gamma, y=cost))
p2 = p + geom_tile(aes(fill=log(error))) + geom_vline(xintercept=g) + geom_hline(yintercept=c) + scale_x_log10() + scale_y_log10() + labs(title = title)
plot(p2)
preds = predict(svm.model, xtest)
rocr_pred = prediction(preds, ytest)
perf <- performance(rocr_pred, "tpr", "fpr")
auc_object = performance(rocr_pred, "auc")
A = as.numeric(slot(auc_object, 'y.values'))
mymain = paste(title, ": AUC", as.character(round(A, 3)))
if(do_roc){
plot(perf, colorize=T, lwd= 3, main=mymain)
}
cat(paste(title, ": AUC =", A, "\n"))
perf2 = performance(rocr_pred, 'acc', 'fnr')
perf3 = performance(rocr_pred, 'tnr')
roc_df = data.frame( fpr = slot(perf, 'x.values')[[1]],
tpr = slot(perf, 'y.values')[[1]],
# fnr = slot(perf2, 'x.values')[[1]],
acc = slot(perf2, 'y.values')[[1]],
sp = slot(perf3, 'y.values')[[1]], # tnr
a = slot(rocr_pred, 'tp')[[1]],
b = slot(rocr_pred, 'fp')[[1]],
c = slot(rocr_pred, 'fn')[[1]],
d = slot(rocr_pred, 'tn')[[1]],
cutoff = slot(perf, 'alpha.values')[[1]]
)
roc_df$dist = sqrt(roc_df$fpr ^ 2 + (1 - roc_df$tpr) ^ 2)
print(roc_df[which.min(roc_df$dist),])
return(svm.model)
}
station_compare = function(Xa, s1, s2) {
### Note! Assumes names of columns of Xa!
### also going to assume that Xa is fully annotated, no more !is.na() subsetting
# Set up test data for station 2
Whole_sdtm = char2sparse(Xa$labpanelcomment)
xtest = Whole_sdtm[Xa$sta3n == s2, ]
ytest = Xa$monoclonal_01[Xa$sta3n == s2]
stopifnot(dim(xtest)[1] == length(ytest))
# Retrieve our model for station 1
line_of_code = paste("svm.model = model_", s1, sep='')
eval(parse(text = line_of_code))
# Apply model to test data.
preds = predict(svm.model, xtest)
rocr_pred = prediction(preds, ytest)
perf <- performance(rocr_pred, "tpr", "fpr")
perf2 = performance(rocr_pred, 'acc', 'fnr')
perf3 = performance(rocr_pred, 'tnr')
roc_df = data.frame( fpr = slot(perf, 'x.values')[[1]],
tpr = slot(perf, 'y.values')[[1]],
# fnr = slot(perf2, 'x.values')[[1]],
acc = slot(perf2, 'y.values')[[1]],
sp = slot(perf3, 'y.values')[[1]], # tnr
a = slot(rocr_pred, 'tp')[[1]],
b = slot(rocr_pred, 'fp')[[1]],
c = slot(rocr_pred, 'fn')[[1]],
d = slot(rocr_pred, 'tn')[[1]],
cutoff = slot(perf, 'alpha.values')[[1]]
)
roc_df$dist = sqrt(roc_df$fpr ^ 2 + (1 - roc_df$tpr) ^ 2)
cat('#### ', s1, '->', s2, '\n')
print(roc_df[which.min(roc_df$dist),])
auc_object = performance(rocr_pred, "auc")
return(as.numeric(slot(auc_object, 'y.values')))
}
sta3n_to_city = function (matrix, key) {
## Generate a data frame w/ numeric stations decoded to strings (station1 station2).
## matrix is a data.frame with numeric cols s1 and s2.
## key is a data.frame that should have numeric col 'sta3n' and string col 'name'.
join1 = merge(x = matrix, y = key, by.x = 's1', by.y = 'sta3n')
join1$station1 = join1$name
join1$name = NULL
join2 = merge(x = join1, y = key, by.x = 's2', by.y = 'sta3n')
join2$station2 = join2$name
join2$name = NULL
return(join2)
}
rules_predict = function (x, sta3n) {
if (sta3n == 523 & (
grepl("identified in gamma region", x, ignore.case=TRUE) |
grepl("monoclonal", x, ignore.case=TRUE) |
grepl("SER PARAPROTEIN Ig", x, ignore.case=TRUE)
)
) {
return (1)
}
if (sta3n == 534 & (
grepl("abnormal monoclonal", x, ignore.case=TRUE) |
grepl("M Spike is too small", x, ignore.case=TRUE) |
grepl("may be due to a monoclonal gammopathy", x, ignore.case=TRUE) |
grepl("present in the gamma globulin", x, ignore.case=TRUE) |
grepl("an abnormal monoclonal band", x, ignore.case=TRUE) |
grepl("monoclonal gammopathy", x, ignore.case=TRUE) |
grepl("evaluation reveals a restricted", x, ignore.case=TRUE)
)
) {
return (1)
}
if (sta3n == 583 & (
grepl("abnormal monoclonal band", x, ignore.case=TRUE) |
grepl("M-spike", x, ignore.case=TRUE) |
grepl("M spike", x, ignore.case=TRUE)
)
) {
return (1)
}
if (sta3n == 621 & (
grepl("a monoclonal band", x, ignore.case=TRUE) |
grepl("faint monoclonal band", x, ignore.case=TRUE)
)
) {
return (1)
}
if (sta3n == 662 & (
grepl("monoclonal pattern", x, ignore.case=TRUE) |
grepl("monclonal pattern", x, ignore.case=TRUE) | ## catching a typo!
grepl("faint band detected", x, ignore.case=TRUE)
)
) {
return (1)
}
return(0)
}
## ok this is copy pasted
rules_big_predict = function (x) {
sta3n = 0 # this is a hack so I don't have to delete a bunch of 'if' statements. I'm in a hurry.
if (sta3n == 0 & (
grepl("identified in gamma region", x, ignore.case=TRUE) |
grepl("monoclonal", x, ignore.case=TRUE) |
grepl("SER PARAPROTEIN Ig", x, ignore.case=TRUE)
)
) {
return (1)
}
if (sta3n == 0 & (
grepl("abnormal monoclonal", x, ignore.case=TRUE) |
grepl("M Spike is too small", x, ignore.case=TRUE) |
grepl("may be due to a monoclonal gammopathy", x, ignore.case=TRUE) |
grepl("present in the gamma globulin", x, ignore.case=TRUE) |
grepl("an abnormal monoclonal band", x, ignore.case=TRUE) |
grepl("monoclonal gammopathy", x, ignore.case=TRUE) |
grepl("evaluation reveals a restricted", x, ignore.case=TRUE)
)
) {
return (1)
}
if (sta3n == 0 & (
grepl("abnormal monoclonal band", x, ignore.case=TRUE) |
grepl("M-spike", x, ignore.case=TRUE) |
grepl("M spike", x, ignore.case=TRUE)
)
) {
return (1)
}
if (sta3n == 0 & (
grepl("a monoclonal band", x, ignore.case=TRUE) |
grepl("faint monoclonal band", x, ignore.case=TRUE)
)
) {
return (1)
}
if (sta3n == 0 & (
grepl("monoclonal pattern", x, ignore.case=TRUE) |
grepl("monclonal pattern", x, ignore.case=TRUE) | ## catching a typo!
grepl("faint band detected", x, ignore.case=TRUE)
)
) {
return (1)
}
return(0)
}