xrobin / pROC
1
/* pROC: Tools Receiver operating characteristic (ROC curves) with
2
   (partial) area under the curve, confidence intervals and comparison. 
3
   Copyright (C) 2014 Xavier Robin
4

5
   This program is free software: you can redistribute it and/or modify
6
   it under the terms of the GNU General Public License as published by
7
   the Free Software Foundation, either version 3 of the License, or
8
   (at your option) any later version.
9
  
10
   This program is distributed in the hope that it will be useful,
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
   GNU General Public License for more details.
14
  
15
   You should have received a copy of the GNU General Public License
16
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
*/
18

19
#include <Rcpp.h> 
20
using namespace Rcpp;
21

22
// [[Rcpp::export]]
23 8
List rocUtilsPerfsAllC(NumericVector thresholds, NumericVector controls, NumericVector cases, std::string direction) {
24 8
  NumericVector se(thresholds.size());
25 8
  NumericVector sp(thresholds.size());
26
  long tp, tn;
27
  long i; // iterator over cases & controls
28
  
29 8
  if (direction == ">") {
30 8
    for (long t = 0; t < thresholds.size(); t++) {
31 8
      if (t % 100 == 0) Rcpp::checkUserInterrupt();
32 8
      double threshold = thresholds(t);
33 8
        tp = 0;
34 8
        for (i = 0; i < cases.size(); i++) {
35 8
          if (cases(i) <= threshold) {
36 8
            tp++;
37
        }
38
      }
39 8
      se(t) = (double)tp / cases.size();
40 8
      tn = 0;
41 8
      for (i = 0; i < controls.size(); i++) {
42 8
        if (controls(i) > threshold) {
43 8
          tn++;
44
        }
45
      }
46 8
      sp(t) = (double)tn / controls.size();
47
    }
48
  }
49
 else {
50 8
    for (long t = 0; t < thresholds.size(); t++) {
51 8
      if (t % 100 == 0) Rcpp::checkUserInterrupt();
52 8
      double threshold = thresholds(t);
53 8
      tp = 0;
54 8
      for (i = 0; i < cases.size(); i++) {
55 8
        if (cases(i) >= threshold) {
56 8
          tp++;
57
        }
58
      }
59 8
      se(t) = (double)tp / cases.size();
60 8
      long tn = 0;
61 8
      for (i = 0; i < controls.size(); i++) {
62 8
        if (controls(i) < threshold) {
63 8
          tn++;
64
        }
65
      }
66 8
      sp(t) = (double)tn / controls.size();
67
    }
68
  }
69 8
  List ret;
70 8
  ret["se"] = se;
71 8
  ret["sp"] = sp;
72 8
  return(ret);
73
}

Read our documentation on viewing source code .

Loading