# [R] how to compute a roc curve

Greg Snow Greg.Snow at imail.org
Fri Oct 31 20:32:59 CET 2008

```There are various packages that do ROC curves (others have mentioned some, you can search for the others), but ROC curves are not that hard to do by hand in R.  Using the classic iris dataset, we can use Sepal.Length as the test/score and species=='virginica' as the true pos/neg that we are trying to predict.  One way to compute the ROC curve, auc and values is:

> xx <- c( -Inf, sort(unique(iris\$Sepal.Length)), Inf )
> sens <- sapply(xx, function(x) with(iris,
+ mean( Sepal.Length[Species=='virginica'] >= x ) ) )
> spec <- sapply(xx, function(x) with(iris,
+ mean( Sepal.Length[Species!='virginica'] < x ) ) )
> plot( 1-spec, sens, type='l' )
>
> ch <- chull(1-spec,sens)
> lines(1-spec[ch], sens[ch], col='green')
>
> integrate( approxfun(spec, sens), 0, 1) # Area under curve

Hope this helps,

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Pau Marc Munoz Torres
> Sent: Friday, October 31, 2008 8:25 AM
> To: r-help at r-project.org
> Subject: [R] how to compute a roc curve
>
> Hi,
>
>  I'm trying to set up a prediction software, now i testing the
> performance of my method, so i need to calculate a ROC curve, specially
> auc, cut-off, sens and spec, i just looking at ROCH package, but it's a
> mass for me,  i'm not a math guy and I'm getting lost
>
> Could any of you recommend me an easy-to-use package to do this task? i
> just have a list of positive/negative samples and his score on my
> program. can I compute a roc curve with this?
>
> thanks
>
> pau
> --
> Pau Marc Muñoz Torres
>
> Laboratori de Biologia Computacional
> Institut de  Biotecnologia   i Biomedicina Vicent Villar
>
> Universitat Autonoma de Barcelona
> E-08193 Bellaterra (Barcelona)
>
> telèfon: 93 5812807
> Email : paumarc.munoz at bioinf.uab.cat
>
>         [[alternative HTML version deleted]]

```