ソースを参照

Adding gtetAUC and compute_roc_metrics

Andrej 1 週間 前
コミット
4769d23d1d
4 ファイル変更84 行追加0 行削除
  1. 2 0
      NAMESPACE
  2. 47 0
      R/roc.R
  3. 18 0
      man/simple.compute_roc_metrics.Rd
  4. 17 0
      man/simple.getAUC.Rd

+ 2 - 0
NAMESPACE

@@ -2,5 +2,7 @@
 
 export(remapVariable)
 export(selectValid)
+export(simple.compute_roc_metrics)
+export(simple.getAUC)
 export(simple.roc)
 export(sumWithNA)

+ 47 - 0
R/roc.R

@@ -58,3 +58,50 @@ psi10 <- function(x, s) {
   v1 + 0.5 * v2
 }
 
+#' Calculate AUC
+#' @param roc as returned by simple.roc
+#'
+#' @return AUC (numeric)
+#'
+#' @export
+simple.getAUC <- function(roc) {
+  V0 = roc[!roc$labels, 'V']
+  mean(V0)
+}
+
+#' Calculate metrics associated with ROC 
+#'
+#' @param ROC roc as returned by simple.roc
+#'
+#' @return a list with elemnts: : FPR, TPR, threshold, FPR CI95:min , FPR CI95: max, TPR CI95:min, TPR CI95:max
+#' all evaluated at optimum point (Youden index)
+#'
+#' @export
+
+simple.compute_roc_metrics <- function(ROC) {
+
+#determine optimal point on ROC curve using Youden index
+#ROC is the output of simple.roc
+#output is a list : tpr, tpr CI95:min , tpr CI95: max, fpr, fpr CI95:min, fpr CI95:max, value of score,
+#all evaluated at optimum point, 
+   
+   n=base::sum(ROC$labels)
+   m=base::sum(!ROC$labels)
+   nt=base::length(ROC$TPR)
+   dist<-base::abs(ROC$TPR-ROC$FPR)
+   imax<-base::which.max(dist)
+   hTPR<-stats::prop.test(x=ROC$TPR[imax]*n,n=n,conf.level=0.95,correct=FALSE)
+   hFPR<-stats::prop.test(x=ROC$FPR[imax]*m,n=m,conf.level=0.95,correct=FALSE)
+   
+  
+   return(base::list(
+    FPR = ROC$FPR[imax],
+    TPR = ROC$TPR[imax],
+    threshold = ROC$scores[imax],
+    lFPR = hFPR$conf.int[1],
+    hFPR = hFPR$conf.int[2],
+    lTPR = hTPR$conf.int[1],
+    hTPR = hTPR$conf.int[2]
+  ))
+}
+

+ 18 - 0
man/simple.compute_roc_metrics.Rd

@@ -0,0 +1,18 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/roc.R
+\name{simple.compute_roc_metrics}
+\alias{simple.compute_roc_metrics}
+\title{Calculate metrics associated with ROC}
+\usage{
+simple.compute_roc_metrics(ROC)
+}
+\arguments{
+\item{ROC}{roc as returned by simple.roc}
+}
+\value{
+a list with elemnts: : FPR, TPR, threshold, FPR CI95:min , FPR CI95: max, TPR CI95:min, TPR CI95:max
+all evaluated at optimum point (Youden index)
+}
+\description{
+Calculate metrics associated with ROC
+}

+ 17 - 0
man/simple.getAUC.Rd

@@ -0,0 +1,17 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/roc.R
+\name{simple.getAUC}
+\alias{simple.getAUC}
+\title{Calculate AUC}
+\usage{
+simple.getAUC(roc)
+}
+\arguments{
+\item{roc}{as returned by simple.roc}
+}
+\value{
+AUC (numeric)
+}
+\description{
+Calculate AUC
+}