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

Read our documentation on viewing source code .

Loading