<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	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/"
	>

<channel>
	<title>【sensitivity】タグの記事一覧｜ドクターフント(Dr. Hund)</title>
	<atom:link href="https://brain-storm.space/tag/sensitivity/feed/" rel="self" type="application/rss+xml" />
	<link>https://brain-storm.space</link>
	<description>脳や研究について発信するブログです。This site is for research and statistics.</description>
	<lastBuildDate>Fri, 24 Mar 2023 17:13:38 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.1</generator>

<image>
	<url>https://brain-storm.space/wp-content/uploads/2021/04/cropped-3d0209af428738b78799159b4ce75ad9-32x32.png</url>
	<title>【sensitivity】タグの記事一覧｜ドクターフント(Dr. Hund)</title>
	<link>https://brain-storm.space</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>[R beginners] Drawing ROC curve</title>
		<link>https://brain-storm.space/roc-curve-en/1304/</link>
					<comments>https://brain-storm.space/roc-curve-en/1304/#respond</comments>
		
		<dc:creator><![CDATA[brainblog]]></dc:creator>
		<pubDate>Sat, 25 Mar 2023 07:00:00 +0000</pubDate>
				<category><![CDATA[Statistical Analysis]]></category>
		<category><![CDATA[pROC]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[ROC]]></category>
		<category><![CDATA[sensitivity]]></category>
		<category><![CDATA[specificity]]></category>
		<guid isPermaLink="false">https://brain-storm.space/?p=1304</guid>

					<description><![CDATA[I will introduce how to draw an ROC curve. In R, it is very easy to draw an ROC curve. There are many librarie]]></description>
										<content:encoded><![CDATA[
<p>I will introduce how to draw an ROC curve. In R, it is very easy to draw an ROC curve. There are many libraries available for drawing ROC curves, but this time I will introduce how to draw it using the pROC library, which is commonly used and easy to use.</p>



<h3 id="rtoc-1" >Creating Sample Data</h3>



<p>This section is about creating sample data. If you don&#8217;t need it, please skip to the next section. Let&#8217;s consider how well we can detect people with the disease using two assays, &#8220;assay1&#8221; and &#8220;assay2&#8221;.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
set.seed(3)
condition &lt;- rep(c(&quot;healthy&quot;, &quot;disease&quot;), each = 50)
assay1 &lt;- c(rnorm(40, mean = 1, sd = 2), 
            rnorm(10, mean = 3, sd = 1), 
            rnorm(40, mean = 5, sd = 2), 
            rnorm(10, mean = 7, sd = 1))
assay2 &lt;- c(rnorm(30, mean = 1, sd = 3), 
            rnorm(20, mean = 2, sd = 2), 
            rnorm(30, mean = 3, sd = 3), 
            rnorm(20, mean = 4, sd = 2))

df = data.frame(condition, assay1, assay2)
</pre></div>


<p>these distributions would look like this:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
library(beeswarm)
beeswarm(data = df, assay1 ~ condition, 
         col = c(&quot;red&quot;,&quot;blue&quot;), pch=19)
beeswarm(data = df, assay2 ~ condition,
         col = c(&quot;red&quot;, &quot;blue&quot;), pch=19)
</pre></div>


<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2023/03/dotplot_assay1.jpeg" alt="" class="wp-image-1220" width="400" height="400"/><figcaption class="wp-element-caption">Assay1</figcaption></figure>



<figure class="wp-block-image size-full is-resized"><img decoding="async" src="https://brain-storm.space/wp-content/uploads/2023/03/dotplot_assay2.jpeg" alt="" class="wp-image-1221" width="400" height="400"/><figcaption class="wp-element-caption">Assay2</figcaption></figure>



<div id="rtoc-mokuji-wrapper" class="rtoc-mokuji-content frame2 preset1 animation-fade rtoc_open default" data-id="1304" data-theme="jin-child">
			<div id="rtoc-mokuji-title" class=" rtoc_left">
			<button class="rtoc_open_close rtoc_open"></button>
			<span>Contents</span>
			</div><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-1">Creating Sample Data</a></li><li class="rtoc-item"><a href="#rtoc-2">Use pROC</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-3">Install pROC package</a></li><li class="rtoc-item"><a href="#rtoc-4">To draw a single ROC curve</a></li><li class="rtoc-item"><a href="#rtoc-5">Convert to Percentage Display</a></li><li class="rtoc-item"><a href="#rtoc-6">Display Confidence Interval of Sensitivity</a></li></ul></li><li class="rtoc-item"><a href="#rtoc-7">Display Optimal Cutoff Point</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-8">display Optimal Cutoff based on Youden Index</a></li><li class="rtoc-item"><a href="#rtoc-9">Display Optimal Cutoff using Top Left Method</a></li></ul></li><li class="rtoc-item"><a href="#rtoc-10">Extracting Necessary Values</a></li><li class="rtoc-item"><a href="#rtoc-11">Comparing two ROC curves by overlapping them</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-12">Overlay Two ROC Curves</a></li><li class="rtoc-item"><a href="#rtoc-13">Compare Two Assays</a></li></ul></li></ol></div><h2 id="rtoc-2" >Use pROC</h2>



<h3 id="rtoc-3" >Install pROC package</h3>



<p>First, you need to install and load the pROC package to use it in R.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
install.packages(&quot;pROC&quot;)
library(pROC)
</pre></div>


<h3 id="rtoc-4" >To draw a single ROC curve</h3>



<p>The data frame looks like this:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
&gt; head(df)
  condition     assay1     assay2
1   healthy -0.9238668  2.8520501
2   healthy  0.4149486 -0.2152325
3   healthy  1.5175764  4.1593113
4   healthy -1.3042638  2.8068527
5   healthy  1.3915657  4.0523835
6   healthy  1.0602479  2.8245020
</pre></div>


<p>There are several ways to proceed from here, but first let&#8217;s use roc() to store the result in an object.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
roc1 &lt;- roc(condition, assay1, data=df, ci=TRUE,
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))
</pre></div>


<p>As an alternative, you can also use the following notation with &#8220;~&#8221;:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
roc1 &lt;- roc(df$condition ~ df$assay1, ci=TRUE, 
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))
</pre></div>


