2017-01-07 3 views
1

SVMモデルを採用し、ROC曲線をROCRパッケージで作成しました。曲線下面積(AUC)をどのように計算できますか?ROCRパッケージでAUCを計算する方法

tune.out=tune(svm ,Negative~.-Positive, data=trainSparse, kernel ="radial", 
       ranges=list(cost=c(0.1,1,10,100,1000),gamma=c(0.5,1,2,3,4), 
       probability = TRUE)) # train svm with probability option true 
summary(tune.out) 
best=tune.out$best.model 
yhat.opt = predict(best,testSparse,probability = TRUE) 

# Roc curve 
library(ROCR) 
# choose the probability column carefully, it may be 
# probabilities[,1] or probabilities[,2], depending on your factor levels 
pred <- prediction(attributes(yhat.opt)$probabilities[,2], testSparse$Negative) 
perf <- performance(pred,"tpr","fpr") 
plot(perf,colorize=TRUE) 

enter image description here

+0

こんにちは、!これを読んでみてください:http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example –

答えて

1

これを試してみてください

... 
prediction.obj <- prediction(...) 
perf <- performance(prediction.obj, measure = "auc") 
print("AUC: ", [email protected]) 

sandipan's codeの後に追加することができます。これはgivあなただけのプロットです。 ftp://ftp.auckland.ac.nz/pub/software/CRAN/doc/packages/ROCR.pdf

"auc"performanceをもたらすことができる可能性対策の一つである:

は、5ページは、performanceためROCRのマニュアルを参照してください。

+0

dfとは何ですか? df $ Negative < - as.factor(df $ Negative) df $でエラーが発生しました。Negative: 'closure'タイプのオブジェクトはサブセット化できません@sandipan –

+0

dfは元のデータフレームです2つの部分、列車とテスト)、あなたはそれについて心配する必要はありません、変数ネガティブが要因であることを確認してください。 –

+0

私はこれを作ったのでokです:つぶやき$ Negative = as.factor(つぶやき$ Sent <= - 1)とあなたのコマンドの他の部分はうまくいきます。ありがとう、私はあなたが評判の15に達するとアップアップします! @サンディパン –

2

あなたの例では、完全ではないようですので、私はそれを実行し、それに応じて変えることができるように見えることはできません、しかし:

set.seed(1) 
tune.out=tune(svm ,Negative~.-Positive, data=trainSparse, kernel ="radial",ranges=list(cost=c(0.1,1,10,100,1000),gamma=c(0.5,1,2,3,4))) 
summary(tune.out) 
best=tune.out$best.model 

##prediction on the test set 
ypred = predict(best,testSparse, type = "class") 
table(testSparse$Negative,ypred) 

###Roc curve 
yhat.opt = predict(best,testSparse,decision.values = TRUE) 
fitted.opt = attributes(yhat.opt)$decision.values 
rocplot(fitted.opt,testSparse ["Negative"], main = "Test Data")## 
0

predictionで始まる方法ROCRパッケージから始まります。プロットにROCを取得する

pred_ROCR <- prediction(df$probabilities, df$target) 

roc_ROCR <- performance(pred_ROCR, measure = "tpr", x.measure = "fpr") 
plot(roc_ROCR, main = "ROC curve", colorize = T) 
abline(a = 0, b = 1) 

及びAUC値を取得:StackOverflowのへの歓迎

auc_ROCR <- performance(pred_ROCR, measure = "auc") 
    auc_ROCR <- [email protected][[1]] 
関連する問題