<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Searching for Packages on R Views</title>
    <link>https://rviews.rstudio.com/tags/searching-for-packages/</link>
    <description>Recent content in Searching for Packages on R Views</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Fri, 01 Mar 2019 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://rviews.rstudio.com/tags/searching-for-packages/" rel="self" type="application/rss+xml" />
    
    
    
    
    <item>
      <title>Some R Packages for ROC Curves</title>
      <link>https://rviews.rstudio.com/2019/03/01/some-r-packages-for-roc-curves/</link>
      <pubDate>Fri, 01 Mar 2019 00:00:00 +0000</pubDate>
      
      <guid>https://rviews.rstudio.com/2019/03/01/some-r-packages-for-roc-curves/</guid>
      <description>
        


&lt;p&gt;In a recent &lt;a href=&#34;https://rviews.rstudio.com/2019/01/17/roc-curves/&#34;&gt;post&lt;/a&gt;, I presented some of the theory underlying ROC curves, and outlined the history leading up to their present popularity for characterizing the performance of machine learning models. In this post, I describe how to search CRAN for packages to plot ROC curves, and highlight six useful packages.&lt;/p&gt;
&lt;p&gt;Although I began with a few ideas about packages that I wanted to talk about, like &lt;a href=&#34;https://cran.r-project.org/package=ROCR&#34;&gt;ROCR&lt;/a&gt; and &lt;a href=&#34;https://cran.r-project.org/package=pROC&#34;&gt;pROC&lt;/a&gt;, which I have found useful in the past, I decided to use Gábor Csárdi’s relatively new package &lt;a href=&#34;https://cran.r-project.org/package=pkgsearch&#34;&gt;pkgsearch&lt;/a&gt; to search through CRAN and see what’s out there. The &lt;code&gt;package_search()&lt;/code&gt; function takes a text string as input and uses basic text mining techniques to search all of CRAN. The algorithm searches through package text fields, and produces a score for each package it finds that is weighted by the number of reverse dependencies and downloads.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(tidyverse)  # for data manipulation
library(dlstats)    # for package download stats
library(pkgsearch)  # for searching packages&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After some trial and error, I settled on the following query, which includes a number of interesting ROC-related packages.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;rocPkg &amp;lt;-  pkg_search(query=&amp;quot;ROC&amp;quot;,size=200)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then, I narrowed down the field to 46 packages by filtering out orphaned packages and packages with a score less than 190.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;rocPkgShort &amp;lt;- rocPkg %&amp;gt;% 
               filter(maintainer_name != &amp;quot;ORPHANED&amp;quot;, score &amp;gt; 190) %&amp;gt;%
               select(score, package, downloads_last_month) %&amp;gt;%
               arrange(desc(downloads_last_month))
