Debian Codegraph @

demo/ROCR.R

raw
1 ## -----------------------------------------------------------------------------------
2 ## Demo file for ROCR; start with 'demo(ROCR)'
3 ## -----------------------------------------------------------------------------------
4
5 # if(dev.cur() <= 1) get(getOption("device"))()
6 if(dev.cur() <= 1) dev.new()
7
8 opar <- par(ask = interactive() &&
9 (.Device %in% c("X11", "GTK", "gnome", "windows","quartz")))
10
11 data(ROCR.hiv)
12 pp <- ROCR.hiv$hiv.svm$predictions
13 ll <- ROCR.hiv$hiv.svm$labels
14
15 par(mfrow=c(2,2))
16 pred<- prediction(pp, ll)
17
18 perf <- performance(pred, "tpr", "fpr")
19 plot(perf, avg= "threshold", colorize=TRUE, lwd= 3,
20 main= "With ROCR you can produce standard plots like ROC curves ...")
21 plot(perf, lty=3, col="grey78", add=TRUE)
22
23 perf <- performance(pred, "prec", "rec")
24 plot(perf, avg= "threshold", colorize=TRUE, lwd= 3,
25 main= "... Precision/Recall graphs ...")
26 plot(perf, lty=3, col="grey78", add=TRUE)
27
28 perf <- performance(pred, "sens", "spec")
29 plot(perf, avg= "threshold", colorize=TRUE, lwd= 3,
30 main="... Sensitivity/Specificity plots ...")
31 plot(perf, lty=3, col="grey78", add=TRUE)
32
33 perf <- performance(pred, "lift", "rpp")
34 plot(perf, avg= "threshold", colorize=TRUE, lwd= 3,
35 main= "... and Lift charts.")
36 plot(perf, lty=3, col="grey78", add=TRUE)
37
38 # ------------------------------------------------------------------------------------
39
40 data(ROCR.xval)
41 pp <- ROCR.xval$predictions
42 ll <- ROCR.xval$labels
43 pred <- prediction(pp,ll)
44 perf <- performance(pred,'tpr','fpr')
45 par(mfrow=c(2,2))
46 plot(perf, colorize=TRUE, lwd=2,main='ROC curves from 10-fold cross-validation')
47 plot(perf, avg='vertical', spread.estimate='stderror',lwd=3,main='Vertical averaging + 1 standard error',col='blue')
48 plot(perf, avg='horizontal', spread.estimate='boxplot',lwd=3,main='Horizontal averaging + boxplots',col='blue')
49 plot(perf, avg='threshold', spread.estimate='stddev',lwd=2, main='Threshold averaging + 1 standard deviation',colorize=TRUE)
50
51
52 # ------------------------------------------------------------------------------------
53 data(ROCR.hiv)
54 pp.unnorm <- ROCR.hiv$hiv.svm$predictions
55 ll <- ROCR.hiv$hiv.svm$labels
56
57 # normalize predictions to 0..1
58 v <- unlist(pp.unnorm)
59 pp <- lapply(pp.unnorm, function(run) {approxfun(c(min(v), max(v)), c(0,1))(run)})
60
61 par(mfrow=c(2,2))
62 pred<- prediction(pp, ll)
63 perf <- performance(pred, "tpr", "fpr")
64
65 plot(perf, avg= "threshold", colorize=TRUE, lwd= 3,
66 coloraxis.at=seq(0,1,by=0.2),
67 main= "ROC curve")
68 plot(perf, col="gray78", add=TRUE)
69 plot(perf, avg= "threshold", colorize=TRUE, colorkey=FALSE,lwd= 3,
70 main= "ROC curve",add=TRUE)
71
72 perf <- performance(pred, "acc")
73 plot(perf, avg= "vertical", spread.estimate="boxplot", lwd=3,col='blue',
74 show.spread.at= seq(0.1, 0.9, by=0.1),
75 main= "Accuracy across the range of possible cutoffs")
76
77
78 plot(performance(pred, "cal", window.size= 10),
79 avg="vertical",
80 main= "How well are the probability predictions calibrated?")
81
82 plot(0,0,type="n", xlim= c(0,1), ylim=c(0,7),
83 xlab="Cutoff", ylab="Density",
84 main="How well do the predictions separate the classes?")
85 for (runi in 1:length(pred@predictions)) {
86 lines(density(pred@predictions[[runi]][pred@labels[[runi]]=="-1"]), col= "red")
87 lines(density(pred@predictions[[runi]][pred@labels[[runi]]=="1"]), col="green")
88 }
89
90 #---------------------------------------------------------------------
91
92 par(mfrow= c(2,2))
93
94 # ...you can freely combine performance measures (pcmiss,lift)
95 data(ROCR.xval)
96 pred <- prediction(ROCR.xval$predictions, ROCR.xval$labels)
97 perf <- performance(pred,"pcmiss","lift")
98 # plot(perf, colorize=TRUE)
99 plot(perf, colorize=TRUE, print.cutoffs.at=seq(0,1,by=0.1), text.adj=c(1.2,1.2), avg="threshold", lwd=3,
100 main= "You can freely combine performance measures ...")
101
102 data(ROCR.simple)
103 pred <- prediction(ROCR.simple$predictions, ROCR.simple$labels)
104 perf <- performance(pred,"tpr","fpr")
105 plot(perf, colorize=TRUE, colorkey.pos="top", print.cutoffs.at=seq(0,1,by=0.1), text.cex=1,
106 text.adj=c(1.2, 1.2), lwd=2)
107
108 # ... cutoff stacking
109 data(ROCR.xval)
110 pred <- prediction(ROCR.xval$predictions, ROCR.xval$labels)
111 perf <- performance(pred,"tpr","fpr")
112 plot(perf,
113 print.cutoffs.at=seq(0,1,by=0.2),
114 text.cex=0.8,
115 text.y=lapply(as.list(seq(0,0.5,by=0.05)), function(x) { rep(x,length([email protected][[1]])) } ),
116 col= as.list(terrain.colors(10)),
117 text.col= as.list(terrain.colors(10)),
118 points.col= as.list(terrain.colors(10)),
119 main= "Cutoff stability")
120
121 # .... no functional dependencies needed, truly parametrized curve
122 data(ROCR.xval)
123 pred <- prediction(ROCR.xval$predictions, ROCR.xval$labels)
124 perf <- performance(pred,"acc","lift")
125 plot(perf, colorize=TRUE, main="Truly parametrized curves")
126 plot(perf, colorize=TRUE, print.cutoffs.at=seq(0,1,by=0.1), add=TRUE, text.adj=c(1.2, 1.2), avg="threshold", lwd=3)
127
128 # --------------------------------------------------------------------
129 # (Expected cost) curve + ROC convex hull
130
131 par(mfrow=c(1,2))
132 data(ROCR.hiv)
133 plot(0,0,xlim=c(0,1),ylim=c(0,1),xlab='Probability cost function',
134 ylab="Normalized expected cost",
135 main='HIV data: Expected cost curve (Drummond & Holte)')
136
137 pred<-prediction(ROCR.hiv$hiv.nn$predictions,ROCR.hiv$hiv.nn$labels)
138 lines(c(0,1),c(0,1))
139 lines(c(0,1),c(1,0))
140 perf1 <- performance(pred,'fpr','fnr')
141 for (i in 1:length([email protected])) {
142 for (j in 1:length([email protected][[i]])) {
143 lines(c(0,1),c([email protected][[i]][j], [email protected][[i]][j]),col=rev(terrain.colors(10))[i],lty=3)
144 }
145 }
146 perf<-performance(pred,'ecost')
147 plot(perf,lwd=1.5,xlim=c(0,1),ylim=c(0,1),add=TRUE)
148
149 # RCH
150 data(ROCR.simple)
151 ROCR.simple$labels[ROCR.simple$predictions >= 0.7 & ROCR.simple$predictions < 0.85] <- 0
152 #as.numeric(!labels[predictions >= 0.6 & predictions < 0.85])
153 pred <- prediction(ROCR.simple$predictions, ROCR.simple$labels)
154 perf <- performance(pred,'tpr','fpr')
155 plot(perf, main="ROC curve with concavities (suboptimal) and ROC convex hull (Fawcett)")
156 perf1 <- performance(pred,'rch')
157 plot(perf1,add=TRUE,col='red',lwd=2)
158
159
160 #---------------------------------------------------------------------
161 # (plotting cutoff vs. measure)
162
163 data(ROCR.hiv)
164 pp <- ROCR.hiv$hiv.svm$predictions
165 ll <- ROCR.hiv$hiv.svm$labels
166
167 measures <- c('tpr','fpr','acc','err','rec','sens','fnr','tnr','spec',
168 'ppv','prec','npv','fall','miss','pcfall','pcmiss',
169 'phi','mat','mi','chisq','odds','lift','f')
170
171 ## Don't be surprised by the decreased cutoff regions produced by 'odds ratio'.
172 ## Cf. ?performance for details.
173
174 pred <- prediction(pp, ll)
175 par(mfrow=c(5,5))
176 for (measure in measures) {
177 perf <- performance(pred, measure)
178 plot(perf,avg="vertical",spread.estimate="boxplot")
179 }
180
181 #---------------------------------------------------------------------
182 measures <- c('tpr','err','prec','phi','mi','chisq','odds','lift','f')
183 par(mfrow=c(6,6))
184 for (i in 1:(length(measures)-1)) {
185 for (j in (i+1):length(measures)) {
186 perf <- performance(pred, measures[i], measures[j])
187 plot(perf, avg="threshold", colorize=TRUE)
188 }
189 }
190
191 #---------------------------------------------------------------------
192
193 data(ROCR.hiv)
194 pp <- ROCR.hiv$hiv.svm$predictions
195 ll <- ROCR.hiv$hiv.svm$labels
196
197 data(ROCR.xval)
198 pp <- ROCR.xval$predictions
199 ll <- ROCR.xval$labels
200 pred <- prediction(pp, ll)
201
202 par(mfrow=c(3,3))
203 perf <- performance(pred, "odds", "fpr")
204 plot(perf, colorize=TRUE)
205 plot(perf, avg="threshold", lwd=2, add=TRUE)
206
207 perf <- performance(pred, "phi", "err")
208 plot(perf, colorize=TRUE)
209 plot(perf, avg="threshold", lwd=2, add=TRUE)
210 perf <- performance(pred, "f", "err")
211 plot(perf, colorize=TRUE)
212 plot(perf, avg="threshold", lwd=2, add=TRUE)
213 perf <- performance(pred, "f", "ppv")
214 plot(perf, colorize=TRUE)
215 plot(perf, avg="threshold", lwd=2, add=TRUE)
216 perf <- performance(pred, "mat", "ppv")
217 plot(perf, colorize=TRUE)
218 plot(perf, avg="threshold", lwd=2, add=TRUE)
219 perf <- performance(pred, "npv", "ppv")
220 plot(perf, colorize=TRUE)
221 plot(perf, avg="threshold", lwd=2, add=TRUE)
222 perf <- performance(pred, "acc", "phi")
223 plot(perf, colorize=TRUE)
224 plot(perf, avg="threshold", lwd=2, add=TRUE)
225 perf <- performance(pred, "lift", "phi")
226 plot(perf, colorize=TRUE)
227 plot(perf, avg="threshold", lwd=2, add=TRUE)
228 perf <- performance(pred, "f", "phi")
229 plot(perf, colorize=TRUE)
230 plot(perf, avg="threshold", lwd=2, add=TRUE)
231
232 perf <- performance(pred, "mi", "phi")
233 plot(perf, colorize=TRUE)
234 plot(perf, avg="threshold", lwd=2, add=TRUE)
235 perf <- performance(pred, "chisq", "phi")
236 plot(perf, colorize=TRUE)
237 plot(perf, avg="threshold", lwd=2, add=TRUE)
238 perf <- performance(pred, "acc", "mi")
239 plot(perf, colorize=TRUE)
240 plot(perf, avg="threshold", lwd=2, add=TRUE)
241 perf <- performance(pred, "fall", "odds")
242 plot(perf, colorize=TRUE)
243 plot(perf, avg="threshold", lwd=2, add=TRUE)
244 perf <- performance(pred, "tpr", "lift")
245 plot(perf, colorize=TRUE)
246 plot(perf, avg="threshold", lwd=2, add=TRUE)
247 perf <- performance(pred, "fall", "lift")
248 plot(perf, colorize=TRUE)
249 plot(perf, avg="threshold", lwd=2, add=TRUE)
250 perf <- performance(pred, "npv", "f")
251 plot(perf, colorize=TRUE)
252 plot(perf, avg="threshold", lwd=2, add=TRUE)
253 perf <- performance(pred, "prec", "f")
254 plot(perf, colorize=TRUE)
255 plot(perf, avg="threshold", lwd=2, add=TRUE)
256 perf <- performance(pred, "tpr", "f")
257 plot(perf, colorize=TRUE)
258 plot(perf, avg="threshold", lwd=2, add=TRUE)
259
260 par(opar)
261