<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">

<channel>
	<title>[R] Randomness</title>
	
	<link>http://www.rrandomness.com</link>
	<description>Random insights into the world of R</description>
	<lastBuildDate>Thu, 12 Apr 2012 09:28:12 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5</generator>
		<feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="rrandomness" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="www.rrandomness.com/feed/" /><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://add.my.yahoo.com/rss?url=www.rrandomness.com%2Ffeed%2F" src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif">Subscribe with My Yahoo!</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.newsgator.com/ngs/subscriber/subext.aspx?url=www.rrandomness.com%2Ffeed%2F" src="http://www.newsgator.com/images/ngsub1.gif">Subscribe with NewsGator</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://feeds.my.aol.com/add.jsp?url=www.rrandomness.com%2Ffeed%2F" src="http://o.aolcdn.com/favorites.my.aol.com/webmaster/ffclient/webroot/locale/en-US/images/myAOLButtonSmall.gif">Subscribe with My AOL</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.bloglines.com/sub/www.rrandomness.com/feed/" src="http://www.bloglines.com/images/sub_modern11.gif">Subscribe with Bloglines</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.netvibes.com/subscribe.php?url=www.rrandomness.com%2Ffeed%2F" src="http://www.netvibes.com/img/add2netvibes.gif">Subscribe with Netvibes</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://fusion.google.com/add?feedurl=www.rrandomness.com%2Ffeed%2F" src="http://buttons.googlesyndication.com/fusion/add.gif">Subscribe with Google</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.pageflakes.com/subscribe.aspx?url=www.rrandomness.com%2Ffeed%2F" src="http://www.pageflakes.com/ImageFile.ashx?instanceId=Static_4&amp;fileName=ATP_blu_91x17.gif">Subscribe with Pageflakes</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.plusmo.com/add?url=www.rrandomness.com%2Ffeed%2F" src="http://plusmo.com/res/graphics/fbplusmo.gif">Subscribe with Plusmo</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.thefreedictionary.com/_/hp/AddRSS.aspx?www.rrandomness.com%2Ffeed%2F" src="http://img.tfd.com/hp/addToTheFreeDictionary.gif">Subscribe with The Free Dictionary</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.bitty.com/manual/?contenttype=rssfeed&amp;contentvalue=www.rrandomness.com%2Ffeed%2F" src="http://www.bitty.com/img/bittychicklet_91x17.gif">Subscribe with Bitty Browser</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.live.com/?add=www.rrandomness.com%2Ffeed%2F" src="http://tkfiles.storage.msn.com/x1piYkpqHC_35nIp1gLE68-wvzLZO8iXl_JMledmJQXP-XTBOLfmQv4zhj4MhcWEJh_GtoBIiAl1Mjh-ndp9k47If7hTaFno0mxW9_i3p_5qQw">Subscribe with Live.com</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://mix.excite.eu/add?feedurl=www.rrandomness.com%2Ffeed%2F" src="http://image.excite.co.uk/mix/addtomix.gif">Subscribe with Excite MIX</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.webwag.com/wwgthis.php?url=www.rrandomness.com%2Ffeed%2F" src="http://www.webwag.com/images/wwgthis.gif">Subscribe with Webwag</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.podcastready.com/oneclick_bookmark.php?url=www.rrandomness.com%2Ffeed%2F" src="http://www.podcastready.com/images/podcastready_button.gif">Subscribe with Podcast Ready</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.wikio.com/subscribe?url=www.rrandomness.com%2Ffeed%2F" src="http://www.wikio.com/shared/img/add2wikio.gif">Subscribe with Wikio</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.dailyrotation.com/index.php?feed=www.rrandomness.com%2Ffeed%2F" src="http://www.dailyrotation.com/rss-dr2.gif">Subscribe with Daily Rotation</feedburner:feedFlare><item>
		<title>Simple ROC plots with ggplot2 – Part 2</title>
		<link>http://www.rrandomness.com/r/simple-roc-plots-with-ggplot2-part-2/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=simple-roc-plots-with-ggplot2-part-2</link>
		<comments>http://www.rrandomness.com/r/simple-roc-plots-with-ggplot2-part-2/#comments</comments>
		<pubDate>Wed, 21 Mar 2012 13:04:15 +0000</pubDate>
		<dc:creator>Kate Nambiar</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[plyr]]></category>

		<guid isPermaLink="false">http://www.rrandomness.com/?p=100</guid>
		<description><![CDATA[<p>In the <a href="http://www.rrandomness.com/?paged=2">first part</a> of this article we built a function (rocdata) to calculate the co-ordinates for the ROC plot and its summary statistics. Now we need to actually produce the plot. I make most of my plots in ggplot2 because of it&#8217;s versatility. However there&#8217;s no reason why these plots couldn&#8217;t be produced [...]]]></description>
				<content:encoded><![CDATA[<p>In the <a href="http://www.rrandomness.com/?paged=2">first part</a> of this article we built a function (rocdata) to calculate the co-ordinates for the ROC plot and its summary statistics. Now we need to actually produce the plot. I make most of my plots in ggplot2 because of it&#8217;s versatility. However there&#8217;s no reason why these plots couldn&#8217;t be produced using R base graphics. The first example is a function for producing a simple ROC plot just looking at one test on one set of data.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="rsplus" style="font-family:monospace;">rocplot.<span style="">single</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>grp, pred, <span style="color: #0000FF; font-weight: bold;">title</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;ROC Plot&quot;</span>, p.<span style="">value</span> <span style="color: #080;">=</span> FALSE<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
  <span style="color: #0000FF; font-weight: bold;">require</span><span style="color: #080;">&#40;</span>ggplot2<span style="color: #080;">&#41;</span>
  plotdata <span style="color: #080;">&lt;-</span> rocdata<span style="color: #080;">&#40;</span>grp, pred<span style="color: #080;">&#41;</span>
&nbsp;
  <span style="color: #0000FF; font-weight: bold;">if</span> <span style="color: #080;">&#40;</span>p.<span style="">value</span> <span style="color: #080;">==</span> TRUE<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
    annotation <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">with</span><span style="color: #080;">&#40;</span>plotdata$stats, <span style="color: #0000FF; font-weight: bold;">paste</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;AUC=&quot;</span>,<span style="color: #0000FF; font-weight: bold;">signif</span><span style="color: #080;">&#40;</span>auc, <span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">&quot; (P=&quot;</span>, <span style="color: #0000FF; font-weight: bold;">signif</span><span style="color: #080;">&#40;</span>p.<span style="">value</span>, <span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">&quot;)&quot;</span>, sep<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
  <span style="color: #080;">&#125;</span> <span style="color: #0000FF; font-weight: bold;">else</span> <span style="color: #080;">&#123;</span>
    annotation <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">with</span><span style="color: #080;">&#40;</span>plotdata$stats, <span style="color: #0000FF; font-weight: bold;">paste</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;AUC=&quot;</span>,<span style="color: #0000FF; font-weight: bold;">signif</span><span style="color: #080;">&#40;</span>auc, <span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">&quot; (95%CI &quot;</span>, <span style="color: #0000FF; font-weight: bold;">signif</span><span style="color: #080;">&#40;</span>ci.<span style="">upper</span>, <span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">&quot; - &quot;</span>, <span style="color: #0000FF; font-weight: bold;">signif</span><span style="color: #080;">&#40;</span>ci.<span style="">lower</span>, <span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">&quot;)&quot;</span>, sep<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
  <span style="color: #080;">&#125;</span>
&nbsp;
  p <span style="color: #080;">&lt;-</span> ggplot<span style="color: #080;">&#40;</span>plotdata$roc, aes<span style="color: #080;">&#40;</span>x <span style="color: #080;">=</span> x, y <span style="color: #080;">=</span> y<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
      geom_line<span style="color: #080;">&#40;</span>aes<span style="color: #080;">&#40;</span>colour <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
      geom_abline <span style="color: #080;">&#40;</span>intercept <span style="color: #080;">=</span> <span style="color: #ff0000;">0</span>, slope <span style="color: #080;">=</span> <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
      theme_bw<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
      scale_x_continuous<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;False Positive Rate (1-Specificity)&quot;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
      scale_y_continuous<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;True Positive Rate (Sensitivity)&quot;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
      scale_colour_manual<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">labels</span> <span style="color: #080;">=</span> annotation, values <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;#000000&quot;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
      opts<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">title</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">title</span>,
           plot.<span style="">title</span> <span style="color: #080;">=</span> theme_text<span style="color: #080;">&#40;</span>face<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;bold&quot;</span>, size<span style="color: #080;">=</span><span style="color: #ff0000;">14</span><span style="color: #080;">&#41;</span>, 
           axis.<span style="">title</span>.<span style="">x</span> <span style="color: #080;">=</span> theme_text<span style="color: #080;">&#40;</span>face<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;bold&quot;</span>, size<span style="color: #080;">=</span><span style="color: #ff0000;">12</span><span style="color: #080;">&#41;</span>,
           axis.<span style="">title</span>.<span style="">y</span> <span style="color: #080;">=</span> theme_text<span style="color: #080;">&#40;</span>face<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;bold&quot;</span>, size<span style="color: #080;">=</span><span style="color: #ff0000;">12</span>, angle<span style="color: #080;">=</span><span style="color: #ff0000;">90</span><span style="color: #080;">&#41;</span>,
           panel.<span style="">grid</span>.<span style="">major</span> <span style="color: #080;">=</span> theme_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>,
           panel.<span style="">grid</span>.<span style="">minor</span> <span style="color: #080;">=</span> theme_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>,
           legend.<span style="">justification</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span>, 
           legend.<span style="">position</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span>,
           legend.<span style="">title</span><span style="color: #080;">=</span>theme_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>,
           legend.<span style="">key</span> <span style="color: #080;">=</span> theme_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
           <span style="color: #080;">&#41;</span>
  <span style="color: #0000FF; font-weight: bold;">return</span><span style="color: #080;">&#40;</span>p<span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span></pre></td></tr></table></div>

<p>Let&#8217;s make an example dataset and apply this function to it. The following function generates some random data in two groups.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="rsplus" style="font-family:monospace;">exampledata <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>n <span style="color: #080;">=</span> <span style="color: #ff0000;">50</span>, <span style="color: #0000FF; font-weight: bold;">sd</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#123;</span>
  <span style="color: #228B22;"># Control group</span>
  ctrl <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span><span style="color: #080;">&#40;</span>grp <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">rep</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Control&quot;</span>, n<span style="color: #080;">&#41;</span>,
                    res <span style="color: #080;">=</span> <span style="color: #080;">-</span><span style="color: #ff0000;">1</span> <span style="color: #080;">+</span> <span style="color: #0000FF; font-weight: bold;">rnorm</span><span style="color: #080;">&#40;</span>n, <span style="color: #0000FF; font-weight: bold;">mean</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">0</span>, <span style="color: #0000FF; font-weight: bold;">sd</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">sd</span><span style="color: #080;">&#41;</span>
                    <span style="color: #080;">&#41;</span>
  <span style="color: #228B22;"># Disease group</span>
  dis <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span><span style="color: #080;">&#40;</span>grp <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">rep</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;Disease&quot;</span>, n<span style="color: #080;">&#41;</span>,
                    res <span style="color: #080;">=</span> <span style="color: #ff0000;">1</span> <span style="color: #080;">+</span> <span style="color: #0000FF; font-weight: bold;">rnorm</span><span style="color: #080;">&#40;</span>n, <span style="color: #0000FF; font-weight: bold;">mean</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">0</span>, <span style="color: #0000FF; font-weight: bold;">sd</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">sd</span><span style="color: #080;">&#41;</span>
                    <span style="color: #080;">&#41;</span>
  <span style="color: #0000FF; font-weight: bold;">df</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">rbind</span><span style="color: #080;">&#40;</span>ctrl,dis<span style="color: #080;">&#41;</span>
  <span style="color: #0000FF; font-weight: bold;">return</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">df</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span></pre></td></tr></table></div>

<p>We can then take the data frame that this produces and pass it to the rocplot.simple function. It processes the data through our rocdata function from part 1 and then passes the x and y co-ordinates to ggplot2. I&#8217;ve added a line of non-discrimination (geom_abline) and used the legend to display the statistics (AUC and confidence intervals). In order to get the legend to display for a single plot line I&#8217;ve added an aes colour mapping to geom_line and then used the labels argument of scale_colour_manual to add the annotation to the plot. The annotation can then be customised to display whatever I need &#8211; in this case either the AUC and CI or the AUC and P value.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="rsplus" style="font-family:monospace;">Test1 <span style="color: #080;">&lt;-</span> exampledata<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
p <span style="color: #080;">&lt;-</span> rocplot.<span style="">single</span><span style="color: #080;">&#40;</span>Test1$grp, Test1$res, <span style="color: #0000FF; font-weight: bold;">title</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">print</span><span style="color: #080;">&#40;</span>p<span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p><img class="aligncenter" src="http://www.rrandomness.com/wp-content/uploads/2012/03/rocplot_single.png" alt="Single ROC plot" /></p>
<p>Let&#8217;s try a slightly more complicated situation. How about a hypothetical situation where you apply the same test in a set of different situations. How does the test perform? Well, let&#8217;s make up 3 test scenarios and build up 3 sets of test data. We can then put those together into a list and apply our function. Because we&#8217;re dealing with a list of data objects our rocplot function is slightly different:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="rsplus" style="font-family:monospace;">rocplot.<span style="">multiple</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>test.<span style="">data</span>.<span style="">list</span>, groupName <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;grp&quot;</span>, predName <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;res&quot;</span>, <span style="color: #0000FF; font-weight: bold;">title</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;ROC Plot&quot;</span>, p.<span style="">value</span> <span style="color: #080;">=</span> TRUE<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
  <span style="color: #0000FF; font-weight: bold;">require</span><span style="color: #080;">&#40;</span>plyr<span style="color: #080;">&#41;</span>
  <span style="color: #0000FF; font-weight: bold;">require</span><span style="color: #080;">&#40;</span>ggplot2<span style="color: #080;">&#41;</span>
  plotdata <span style="color: #080;">&lt;-</span> llply<span style="color: #080;">&#40;</span>test.<span style="">data</span>.<span style="">list</span>, <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">with</span><span style="color: #080;">&#40;</span>x, rocdata<span style="color: #080;">&#40;</span>grp <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">eval</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">parse</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">text</span> <span style="color: #080;">=</span> groupName<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>, pred <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">eval</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">parse</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">text</span> <span style="color: #080;">=</span> predName<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
  plotdata <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">list</span><span style="color: #080;">&#40;</span>roc <span style="color: #080;">=</span> ldply<span style="color: #080;">&#40;</span>plotdata, <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span> x$roc<span style="color: #080;">&#41;</span>,
                   stats <span style="color: #080;">=</span> ldply<span style="color: #080;">&#40;</span>plotdata, <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span> x$stats<span style="color: #080;">&#41;</span>
                   <span style="color: #080;">&#41;</span>
&nbsp;
  <span style="color: #0000FF; font-weight: bold;">if</span> <span style="color: #080;">&#40;</span>p.<span style="">value</span> <span style="color: #080;">==</span> TRUE<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
    annotation <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">with</span><span style="color: #080;">&#40;</span>plotdata$stats, <span style="color: #0000FF; font-weight: bold;">paste</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;AUC=&quot;</span>,<span style="color: #0000FF; font-weight: bold;">signif</span><span style="color: #080;">&#40;</span>auc, <span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">&quot; (P=&quot;</span>, <span style="color: #0000FF; font-weight: bold;">signif</span><span style="color: #080;">&#40;</span>p.<span style="">value</span>, <span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">&quot;)&quot;</span>, sep<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
  <span style="color: #080;">&#125;</span> <span style="color: #0000FF; font-weight: bold;">else</span> <span style="color: #080;">&#123;</span>
    annotation <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">with</span><span style="color: #080;">&#40;</span>plotdata$stats, <span style="color: #0000FF; font-weight: bold;">paste</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;AUC=&quot;</span>,<span style="color: #0000FF; font-weight: bold;">signif</span><span style="color: #080;">&#40;</span>auc, <span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">&quot; (95%CI &quot;</span>, <span style="color: #0000FF; font-weight: bold;">signif</span><span style="color: #080;">&#40;</span>ci.<span style="">upper</span>, <span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">&quot; - &quot;</span>, <span style="color: #0000FF; font-weight: bold;">signif</span><span style="color: #080;">&#40;</span>ci.<span style="">lower</span>, <span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">&quot;)&quot;</span>, sep<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
  <span style="color: #080;">&#125;</span>
&nbsp;
  p <span style="color: #080;">&lt;-</span> ggplot<span style="color: #080;">&#40;</span>plotdata$roc, aes<span style="color: #080;">&#40;</span>x <span style="color: #080;">=</span> x, y <span style="color: #080;">=</span> y<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
       geom_line<span style="color: #080;">&#40;</span>aes<span style="color: #080;">&#40;</span>colour <span style="color: #080;">=</span> .<span style="">id</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
       geom_abline <span style="color: #080;">&#40;</span>intercept <span style="color: #080;">=</span> <span style="color: #ff0000;">0</span>, slope <span style="color: #080;">=</span> <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
       theme_bw<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
       scale_x_continuous<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;False Positive Rate (1-Specificity)&quot;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
       scale_y_continuous<span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;True Positive Rate (Sensitivity)&quot;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
       scale_colour_brewer<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">palette</span><span style="color: #080;">=</span><span style="color: #ff0000;">&quot;Set1&quot;</span>, breaks <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">names</span><span style="color: #080;">&#40;</span>test.<span style="">data</span>.<span style="">list</span><span style="color: #080;">&#41;</span>, <span style="color: #0000FF; font-weight: bold;">labels</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">paste</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">names</span><span style="color: #080;">&#40;</span>test.<span style="">data</span>.<span style="">list</span><span style="color: #080;">&#41;</span>, <span style="color: #ff0000;">&quot;: &quot;</span>, annotation, sep <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span>
       opts<span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">title</span> <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">title</span>,
            plot.<span style="">title</span> <span style="color: #080;">=</span> theme_text<span style="color: #080;">&#40;</span>face<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;bold&quot;</span>, size<span style="color: #080;">=</span><span style="color: #ff0000;">14</span><span style="color: #080;">&#41;</span>, 
            axis.<span style="">title</span>.<span style="">x</span> <span style="color: #080;">=</span> theme_text<span style="color: #080;">&#40;</span>face<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;bold&quot;</span>, size<span style="color: #080;">=</span><span style="color: #ff0000;">12</span><span style="color: #080;">&#41;</span>,
            axis.<span style="">title</span>.<span style="">y</span> <span style="color: #080;">=</span> theme_text<span style="color: #080;">&#40;</span>face<span style="color: #080;">=</span><span style="color: #ff0000;">&quot;bold&quot;</span>, size<span style="color: #080;">=</span><span style="color: #ff0000;">12</span>, angle<span style="color: #080;">=</span><span style="color: #ff0000;">90</span><span style="color: #080;">&#41;</span>,
            panel.<span style="">grid</span>.<span style="">major</span> <span style="color: #080;">=</span> theme_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>,
            panel.<span style="">grid</span>.<span style="">minor</span> <span style="color: #080;">=</span> theme_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>,
            legend.<span style="">justification</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span>, 
            legend.<span style="">position</span><span style="color: #080;">=</span><span style="color: #0000FF; font-weight: bold;">c</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span>,
            legend.<span style="">title</span><span style="color: #080;">=</span>theme_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>,
            legend.<span style="">key</span> <span style="color: #080;">=</span> theme_blank<span style="color: #080;">&#40;</span><span style="color: #080;">&#41;</span>
            <span style="color: #080;">&#41;</span>
  <span style="color: #0000FF; font-weight: bold;">return</span><span style="color: #080;">&#40;</span>p<span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span></pre></td></tr></table></div>

<p>We can generate some test data like this</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="rsplus" style="font-family:monospace;">TestData1 <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">list</span><span style="color: #080;">&#40;</span>TrialA <span style="color: #080;">=</span> exampledata<span style="color: #080;">&#40;</span>n<span style="color: #080;">=</span><span style="color: #ff0000;">50</span>, <span style="color: #0000FF; font-weight: bold;">sd</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span>, 
                  TrialB <span style="color: #080;">=</span> exampledata<span style="color: #080;">&#40;</span>n<span style="color: #080;">=</span><span style="color: #ff0000;">50</span>, <span style="color: #0000FF; font-weight: bold;">sd</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">1.5</span><span style="color: #080;">&#41;</span>,
                  TrialC <span style="color: #080;">=</span> exampledata<span style="color: #080;">&#40;</span>n<span style="color: #080;">=</span><span style="color: #ff0000;">50</span>, <span style="color: #0000FF; font-weight: bold;">sd</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span>
                  <span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>Each data frame containing our data is then put together into a list object which we pass to our rocplot.multiple function.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="rsplus" style="font-family:monospace;">p <span style="color: #080;">&lt;-</span> rocplot.<span style="">multiple</span><span style="color: #080;">&#40;</span>TestData1, <span style="color: #0000FF; font-weight: bold;">title</span> <span style="color: #080;">=</span> <span style="color: #ff0000;">&quot;&quot;</span>, p.<span style="">value</span> <span style="color: #080;">=</span> FALSE<span style="color: #080;">&#41;</span>
<span style="color: #0000FF; font-weight: bold;">print</span><span style="color: #080;">&#40;</span>p<span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p><img class="aligncenter" src="http://www.rrandomness.com/wp-content/uploads/2012/03/rocplot_multiple1.png" alt="Multiple ROC plot" /></p>
<p>In addition to ggplot2 this function makes use of the excelent tools available from the plyr library also written by Hadley Wickham. The first step is to apply our rocdata function to each of the 3 data frames in our list using the llply function (equivalent to lapply in R base). We then put together the roc elements and the stats elements from each rocdata output by using the ldply function on each of them. This gives us 2 dataframes with an .id column corresponding to the name of the original data frame it came from. We can put those together into a list so just like our first example we supply ggplot with a list with all the ROC co-ordinates in one element and all the stats in the second. We can then plot one line for each set of data defined by the .id column. Like the first example I&#8217;ve used the the legend to display our summary statistics.</p>
<p>Code tested using R version 2.14.2 with the following packages:<br />
<a href="http://plyr.had.co.nz/" title="plyr">plyr</a> &#8211; version 1.7.1<br />
<a href="http://had.co.nz/ggplot2/" title="ggplot2">ggplot2</a> &#8211; version 0.9.0</p>
<div class="twttr_button">
				<a href="http://twitter.com/share?url=http://www.rrandomness.com/r/simple-roc-plots-with-ggplot2-part-2/&text=Simple ROC plots with ggplot2 - Part 2" target="_blank" title="Click here if you liked this article.">
					<img src="http://www.rrandomness.com/wp-content/plugins/twitter-plugin/images/twitt.gif" alt="Twitt" />
				</a>
			</div>]]></content:encoded>
			<wfw:commentRss>http://www.rrandomness.com/r/simple-roc-plots-with-ggplot2-part-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Simple ROC plots with ggplot2 – Part 1</title>
		<link>http://www.rrandomness.com/r/simple-roc-plots-with-ggplot2-part-1/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=simple-roc-plots-with-ggplot2-part-1</link>
		<comments>http://www.rrandomness.com/r/simple-roc-plots-with-ggplot2-part-1/#comments</comments>
		<pubDate>Wed, 21 Mar 2012 12:09:23 +0000</pubDate>
		<dc:creator>Kate Nambiar</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[ggplot2]]></category>
		<category><![CDATA[plyr]]></category>

		<guid isPermaLink="false">http://www.rrandomness.com/?p=33</guid>
		<description><![CDATA[<p><br /> I was recently asked to summarise an analysis using a ROC (Receiver-operator characteristics) plot. R has a number of particularly good tools to produce ROC plots – <a href="http://rocr.bioinf.mpi-sb.mpg.de/" title="ROCR">ROCR</a>, <a href="http://web.expasy.org/pROC/" title="pROC">pROC</a> and the Bioconductor package <a href="http://www.bioconductor.org/packages/2.3/bioc/html/ROC.html" title="ROC">ROC</a> to name a few. However I thought it would be a useful exercise [...]]]></description>
				<content:encoded><![CDATA[<p><img class="alignleft" src="http://www.rrandomness.com/wp-content/uploads/2012/03/rocplot_multiple1.png" alt="ROC Plot - Multiple" /><br />
I was recently asked to summarise an analysis using a ROC (Receiver-operator characteristics) plot. R has a number of particularly good tools to produce ROC plots – <a href="http://rocr.bioinf.mpi-sb.mpg.de/" title="ROCR">ROCR</a>, <a href="http://web.expasy.org/pROC/" title="pROC">pROC</a> and the Bioconductor package <a href="http://www.bioconductor.org/packages/2.3/bioc/html/ROC.html" title="ROC">ROC</a> to name a few. However I thought it would be a useful exercise to build such a tool from first principles – partly so I could customise the output to my needs but mainly to understand better the methods behind computing such a plot.</p>
<p>If you&#8217;re not sure what a ROC plot is a good summary is given <a href="http://www.anaesthetist.com/mnm/stats/roc/Findex.htm">here</a>. So now that we know what we will be building, just how can we accomplish this in R? Let&#8217;s start by building a function that computes the co-ordinates for the ROC curve and gives us some summary statistics.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="rsplus" style="font-family:monospace;">rocdata <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>grp, pred<span style="color: #080;">&#41;</span><span style="color: #080;">&#123;</span>
  <span style="color: #228B22;"># Produces x and y co-ordinates for ROC curve plot</span>
  <span style="color: #228B22;"># Arguments: grp - labels classifying subject status</span>
  <span style="color: #228B22;">#            pred - values of each observation</span>
  <span style="color: #228B22;"># Output: List with 2 components:</span>
  <span style="color: #228B22;">#         roc = data.frame with x and y co-ordinates of plot</span>
  <span style="color: #228B22;">#         stats = data.frame containing: area under ROC curve, p value, upper and lower 95% confidence interval</span>
&nbsp;
  grp <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">as.<span style="">factor</span></span><span style="color: #080;">&#40;</span>grp<span style="color: #080;">&#41;</span>
  <span style="color: #0000FF; font-weight: bold;">if</span> <span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>pred<span style="color: #080;">&#41;</span> <span style="color: #080;">!=</span> <span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>grp<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#123;</span>
    <span style="color: #0000FF; font-weight: bold;">stop</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;The number of classifiers must match the number of data points&quot;</span><span style="color: #080;">&#41;</span>
  <span style="color: #080;">&#125;</span> 
&nbsp;
  <span style="color: #0000FF; font-weight: bold;">if</span> <span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">levels</span><span style="color: #080;">&#40;</span>grp<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">!=</span> <span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#123;</span>
    <span style="color: #0000FF; font-weight: bold;">stop</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;There must only be 2 values for the classifier&quot;</span><span style="color: #080;">&#41;</span>
  <span style="color: #080;">&#125;</span>
&nbsp;
  <span style="color: #0000FF; font-weight: bold;">cut</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">unique</span><span style="color: #080;">&#40;</span>pred<span style="color: #080;">&#41;</span>
  tp <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">sapply</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">cut</span>, <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">which</span><span style="color: #080;">&#40;</span>pred <span style="color: #080;">&gt;</span> x <span style="color: #080;">&amp;</span> grp <span style="color: #080;">==</span> <span style="color: #0000FF; font-weight: bold;">levels</span><span style="color: #080;">&#40;</span>grp<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
  fn <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">sapply</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">cut</span>, <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">which</span><span style="color: #080;">&#40;</span>pred <span style="color: #080;">&lt;</span> x <span style="color: #080;">&amp;</span> grp <span style="color: #080;">==</span> <span style="color: #0000FF; font-weight: bold;">levels</span><span style="color: #080;">&#40;</span>grp<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
  fp <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">sapply</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">cut</span>, <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">which</span><span style="color: #080;">&#40;</span>pred <span style="color: #080;">&gt;</span> x <span style="color: #080;">&amp;</span> grp <span style="color: #080;">==</span> <span style="color: #0000FF; font-weight: bold;">levels</span><span style="color: #080;">&#40;</span>grp<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
  tn <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">sapply</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">cut</span>, <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">which</span><span style="color: #080;">&#40;</span>pred <span style="color: #080;">&lt;</span> x <span style="color: #080;">&amp;</span> grp <span style="color: #080;">==</span> <span style="color: #0000FF; font-weight: bold;">levels</span><span style="color: #080;">&#40;</span>grp<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
  tpr <span style="color: #080;">&lt;-</span> tp <span style="color: #080;">/</span> <span style="color: #080;">&#40;</span>tp <span style="color: #080;">+</span> fn<span style="color: #080;">&#41;</span>
  fpr <span style="color: #080;">&lt;-</span> fp <span style="color: #080;">/</span> <span style="color: #080;">&#40;</span>fp <span style="color: #080;">+</span> tn<span style="color: #080;">&#41;</span>
  roc <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span><span style="color: #080;">&#40;</span>x <span style="color: #080;">=</span> fpr, y <span style="color: #080;">=</span> tpr<span style="color: #080;">&#41;</span>
  roc <span style="color: #080;">&lt;-</span> roc<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">order</span><span style="color: #080;">&#40;</span>roc$x, roc$y<span style="color: #080;">&#41;</span>,<span style="color: #080;">&#93;</span>
&nbsp;
  i <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">2</span><span style="color: #080;">:</span><span style="color: #0000FF; font-weight: bold;">nrow</span><span style="color: #080;">&#40;</span>roc<span style="color: #080;">&#41;</span>
  auc <span style="color: #080;">&lt;-</span> <span style="color: #080;">&#40;</span>roc$x<span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span> <span style="color: #080;">-</span> roc$x<span style="color: #080;">&#91;</span>i <span style="color: #080;">-</span> <span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">%*%</span> <span style="color: #080;">&#40;</span>roc$y<span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span> <span style="color: #080;">+</span> roc$y<span style="color: #080;">&#91;</span>i <span style="color: #080;">-</span> <span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #ff0000;">2</span>
&nbsp;
  pos <span style="color: #080;">&lt;-</span> pred<span style="color: #080;">&#91;</span>grp <span style="color: #080;">==</span> <span style="color: #0000FF; font-weight: bold;">levels</span><span style="color: #080;">&#40;</span>grp<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span>
  neg <span style="color: #080;">&lt;-</span> pred<span style="color: #080;">&#91;</span>grp <span style="color: #080;">==</span> <span style="color: #0000FF; font-weight: bold;">levels</span><span style="color: #080;">&#40;</span>grp<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span>
  q1 <span style="color: #080;">&lt;-</span> auc<span style="color: #080;">/</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">2</span><span style="color: #080;">-</span>auc<span style="color: #080;">&#41;</span>
  q2 <span style="color: #080;">&lt;-</span> <span style="color: #080;">&#40;</span><span style="color: #ff0000;">2</span><span style="color: #080;">*</span>auc<span style="color: #080;">^</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">+</span>auc<span style="color: #080;">&#41;</span>
  se.<span style="">auc</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">sqrt</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span>auc <span style="color: #080;">*</span> <span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span> <span style="color: #080;">-</span> auc<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> <span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>pos<span style="color: #080;">&#41;</span> <span style="color: #080;">-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span><span style="color: #080;">&#40;</span>q1 <span style="color: #080;">-</span> auc<span style="color: #080;">^</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> <span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>neg<span style="color: #080;">&#41;</span> <span style="color: #080;">-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span><span style="color: #080;">&#40;</span>q2 <span style="color: #080;">-</span> auc<span style="color: #080;">^</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>pos<span style="color: #080;">&#41;</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>neg<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
  ci.<span style="">upper</span> <span style="color: #080;">&lt;-</span> auc <span style="color: #080;">+</span> <span style="color: #080;">&#40;</span>se.<span style="">auc</span> <span style="color: #080;">*</span> <span style="color: #ff0000;">0.96</span><span style="color: #080;">&#41;</span>
  ci.<span style="">lower</span> <span style="color: #080;">&lt;-</span> auc <span style="color: #080;">-</span> <span style="color: #080;">&#40;</span>se.<span style="">auc</span> <span style="color: #080;">*</span> <span style="color: #ff0000;">0.96</span><span style="color: #080;">&#41;</span>
&nbsp;
  se.<span style="">auc</span>.<span style="">null</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">sqrt</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span> <span style="color: #080;">+</span> <span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>pos<span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> <span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>neg<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">12</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>pos<span style="color: #080;">&#41;</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>neg<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
  z <span style="color: #080;">&lt;-</span> <span style="color: #080;">&#40;</span>auc <span style="color: #080;">-</span> <span style="color: #ff0000;">0.5</span><span style="color: #080;">&#41;</span><span style="color: #080;">/</span>se.<span style="">auc</span>.<span style="">null</span>
  p <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">2</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">pnorm</span><span style="color: #080;">&#40;</span><span style="color: #080;">-</span><span style="color: #0000FF; font-weight: bold;">abs</span><span style="color: #080;">&#40;</span>z<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
&nbsp;
  stats <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span> <span style="color: #080;">&#40;</span>auc <span style="color: #080;">=</span> auc,
                       p.<span style="">value</span> <span style="color: #080;">=</span> p,
                       ci.<span style="">upper</span> <span style="color: #080;">=</span> ci.<span style="">upper</span>,
                       ci.<span style="">lower</span> <span style="color: #080;">=</span> ci.<span style="">lower</span>
                       <span style="color: #080;">&#41;</span>
&nbsp;
  <span style="color: #0000FF; font-weight: bold;">return</span> <span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">list</span><span style="color: #080;">&#40;</span>roc <span style="color: #080;">=</span> roc, stats <span style="color: #080;">=</span> stats<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span></pre></td></tr></table></div>

<p>So let&#8217;s go through how this works. The function requires two arguments: Firstly a numeric vector giving the results of the test being applied, and secondly a factor vector (or a vector that can be coerced to a factor with two levels) containing the status of the instance being tested eg. disease or healthy, case or control etc. We firstly ensure this grouping vector is a factor</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="rsplus" style="font-family:monospace;">grp <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">as.<span style="">factor</span></span><span style="color: #080;">&#40;</span>grp<span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>and then run some checks to ensure there is one classifier for each test result and that there are only two levels of the classifier.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="rsplus" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">if</span> <span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>pred<span style="color: #080;">&#41;</span> <span style="color: #080;">!=</span> <span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>grp<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#123;</span>
    <span style="color: #0000FF; font-weight: bold;">stop</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;The number of classifiers must match the number of data points&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span> 
&nbsp;
<span style="color: #0000FF; font-weight: bold;">if</span> <span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">levels</span><span style="color: #080;">&#40;</span>grp<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">!=</span> <span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#123;</span>
   <span style="color: #0000FF; font-weight: bold;">stop</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">&quot;There must only be 2 values for the classifier&quot;</span><span style="color: #080;">&#41;</span>
<span style="color: #080;">&#125;</span></pre></td></tr></table></div>

<p>The ROC plot should show the proportion of true positives against the proportion of false positives for any threshold value. However using the dataset we have we can define a set of thresholds at all the unique values of the test result.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="rsplus" style="font-family:monospace;"><span style="color: #0000FF; font-weight: bold;">cut</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">unique</span><span style="color: #080;">&#40;</span>pred<span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>Then at each cutoff point we want to calculate the number of true positives, false positives, true negatives and false negatives. We can do this using the sapply function in R. Looking at the true positives as an example, for every cutoff value we count the number of entries where the predictor vector is greater than the cutoff AND the group vector is the higher factor level (hence the reason for ensuring it is an ordered factor).</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="rsplus" style="font-family:monospace;">tp <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">sapply</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">cut</span>, <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">which</span><span style="color: #080;">&#40;</span>pred <span style="color: #080;">&gt;</span> x <span style="color: #080;">&amp;</span> grp <span style="color: #080;">==</span> <span style="color: #0000FF; font-weight: bold;">levels</span><span style="color: #080;">&#40;</span>grp<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
fn <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">sapply</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">cut</span>, <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">which</span><span style="color: #080;">&#40;</span>pred <span style="color: #080;">&lt;</span> x <span style="color: #080;">&amp;</span> grp <span style="color: #080;">==</span> <span style="color: #0000FF; font-weight: bold;">levels</span><span style="color: #080;">&#40;</span>grp<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
fp <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">sapply</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">cut</span>, <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">which</span><span style="color: #080;">&#40;</span>pred <span style="color: #080;">&gt;</span> x <span style="color: #080;">&amp;</span> grp <span style="color: #080;">==</span> <span style="color: #0000FF; font-weight: bold;">levels</span><span style="color: #080;">&#40;</span>grp<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
tn <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">sapply</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">cut</span>, <span style="color: #0000FF; font-weight: bold;">function</span><span style="color: #080;">&#40;</span>x<span style="color: #080;">&#41;</span> <span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">which</span><span style="color: #080;">&#40;</span>pred <span style="color: #080;">&lt;</span> x <span style="color: #080;">&amp;</span> grp <span style="color: #080;">==</span> <span style="color: #0000FF; font-weight: bold;">levels</span><span style="color: #080;">&#40;</span>grp<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>Each of the above produces a vector with the same length as cut. We can then calculate the true positive rate as the vector of true positives divided by the sum of the true positives and false negatives vector</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="rsplus" style="font-family:monospace;">tpr <span style="color: #080;">&lt;-</span> tp <span style="color: #080;">/</span> <span style="color: #080;">&#40;</span>tp <span style="color: #080;">+</span> fn<span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>and the false positive rate as the false positive vector divided by the sum of the false positive vector and the true negative vector.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="rsplus" style="font-family:monospace;">fpr <span style="color: #080;">&lt;-</span> fp <span style="color: #080;">/</span> <span style="color: #080;">&#40;</span>fp <span style="color: #080;">+</span> tn<span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>these two vectors give our y and x co-ordinates for our ROC plot. It&#8217;s then a simple matter of writing them into a data frame and ordering them for plotting to produce our basic ROC plot data.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="rsplus" style="font-family:monospace;">roc <span style="color: #080;">=</span> <span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span><span style="color: #080;">&#40;</span>x <span style="color: #080;">=</span> fpr, y <span style="color: #080;">=</span> tpr<span style="color: #080;">&#41;</span>
roc <span style="color: #080;">&lt;-</span> roc<span style="color: #080;">&#91;</span><span style="color: #0000FF; font-weight: bold;">order</span><span style="color: #080;">&#40;</span>roc$x, roc$y<span style="color: #080;">&#41;</span>,<span style="color: #080;">&#93;</span></pre></td></tr></table></div>

<p><strong>Summary Statistics</strong></p>
<p>Probably the commonest way of summarising the ROC plot is to compute the area under the curve (AUC). This reduces the ROC plot to a single scalar value representing the performance of the test. A perfect test has an AUC of 1 and a test that is equivalent to random guessing has an AUC of 0.5. There are a number of ways of estimating the AUC of a plot; the trapezoid rule is employed here. The function used here is the same as the trapz function from the <a href="http://cran.r-project.org/web/packages/caTools/index.html" title="caTools">caTools</a> package.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="rsplus" style="font-family:monospace;">i <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">2</span><span style="color: #080;">:</span><span style="color: #0000FF; font-weight: bold;">nrow</span><span style="color: #080;">&#40;</span>roc<span style="color: #080;">&#41;</span>
auc <span style="color: #080;">&lt;-</span> <span style="color: #080;">&#40;</span>roc$x<span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span> <span style="color: #080;">-</span> roc$x<span style="color: #080;">&#91;</span>i <span style="color: #080;">-</span> <span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">%*%</span> <span style="color: #080;">&#40;</span>roc$y<span style="color: #080;">&#91;</span>i<span style="color: #080;">&#93;</span> <span style="color: #080;">+</span> roc$y<span style="color: #080;">&#91;</span>i <span style="color: #080;">-</span> <span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #ff0000;">2</span></pre></td></tr></table></div>

<p>An interesting statistical property of the AUC is that it represents the probability that the test value of a positive case will rank higher than a negative case. This is essentially equivalent to the Wilcoxon-Mann-Whitney statistic. Hence an alternative way of computing the AUC would be</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="rsplus" style="font-family:monospace;">pos <span style="color: #080;">&lt;-</span> pred<span style="color: #080;">&#91;</span>grp <span style="color: #080;">==</span> <span style="color: #0000FF; font-weight: bold;">levels</span><span style="color: #080;">&#40;</span>grp<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span>
neg <span style="color: #080;">&lt;-</span> pred<span style="color: #080;">&#91;</span>grp <span style="color: #080;">==</span> <span style="color: #0000FF; font-weight: bold;">levels</span><span style="color: #080;">&#40;</span>grp<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span>
auc.<span style="">wilcox</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">wilcox.<span style="">test</span></span><span style="color: #080;">&#40;</span>pos,neg,exact<span style="color: #080;">=</span><span style="color: #ff0000;">0</span><span style="color: #080;">&#41;</span>$statistic <span style="color: #080;">/</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>pos<span style="color: #080;">&#41;</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>neg<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>The standard error of the ROC curve is given by: (Hanley and McNeil, 1982)</p>
<p class="ql-center-displayed-equation" style="line-height: 54px;"><span class="ql-right-eqno"> (1) </span><span class="ql-left-eqno"> &nbsp; </span><img src="http://www.rrandomness.com/wp-content/ql-cache/quicklatex.com-8e324c333ed888362f4ad0c61427bb2f_l3.png" height="54" width="479" class="ql-img-displayed-equation " alt="&#92;&#98;&#101;&#103;&#105;&#110;&#123;&#101;&#113;&#117;&#97;&#116;&#105;&#111;&#110;&#42;&#125; &#92;&#109;&#97;&#116;&#104;&#105;&#116;&#123;&#83;&#69;&#125;&#40;&#92;&#116;&#104;&#101;&#116;&#97;&#41;&#32;&#61;&#32;&#92;&#115;&#113;&#114;&#116;&#123;&#92;&#102;&#114;&#97;&#99;&#123;&#92;&#116;&#104;&#101;&#116;&#97;&#40;&#49;&#45;&#92;&#116;&#104;&#101;&#116;&#97;&#41;&#32;&#43;&#32;&#40;&#110;&#95;&#65;&#32;&#45;&#32;&#49;&#41;&#40;&#81;&#95;&#49;&#32;&#45;&#32;&#92;&#116;&#104;&#101;&#116;&#97;&#94;&#50;&#41;&#32;&#43;&#32;&#40;&#110;&#95;&#78;&#32;&#45;&#32;&#49;&#41;&#40;&#81;&#95;&#50;&#32;&#45;&#32;&#92;&#116;&#104;&#101;&#116;&#97;&#94;&#50;&#41;&#125;&#123;&#110;&#95;&#65;&#32;&#92;&#99;&#100;&#111;&#116;&#32;&#110;&#95;&#78;&#125;&#125; &#92;&#101;&#110;&#100;&#123;&#101;&#113;&#117;&#97;&#116;&#105;&#111;&#110;&#42;&#125;" title="Rendered by QuickLaTeX.com"/></p>
<p>where <img src="http://www.rrandomness.com/wp-content/ql-cache/quicklatex.com-356a08e839ab6974a16448e16e56745d_l3.png" class="ql-img-inline-formula " alt="&#92;&#116;&#104;&#101;&#116;&#97;" title="Rendered by QuickLaTeX.com" height="12" width="9" style="vertical-align: 0px;"/> is the AUC, <img src="http://www.rrandomness.com/wp-content/ql-cache/quicklatex.com-a3b40f056b6871ee26dc5a2f5b07afb2_l3.png" class="ql-img-inline-formula " alt="&#110;&#95;&#65;" title="Rendered by QuickLaTeX.com" height="11" width="21" style="vertical-align: -3px;"/> is the number of positive cases, <img src="http://www.rrandomness.com/wp-content/ql-cache/quicklatex.com-a0d1eb6cbd60647f5b872290e33b02b4_l3.png" class="ql-img-inline-formula " alt="&#110;&#95;&#78;" title="Rendered by QuickLaTeX.com" height="11" width="23" style="vertical-align: -3px;"/> is the number of negative cases, <img src="http://www.rrandomness.com/wp-content/ql-cache/quicklatex.com-21c90d3cabe295a4d007cfd75d360e82_l3.png" class="ql-img-inline-formula " alt="&#81;&#95;&#49;&#32;&#61;&#32;&#92;&#116;&#104;&#101;&#116;&#97;&#32;&#47;&#32;&#40;&#50;&#32;&#45;&#32;&#92;&#116;&#104;&#101;&#116;&#97;&#41;" title="Rendered by QuickLaTeX.com" height="19" width="115" style="vertical-align: -5px;"/> and  <img src="http://www.rrandomness.com/wp-content/ql-cache/quicklatex.com-5aa6c137b0f4dc2705eeddab61668f5c_l3.png" class="ql-img-inline-formula " alt="&#81;&#95;&#50;&#32;&#61;&#32;&#50;&#92;&#116;&#104;&#101;&#116;&#97;&#94;&#50;&#32;&#47;&#32;&#40;&#49;&#32;&#43;&#32;&#92;&#116;&#104;&#101;&#116;&#97;&#41;" title="Rendered by QuickLaTeX.com" height="20" width="132" style="vertical-align: -5px;"/>. Using the standard error we can calculate the upper and lower 95% confidence intervals for the AUC</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="rsplus" style="font-family:monospace;">pos <span style="color: #080;">&lt;-</span> pred<span style="color: #080;">&#91;</span>grp <span style="color: #080;">==</span> <span style="color: #0000FF; font-weight: bold;">levels</span><span style="color: #080;">&#40;</span>grp<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span>
neg <span style="color: #080;">&lt;-</span> pred<span style="color: #080;">&#91;</span>grp <span style="color: #080;">==</span> <span style="color: #0000FF; font-weight: bold;">levels</span><span style="color: #080;">&#40;</span>grp<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span>
q1 <span style="color: #080;">&lt;-</span> auc<span style="color: #080;">/</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">2</span><span style="color: #080;">-</span>auc<span style="color: #080;">&#41;</span>
q2 <span style="color: #080;">&lt;-</span> <span style="color: #080;">&#40;</span><span style="color: #ff0000;">2</span><span style="color: #080;">*</span>auc<span style="color: #080;">^</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span><span style="color: #080;">+</span>auc<span style="color: #080;">&#41;</span>
se.<span style="">auc</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">sqrt</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span>auc <span style="color: #080;">*</span> <span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span> <span style="color: #080;">-</span> auc<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> <span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>pos<span style="color: #080;">&#41;</span> <span style="color: #080;">-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span><span style="color: #080;">&#40;</span>q1 <span style="color: #080;">-</span> auc<span style="color: #080;">^</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> <span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>neg<span style="color: #080;">&#41;</span> <span style="color: #080;">-</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">*</span><span style="color: #080;">&#40;</span>q2 <span style="color: #080;">-</span> auc<span style="color: #080;">^</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>pos<span style="color: #080;">&#41;</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>neg<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
ci.<span style="">upper</span> <span style="color: #080;">&lt;-</span> auc <span style="color: #080;">+</span> <span style="color: #080;">&#40;</span>se.<span style="">auc</span> <span style="color: #080;">*</span> <span style="color: #ff0000;">0.96</span><span style="color: #080;">&#41;</span>
ci.<span style="">lower</span> <span style="color: #080;">&lt;-</span> auc <span style="color: #080;">-</span> <span style="color: #080;">&#40;</span>se.<span style="">auc</span> <span style="color: #080;">*</span> <span style="color: #ff0000;">0.96</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>We can also use equation (1) to calculate a p value for the AUC &#8211; ie. the probabality that the AUC = 0.5 (no discrimination). Substituting 0.5 into (1) and simplifying gives us the standard error for our null hypothesis.</p>
<p class="ql-center-displayed-equation" style="line-height: 43px;"><span class="ql-right-eqno"> (2) </span><span class="ql-left-eqno"> &nbsp; </span><img src="http://www.rrandomness.com/wp-content/ql-cache/quicklatex.com-8fd7d2c6822f112a2bbfaa0150f3631b_l3.png" height="43" width="189" class="ql-img-displayed-equation " alt="&#92;&#98;&#101;&#103;&#105;&#110;&#123;&#101;&#113;&#117;&#97;&#116;&#105;&#111;&#110;&#42;&#125; &#92;&#109;&#97;&#116;&#104;&#105;&#116;&#123;&#83;&#69;&#125;&#40;&#92;&#116;&#104;&#101;&#116;&#97;&#41;&#32;&#61;&#32;&#92;&#115;&#113;&#114;&#116;&#123;&#92;&#102;&#114;&#97;&#99;&#123;&#49;&#32;&#43;&#32;&#110;&#95;&#65;&#32;&#43;&#32;&#110;&#95;&#78;&#125;&#123;&#49;&#50;&#110;&#95;&#65;&#32;&#92;&#99;&#100;&#111;&#116;&#32;&#110;&#95;&#78;&#125; &#92;&#101;&#110;&#100;&#123;&#101;&#113;&#117;&#97;&#116;&#105;&#111;&#110;&#42;&#125;" title="Rendered by QuickLaTeX.com"/></p>
<p>We can then calculate a Z score and from that the p value using the normal distribution</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="rsplus" style="font-family:monospace;">pos <span style="color: #080;">&lt;-</span> pred<span style="color: #080;">&#91;</span>grp <span style="color: #080;">==</span> <span style="color: #0000FF; font-weight: bold;">levels</span><span style="color: #080;">&#40;</span>grp<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">2</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span>
neg <span style="color: #080;">&lt;-</span> pred<span style="color: #080;">&#91;</span>grp <span style="color: #080;">==</span> <span style="color: #0000FF; font-weight: bold;">levels</span><span style="color: #080;">&#40;</span>grp<span style="color: #080;">&#41;</span><span style="color: #080;">&#91;</span><span style="color: #ff0000;">1</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#93;</span>
se.<span style="">auc</span>.<span style="">null</span> <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">sqrt</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">1</span> <span style="color: #080;">+</span> <span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>pos<span style="color: #080;">&#41;</span> <span style="color: #080;">+</span> <span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>neg<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">/</span><span style="color: #080;">&#40;</span><span style="color: #ff0000;">12</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>pos<span style="color: #080;">&#41;</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">length</span><span style="color: #080;">&#40;</span>neg<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>
z <span style="color: #080;">&lt;-</span> <span style="color: #080;">&#40;</span>auc <span style="color: #080;">-</span> <span style="color: #ff0000;">0.5</span><span style="color: #080;">&#41;</span><span style="color: #080;">/</span>se.<span style="">auc</span>.<span style="">null</span>
p <span style="color: #080;">&lt;-</span> <span style="color: #ff0000;">2</span><span style="color: #080;">*</span><span style="color: #0000FF; font-weight: bold;">pnorm</span><span style="color: #080;">&#40;</span><span style="color: #080;">-</span><span style="color: #0000FF; font-weight: bold;">abs</span><span style="color: #080;">&#40;</span>z<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p>To finish off our rocdata function we just need to output our roc curve co-ordinates, auc and p value. I&#8217;ve chosen to put the stats measures into one data frame and the roc curve data into another and then put the two dataframes into a list.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="rsplus" style="font-family:monospace;">stats <span style="color: #080;">&lt;-</span> <span style="color: #0000FF; font-weight: bold;">data.<span style="">frame</span></span> <span style="color: #080;">&#40;</span>auc <span style="color: #080;">=</span> auc,
                     p.<span style="">value</span> <span style="color: #080;">=</span> p,
                     ci.<span style="">upper</span> <span style="color: #080;">=</span> ci.<span style="">upper</span>,
                     ci.<span style="">lower</span> <span style="color: #080;">=</span> ci.<span style="">lower</span>
                     <span style="color: #080;">&#41;</span>
&nbsp;
<span style="color: #0000FF; font-weight: bold;">return</span> <span style="color: #080;">&#40;</span><span style="color: #0000FF; font-weight: bold;">list</span><span style="color: #080;">&#40;</span>roc <span style="color: #080;">=</span> roc, stats <span style="color: #080;">=</span> stats<span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span></pre></td></tr></table></div>

<p><strong>References</strong><br />
Hanley JA, McNeil BJ. The meaning and use of the area under a receiver operating characteristic (ROC) curve. Radiology 1982;143:29-36.</p>
<div class="twttr_button">
				<a href="http://twitter.com/share?url=http://www.rrandomness.com/r/simple-roc-plots-with-ggplot2-part-1/&text=Simple ROC plots with ggplot2 - Part 1" target="_blank" title="Click here if you liked this article.">
					<img src="http://www.rrandomness.com/wp-content/plugins/twitter-plugin/images/twitt.gif" alt="Twitt" />
				</a>
			</div>]]></content:encoded>
			<wfw:commentRss>http://www.rrandomness.com/r/simple-roc-plots-with-ggplot2-part-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Collaborative R coding using Dropbox</title>
		<link>http://www.rrandomness.com/uncategorized/collaborative-r-coding-using-dropbox/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=collaborative-r-coding-using-dropbox</link>
		<comments>http://www.rrandomness.com/uncategorized/collaborative-r-coding-using-dropbox/#comments</comments>
		<pubDate>Fri, 27 May 2011 21:10:20 +0000</pubDate>
		<dc:creator>Kate Nambiar</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Dropbox]]></category>
		<category><![CDATA[R]]></category>

		<guid isPermaLink="false">http://www.rrandomness.com/?p=4</guid>
		<description><![CDATA[<p><a href="http://www.rrandomness.com/wp-content/uploads/2011/05/DropboxLogo.jpg"></a>I do like <a href="http://www.dropbox.com" target="_blank">Dropbox</a>. I&#8217;m a big fan of easy to use solutions to common problems. For me I work at two different sites and I do a significant amount of work at my desktop machine at home as well as on my laptop. Dropbox solved the problem of synchronising files between [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.rrandomness.com/wp-content/uploads/2011/05/DropboxLogo.jpg"><img class="alignleft size-thumbnail wp-image-5" title="DropboxLogo" src="http://www.rrandomness.com/wp-content/uploads/2011/05/DropboxLogo-150x150.jpg" alt="" width="150" height="150" /></a>I do like <a href="http://www.dropbox.com" target="_blank">Dropbox</a>. I&#8217;m a big fan of easy to use solutions to common problems. For me I work at two different sites and I do a significant amount of work at my desktop machine at home as well as on my laptop. Dropbox solved the problem of synchronising files between multiple machines. However Dropbox can go that bit further and take that nice synchronised file store and share it with someone else with a Dropbox account. Now file sharing is nothing new but Dropbox makes it dead easy. Added to that a basic version control system built into the software and you have the workings of a reasonably decent code hosting and development system. Now this isn&#8217;t going to be what you&#8217;d use to develop a big project with multiple collaborators &#8211; there are tools out there like <a href="http://subversion.tigris.org/" target="_blank">SVN</a> or <a href="https://github.com/" target="_blank">github</a> far better suited to that task. But when a friend and I found that we needed to share and develop some R code we figured that Dropbox would be perfect for the job.</p>
<p>Setting up shared folders on Dropbox is pretty straightforward but if you&#8217;ve never done it before there&#8217;s a guide <a href="https://www.dropbox.com/help/19" target="_blank">here</a>. The next problem we found was that we had to make sure the script could correctly reference the path to data files and other scripts. Fortunately if your Dropbox folder is located in the same place as the R home directory (is is for a default install on Windows and Mac machines &#8211; I haven&#8217;t tested this on a Linux box yet) then it should be straightforward process.</p>
<p>You can check your R home directory using (example for Windows XP)</p>
<p>&nbsp;</p>
<div style="overflow:auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family:monospace;"><a href="http://inside-r.org/r-doc/base/Sys.getenv"><span style="color: #003399; font-weight: bold;">Sys.getenv</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;R_USER&quot;</span><span style="color: #009900;">&#41;</span></pre>
</div>
</div>
<pre>[1] "C:/Documents and Settings/Kate/My Documents"</pre>
<p>&nbsp;</p>
<p>Your Dropbox folder should be in My Documents too. Then in your R script set your working directory or file locations as follows:</p>
<p>&nbsp;</p>
<div style="overflow:auto;">
<div class="geshifilter">
<pre class="r geshifilter-R" style="font-family:monospace;"><a href="http://inside-r.org/r-doc/base/setwd"><span style="color: #003399; font-weight: bold;">setwd</span></a><span style="color: #009900;">&#40;</span><a href="http://inside-r.org/r-doc/base/path.expand"><span style="color: #003399; font-weight: bold;">path.expand</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;~/Dropbox/SharedRScript&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre>
</div>
</div>
<p>&nbsp;</p>
<p>That way you can define the location of any other called scripts or data relative to your shared folder and they should work irrespective of which machine you are on.  So far Dropbox is working pretty well for our needs. It&#8217;s free (at least for 2Gb storage), synchronises quickly and is easy to use. You might run into problems if several people are working on a file at the same time &#8211; it will save multiple versions rather than try to merge the files.  However for us it&#8217;s a good and easy solution to collaborative coding.</p>
<p>&nbsp;</p>
<div class="twttr_button">
				<a href="http://twitter.com/share?url=http://www.rrandomness.com/uncategorized/collaborative-r-coding-using-dropbox/&text=Collaborative R coding using Dropbox" target="_blank" title="Click here if you liked this article.">
					<img src="http://www.rrandomness.com/wp-content/plugins/twitter-plugin/images/twitt.gif" alt="Twitt" />
				</a>
			</div>]]></content:encoded>
			<wfw:commentRss>http://www.rrandomness.com/uncategorized/collaborative-r-coding-using-dropbox/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
