| 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 |
|
| 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) |
| 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") |
| 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 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
}) |