備忘録的なやつです。
前回の続き。
【R】ROC曲線を書く
Rを使ってROC曲線を比較します。
使う関数は**roc()とroc.test()**です。
ROC曲線を書く
# Rをきれいにする
rm(list = ls())
# ライブラリの読み込み
library(pROC)
# データの読み込み
df <- aSAH
# データの確認
summary(df)
⬇実行結果
> #Rをきれいにする
> rm(list = ls())
>
> #ライブラリの読み込み
> library(pROC)
Type 'citation("pROC")' for a citation.
次のパッケージを付け加えます: ‘pROC’
以下のオブジェクトは ‘package:stats’ からマスクされています:
cov, smooth, var
>
> #データの読み込み
> df <- aSAH
>
> #データの確認
> summary(df)
gos6 outcome gender age wfns s100b ndka
1:28 Good:72 Male :42 Min. :18.0 1:39 Min. :0.030 Min. : 3.01
2: 0 Poor:41 Female:71 1st Qu.:42.0 2:32 1st Qu.:0.090 1st Qu.: 9.01
3:13 Median :51.0 3: 4 Median :0.140 Median : 12.22
4: 6 Mean :51.1 4:16 Mean :0.247 Mean : 19.66
5:66 3rd Qu.:61.0 5:22 3rd Qu.:0.330 3rd Qu.: 17.30
Max. :81.0 Max. :2.070 Max. :419.19
ここまでは前回と同じ。
今回は、s100b、ndkaがどの程度outcomeを予測するのかをROC曲線で評価し、比較します。
まず、s100bについて。
ROC1 <- roc(outcome ~ s100b, data = df, ci = TRUE)
ROC1
⬇実行結果
> ROC1 <- roc(outcome ~ s100b, data = df, ci = TRUE)
Setting levels: control = Good, case = Poor
Setting direction: controls < cases
> ROC1
Call:
roc.formula(formula = outcome ~ s100b, data = df, ci = TRUE)
Data: s100b in 72 controls (outcome Good) < 41 cases (outcome Poor).
Area under the curve: 0.7314
95% CI: 0.6301-0.8326 (DeLong)
ROC曲線を描きます。
plot(ROC1,
lty = 1, #直線
legacy.axes = TRUE
)
⬇描画結果
続いて、ndkaについて。
ROC2 <- roc(outcome ~ ndka, data = df, ci = TRUE)
ROC2
⬇実行結果
> ROC2 <- roc(outcome ~ ndka, data = df, ci = TRUE)
Setting levels: control = Good, case = Poor
Setting direction: controls < cases
> ROC2
Call:
roc.formula(formula = outcome ~ ndka, data = df, ci = TRUE)
Data: ndka in 72 controls (outcome Good) < 41 cases (outcome Poor).
Area under the curve: 0.612
95% CI: 0.5012-0.7227 (DeLong)
ROC曲線を描きます。
add = TRUE
でさっき作成したs100bのグラフにndkaの曲線を加えます。
plot(ROC2,
lty = 2, #点線
add = TRUE # add = TRUEでさっきのグラフに加える
)
⬇実行結果
ROC曲線の比較
**roc.test()**関数で2つのAUCを比較します。
roc.test(ROC1, ROC2)
ROC1, 2
:**roc()**で作成したオブジェクト
roc.test(ROC1, ROC2)
⬇実行結果
> roc.test(ROC1, ROC2)
DeLong's test for two correlated ROC curves
data: ROC1 and ROC2
Z = 1.3908, p-value = 0.1643
alternative hypothesis: true difference in AUC is not equal to 0
sample estimates:
AUC of roc1 AUC of roc2
0.7313686 0.6119580
この結果から、2つのROCを比較した検定結果はp = 0.1643でした。
s100bとndkaの予測能に有意差は見られませんでした。