Debian Codegraph @

inst/doc/ROCR.R

raw
1 ## ----include = FALSE----------------------------------------------------------
2 knitr::opts_chunk$set(
3 collapse = TRUE,
4 comment = "#>"
5 )
6
7 ## ----setup--------------------------------------------------------------------
8 library(ROCR)
9
10 ## ----echo = FALSE, results = 'asis'-------------------------------------------
11 table <- data.frame(group = c("Contingency ratios",
12 "Discrete covariation measures",
13 "Information retrieval measures",
14 "Performance in ROC space",
15 "Absolute scoring performance",
16 "Cost measures"),
17 measure = c("error rate, accuracy, sensitivity, specificity, true/false positive rate, fallout, miss, precision, recall, negative predictive value, prediction-conditioned fallout/miss.",
18 "Phi/Matthews correlation coefficient, mutual information, Chi-squared test statistic, odds ratio",
19 "F-measure, lift, precision-recall break-even point",
20 "ROC convex hull, area under the ROC curve",
21 "calibration error, mean cross-entropy, root mean-squared error",
22 "expected cost, explicit cost"))
23 knitr::kable(table,
24 caption = "***Table 1:**Performance measures in the ROCR package*",
25 col.names = c("",""),
26 align = "l")
27
28 ## -----------------------------------------------------------------------------
29 data(ROCR.hiv)
30 predictions <- ROCR.hiv$hiv.svm$predictions
31 labels <- ROCR.hiv$hiv.svm$labels
32 pred <- prediction(predictions, labels)
33 pred
34
35 ## ----fig.asp=1, fig.width=5, fig.align='center'-------------------------------
36 perf <- performance(pred, "tpr", "fpr")
37 perf
38 plot(perf,
39 avg="threshold",
40 spread.estimate="boxplot")
41
42 ## ----echo=FALSE, results='asis', fig.asp=0.35, fig.width=7, fig.align='center',fig.cap="***Fig 1:** Visualizations of classifier performance (HIV coreceptor usage data): (a) receiver operating characteristic (ROC) curve; (b) peak accuracy across a range of cutoffs; (c) absolute difference between empirical and predicted rate of positives for windowed cutoff ranges, in order to evaluate how well the scores are calibrated as probability estimates. Owing to the probabilistic interpretation, cutoffs need to be in the interval [0,1], in contrast to other performance plots. (d) Score density estimates for the negative (solid) and positive (dotted) class.*"----
43 data(ROCR.hiv)
44 pp.unnorm <- ROCR.hiv$hiv.svm$predictions
45 ll <- ROCR.hiv$hiv.svm$labels
46
47 # normalize predictions to 0..1
48 v <- unlist(pp.unnorm)
49 pp <- lapply(pp.unnorm, function(run) {approxfun(c(min(v), max(v)), c(0,1))(run)})
50
51 par(mfrow=c(1,4))
52 pred<- prediction(pp, ll)
53 perf <- performance(pred, "tpr", "fpr")
54
55 plot(perf, avg= "threshold", colorize=TRUE, lwd= 3,
56 coloraxis.at=seq(0,1,by=0.2),)
57 plot(perf, col="gray78", add=TRUE)
58 plot(perf, avg= "threshold", colorize=TRUE, colorkey=FALSE,lwd= 3,,add=TRUE)
59 mtext(paste0("(a)"), side = 3, adj = 0.01,line = 1)
60
61 perf <- performance(pred, "acc")
62 plot(perf, avg= "vertical", spread.estimate="boxplot", lwd=3,col='blue',
63 show.spread.at= seq(0.1, 0.9, by=0.1),)
64 mtext(paste0("(b)"), side = 3, adj = 0.01,line = 1)
65
66
67 plot(performance(pred, "cal", window.size= 10),
68 avg="vertical",)
69 mtext(paste0("(c)"), side = 3, adj = 0.01,line = 1)
70
71 plot(0,0,type="n", xlim= c(0,1), ylim=c(0,7),
72 xlab="Cutoff", ylab="Density",)
73 mtext(paste0("(d)"), side = 3, adj = 0.01,line = 1)
74 for (runi in 1:length(pred@predictions)) {
75 lines(density(pred@predictions[[runi]][pred@labels[[runi]]=="-1"]), col= "red")
76 lines(density(pred@predictions[[runi]][pred@labels[[runi]]=="1"]), col="green")
77 }
78
79 ## ----fig.asp=1, fig.width=5, fig.align='center'-------------------------------
80 perf <- performance(pred, "tpr", "fpr")
81 plot(perf,
82 avg= "threshold",
83 colorize=TRUE,
84 lwd= 3,
85 main= "With ROCR you can produce standard plots\nlike ROC curves ...")
86 plot(perf,
87 lty=3,
88 col="grey78",
89 add=TRUE)
90
91 ## ----fig.asp=1, fig.width=5, fig.align='center'-------------------------------
92 perf <- performance(pred, "prec", "rec")
93 plot(perf,
94 avg= "threshold",
95 colorize=TRUE,
96 lwd= 3,
97 main= "... Precision/Recall graphs ...")
98 plot(perf,
99 lty=3,
100 col="grey78",
101 add=TRUE)
102
103 ## ----fig.asp=1, fig.width=5, fig.align='center'-------------------------------
104 perf <- performance(pred, "sens", "spec")
105 plot(perf,
106 avg= "threshold",
107 colorize=TRUE,
108 lwd= 3,
109 main="... Sensitivity/Specificity plots ...")
110 plot(perf,
111 lty=3,
112 col="grey78",
113 add=TRUE)
114
115 ## ----fig.asp=1, fig.width=5, fig.align='center'-------------------------------
116 perf <- performance(pred, "lift", "rpp")
117 plot(perf,
118 avg= "threshold",
119 colorize=TRUE,
120 lwd= 3,
121 main= "... and Lift charts.")
122 plot(perf,
123 lty=3,
124 col="grey78",
125 add=TRUE)
126
127 ## -----------------------------------------------------------------------------
128 data(ROCR.xval)
129 predictions <- ROCR.xval$predictions
130 labels <- ROCR.xval$labels
131 length(predictions)
132
133 ## -----------------------------------------------------------------------------
134 pred <- prediction(predictions, labels)
135 perf <- performance(pred,'tpr','fpr')
136
137 ## ----fig.asp=1, fig.width=5, fig.align='center'-------------------------------
138 plot(perf,
139 colorize=TRUE,
140 lwd=2,
141 main='ROC curves from 10-fold cross-validation')
142
143 ## ----fig.asp=1, fig.width=5, fig.align='center'-------------------------------
144 plot(perf,
145 avg='vertical',
146 spread.estimate='stderror',
147 lwd=3,main='Vertical averaging + 1 standard error',
148 col='blue')
149
150 ## ----fig.asp=1, fig.width=5, fig.align='center'-------------------------------
151 plot(perf,
152 avg='horizontal',
153 spread.estimate='boxplot',
154 lwd=3,
155 main='Horizontal averaging + boxplots',
156 col='blue')
157
158 ## ----fig.asp=1, fig.width=5, fig.align='center'-------------------------------
159 plot(perf,
160 avg='threshold',
161 spread.estimate='stddev',
162 lwd=2,
163 main='Threshold averaging + 1 standard deviation',
164 colorize=TRUE)
165
166 ## ----fig.asp=1, fig.width=6, fig.align='center'-------------------------------
167 plot(perf,
168 print.cutoffs.at=seq(0,1,by=0.2),
169 text.cex=0.8,
170 text.y=lapply(as.list(seq(0,0.5,by=0.05)), function(x) { rep(x,length([email protected][[1]])) } ),
171 col= as.list(terrain.colors(10)),
172 text.col= as.list(terrain.colors(10)),
173 points.col= as.list(terrain.colors(10)),
174 main= "Cutoff stability")
175
176 ## -----------------------------------------------------------------------------
177 perf <- performance(pred,"pcmiss","lift")
178
179 ## ----fig.asp=1, fig.width=5, fig.align='center'-------------------------------
180 plot(perf,
181 colorize=TRUE,
182 print.cutoffs.at=seq(0,1,by=0.1),
183 text.adj=c(1.2,1.2),
184 avg="threshold",
185 lwd=3,
186 main= "You can freely combine performance measures ...")
187