<p>The <code>condition</code> column can be 0 and 1, as it is a binary variable. <code>ci</code> refers to the confidence interval.</p>



<p>you can use the <code>plot()</code> function to draw the ROC curve.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
plot(roc1)
</pre></div>


<figure class="wp-block-image size-full"><img decoding="async" width="400" height="400" src="https://brain-storm.space/wp-content/uploads/2023/03/roc_1.jpeg" alt="" class="wp-image-1224"/><figcaption class="wp-element-caption">Assay1</figcaption></figure>



<h3 id="rtoc-5" >Convert to Percentage Display</h3>



<p>You can customize the axis to display in percentage format by adding <code>percent=TRUE</code>.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
roc1 &lt;- roc(condition, assay1, data=df, percent=TRUE,
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))
plot(roc1)
</pre></div>


<figure class="wp-block-image size-full"><img decoding="async" width="400" height="400" src="https://brain-storm.space/wp-content/uploads/2023/03/roc_plot2_percent.jpeg" alt="" class="wp-image-1225"/></figure>



<h3 id="rtoc-6" >Display Confidence Interval of Sensitivity</h3>



<p>You can display the confidence interval of the sensitivity. To calculate the confidence interval of sensitivity, use <code>ci.se()</code>. <code>col</code> is used to specify the color.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
roc2 &lt;- roc(condition, assay2, data=df, ci=TRUE,
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))
plot(roc2)
rocCI &lt;- ci.se(roc2)
plot(rocCI, type=&quot;shape&quot;, col=&quot;lightblue&quot;)
</pre></div>


<figure class="wp-block-image size-full"><img decoding="async" width="400" height="400" src="https://brain-storm.space/wp-content/uploads/2023/03/roc_ci_3.jpeg" alt="" class="wp-image-1227"/></figure>



