<?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>【発表】タグの記事一覧｜ドクターフント(Dr. Hund)</title>
	<atom:link href="https://brain-storm.space/tag/%E7%99%BA%E8%A1%A8/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 16:54:45 +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>【発表】タグの記事一覧｜ドクターフント(Dr. Hund)</title>
	<link>https://brain-storm.space</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【R初心者向け】自分でROC曲線(ROC curve)を書く</title>
		<link>https://brain-storm.space/roc-curve/1212/</link>
					<comments>https://brain-storm.space/roc-curve/1212/#respond</comments>
		
		<dc:creator><![CDATA[brainblog]]></dc:creator>
		<pubDate>Thu, 23 Mar 2023 10:11:56 +0000</pubDate>
				<category><![CDATA[論文作成・統計]]></category>
		<category><![CDATA[pROC]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[ROC]]></category>
		<category><![CDATA[sentitivity]]></category>
		<category><![CDATA[specificity]]></category>
		<category><![CDATA[医療]]></category>
		<category><![CDATA[学会]]></category>
		<category><![CDATA[感度]]></category>
		<category><![CDATA[曲線]]></category>
		<category><![CDATA[特異度]]></category>
		<category><![CDATA[発表]]></category>
		<category><![CDATA[統計]]></category>
		<category><![CDATA[論文]]></category>
		<guid isPermaLink="false">https://brain-storm.space/?p=1212</guid>

					<description><![CDATA[ROC曲線の書き方を紹介します。RではROC曲線を書くのはとても簡単です。ROC曲線を描くためのライブラリはたくさんありますが、今回はよく使われていて使いやすいpROCライブラリでの書き方を紹介します。EZRなどでも書く]]></description>
										<content:encoded><![CDATA[
<p>ROC曲線の書き方を紹介します。RではROC曲線を書くのはとても簡単です。ROC曲線を描くためのライブラリはたくさんありますが、今回はよく使われていて使いやすいpROCライブラリでの書き方を紹介します。EZRなどでも書くことはできますが、自分で書ける方が色々と応用できます。</p>



<h3 id="rtoc-1" >サンプルデータを作る</h3>



<p>ここはサンプルデータを作るだけのところですので、不要な方は次の見出しに読み飛ばしてください。二つの検査、&#8221;assay1&#8243;と&#8221;assay2&#8243;でどの程度疾患の人を検出できるのかを検討してみます。</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>ちなみにこれらの分布はこのようなものになります。</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のdotplot</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のdotplot</figcaption></figure>



<div id="rtoc-mokuji-wrapper" class="rtoc-mokuji-content frame2 preset1 animation-fade rtoc_open default" data-id="1212" 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">サンプルデータを作る</a></li><li class="rtoc-item"><a href="#rtoc-2">pROCを使ってみる</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-3">pROCパッケージのインストール</a></li><li class="rtoc-item"><a href="#rtoc-4">1つのROC曲線を描く</a></li><li class="rtoc-item"><a href="#rtoc-5">パーセント表示にする</a></li><li class="rtoc-item"><a href="#rtoc-6">感度の信頼区間を表示する</a></li></ul></li><li class="rtoc-item"><a href="#rtoc-7">カットオフに最適な点を表示する</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-8">Youden Indexによる最適なカットオフを表示する</a></li><li class="rtoc-item"><a href="#rtoc-9">Top Leftによる最適なカットオフを表示する</a></li></ul></li><li class="rtoc-item"><a href="#rtoc-10">必要な数値を取り出す</a></li><li class="rtoc-item"><a href="#rtoc-11">2つのROC曲線を重ねて比較する</a><ul class="rtoc-mokuji mokuji_ul level-2"><li class="rtoc-item"><a href="#rtoc-12">2つのROC曲線を重ねる</a></li><li class="rtoc-item"><a href="#rtoc-13">2つの検査について比較する</a></li></ul></li></ol></div><h2 id="rtoc-2" >pROCを使ってみる</h2>



<h3 id="rtoc-3" >pROCパッケージのインストール</h3>



<p>pROCパッケージをインストールして、起動します。</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" >1つのROC曲線を描く</h3>



<p>早速いってみましょう。まずデータフレームは次のようになっています。</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>ここからもいくつか方法はありますが、まずroc()を使って、その結果をオブジェクトに格納してみましょう。</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>次のように&#8221;~&#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>conditionの列は0, 1でもいけますが2値変数です。ciは信頼区間です。</p>



<p>plot()でROC曲線を描くことができます。</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" >パーセント表示にする</h3>



<p>好みにより軸をパーセント表示にすることができます。percent=TRUEを付け加えます。</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" >感度の信頼区間を表示する</h3>



<p>感度の信頼区間を計算するには ci.se()を使います。colは色を指定します。</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" >カットオフに最適な点を表示する</h2>



<p>ROC曲線から大事なことは、カットオフに最も適した点を調べることです。最適なカットオフは何か、というところも大きなテーマですが、ここでは２つの方法を紹介します。&#8221;Youden Index&#8221;と&#8221;top left&#8221;です。簡単に説明すれば、Youden Indexは&#8221;<span class="marker">感度+特異度-1</span>&#8221; で計算され、その数値が一番高いところを選びます。また、top leftは<span class="marker">ROC曲線の一番左上に近い点</span>を抽出します。</p>



<h3 id="rtoc-8" >Youden Indexによる最適なカットオフを表示する</h3>



<p>この時にはplotの中に追記をしていきます。<span class="marker2">print.thres = &#8220;best&#8221;, print.thres.bes.metod = &#8220;youden&#8221;</span>とすると最適点が表示されます。また、<span class="marker">legacy.axes=TRUEにするとx軸を1-Specificityになります。</span></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">Youden Index (J = 感度 + 特異度 &#8211; 1) による最適なカットオフ</figcaption></figure>



<p>この場合カットオフは3.912で感度が0.820、特異度は0.960です。</p>



<h3 id="rtoc-9" >Top Leftによる最適なカットオフを表示する</h3>



<p>top leftの場合は<span class="marker">print.thres.best.method=&#8221;closest.topleft&#8221;</span>にします。</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">topleftによる最適なカットオフ</figcaption></figure>



<p>この場合のカットオフは3.365、感度0.860、特異度0.880になっています。括弧内は特異度、感度の順番になっていることに注意をしてください。</p>



<h2 id="rtoc-10" >必要な数値を取り出す</h2>



<p>AUC(area under the curve)の値や感度、特異度などの数値を発表や論文で示す必要があります。その時には次のように表示できます。</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" >2つのROC曲線を重ねて比較する</h2>



<p>今回2つの検査を想定してサンプルデータを作りました。どちらの検査が優れているかみていきます。</p>



<h3 id="rtoc-12" >2つのROC曲線を重ねる</h3>



<p>２つのROC曲線を重ねる時には後から重ねる方をlines()にします。または単純にplot(&#8230;, add=TRUE)でも同じ結果になります。colで色を指定しています。</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: r; title: Code example; notranslate">
# ２つの検査について、roc()を使ってその結果をオブジェクトに格納する
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;))

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

# 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" >2つの検査について比較する</h3>



<p>2つの検査について比較する時には<span class="marker">roc.test()を使います</span>。デフォルトではAUC(area under the curve)をDeLong法で比較しますが、method = &#8220;bootstrap&#8221;と指定すればブートストラップ法などにも変更できます。textで中央にp値を表示しています。また<span class="marker">タイトルはmain=で指定できます。</span></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>比較の結果を見るだけだったらroc.test()で結果が出てきます。</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>最後に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>綺麗なROC曲線を書くことができました。<br>いかがでしたでしょうか。お役に立ちましたら幸いです。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://brain-storm.space/roc-curve/1212/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
