Browse code

Merge pull request #55 from SydneyBioX/master

Fix for Sint R Removal and Default Random Forest Now Supports Survival

Dario Strbenac authored on 25/11/2022 13:40:13 • GitHub committed on 25/11/2022 13:40:13
Showing 9 changed files

... ...
@@ -2,6 +2,8 @@
2 2
 .Rhistory
3 3
 .RData
4 4
 .Ruserdata
5
+src/ClassifyR.so
6
+src/coxformatrices.o
5 7
 
6 8
 
7 9
 # vscode stuff
... ...
@@ -3,8 +3,8 @@ Type: Package
3 3
 Title: A framework for cross-validated classification problems, with
4 4
        applications to differential variability and differential
5 5
        distribution testing
6
-Version: 3.3.4
7
-Date: 2022-11-22
6
+Version: 3.3.5
7
+Date: 2022-11-25
8 8
 Authors@R:
9 9
     c(
10 10
     person(given = "Dario", family = "Strbenac", email = "dario.strbenac@sydney.edu.au", role = c("aut", "cre")),
... ...
@@ -36,7 +36,7 @@ Description: The software formalises a framework for classification in R.
36 36
              may be developed by the user, by creating an interface to the framework.
37 37
 License: GPL-3
38 38
 Packaged: 2014-10-18 11:16:55 UTC; dario
39
-RoxygenNote: 7.2.1
39
+RoxygenNote: 7.2.2
40 40
 NeedsCompilation: yes
41 41
 Collate:
42 42
     'ROCplot.R'
... ...
@@ -88,7 +88,7 @@ setClassUnion("tabularOrList", c("tabular", "list"))
88 88
 #' @examples
89 89
 #' 
90 90
 #'   CrossValParams() # Default is 100 permutations and 5 folds of each.
91
-#'   snow <- SnowParam(workers = 4, RNGseed = 999)
91
+#'   snow <- SnowParam(workers = 2, RNGseed = 999)
92 92
 #'   CrossValParams("Leave-k-Out", leave = 2, parallelParams = snow)
93 93
 #'   # Fully reproducible Leave-2-out cross-validation on 4 cores,
94 94
 #'   # even if feature selection or classifier use random sampling.
... ...
@@ -22,13 +22,20 @@ randomForestPredictInterface <- function(forest, measurementsTest, ..., returnTy
22 22
   if(verbose == 3)
23 23
     message("Predicting using random forest.")  
24 24
   measurementsTest <- as.data.frame(measurementsTest)
25
-  classPredictions <- predict(forest, measurementsTest)$predictions
26
-  classScores <- predict(forest, measurementsTest, predict.all = TRUE)[[1]]
27
-  classScores <- t(apply(classScores, 1, function(sampleRow) table(factor(classes[sampleRow], levels = classes)) / forest$forest$num.trees))
28
-  rownames(classScores) <- names(classPredictions) <- rownames(measurementsTest)
29
-  switch(returnType, class = classPredictions,
30
-         score = classScores,
31
-         both = data.frame(class = classPredictions, classScores, check.names = FALSE))
25
+  
26
+  predictions <- predict(forest, measurementsTest)
27
+  if(predictions$treetype == "Classification")
28
+  {
29
+    classPredictions <- predictions$predictions
30
+    classScores <- predict(forest, measurementsTest, predict.all = TRUE)[[1]]
31
+    classScores <- t(apply(classScores, 1, function(sampleRow) table(factor(classes[sampleRow], levels = classes)) / forest$forest$num.trees))
32
+    rownames(classScores) <- names(classPredictions) <- rownames(measurementsTest)
33
+    switch(returnType, class = classPredictions,
34
+           score = classScores,
35
+           both = data.frame(class = classPredictions, classScores, check.names = FALSE))
36
+  } else { # It is "Survival".
37
+      rowSums(predictions$survival)
38
+  }
32 39
 }
33 40
 
34 41
 ################################################################################
... ...
@@ -150,6 +150,7 @@ setMethod("performancePlot", "list",
150 150
                     }, results, 1:length(results), SIMPLIFY = FALSE))
151 151
   
152 152
   plotData <- plotData[, !duplicated(colnames(plotData))]