<h2 id="rtoc-7" >Display Optimal Cutoff Point</h2>



<p>One important aspect of the ROC curve is determining the optimal cut-off point. While determining the optimal cut-off point is a big topic on its own, here we introduce two methods: the Youden Index and top left.</p>



<p>To explain briefly, the Youden Index is calculated as &#8220;sensitivity + specificity &#8211; 1&#8221;, and the point with the highest value is selected as the optimal cut-off point. On the other hand, the top left method extracts the point closest to the upper left corner of the ROC curve.</p>



<h3 id="rtoc-8" >display Optimal Cutoff based on Youden Index</h3>



<p>You can add the optimal cutoff to the plot by modifying the <code>plot()</code> function call. Setting <code>print.thres = "best"</code> and <code>print.thres.best.method = "youden"</code> will display the optimal point. Setting <code>legacy.axes = TRUE</code> will make the x-axis display as 1-Specificity.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
plot(roc1, main = &quot;ROC Curve&quot;,      
     identity = TRUE,
     print.thres = &quot;best&quot;,
     print.thres.best.method = &quot;youden&quot;,
     legacy.axes = TRUE)
</pre></div>


<figure class="wp-block-image size-full"><img decoding="async" width="400" height="400" src="https://brain-storm.space/wp-content/uploads/2023/03/roc_youden_5.jpeg" alt="" class="wp-image-1231"/><figcaption class="wp-element-caption">optimal cutoff using the Youden Index (J = Sensitivity + Specificity &#8211; 1)</figcaption></figure>



<p>The optimal cut-off point in this case is 3.912 with sensitivity of 0.820 and specificity of 0.960.</p>



<h3 id="rtoc-9" >Display Optimal Cutoff using Top Left Method</h3>



<p>In the case of top left, set <code>print.thres.best.method="closest.topleft"</code> in <code>plot()</code> function.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
plot(roc1, main = &quot;ROC Curve&quot;,      
     identity = TRUE,
     print.thres = &quot;best&quot;,
     print.thres.best.method = &quot;closest.topleft&quot;
     legacy.axes = TRUE)

</pre></div>


<figure class="wp-block-image size-full"><img decoding="async" width="400" height="400" src="https://brain-storm.space/wp-content/uploads/2023/03/roc_topleft_4.jpeg" alt="" class="wp-image-1234"/><figcaption class="wp-element-caption">The optimal cutoff based on the top left approach</figcaption></figure>



<p>Note that in this case, the optimal cutoff is 3.365, with a sensitivity of 0.860 and specificity of 0.880.</p>



<h2 id="rtoc-10" >Extracting Necessary Values</h2>



<p>When presenting or publishing the values of AUC (area under the curve), sensitivity, specificity, etc., you can display them as follows.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
&gt; auc(roc1)
Area under the curve: 0.9536

# youden
&gt; coords(roc1, &quot;best&quot;, ret=c(&quot;threshold&quot;, &quot;sens&quot;, &quot;spec&quot;, &quot;ppv&quot;, &quot;npv&quot;))
          threshold sensitivity specificity       ppv       npv
threshold  3.912407        0.82        0.96 0.9534884 0.8421053

# topleft
&gt; coords(roc1, &quot;best&quot;, best.method=&quot;closest.topleft&quot;,　ret=c(&quot;threshold&quot;, &quot;sens&quot;, &quot;spec&quot;, &quot;ppv&quot;, &quot;npv&quot;))
          threshold sensitivity specificity      ppv       npv
threshold  3.364778        0.86        0.88 0.877551 0.8627451
</pre></div>


<h2 id="rtoc-11" >Comparing two ROC curves by overlapping them</h2>



<p>We have created sample data assuming two tests. Let&#8217;s see which test is better by comparing the two ROC curves.</p>



<h3 id="rtoc-12" >Overlay Two ROC Curves</h3>



<p>When overlapping two ROC curves, you can use the <code>lines()</code> function for the one that you want to add later, or simply use <code>plot(..., add=TRUE)</code> for the second curve. You can specify the color using <code>col</code>.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
# Use roc() to create objects for the two assays
roc1 &lt;- roc(condition, assay1, data=df, ci=TRUE,
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))
roc2 &lt;- roc(condition, assay2, data=df, ci=TRUE, 
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))