head(rocPkgShort)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## # A tibble: 6 x 3
##   score package  downloads_last_month
##   &amp;lt;dbl&amp;gt; &amp;lt;chr&amp;gt;                   &amp;lt;int&amp;gt;
## 1  690. ROCR                    56356
## 2 7938. pROC                    39584
## 3 1328. PRROC                    9058
## 4  833. sROC                     4236
## 5  266. hmeasure                 1946
## 6 1021. plotROC                  1672&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To complete the selection process, I did the hard work of browsing the documentation for the packages to pick out what I thought would be generally useful to most data scientists. The following plot uses Guangchuang Yu’s &lt;code&gt;dlstats&lt;/code&gt; package to look at the download history for the six packages I selected to profile.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(dlstats)
shortList &amp;lt;- c(&amp;quot;pROC&amp;quot;,&amp;quot;precrec&amp;quot;,&amp;quot;ROCit&amp;quot;, &amp;quot;PRROC&amp;quot;,&amp;quot;ROCR&amp;quot;,&amp;quot;plotROC&amp;quot;)
downloads &amp;lt;- cran_stats(shortList)
ggplot(downloads, aes(end, downloads, group=package, color=package)) +
  geom_line() + geom_point(aes(shape=package)) +
  scale_y_continuous(trans = &amp;#39;log2&amp;#39;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;/post/2019-02-08-some-r-packages-for-roc-curves_files/figure-html/unnamed-chunk-5-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;div id=&#34;rocr---2005&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;&lt;a href=&#34;https://cran.r-project.org/package=ROCR&#34;&gt;ROCR&lt;/a&gt; - 2005&lt;/h3&gt;
&lt;p&gt;ROCR has been around for almost 14 years, and has be a rock-solid workhorse for drawing ROC curves. I particularly like the way the &lt;code&gt;performance()&lt;/code&gt; function has you set up calculation of the curve by entering the true positive rate, &lt;code&gt;tpr&lt;/code&gt;, and false positive rate, &lt;code&gt;fpr&lt;/code&gt;, parameters. Not only is this reassuringly transparent, it shows the flexibility to calculate nearly every performance measure for a &lt;a href=&#34;https://en.wikipedia.org/wiki/Binary_classification&#34;&gt;binary classifier&lt;/a&gt; by entering the appropriate parameter. For example, to produce a precision-recall curve, you would enter &lt;code&gt;prec&lt;/code&gt; and &lt;code&gt;rec&lt;/code&gt;. Although there is no vignette, the documentation of the package is very good.&lt;/p&gt;
&lt;p&gt;The following code sets up and plots the default &lt;code&gt;ROCR&lt;/code&gt; ROC curve using a synthetic data set that comes with the package. I will use this same data set throughout this post.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(ROCR)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Loading required package: gplots&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
## Attaching package: &amp;#39;gplots&amp;#39;&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## The following object is masked from &amp;#39;package:stats&amp;#39;:
## 
##     lowess&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# plot a ROC curve for a single prediction run
# and color the curve according to cutoff.
data(ROCR.simple)
df &amp;lt;- data.frame(ROCR.simple)
pred &amp;lt;- prediction(df$predictions, df$labels)
perf &amp;lt;- performance(pred,&amp;quot;tpr&amp;quot;,&amp;quot;fpr&amp;quot;)
plot(perf,colorize=TRUE)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;/post/2019-02-08-some-r-packages-for-roc-curves_files/figure-html/unnamed-chunk-6-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;proc---2010&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;&lt;a href=&#34;https://CRAN.R-project.org/package=pROC&#34;&gt;pROC&lt;/a&gt; - 2010&lt;/h3&gt;
&lt;p&gt;It is clear from the downloads curve that &lt;code&gt;pROC&lt;/code&gt; is also popular with data scientists. I like that it is pretty easy to get confidence intervals for the Area Under the Curve, &lt;code&gt;AUC&lt;/code&gt;, on the plot.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(pROC)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Type &amp;#39;citation(&amp;quot;pROC&amp;quot;)&amp;#39; for a citation.&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
## Attaching package: &amp;#39;pROC&amp;#39;&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## The following objects are masked from &amp;#39;package:stats&amp;#39;:
## 
##     cov, smooth, var&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;pROC_obj &amp;lt;- roc(df$labels,df$predictions,
            smoothed = TRUE,
            # arguments for ci
            ci=TRUE, ci.alpha=0.9, stratified=FALSE,
            # arguments for plot
            plot=TRUE, auc.polygon=TRUE, max.auc.polygon=TRUE, grid=TRUE,
            print.auc=TRUE, show.thres=TRUE)


