Debian Codegraph @

tests/testthat/test-consistency.r

raw symbols SCIP
1 context("consistency")
2 test_that("consistency:",{
3 .get.performance.measures <- function(pred, measures) {
4
5 ans <- list()
6 for (measure in measures) {
7 ## need to enclose y.values into a list to avoid flattening
8 perf <- performance(pred, measure)
9 .check.performance.object( perf )
10 ans <- c(ans, list([email protected]))
11 }
12 names(ans) <- measures
13 ans
14 }
15
16 .check.consistency <- function(measures) {
17 ## check entries of contingency table for consistency
18
19 for (measure in c("acc", "err", "fnr", "tpr", "fpr", "tnr", "pcfall", "prec", "npv", "pcmiss",'rpp','rnp')) {
20 if (!measure %in% names(measures)) {
21 stop(paste("Performance measure", measure, "not in argument list."))
22 }
23 }
24 for (i in 1:length(measures$acc)) {
25 finite.bool <- is.finite(measures$acc[[i]]) & is.finite(measures$err[[i]])
26 expect_equal(measures$acc[[i]][finite.bool] + measures$err[[i]][finite.bool],
27 rep(1,length(measures$acc[[i]]))[finite.bool])
28
29 finite.bool <- is.finite(measures$fnr[[i]]) & is.finite(measures$tpr[[i]])
30 expect_equal(measures$fnr[[i]][finite.bool] + measures$tpr[[i]][finite.bool],
31 rep(1,length(measures$fnr[[i]]))[finite.bool])
32
33 finite.bool <- is.finite(measures$fpr[[i]]) & is.finite(measures$tnr[[i]])
34 expect_equal(measures$fpr[[i]][finite.bool] + measures$tnr[[i]][finite.bool],
35 rep(1,length(measures$fpr[[i]]))[finite.bool])
36
37 finite.bool <- is.finite(measures$prec[[i]]) & is.finite(measures$pcfall[[i]])
38 expect_equal(measures$prec[[i]][finite.bool] + measures$pcfall[[i]][finite.bool],
39 rep(1,length(measures$acc[[i]]))[finite.bool])
40
41 finite.bool <- is.finite(measures$npv[[i]]) & is.finite(measures$pcmiss[[i]])
42 expect_equal(measures$npv[[i]][finite.bool] + measures$pcmiss[[i]][finite.bool],
43 rep(1,length(measures$acc[[i]]))[finite.bool])
44
45 expect_equal(measures$rpp[[i]] + measures$rnp[[i]], rep(1, length(measures$rpp[[i]])))
46 }
47
48 }
49
50 ############################################################
51 # test length of performance measures
52
53 .check.performance.object <- function(perf) {
54 ylen <- length([email protected])
55 xlen <- length([email protected])
56 alphalen <- length([email protected])
57
58 expect_equal( (xlen==0 || xlen==ylen) && (alphalen==0 || (alphalen==xlen && alphalen==ylen)), T )
59
60 if (xlen==ylen) {
61 for (i in 1:ylen) expect_equal( length([email protected][[i]]), length([email protected][[i]]) )
62 }
63 if (alphalen==ylen) {
64 for (i in 1:ylen) expect_equal( length([email protected][[i]]), length([email protected][[i]]) )
65 }
66 }
67
68 .check.prediction.object <- function( pred) {
69 # 1. all entries in prediction object must have equals number of cross-validation runs
70 lenvec <- c(length(pred@predictions), length(pred@labels), length(pred@cutoffs), length(pred@fp),
71 length(pred@tp), length(pred@fn), length(pred@tn), length([email protected]),
72 length([email protected]), length([email protected]), length([email protected]))
73 expect_equal( length(unique(lenvec)), 1)
74
75 # 2. inside: xval runs:
76 for (i in 1:length(pred@predictions)) {
77 expect_equal( length(pred@predictions[[i]]), length(pred@labels[[i]]))
78 lenvec <- c(length(pred@cutoffs[[i]]), length(pred@fp[[i]]),
79 length(pred@tp[[i]]), length(pred@fn[[i]]),
80 length(pred@tn[[i]]), length([email protected][[i]]), length([email protected][[i]]))
81 expect_equal( length(unique(lenvec)), 1)
82 expect_equal( unique(lenvec), length(unique(pred@predictions[[i]]))+1 )
83 }
84 # 3. cutoffs sorted in descending order?
85 for (i in 1:length(pred@predictions)) {
86 expect_equal( sort(pred@cutoffs[[i]], decreasing=TRUE ), pred@cutoffs[[i]] )
87 }
88
89 # 4. check 2x2 table for consistency with marginal sums
90 for (i in 1:length(pred@predictions)) {
91 expect_equal( pred@tp[[i]] + pred@fp[[i]], [email protected][[i]] )
92 expect_equal( pred@fn[[i]] + pred@tn[[i]], [email protected][[i]] )
93 expect_equal( pred@tp[[i]] + pred@fn[[i]], rep( [email protected][[i]], length(pred@tp[[i]])) )
94 expect_equal( pred@fp[[i]] + pred@tn[[i]], rep( [email protected][[i]], length(pred@tp[[i]])) )
95 expect_equal([email protected][[i]] + [email protected][[i]],
96 rep( [email protected][[i]] + [email protected][[i]], length([email protected][[i]])) )
97 expect_equal([email protected][[i]] + [email protected][[i]], length(pred@labels[[i]]))
98 }
99
100 }
101
102 #
103 .mock.prediction <- function( n.predictions, error.rate ) {
104 if ( length(n.predictions) > 1 && length(error.rate)==1) {
105 error.rate <- rep(error.rate, length(n.predictions) )
106 }
107
108 if (length(n.predictions)>1) {
109 predictions <- list()
110 labels <- list()
111 } else {
112 predictions <- c()
113 labels <- c()
114 }
115
116 for (i in 1:length(n.predictions)) {
117 current.predictions <- runif( n.predictions[i] )
118 current.labels <- as.numeric( current.predictions >= 0.5)
119 flip.indices <- sample( n.predictions[i], round( error.rate[i] * n.predictions[i] ))
120 current.labels[ flip.indices ] <- !current.labels[ flip.indices ]
121 # current.labels[ current.labels=="1" ] <- "+"
122 # current.labels[ current.labels=="0" ] <- "-"
123
124 if (length(n.predictions)>1) {
125 predictions <- c( predictions, list( current.predictions ))
126 labels <- c( labels, list( current.labels ))
127 }
128 }
129
130 if (length( n.predictions)==1) {
131 predictions <- list(current.predictions)
132 labels <- list(current.labels)
133 }
134
135 ans <- list(predictions= predictions, labels= labels)
136
137 # ensure, that random labels have exactly two levels
138 if (any( sapply(labels, function(run) {length(unique(run))}) != rep(2, length(labels)) )) {
139 # print(paste("XXX", labels, str(n.predictions), str(error.rate)))
140 return(.mock.prediction(n.predictions, error.rate))
141 }
142 else return( ans )
143 }
144
145 ##############################################################################
146 # consistency
147 for (i in 1:100) {
148 n.folds <- sample(1:10,1)
149 fold.sizes <- sample(10:100, n.folds, replace=T)
150 error.rates <- runif( n.folds )
151 pp <- .mock.prediction( fold.sizes, error.rates )
152 pred <- prediction( pp$predictions, pp$labels )
153 .check.prediction.object(pred)
154 a <- .get.performance.measures( pred, c('acc','err','fpr','tpr','fnr','tnr','prec','pcfall','npv','pcmiss','rpp','rnp'))
155 .check.consistency( a)
156 }
157
158 ##############################################################################
159 # test errors
160 crashCases <- list( ## cases that are ok to crash:
161 list(pred= c(0), lab= c(0)), #-> Number of classes is not equal to 2.
162 list(pred= c(1), lab= c(1)), #-> Number of classes is not equal to 2.
163 list(pred= c(0.1, 0.2, 0.5), lab= c(1,1,1)), #-> Number of classes is not equal to 2.
164 list(pred= c(0.1, 0.2, 0.5), lab= c(0,0,0)), #-> Number of classes is not equal to 2.
165 list(pred= c(0.1, 0.2, 0.5), lab= c("a", "a", "a")), #-> Number of classes is not equal to 2.
166 list(pred= c(0.1, 0.2, 0.5), lab= c(T, T, T)), #-> Number of classes is not equal to 2.
167 list(pred= c(0.1, 0.2, 0.5), lab= c(F, F, F)) #-> Number of classes is not equal to 2.
168 )
169 for (case in crashCases) {
170 # cat(case$pred, " ", case$lab, "\n")
171 expect_error(pred <- prediction(case$pred, case$lab))
172 #checkException(measures <- .get.performance.measures(pred))
173 }
174
175 ##############################################################################
176 ## use consistency checks to validate results on pathological input cases
177 performance.measures <- c('tpr','fpr','acc','err','rec','sens','fnr','tnr','spec',
178 'ppv','prec','npv','fall','miss','pcfall','pcmiss','rpp','rnp',
179 'auc','prbe','rch','mxe','rmse','phi','mat','mi','chisq',
180 'odds','lift','f','sar','ecost','cost')
181
182 # mxe needs 0,1 labels (warning otherwise),
183 # rmse needs numeric labels (warning otherwise), sar as well
184 pred <- prediction( c(0.1, 0.2, 0.5), c("a", "a", "b"))
185 .check.prediction.object(pred)
186 measures.to.evaluate <- performance.measures[ performance.measures != 'mxe' &
187 performance.measures != 'rmse' &
188 performance.measures != 'sar']
189 measures <- expect_warning(.get.performance.measures(pred, measures.to.evaluate),
190 "Chi-squared approximation may be incorrect")
191 .check.consistency( measures)
192
193 pred <- prediction( c(0.1, 0.2, 0.5), c(F, F, T))
194 .check.prediction.object(pred)
195 measures.to.evaluate <- performance.measures[ performance.measures != 'mxe' &
196 performance.measures != 'rmse' &
197 performance.measures != 'sar']
198
199 measures <- expect_warning(.get.performance.measures(pred, measures.to.evaluate),
200 "Chi-squared approximation may be incorrect")
201 .check.consistency( measures)
202
203 pred <- prediction( c(0.1, 0.2, 0.5), c("1", "1", "0"))
204 .check.prediction.object(pred)
205 measures.to.evaluate <- performance.measures
206 measures <- expect_warning(.get.performance.measures(pred, measures.to.evaluate),
207 "Chi-squared approximation may be incorrect")
208 .check.consistency( measures)
209
210 pred <- prediction( c(0.1, 0.2, 0.5), c(T, F, F))
211 .check.prediction.object(pred)
212 measures.to.evaluate <- performance.measures[ performance.measures != 'mxe' &
213 performance.measures != 'rmse' &
214 performance.measures != 'sar' ]
215 measures <- expect_warning(.get.performance.measures(pred, measures.to.evaluate),
216 "Chi-squared approximation may be incorrect")
217 .check.consistency( measures)
218
219 # prbe cannot be computed, because only one prec/rec pair available.
220 pred <- prediction( c(0,0,0), c(0,1,1))
221 .check.prediction.object(pred)
222 measures.to.evaluate <- performance.measures[ performance.measures != 'prbe' ]
223 measures <- expect_warning(.get.performance.measures(pred, measures.to.evaluate),
224 "Chi-squared approximation may be incorrect")
225 .check.consistency( measures)
226
227 pred <- prediction( c(0,0,0), ordered(c(0,0,0), levels=c(0,1)))
228 .check.prediction.object(pred)
229 measures.to.evaluate <- performance.measures[ performance.measures != 'auc' &
230 performance.measures != 'prbe' &
231 performance.measures != 'rch' &
232 performance.measures != 'sar' &
233 performance.measures != 'ecost']
234 measures <- expect_warning(.get.performance.measures(pred, measures.to.evaluate),
235 "Chi-squared approximation may be incorrect")
236 .check.consistency( measures)
237
238 pred <- prediction( c(-1,-0.2,-0.6), ordered(c(1,0,1), levels=c(0,1)))
239 .check.prediction.object(pred)
240 measures.to.evaluate <- performance.measures[ performance.measures != 'mxe' ]
241 measures <- expect_warning(.get.performance.measures(pred, measures.to.evaluate),
242 "Chi-squared approximation may be incorrect")
243 .check.consistency( measures)
244
245 pred <- prediction( c(-1,-0.2,-0.6), c(-1,1,-1))
246 .check.prediction.object(pred)
247 measures.to.evaluate <- performance.measures[ performance.measures != 'mxe']
248 measures <- expect_warning(.get.performance.measures(pred, measures.to.evaluate),
249 "Chi-squared approximation may be incorrect")
250 .check.consistency( measures)
251
252 pred <- prediction( c(-1,-0.2,-0.6), c(3,2,3))
253 .check.prediction.object(pred)
254 measures.to.evaluate <- performance.measures[ performance.measures != 'mxe']
255 measures <- expect_warning(.get.performance.measures(pred, measures.to.evaluate),
256 "Chi-squared approximation may be incorrect")
257 .check.consistency( measures)
258
259 pred <- prediction( c(1), ordered(c("a"),levels=c('a','b')))
260 .check.prediction.object(pred)
261 measures.to.evaluate <- performance.measures[ performance.measures != 'auc' &
262 performance.measures != 'prbe' &
263 performance.measures != 'rch' &
264 performance.measures != 'mxe' &
265 performance.measures != 'rmse' &
266 performance.measures != 'sar' &
267 performance.measures != 'ecost']
268 measures <- expect_warning(.get.performance.measures(pred, measures.to.evaluate),
269 "Chi-squared approximation may be incorrect")
270
271 .check.consistency( measures)
272
273 ##############################################################################
274 # test measures for consistency on supplied data sets
275 data(ROCR.simple)
276 pred <- prediction(ROCR.simple$predictions, ROCR.simple$labels)
277 .check.prediction.object(pred)
278 measures.to.evaluate <- performance.measures
279 measures <- expect_warning(.get.performance.measures(pred, measures.to.evaluate),
280 "Chi-squared approximation may be incorrect")
281 .check.consistency( measures)
282
283 data(ROCR.xval)
284 pred <- prediction(ROCR.xval$predictions, ROCR.xval$labels)
285 .check.prediction.object(pred)
286 measures.to.evaluate <- performance.measures
287 measures <- expect_warning(.get.performance.measures(pred, measures.to.evaluate),
288 "Chi-squared approximation may be incorrect")
289 .check.consistency( measures)
290
291 data(ROCR.hiv)
292 pred <- prediction(ROCR.hiv$hiv.nn$predictions, ROCR.hiv$hiv.nn$labels)
293 .check.prediction.object(pred)
294 measures.to.evaluate <- performance.measures[performance.measures != 'mxe' &
295 performance.measures != 'cal']
296 measures <- expect_warning(.get.performance.measures(pred, measures.to.evaluate),
297 "Chi-squared approximation may be incorrect")
298 .check.consistency( measures)
299
300 pred <- prediction(ROCR.hiv$hiv.svm$predictions, ROCR.hiv$hiv.svm$labels)
301 .check.prediction.object(pred)
302 measures.to.evaluate <- performance.measures[performance.measures != 'mxe' &
303 performance.measures != 'cal']
304 measures <- expect_warning(.get.performance.measures(pred, measures.to.evaluate),
305 "Chi-squared approximation may be incorrect")
306 .check.consistency( measures)
307
308 skip_on_cran()
309 skip_on_ci()
310 ##############################################################################
311 # Combining
312 measures <- c('tpr','fpr','acc','err','rec','sens','fnr','tnr','spec',
313 'ppv','prec','npv','fall','miss','pcfall','pcmiss','rpp','rnp',
314 'phi','mat','mi','chisq','odds','lift')
315 # 'auc','prbe','rch','mxe','rmse','phi','mat','mi','chisq',
316 # 'odds','lift','f','sar','ecost','cost')
317
318 for (measure1 in measures) {
319 # print(measure1)
320 for (measure2 in measures) {
321 n.folds <- sample(1:2,1)
322 fold.sizes <- sample(10:20, n.folds, replace=T)
323 error.rates <- runif( n.folds )
324 pp <- .mock.prediction( fold.sizes, error.rates )
325 pred <- prediction( pp$predictions, pp$labels )
326 .check.prediction.object(pred)
327 perf1 <- suppressWarnings(performance( pred, measure1 ))
328 perf2 <- suppressWarnings(performance( pred, measure2 ))
329 perf3 <- suppressWarnings(performance( pred, measure2, measure1 ))
330 .check.performance.object(perf1)
331 .check.performance.object(perf2)
332 .check.performance.object(perf3)
333
334 for (i in 1:n.folds) {
335 #check elements
336 expect_equal(setequal( c( [email protected][[i]], [email protected][[i]]), [email protected][[i]] ),T)
337 expect_equal(setequal( [email protected][[i]], [email protected][[i]] ),T)
338 expect_equal(setequal( [email protected][[i]], [email protected][[i]] ),T)
339
340 #check order
341 ind <- sapply( [email protected][[i]], function(x) { min(which([email protected][[i]]))})
342 expect_equal( unname([email protected][[i]]), [email protected][[i]][ind] )
343 expect_equal( unname([email protected][[i]]), [email protected][[i]][ind] )
344
345 }
346 }
347 }
348
349 ##############################################################################
350 # test datavase combine
351 measures <- c('tpr','fpr','acc','err','rec','sens','fnr','tnr','spec',
352 'ppv','prec','npv','fall','miss','pcfall','pcmiss','rpp','rnp',
353 'phi','mat','mi','chisq','odds','lift')
354 #'auc','prbe','rch','mxe','rmse','phi','mat','mi','chisq',
355 #'odds','lift','f','sar','ecost','cost')
356 # print("Database combine test deactivated.")
357 data(ROCR.simple)
358 data(ROCR.xval)
359 data(ROCR.hiv)
360 all.pred <- list(prediction(ROCR.simple$predictions, ROCR.simple$labels),
361 prediction(ROCR.xval$predictions, ROCR.xval$labels),
362 prediction(ROCR.hiv$hiv.nn$predictions, ROCR.hiv$hiv.nn$labels),
363 prediction(ROCR.hiv$hiv.svm$predictions, ROCR.hiv$hiv.svm$labels))
364 lapply(all.pred, .check.prediction.object)
365 for (pred in all.pred) {
366 for (measure1 in measures) {
367 # print(measure1)
368 for (measure2 in measures) {
369 perf1 <- suppressWarnings(performance( pred, measure1 ))
370 perf2 <- suppressWarnings(performance( pred, measure2 ))
371 perf3 <- suppressWarnings(performance( pred, measure2, measure1 ))
372 .check.performance.object(perf1)
373 .check.performance.object(perf2)
374 .check.performance.object(perf3)
375
376 for (i in 1:length(pred@labels)) {
377 #check elements
378 expect_equal(setequal( c( [email protected][[i]], [email protected][[i]]), [email protected][[i]] ),T)
379 expect_equal(setequal( [email protected][[i]], [email protected][[i]] ),T)
380 expect_equal(setequal( [email protected][[i]], [email protected][[i]] ),T)
381
382 # check order
383 ind <- sapply( [email protected][[i]], function(x) { min(which([email protected][[i]]))})
384 expect_equal( unname([email protected][[i]]), [email protected][[i]][ind] )
385 expect_equal( unname([email protected][[i]]), [email protected][[i]][ind] )
386
387 }
388 }
389 }
390 }
391 })