{ROCR}はRでROC分析を行うためのパッケージである。
作者のホームページからパッケージ本体および使用法の説明文書などダウンロード可能である。
使用法
以下3つのコマンドを順番に実行するだけと極めてシンプルである。
# (1) 予測器(classifier)を生成する # 引数scoresは検査結果の値(数値型)のベクトル、引数labelsは判定結果(任意の型)のベクトルである。 pred <- prediction( scores, labels ) # (pred: S4 object of class prediction) # (2) 予測器の指定した性能指標を取得する # 引数predは上記(1)の実行結果。 # 引数measure.Y、measure.XはそれぞれY軸、X軸に表示したい測定指標("tpr"真陽性率など規定の文字列から選択) perf <- performance( pred, measure.Y, measure.X) # (perf: S4 object of class performance) # (3) (2)で取得した指標を描画する plot( perf )
実際には描画したい項目にあわせて(2)の引数(measure.Y、measure.X)をその都度指定することになる。描画時には、引数の名の通りmeasure.YがY軸に、measure.XがX軸に配置される。
以下、実際に幾つかの例を提示する。使用例は作者自身による説明スライドを改変したものである。なおパッケージ{ROCR}には組込データセットROCR.simpleが含まれているので以下の例ではこれを使用する。
ROC曲線を描画する(ROC curves)
> data(ROCR.simple) > pred <- prediction(ROCR.simple$predictions,ROCR.simple$labels) > perf<-performance(pred,"tpr","fpr") > plot(perf)
Precision/recall curves
> data(ROCR.simple) > pred <- prediction(ROCR.simple$predictions,ROCR.simple$labels) > perf<-performance(pred,"prec","rec") > plot(perf)
Averaging across multiple runs
> data(ROCR.simple) > pred <- prediction(ROCR.simple$predictions,ROCR.simple$labels) > perf<-performance(pred,"tpr","fpr") > plot(perf, avg='threshold', spread.estimate='stddev', colorize=T)
Performance vs. cutoff
Performance
> data(ROCR.simple) > pred <- prediction(ROCR.simple$predictions,ROCR.simple$labels) perf <- performance(pred, "cal", window.size=50) plot(perf)
Cutoff
> data(ROCR.simple) > pred <- prediction(ROCR.simple$predictions,ROCR.simple$labels) > perf <- performance(pred, "acc") > plot(perf, avg= "vertical", spread.estimate="boxplot", show.spread.at= seq(0.1, 0.9, by=0.1))
作者のスライドでは上記に加え、Cutoff labeling、Cutoff labeling – multiple runs、More complex trade-offs、Some other examplesという使用例も提示されている。
AUCを算出する
AUCを算出するにはperformance()に”auc”引数を渡して実行した返り値から、y.valuesの値を取り出す手続きが必要である。
> data(ROCR.simple) > pred auc auc [1] 0.8341875
カットオフ値を変えながら真陽性率(TP)、偽陽性率(FP)、偽陰性率(FN)、真陰性率(TN)、感度、特異度、正診率を算出する
こちらの記事で紹介されているロジックを参考に関数化した。下記関数の引数はprediction()と同じものを渡せばよい。
# MyROC.getAccuracy ====
# 生データからTP数、FP数、FN数、TN数、感度、特異度、正診率を出力する
# 正診率が最大となる行から最適なカットオフ値を求めることが出来る
# predictions : 診断(分類)の根拠となる数値
# labels : 診断(分類)されたカテゴリー
# RETURN : 結果をまとめたデータフレームオブジェクト
MyROC.getAccuracy <- function(predictions,labels){
pred <- prediction(predictions,labels)
tbl1 <- data.frame(
Cutoff=unlist(pred@cutoffs),
TP=unlist(pred@tp),
FP=unlist(pred@fp),
FN=unlist(pred@fn),
TN=unlist(pred@tn),
Sensitivity=unlist(pred@tp)/(unlist(pred@tp)+unlist(pred@fn)),
Specificity=unlist(pred@tn)/(unlist(pred@fp)+unlist(pred@tn)),
Accuracy=((unlist(pred@tp)+unlist(pred@tn))/ (unlist(pred@tp)+unlist(pred@fp)+unlist(pred@fn)+unlist(pred@tn)) )
)
return(tbl1)
}
参考
Visualizing the Performance of Scoring Classifiers
ROC graphs, sensitivity/specificity curves, lift charts, and precision/recall plots are popular examples of trade-off visualizations for specific pairs of p...

RでのROC解析:ROCRパッケージを使ったROC曲線とAUCの求め方
研究でROC解析を行う必要があり、Rでどうやったらできるのか調べてみました。 そうしたところ、ROCRというパ…
コメント