sens.ci &amp;lt;- ci.se(pROC_obj)
plot(sens.ci, type=&amp;quot;shape&amp;quot;, col=&amp;quot;lightblue&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Warning in plot.ci.se(sens.ci, type = &amp;quot;shape&amp;quot;, col = &amp;quot;lightblue&amp;quot;): Low
## definition shape.&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;plot(sens.ci, type=&amp;quot;bars&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;/post/2019-02-08-some-r-packages-for-roc-curves_files/figure-html/unnamed-chunk-7-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;prroc---2014&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;&lt;a href=&#34;https://cran.r-project.org/package=PRROC&#34;&gt;PRROC&lt;/a&gt; - 2014&lt;/h3&gt;
&lt;p&gt;Although not nearly as popular as &lt;code&gt;ROCR&lt;/code&gt; and &lt;code&gt;pROC&lt;/code&gt;, &lt;code&gt;PRROC&lt;/code&gt; seems to be making a bit of a comeback lately. The terminology for the inputs is a bit eclectic, but once you figure that out the &lt;code&gt;roc.curve()&lt;/code&gt; function plots a clean ROC curve with minimal fuss. &lt;code&gt;PRROC&lt;/code&gt; is really set up to do precision-recall curves as the &lt;a href=&#34;https://cran.r-project.org/web/packages/PRROC/vignettes/PRROC.pdf&#34;&gt;vignette&lt;/a&gt; indicates.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(PRROC)

PRROC_obj &amp;lt;- roc.curve(scores.class0 = df$predictions, weights.class0=df$labels,
                       curve=TRUE)
plot(PRROC_obj)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;/post/2019-02-08-some-r-packages-for-roc-curves_files/figure-html/unnamed-chunk-8-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;plotroc---2014&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;&lt;a href=&#34;https://CRAN.R-project.org/package=plotROC&#34;&gt;plotROC&lt;/a&gt; - 2014&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;plotROC&lt;/code&gt; is an excellent choice for drawing ROC curves with &lt;code&gt;ggplot()&lt;/code&gt;. My guess is that it appears to enjoy only limited popularity because the documentation uses medical terminology like “disease status” and “markers”. Nevertheless, the documentation, which includes both a &lt;a href=&#34;https://cran.r-project.org/web/packages/plotROC/vignettes/examples.html&#34;&gt;vignette&lt;/a&gt; and a &lt;a href=&#34;https://sachsmc.shinyapps.io/plotROC/&#34;&gt;Shiny application&lt;/a&gt;, is very good.&lt;/p&gt;
&lt;p&gt;The package offers a number of feature-rich &lt;code&gt;ggplot()&lt;/code&gt; geoms that enable the production of elaborate plots. The following plot contains some styling, and includes &lt;a href=&#34;https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval#Clopper%E2%80%93Pearson_interval&#34;&gt;Clopper and Pearson (1934) exact method&lt;/a&gt; confidence intervals.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(plotROC)
rocplot &amp;lt;- ggplot(df, aes(m = predictions, d = labels))+ geom_roc(n.cuts=20,labels=FALSE)
rocplot + style_roc(theme = theme_grey) + geom_rocci(fill=&amp;quot;pink&amp;quot;) &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;/post/2019-02-08-some-r-packages-for-roc-curves_files/figure-html/unnamed-chunk-9-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;precrec---2015&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;&lt;a href=&#34;https://cran.r-project.org/package=precrec&#34;&gt;precrec&lt;/a&gt; - 2015&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;precrec&lt;/code&gt; is another library for plotting ROC and precision-recall curves.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(precrec)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## 
## Attaching package: &amp;#39;precrec&amp;#39;&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## The following object is masked from &amp;#39;package:pROC&amp;#39;:
## 
##     auc&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;precrec_obj &amp;lt;- evalmod(scores = df$predictions, labels = df$labels)
autoplot(precrec_obj)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;/post/2019-02-08-some-r-packages-for-roc-curves_files/figure-html/unnamed-chunk-10-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Parameter options for the &lt;code&gt;evalmod()&lt;/code&gt; function make it easy to produce basic plots of various model features.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;precrec_obj2 &amp;lt;- evalmod(scores = df$predictions, labels = df$labels, mode=&amp;quot;basic&amp;quot;)
autoplot(precrec_obj2)   &lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;/post/2019-02-08-some-r-packages-for-roc-curves_files/figure-html/unnamed-chunk-11-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div id=&#34;rocit---2019&#34; class=&#34;section level3&#34;&gt;
&lt;h3&gt;&lt;a href=&#34;https://cran.r-project.org/package=ROCit&#34;&gt;ROCit&lt;/a&gt; - 2019&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;ROCit&lt;/code&gt; is a new package for plotting ROC curves and other binary classification visualizations that rocketed onto the scene in January, and is climbing quickly in popularity. I would never have discovered it if I had automatically filtered my original search by downloads. The default plot includes the location of the &lt;a href=&#34;https://en.wikipedia.org/wiki/Youden%27s_J_statistic&#34;&gt;Yourden’s J Statistic&lt;/a&gt;.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(ROCit)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Warning: package &amp;#39;ROCit&amp;#39; was built under R version 3.5.2&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ROCit_obj &amp;lt;- rocit(score=df$predictions,class=df$labels)
plot(ROCit_obj)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;/post/2019-02-08-some-r-packages-for-roc-curves_files/figure-html/unnamed-chunk-12-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;Several other visualizations are possible. The following plot shows the cumulative densities of the positive and negative responses. The KS statistic shows the maximum distance between the two curves.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;ksplot(ROCit_obj)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;/post/2019-02-08-some-r-packages-for-roc-curves_files/figure-html/unnamed-chunk-13-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;In this attempt to dig into CRAN and uncover some of the resources R contains for plotting ROC curves and other binary classifier visualizations, I have only scratched the surface. Moreover, I have deliberately ignored the many packages available for specialized applications, such as &lt;a href=&#34;https://cran.r-project.org/package=survivalROC&#34;&gt;survivalROC&lt;/a&gt; for computing time-dependent ROC curves from censored survival data, and &lt;a href=&#34;https://cran.r-project.org/web/packages/cvAUC/index.html&#34;&gt;cvAUC&lt;/a&gt;, which contains functions for evaluating cross-validated AUC measures. Nevertheless, I hope that this little exercise will help you find what you are looking for.&lt;/p&gt;
&lt;/div&gt;

        &lt;script&gt;window.location.href=&#39;https://rviews.rstudio.com/2019/03/01/some-r-packages-for-roc-curves/&#39;;&lt;/script&gt;
      </description>
    </item>
    
  </channel>
</rss>