# Use lines()
obj1 &lt;- plot(roc1,
             col=&quot;red&quot;)
obj2 &lt;- lines(roc2,
              col=&quot;blue&quot;)

# Use add = TRUE
obj1 &lt;- plot(roc1,
             col=&quot;red&quot;)
obj2 &lt;- plot(roc2,
             col=&quot;blue&quot;,
             add=TRUE)
</pre></div>


<figure class="wp-block-image size-full"><img decoding="async" width="400" height="400" src="https://brain-storm.space/wp-content/uploads/2023/03/roc_two_assay_6.jpeg" alt="" class="wp-image-1239"/></figure>



<h3 id="rtoc-13" >Compare Two Assays</h3>



<p>o compare two assays, use <code>roc.test()</code>. By default, it compares the AUCs (area under the curve) using the DeLong method, but you can change it to the bootstrap method by specifying <code>method = "bootstrap"</code>. The p-value is displayed in the center using text(). You can also specify the title using <code>main=</code>.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
roc1 &lt;- roc(condition, assay1, data=df, ci=TRUE,
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))
roc2 &lt;- roc(condition, assay2, data=df, ci=TRUE, 
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))

obj1 &lt;- plot(roc1,
             main=&quot;Comparison&quot;,
             col=&quot;red&quot;)
obj2 &lt;- lines(roc2,
              col=&quot;blue&quot;)
obj &lt;- roc.test(obj1, obj2)
text(.5, .5, labels=paste(&quot;p-value =&quot;, format.pval(obj$p.value, 3)), 
     adj=c(0, .5))
</pre></div>


<figure class="wp-block-image size-full"><img decoding="async" width="400" height="400" src="https://brain-storm.space/wp-content/uploads/2023/03/roc_comparison_7.jpeg" alt="" class="wp-image-1242"/></figure>



<p>If you just want to see the comparison results, <code>roc.test()</code> will provide you with the results.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
&gt; roc.test(roc1, roc2)

	DeLong's test for two correlated ROC curves

data:  roc1 and roc2
Z = 3.7621, p-value = 0.0001685
alternative hypothesis: true difference in AUC is not equal to 0
95 percent confidence interval:
 0.0969536 0.3078464
sample estimates:
AUC of roc1 AUC of roc2 
     0.9536      0.7512 
</pre></div>


<p>Finally, let&#8217;s add a legend.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
roc1 &lt;- roc(condition, assay1, data=df, ci=TRUE,
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))
roc2 &lt;- roc(condition, assay2, data=df, ci=TRUE, 
            levels=c(&quot;healthy&quot;, &quot;disease&quot;))

obj1 &lt;- plot(roc1,
             main=&quot;Comparison&quot;,
             col=&quot;red&quot;)
obj2 &lt;- lines(roc2,
              col=&quot;blue&quot;)

obj &lt;- roc.test(obj1, obj2)

# p values in the graph
text(.5, .5, labels=paste(&quot;p-value =&quot;, format.pval(obj$p.value, 3)), 
     adj=c(0, .5))

#legend
legend(&quot;bottomright&quot;, legend=c(&quot;Assay1&quot;, &quot;Assay2&quot;),
       col=c(&quot;red&quot;, &quot;blue&quot;), lty=1, lwd=2)
</pre></div>


<figure class="wp-block-image size-full"><img decoding="async" width="400" height="400" src="https://brain-storm.space/wp-content/uploads/2023/03/roc_comparison_final_8-1.jpeg" alt="" class="wp-image-1251"/></figure>



<p>That was a helpful tutorial on how to draw ROC curves in R using the pROC package. Thank you for sharing!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://brain-storm.space/roc-curve-en/1304/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