153
+  if(length(orderingList) > 0) plotData <- .addUserLevels(plotData, orderingList)
153 154
 
154 155
   # Fill in any missing variables needed for ggplot2 code.
155 156
   if("fillColour" %in% names(characteristicsList))
... ...
@@ -62,7 +62,7 @@ Collects and checks necessary parameters required for cross-validation by
62 62
 \examples{
63 63
 
64 64
   CrossValParams() # Default is 100 permutations and 5 folds of each.
65
-  snow <- SnowParam(workers = 4, RNGseed = 999)
65
+  snow <- SnowParam(workers = 2, RNGseed = 999)
66 66
   CrossValParams("Leave-k-Out", leave = 2, parallelParams = snow)
67 67
   # Fully reproducible Leave-2-out cross-validation on 4 cores,
68 68
   # even if feature selection or classifier use random sampling.
69 69
new file mode 100644
... ...
@@ -0,0 +1 @@
1
+CXX_STD = CXX11
0 2
\ No newline at end of file
... ...
@@ -9,19 +9,19 @@
9 9
 extern"C" {
10 10
     void coxmat(double *regmat, int *ncolmat, int *nrowmat, 
11 11
                 double *reg,double *zscores,double *coefs,
12
-                Sint   *maxiter,   Sint   *nusedx,    Sint   *nvarx, 
13
-                double *time,      Sint   *status, 
14
-                double *offset,	double *weights,   Sint   *strata,
12
+                int   *maxiter,   int   *nusedx,    int   *nvarx, 
13
+                double *time,      int   *status, 
14
+                double *offset,	double *weights,   int   *strata,
15 15
                 double *means,     double *beta,      double *u, 
16
-                double *imat2,     double loglik[2],  Sint   *flag, 
16
+                double *imat2,     double loglik[2],  int   *flag, 
17 17
                 double *work,	double *eps,       double *tol_chol,
18 18
                 double *sctest,double *sctest2,double *sctest3){
19 19
         // int reg[*nrowmat];
20 20
         int i=0;
21 21
         int j=0;
22 22
         double sclback=*sctest;
23
-        Sint maxlback=*maxiter;
24
-        //Sint stratalback;
23
+        int maxlback=*maxiter;
24
+        //int stratalback;
25 25
         double betalback=1;
26 26
         
27 27
         for(j=0;j< *ncolmat;j++){
... ...
@@ -142,11 +142,11 @@ extern"C" {
142 142
  */
143 143
 
144 144
 
145
-void coxfit2(Sint   *maxiter,   Sint   *nusedx,    Sint   *nvarx, 
146
-             double *time,      Sint   *status,    double *covar2, 
147
-             double *offset,	double *weights,   Sint   *strata,
145
+void coxfit2(int   *maxiter,   int   *nusedx,    int   *nvarx, 
146
+             double *time,      int   *status,    double *covar2, 
147
+             double *offset,	double *weights,   int   *strata,
148 148
              double *means,     double *beta,      double *u, 
149
-             double *imat2,     double loglik[2],  Sint   *flag, 
149
+             double *imat2,     double loglik[2],  int   *flag, 
150 150
              double *work,	double *eps,       double *tol_chol,
151 151
              double *sctest, double *sctest2,double *sctest3){
152 152
     int i,j,k, person;
... ...
@@ -5,64 +5,64 @@
5 5
  **  Including this in each routine helps prevent mismatched argument errors
6 6
  */
7 7
 
8
-void agexact(Sint *maxiter,  Sint *nusedx,   Sint *nvarx,   double *start, 
9
-             double *stop,   Sint *event,    double *covar2,double *offset, 
10
-             Sint   *strata, double *means,  double *beta,  double *u, 
11
-             double *imat2,  double loglik[2], Sint *flag,  double *work, 
12
-             Sint   *work2,  double *eps,    double *tol_chol, double *sctest);
13
-             
14
-             void agfit2( Sint   *maxiter,  Sint   *nusedx,  Sint   *nvarx, 
15
-                          double *start,    double *stop,    Sint   *event, 
8
+void agexact(int *maxiter,  int *nusedx,   int *nvarx,   double *start, 
9
+             double *stop,   int *event,    double *covar2,double *offset, 
10
+             int   *strata, double *means,  double *beta,  double *u, 
11
+             double *imat2,  double loglik[2], int *flag,  double *work, 
12
+             int   *work2,  double *eps,    double *tol_chol, double *sctest);
13
+             
14
+             void agfit2( int   *maxiter,  int   *nusedx,  int   *nvarx, 
15
+                          double *start,    double *stop,    int   *event, 
16 16
                           double *covar2,   double *offset,  double *weights,
17
-                          Sint   *strata,   double *means,   double *beta, 
17
+                          int   *strata,   double *means,   double *beta, 
18 18
                           double *u,        double *imat2,   double loglik[2], 
19
-                                                                          Sint   *flag,     double *work,    Sint   *end,
19
+                                                                          int   *flag,     double *work,    int   *end,
20 20
                                                                           double *eps,      double *tol_chol,double *sctest);
21 21
              
22
-             void agfit4_a(Sint *nusedx, Sint *nvarx, double *yy, 
22
+             void agfit4_a(int *nusedx, int *nvarx, double *yy, 
23 23
                            double *covar2, double *offset2,
24
-                           double *weights2, Sint *strata2,
24
+                           double *weights2, int *strata2,
25 25
                            double *means, double *beta, double *u, 
26 26
                            double *loglik, 
27
-                           Sint *methodx, Sint *ptype2, Sint *pdiag2,
28
-                           Sint *nfrail,  Sint *frail2);
27
+                           int *methodx, int *ptype2, int *pdiag2,
28
+                           int *nfrail,  int *frail2);
29 29
              
30
-             void agfit4_b(Sint *maxiter, Sint *nusedx, Sint *nvarx, 
30
+             void agfit4_b(int *maxiter, int *nusedx, int *nvarx, 
31 31
                            double *beta, double *u,
32 32
                            double *imat2,  double *jmat2, double *loglik, 
33
-                           Sint *flag,  double *eps, double *tolerch, Sint *methodx, 
34
-                           Sint *nfrail, double *fbeta, double *fdiag);
33
+                           int *flag,  double *eps, double *tolerch, int *methodx, 
34
+                           int *nfrail, double *fbeta, double *fdiag);
35 35
              
36
-             void agfit_null(Sint   *n,      Sint   *method,   double *start, double *stop, 
37
-                             Sint   *event,  double * offset,  double *weights,
38
-                             Sint   *strata, double loglik[2]);
36
+             void agfit_null(int   *n,      int   *method,   double *start, double *stop, 
37
+                             int   *event,  double * offset,  double *weights,
38
+                             int   *strata, double loglik[2]);
39 39
              
40
-             void aghaz2(Sint   *n,     double *start,   double *stop,   Sint   *event, 
41
-                         double *score, Sint   * strata, double *hazard, double * cumhaz);
40
+             void aghaz2(int   *n,     double *start,   double *stop,   int   *event, 
41
+                         double *score, int   * strata, double *hazard, double * cumhaz);
42 42
              
43
-             void agmart(Sint   *n,     Sint   *method,  double *start,   double *stop, 
44
-                         Sint   *event, double *score,   double *wt,      Sint   *strata, 
43
+             void agmart(int   *n,     int   *method,  double *start,   double *stop, 
44
+                         int   *event, double *score,   double *wt,      int   *strata, 
45 45
                          double *resid);
46 46
              
47
-             void agres12(Sint   *nx,     Sint   *nvarx,   double *y,    double *covar2, 
48
-                          Sint   *strata, double *score,   Sint *method, double *resid2, 
47
+             void agres12(int   *nx,     int   *nvarx,   double *y,    double *covar2, 
48
+                          int   *strata, double *score,   int *method, double *resid2, 
49 49
                           double *a);
50 50
              
51
-             void agscore(Sint   *nx,       Sint   *nvarx,      double *y,
52
-                          double *covar2,   Sint   *strata,     double *score,
53
-                          double *weights,  Sint   *method,     double *resid2, double *a);
51
+             void agscore(int   *nx,       int   *nvarx,      double *y,
52
+                          double *covar2,   int   *strata,     double *score,
53
+                          double *weights,  int   *method,     double *resid2, double *a);
54 54
              
55
-             void agsurv1(Sint   *sn,     Sint   *snvar,  double *y,      double *score, 
56
-                          Sint   *strata, 
57
-                          double *surv,   double *varh,   Sint   *snsurv,
55
+             void agsurv1(int   *sn,     int   *snvar,  double *y,      double *score, 
56
+                          int   *strata, 
57
+                          double *surv,   double *varh,   int   *snsurv,
58 58
                           double *xmat,   double *d,      double *varcov, double *yy,
59
-                          Sint   *shisn,  double *hisy,   double *hisxmat,double *hisrisk, 
60
-                          Sint   *hisstrat);
59
+                          int   *shisn,  double *hisy,   double *hisxmat,double *hisrisk, 
60
+                          int   *hisstrat);
61 61
              
62
-             void agsurv2(Sint   *sn,      Sint   *snvar,    double *y, 
63
-                          double *score,   Sint   *strata,   double *wt,    double *surv, 
62
+             void agsurv2(int   *sn,      int   *snvar,    double *y, 
63
+                          double *score,   int   *strata,   double *wt,    double *surv, 
64 64
                           double *varh,    double *xmat,     double *varcov, 
65
-                          Sint   *snsurv,  double *d,        Sint   *sncurve,
65
+                          int   *snsurv,  double *d,        int   *sncurve,
66 66
                           double *newx,    double *newrisk);
67 67
              
68 68
              void chinv2  (double **matrix, int n);
... ...
@@ -72,57 +72,57 @@ void agexact(Sint *maxiter,  Sint *nusedx,   Sint *nvarx,   double *start,
72 72
              int cholesky3(double **matrix, int n, int m, double *diag, double toler);
73 73
              void chsolve3(double **matrix, int n, int m, double *diag, double *y);
74 74
              
75
-             void coxdetail(Sint   *nusedx,   Sint   *nvarx,    Sint   *ndeadx, 
76
-                            double *y,        double *covar2,   Sint   *strata,  
75
+             void coxdetail(int   *nusedx,   int   *nvarx,    int   *ndeadx, 
76
+                            double *y,        double *covar2,   int   *strata,  
77 77
                             double *score,    double *weights,  double *means2, 
78
-                            double *u2,       double *var,      Sint   *rmat,
78
+                            double *u2,       double *var,      int   *rmat,
79 79
                             double *nrisk2,   double *work);
80 80
              
81
-             void coxfit2(Sint   *maxiter,   Sint   *nusedx,    Sint   *nvarx, 
82
-                          double *time,      Sint   *status,    double *covar2, 
83
-                          double *offset,	double *weights,   Sint   *strata,
81
+             void coxfit2(int   *maxiter,   int   *nusedx,    int   *nvarx, 
82
+                          double *time,      int   *status,    double *covar2, 
83
+                          double *offset,	double *weights,   int   *strata,
84 84
                           double *means,     double *beta,      double *u, 
85
-                          double *imat2,     double loglik[2],  Sint   *flag, 
85
+                          double *imat2,     double loglik[2],  int   *flag, 
86 86
                           double *work,	double *eps,       double *tol_chol,
87 87
                           double *sctest,double *sctest2, double *sctest3);
88 88
              
89
-             void coxfit4_a(Sint *nusedx, Sint *nvarx, double *yy, 
89
+             void coxfit4_a(int *nusedx, int *nvarx, double *yy, 
90 90
                             double *covar2, double *offset2,
91
-                            double *weights2, Sint *strata2,
91
+                            double *weights2, int *strata2,
92 92
                             double *means, double *beta, double *u, 
93 93
                             double *loglik, 
94
-                            Sint *methodx, Sint *ptype2, Sint *pdiag2,
95
-                            Sint *nfrail,  Sint *frail2);
94
+                            int *methodx, int *ptype2, int *pdiag2,
95
+                            int *nfrail,  int *frail2);
96 96
              
97
-             void coxfit4_b(Sint *maxiter, Sint *nusedx, Sint *nvarx, 
97
+             void coxfit4_b(int *maxiter, int *nusedx, int *nvarx, 
98 98
                             double *beta, double *u,
99 99
                             double *imat2,  double *jmat2, double *loglik, 
100
-                            Sint *flag,  double *eps, double *tolerch, Sint *methodx, 
101
-                            Sint *nfrail, double *fbeta, double *fdiag);
100
+                            int *flag,  double *eps, double *tolerch, int *methodx, 
101
+                            int *nfrail, double *fbeta, double *fdiag);
102 102
              
103
-             void coxfit4_c (Sint *nusedx, Sint *nvar, Sint *methodx, double *expect);
103
+             void coxfit4_c (int *nusedx, int *nvar, int *methodx, double *expect);
104 104
              
105
-             void coxfit_null(Sint   *nusedx,    Sint   *method,   double *time, 
106
-                              Sint   *status,    double *score,    double *weights, 
107
-                              Sint   *strata,    double *loglik, double *resid);
105
+             void coxfit_null(int   *nusedx,    int   *method,   double *time, 
106
+                              int   *status,    double *score,    double *weights, 
107
+                              int   *strata,    double *loglik, double *resid);
108 108
              
109
-             void coxhaz2(Sint   *n,      double *score,   Sint   *mark, 
110
-                          Sint   *strata, double *hazard,  double *cumhaz);
109
+             void coxhaz2(int   *n,      double *score,   int   *mark, 
110
+                          int   *strata, double *hazard,  double *cumhaz);
111 111
              
112
-             void coxmart(Sint   *sn,     Sint   *method,    double *time, 
113
-                          Sint   *status, Sint   * strata,   double *score, 
112
+             void coxmart(int   *sn,     int   *method,    double *time, 
113
+                          int   *status, int   * strata,   double *score, 
114 114
                           double *wt,     double *expect);
115 115
              
116
-             void coxph_wtest(Sint *nvar2, Sint *ntest, double *var, double *b,
116
+             void coxph_wtest(int *nvar2, int *ntest, double *var, double *b,
117 117
                               double *scratch, double *tolerch);
118 118
              
119
-             void coxscho(Sint   *nusedx,    Sint   *nvarx,    double *y, 
120
-                          double *covar2,    double *score,    Sint   *strata,  
121
-                          Sint   *method2,   double *work);
119
+             void coxscho(int   *nusedx,    int   *nvarx,    double *y, 
120
+                          double *covar2,    double *score,    int   *strata,  
121
+                          int   *method2,   double *work);
122 122
              
123
-             void coxscore(Sint   *nx,      Sint   *nvarx,    double *y, 
124
-                           double *covar2,  Sint   *strata,   double *score, 
125
-                           double *weights, Sint   *method,   double *resid2,
123
+             void coxscore(int   *nx,      int   *nvarx,    double *y, 
124
+                           double *covar2,  int   *strata,   double *score, 
125
+                           double *weights, int   *method,   double *resid2,
126 126
                            double *scratch);
127 127
              
128 128
              double coxsafe(double x);
... ...
@@ -131,40 +131,40 @@ void agexact(Sint *maxiter,  Sint *nusedx,   Sint *nvarx,   double *start,
131 131
              void init_doloop(int min, int max);
132 132
              int doloop      (int nloops, int *index);
133 133
              
134
-             void pyears1(Sint   *sn,      Sint   *sny,      Sint   *sdoevent, 
134
+             void pyears1(int   *sn,      int   *sny,      int   *sdoevent, 
135 135
                           double *sy,      double *wt,       
136
-                          Sint   *sedim,   Sint   *efac, 
137
-                          Sint   *edims,   double *secut,    double *expect, 
138
-                          double *sedata,  Sint   *sodim,    Sint   *ofac, 
139
-                          Sint   *odims,   double *socut,    Sint   *smethod, 
136
+                          int   *sedim,   int   *efac, 
137
+                          int   *edims,   double *secut,    double *expect, 
138
+                          double *sedata,  int   *sodim,    int   *ofac, 
139
+                          int   *odims,   double *socut,    int   *smethod, 
140 140
                           double *sodata,  double *pyears,   double *pn, 
141 141
                           double *pcount,  double *pexpect,  double *offtable);
142 142
              
143
-             void pyears2(Sint   *sn,      Sint   *sny,   Sint   *sdoevent, 
143
+             void pyears2(int   *sn,      int   *sny,   int   *sdoevent, 
144 144
                           double *sy,      double *wt,    
145
-                          Sint   *sodim,   Sint   *ofac, 
146
-                          Sint   *odims,   double *socut, double *sodata,
145
+                          int   *sodim,   int   *ofac, 
146
+                          int   *odims,   double *socut, double *sodata,
147 147
                           double *pyears,  double *pn,    double *pcount, 
148 148
                           double *offtable);
149 149
              
150 150
              double pystep(int nc,        int  *index,  int  *index2,   double *wt, 
151
-                           double *data,  Sint *fac,    Sint *dims,     double **cuts, 
151
+                           double *data,  int *fac,    int *dims,     double **cuts, 
152 152
                            double step,   int  edge);
153 153
              
154
-             void survdiff2(Sint   *nn,     Sint   *nngroup,    Sint   *nstrat, 
155
-                            double *rho,    double *time,       Sint   *status, 
156
-                            Sint   *group,  Sint   *strata,	   double *obs, 
154
+             void survdiff2(int   *nn,     int   *nngroup,    int   *nstrat, 
155
+                            double *rho,    double *time,       int   *status, 
156
+                            int   *group,  int   *strata,	   double *obs, 
157 157
                             double *exp,    double *var,        double *risk, 
158 158
                             double *kaplan);
159 159
              
160
-             void survfit2(Sint   *sn,      double *y,       double *wt,
161
-                           Sint   *strata,  Sint   *method,  Sint   *error, 
160
+             void survfit2(int   *sn,      double *y,       double *wt,
161
+                           int   *strata,  int   *method,  int   *error, 
162 162
                            double *mark,    double *surv,	double *varh,
163 163
                            double *risksum);
164 164
              
165
-             void survfit3(Sint   *sn,        double *y,               double *wt,
166
-                           Sint   *strata,    Sint   *method,          Sint   *error, 
167
-                           Sint   *nstrat,    double *ntimes_strata,  
165
+             void survfit3(int   *sn,        double *y,               double *wt,
166
+                           int   *strata,    int   *method,          int   *error, 
167
+                           int   *nstrat,    double *ntimes_strata,  
168 168
                            double *timelist,  double *weighted_event,  double *surv,
169 169
                            double *varh,	 double *risksum,         double *enter,
170 170
                            double *exit_censored);
... ...
@@ -177,18 +177,18 @@ void agexact(Sint *maxiter,  Sint *nusedx,   Sint *nvarx,   double *start,
177 177
                            SEXP dist,       SEXP expr,   SEXP rho);
178 178
              
179 179
              double survregc1(int n,          int nvar,     int nstrat,      int whichcase,
180
-                              double *beta,   int dist,     Sint *strat,     double *offset,
180
+                              double *beta,   int dist,     int *strat,     double *offset,
181 181
                               double *time1,  double *time2, double *status, double *wt,
182 182
                               double **covar, double **imat, double **JJ,    double *u, 
183 183
                               SEXP expr,      SEXP rho,      double *dummy,  int nf,
184
-                              Sint *frail,    double *fdiag, double *jdiag );
184
+                              int *frail,    double *fdiag, double *jdiag );
185 185
              
186 186
              double survregc2(int n,          int nvar,     int nstrat,      int whichcase,
187
-                              double *beta,   int dist,     Sint *strat,     double *offset,
187
+                              double *beta,   int dist,     int *strat,     double *offset,
188 188
                               double *time1,  double *time2, double *status, double *wt,
189 189
                               double **covar, double **imat, double **JJ,    double *u, 
190 190
                               SEXP expr,      SEXP rho,      double *dummy,  int nf,
191
-                              Sint *frail,    double *fdiag, double *jdiag );
191
+                              int *frail,    double *fdiag, double *jdiag );
192 192
              
193 193
              void survpenal(int whichcase, int nfrail,    int  nvar2,    double **hmat, 
194 194
                             double **JJ,   double *hdiag, double *jdiag,