Debian Codegraph @

tests/testthat/test-plot.r

raw symbols SCIP
1
2 context("plot")
3 test_that("plot:",{
4 some.predictions <- c(0.02495517, 0.92535646,
5 0.86251887, 0.80946685,
6 0.70922858, 0.69762824,
7 0.50604485, 0.25446810,
8 0.10837728, 0.07250349)
9 some.labels <- c(0,1,1,0,1,1,0,1,0,0)
10
11 .get.performance <- function(pred) {
12
13 tpr <- performance(pred, "tpr")
14 fpr <- performance(pred, "fpr")
15 acc <- performance(pred, "acc")
16 err <- performance(pred, "err")
17
18 rec <- performance(pred, "rec")
19 sens<- performance(pred, "sens")
20 fnr <- performance(pred, "fnr")
21 tnr <- performance(pred, "tnr")
22 spec<- performance(pred, "spec")
23 ppv <- performance(pred, "ppv")
24 prec<- performance(pred, "prec")
25 npv <- performance(pred, "npv")
26
27 fall<- performance(pred, "fall")
28 miss<- performance(pred, "miss")
29 pcfall <- performance(pred, "pcfall")
30 pcmiss <- performance(pred, "pcmiss")
31 rpp <- performance(pred, "rpp")
32 rnp <- performance(pred, "rnp")
33
34 auc <- performance(pred, "auc")
35 prbe<- performance(pred, "prbe")
36 rch <- performance(pred, "rch")
37
38 mxe <- performance(pred, "mxe")
39 rmse<- performance(pred, "rmse")
40
41 phi <- performance(pred, "phi")
42 mat <- performance(pred, "mat")
43 mi <- performance(pred, "mi")
44 chisq<- performance(pred, "chisq")
45 odds<- performance(pred, "odds")
46 lift<- performance(pred, "lift")
47 f <- performance(pred, "f")
48 sar <- performance(pred,"sar")
49 ecost <- performance(pred, "ecost")
50 cost <- performance(pred, "cost")
51 return(list(tpr=tpr, fpr=fpr, acc=acc, err=err,
52 rec=rec, sens=sens, fnr=fnr, tnr=tnr,
53 spec=spec, ppv=ppv, prec=prec, npv=npv,
54 fall=fall, miss=miss, pcfall=pcfall, pcmiss=pcmiss, rpp=rpp, rnp=rnp,
55 auc=auc, prbe=prbe, rch=rch, mxe=mxe,
56 rmse=rmse, phi=phi, mat=mat, mi=mi, chisq=chisq, odds=odds,
57 lift=lift, f=f, sar=sar, ecost=ecost, cost=cost))
58
59 }
60 pred <- prediction(some.predictions, some.labels)
61 measures <- expect_warning(.get.performance(pred),
62 "Chi-squared approximation may be incorrect")
63 actual1 <- measures[[1]]
64 expect_error(plot(measures[[1]], colorize = TRUE),
65 "Threshold coloring or labeling cannot be performed")
66 for(i in seq_along(measures)){
67 if(names(measures[i]) %in% c("auc","mxe","rmse")){
68 expect_error(plot(measures[[i]]))
69 }
70 }
71
72 data(ROCR.hiv)
73 pp <- ROCR.hiv$hiv.svm$predictions
74 ll <- ROCR.hiv$hiv.svm$labels
75 pred <- prediction(pp, ll)
76 expect_error(ROCR:::.combine.performance.objects(actual1,performance(pred, "fpr")),
77 "Only performance objects with identical number of cross-validation")
78 # plot failures
79 perf <- performance(pred, "tpr", "fpr")
80 [email protected] <- list(c(1))
81 expect_error(plot(perf),
82 "Performance object cannot be plotted")
83 perf <- performance(pred, "tpr", "fpr")
84 [email protected] <- list(c(1))
85 expect_error(plot(perf),
86 "Performance object cannot be plotted")
87 perf <- performance(pred, "tpr", "fpr")
88 [email protected] <- list()
89 expect_null({
90 plot <- plot(perf) # no error
91 })
92 expect_error(plot(perf,colorize = TRUE),
93 "Threshold coloring or labeling cannot be performed")
94 expect_error(plot(perf,print.cutoffs.at = 0.5),
95 "Threshold coloring or labeling cannot be performed")
96 perf <- performance(pred, "tpr", "fpr")
97 expect_null({
98 plot <- plot(perf,avg = "horizontal") # no error
99 })
100 expect_error(plot(perf,avg = "horizontal", colorize=TRUE),
101 "Threshold coloring or labeling is only")
102 expect_error(plot(perf,avg = "horizontal", print.cutoffs.at=0.5),
103 "Threshold coloring or labeling is only")
104 #
105 perf <- performance(pred, "tpr", "fpr")
106 expect_null({
107 plot(perf, avg= "threshold", colorize=TRUE, lwd= 3,
108 main= "With ROCR you can produce standard plots like ROC curves ...")
109 plot(perf, lty=3, col="grey78", add=TRUE)
110 })
111 expect_null({
112 plot.performance(perf, avg= "threshold", colorize=TRUE, lwd= 3,
113 main= "With ROCR you can produce standard plots like ROC curves ...")
114 plot.performance(perf, lty=3, col="grey78", add=TRUE)
115 })
116 perf <- performance(pred, "prec", "rec")
117 expect_null({
118 plot(perf, avg= "threshold", colorize=TRUE, lwd= 3,
119 main= "... Precision/Recall graphs ...")
120 plot(perf, lty=3, col="grey78", add=TRUE)
121 })
122 perf <- performance(pred, "sens", "spec")
123 expect_null({
124 plot(perf, avg= "threshold", colorize=TRUE, lwd= 3,
125 main="... Sensitivity/Specificity plots ...")
126 plot(perf, lty=3, col="grey78", add=TRUE)
127 })
128 perf <- performance(pred, "lift", "rpp")
129 expect_null({
130 plot(perf, avg= "threshold", colorize=TRUE, lwd= 3,
131 main= "... and Lift charts.")
132 plot(perf, lty=3, col="grey78", add=TRUE)
133 })
134
135 perf <- performance(pred, "tpr", "fpr")
136 expect_null({
137 plot(perf, avg= "threshold", colorize=TRUE, lwd= 3,
138 main= "With ROCR you can produce standard plots like ROC curves ...",
139 downsampling = 0.5)
140 })
141 expect_null({
142 plot(perf, avg= "threshold", colorize=TRUE, lwd= 3,
143 main= "With ROCR you can produce standard plots like ROC curves ...",
144 downsampling = 0.9)
145 })
146 expect_null({
147 plot(perf, avg= "threshold", colorize=TRUE, lwd= 3,
148 main= "With ROCR you can produce standard plots like ROC curves ...",
149 downsampling = 1)
150 })
151
152 data(ROCR.xval)
153 pp <- ROCR.xval$predictions
154 ll <- ROCR.xval$labels
155 pred <- prediction(pp,ll)
156 perf <- performance(pred,'tpr','fpr')
157
158 expect_null({
159 plot(perf, colorize=TRUE, lwd=2,
160 main='ROC curves from 10-fold cross-validation')
161 })
162 expect_null({
163 plot(perf, avg='vertical', spread.estimate='stderror',lwd=3,
164 main='Vertical averaging + 1 standard error',col='blue')
165 })
166 expect_null({
167 plot(perf, avg='horizontal', spread.estimate='stderror',lwd=3,
168 main='Horizontal averaging + boxplots',col='blue')
169 })
170 expect_null({
171 plot(perf, avg='horizontal', spread.estimate='boxplot',lwd=3,
172 main='Horizontal averaging + boxplots',col='blue')
173 })
174 expect_null({
175 plot(perf, avg='vertical', spread.estimate='boxplot',lwd=3,
176 main='Horizontal averaging + boxplots',col='blue')
177 })
178 expect_null({
179 plot(perf, avg='threshold', spread.estimate='stddev',lwd=2,
180 main='Threshold averaging + 1 standard deviation',colorize=TRUE)
181 })
182 expect_null({
183 plot(perf, avg='threshold', spread.estimate='boxplot',lwd=2,
184 main='Threshold averaging + 1 standard deviation',colorize=TRUE)
185 })
186 expect_null({
187 plot(perf, avg='threshold', spread.estimate='boxplot',lwd=2,
188 main='Threshold averaging + 1 standard deviation',colorize=TRUE,
189 colorkey.pos="top")
190 })
191 expect_null({
192 plot(perf,
193 print.cutoffs.at=seq(0,1,by=0.2),
194 text.cex=0.8,
195 text.y=lapply(as.list(seq(0,0.5,by=0.05)),
196 function(x) { rep(x,length([email protected][[1]])) } ),
197 col= as.list(terrain.colors(10)),
198 text.col= as.list(terrain.colors(10)),
199 points.col= as.list(terrain.colors(10)),
200 main= "Cutoff stability")
201 })
202
203
204
205 ############################################################################
206 # removed because vdiffr is not available on mac
207 ############################################################################
208
209 # vdiffr
210 # skip_on_ci()
211 # skip_on_os("mac")
212 # skip_if_not_installed("vdiffr")
213 # for(i in seq_along(measures)){
214 # if(!(names(measures[i]) %in% c("auc","mxe","rmse"))){
215 # vdiffr::expect_doppelganger(names(measures[i]), plot(measures[[i]]))
216 # } else {
217 # expect_error(plot(measures[[i]]))
218 # }
219 # }
220 #
221 # data(ROCR.hiv)
222 # pp <- ROCR.hiv$hiv.svm$predictions
223 # ll <- ROCR.hiv$hiv.svm$labels
224 # pred <- prediction(pp, ll)
225 # expect_error(ROCR:::.combine.performance.objects(actual1,performance(pred, "fpr")),
226 # "Only performance objects with identical number of cross-validation")
227 # perf <- performance(pred, "tpr", "fpr")
228 # vdiffr::expect_doppelganger("ROC-curve",{
229 # plot(perf, avg= "threshold", colorize=TRUE, lwd= 3,
230 # main= "With ROCR you can produce standard plots like ROC curves ...")
231 # plot(perf, lty=3, col="grey78", add=TRUE)
232 # })
233 # perf <- performance(pred, "prec", "rec")
234 # vdiffr::expect_doppelganger("Precision-Recall-graph",{
235 # plot(perf, avg= "threshold", colorize=TRUE, lwd= 3,
236 # main= "... Precision/Recall graphs ...")
237 # plot(perf, lty=3, col="grey78", add=TRUE)
238 # })
239 # perf <- performance(pred, "sens", "spec")
240 # vdiffr::expect_doppelganger("Sensitivity-Specificity-plots",{
241 # plot(perf, avg= "threshold", colorize=TRUE, lwd= 3,
242 # main="... Sensitivity/Specificity plots ...")
243 # plot(perf, lty=3, col="grey78", add=TRUE)
244 # })
245 # perf <- performance(pred, "lift", "rpp")
246 # vdiffr::expect_doppelganger("lift-chart",{
247 # plot(perf, avg= "threshold", colorize=TRUE, lwd= 3,
248 # main= "... and Lift charts.")
249 # plot(perf, lty=3, col="grey78", add=TRUE)
250 # })
251 #
252 # perf <- performance(pred, "tpr", "fpr")
253 # vdiffr::expect_doppelganger("ROC-curve-downsampling1",{
254 # plot(perf, avg= "threshold", colorize=TRUE, lwd= 3,
255 # main= "With ROCR you can produce standard plots like ROC curves ...",
256 # downsampling = 0.5)
257 # })
258 # vdiffr::expect_doppelganger("ROC-curve-downsampling2",{
259 # plot(perf, avg= "threshold", colorize=TRUE, lwd= 3,
260 # main= "With ROCR you can produce standard plots like ROC curves ...",
261 # downsampling = 0.9)
262 # })
263 # vdiffr::expect_doppelganger("ROC-curve-downsampling3",{
264 # plot(perf, avg= "threshold", colorize=TRUE, lwd= 3,
265 # main= "With ROCR you can produce standard plots like ROC curves ...",
266 # downsampling = 1)
267 # })
268 # expect_error(plot(perf, avg= "threshold", colorize=TRUE, lwd= 3,
269 # main= "With ROCR you can produce standard plots like ROC curves ...",
270 # downsampling = 1.1),
271 # "'from' must be a finite number")
272 # dev.off()
273 #
274 # data(ROCR.xval)
275 # pp <- ROCR.xval$predictions
276 # ll <- ROCR.xval$labels
277 # pred <- prediction(pp,ll)
278 # perf <- performance(pred,'tpr','fpr')
279 #
280 # vdiffr::expect_doppelganger("ROC-cross-valid",{
281 # plot(perf, colorize=TRUE, lwd=2,
282 # main='ROC curves from 10-fold cross-validation')
283 # })
284 # vdiffr::expect_doppelganger("ROC-vertical-avg",{
285 # plot(perf, avg='vertical', spread.estimate='stderror',lwd=3,
286 # main='Vertical averaging + 1 standard error',col='blue')
287 # })
288 # vdiffr::expect_doppelganger("ROC-horizontal-avg",{
289 # plot(perf, avg='horizontal', spread.estimate='boxplot',lwd=3,
290 # main='Horizontal averaging + boxplots',col='blue')
291 # })
292 # vdiffr::expect_doppelganger("ROC-vertical-avg-box",{
293 # plot(perf, avg='vertical', spread.estimate='boxplot',lwd=3,
294 # main='Horizontal averaging + boxplots',col='blue')
295 # })
296 # vdiffr::expect_doppelganger("ROC-threshold-avg",{
297 # plot(perf, avg='threshold', spread.estimate='stddev',
298 # lwd=2,
299 # main='Threshold averaging + 1 standard deviation',colorize=TRUE)
300 # })
301
302 })