Predict.c

Go to the documentation of this file.
00001 
00009 #include "party.h"
00010 
00011 
00021 void C_splitnode(SEXP node, SEXP learnsample, SEXP control) {
00022 
00023     SEXP weights, leftnode, rightnode, split;
00024     SEXP responses, inputs, whichNA;
00025     double cutpoint, *dx, *dweights, *leftweights, *rightweights;
00026     double sleft = 0.0, sright = 0.0;
00027     int *ix, *levelset, *iwhichNA;
00028     int nobs, i, nna;
00029                     
00030     weights = S3get_nodeweights(node);
00031     dweights = REAL(weights);
00032     responses = GET_SLOT(learnsample, PL2_responsesSym);
00033     inputs = GET_SLOT(learnsample, PL2_inputsSym);
00034     nobs = get_nobs(learnsample);
00035             
00036     /* set up memory for the left daughter */
00037     SET_VECTOR_ELT(node, S3_LEFT, leftnode = allocVector(VECSXP, NODE_LENGTH));
00038     C_init_node(leftnode, nobs, 
00039         get_ninputs(learnsample), get_maxsurrogate(get_splitctrl(control)),
00040         ncol(get_predict_trafo(GET_SLOT(learnsample, PL2_responsesSym))));
00041     leftweights = REAL(S3get_nodeweights(leftnode));
00042 
00043     /* set up memory for the right daughter */
00044     SET_VECTOR_ELT(node, S3_RIGHT, 
00045                    rightnode = allocVector(VECSXP, NODE_LENGTH));
00046     C_init_node(rightnode, nobs, 
00047         get_ninputs(learnsample), get_maxsurrogate(get_splitctrl(control)),
00048         ncol(get_predict_trafo(GET_SLOT(learnsample, PL2_responsesSym))));
00049     rightweights = REAL(S3get_nodeweights(rightnode));
00050 
00051     /* split according to the primary split */
00052     split = S3get_primarysplit(node);
00053     if (has_missings(inputs, S3get_variableID(split))) {
00054         whichNA = get_missings(inputs, S3get_variableID(split));
00055         iwhichNA = INTEGER(whichNA);
00056         nna = LENGTH(whichNA);
00057     } else {
00058         nna = 0;
00059         whichNA = R_NilValue;
00060         iwhichNA = NULL;
00061     }
00062     
00063     if (S3is_ordered(split)) {
00064         cutpoint = REAL(S3get_splitpoint(split))[0];
00065         dx = REAL(get_variable(inputs, S3get_variableID(split)));
00066         for (i = 0; i < nobs; i++) {
00067             if (nna > 0) {
00068                 if (i_in_set(i + 1, iwhichNA, nna)) continue;
00069             }
00070             if (dx[i] <= cutpoint) 
00071                 leftweights[i] = dweights[i]; 
00072             else 
00073                 leftweights[i] = 0.0;
00074             rightweights[i] = dweights[i] - leftweights[i];
00075             sleft += leftweights[i];
00076             sright += rightweights[i];
00077         }
00078     } else {
00079         levelset = INTEGER(S3get_splitpoint(split));
00080         ix = INTEGER(get_variable(inputs, S3get_variableID(split)));
00081 
00082         for (i = 0; i < nobs; i++) {
00083             if (nna > 0) {
00084                 if (i_in_set(i + 1, iwhichNA, nna)) continue;
00085             }
00086             if (levelset[ix[i] - 1])
00087                 leftweights[i] = dweights[i];
00088             else 
00089                 leftweights[i] = 0.0;
00090             rightweights[i] = dweights[i] - leftweights[i];
00091             sleft += leftweights[i];
00092             sright += rightweights[i];
00093         }
00094     }
00095     
00096     /* for the moment: NA's go with majority */
00097     if (nna > 0) {
00098         for (i = 0; i < nna; i++) {
00099             if (sleft > sright) {
00100                 leftweights[iwhichNA[i] - 1] = dweights[iwhichNA[i] - 1];
00101                 rightweights[iwhichNA[i] - 1] = 0.0;
00102             } else {
00103                 rightweights[iwhichNA[i] - 1] = dweights[iwhichNA[i] - 1];
00104                 leftweights[iwhichNA[i] - 1] = 0.0;
00105             }
00106         }
00107     }
00108 }
00109 
00110 
00120 SEXP C_get_node(SEXP subtree, SEXP newinputs, 
00121                 double mincriterion, int numobs) {
00122 
00123     SEXP split, whichNA, weights, ssplit, surrsplit;
00124     double cutpoint, x, *dweights, swleft, swright;
00125     int level, *levelset, i, ns;
00126 
00127     if (S3get_nodeterminal(subtree) || 
00128         REAL(S3get_maxcriterion(subtree))[0] < mincriterion) 
00129         return(subtree);
00130     
00131     split = S3get_primarysplit(subtree);
00132 
00133     /* missing values. Maybe store the proportions left / 
00134        right in each node? */
00135     if (has_missings(newinputs, S3get_variableID(split))) {
00136         whichNA = get_missings(newinputs, S3get_variableID(split));
00137     
00138         if (C_i_in_set(numobs, whichNA)) {
00139         
00140             surrsplit = S3get_surrogatesplits(subtree);
00141             ns = 0;
00142             i = numobs;      
00143 
00144             /* try to find a surrogate split */
00145             while(TRUE) {
00146     
00147                 if (ns >= LENGTH(surrsplit)) break;
00148             
00149                 ssplit = VECTOR_ELT(surrsplit, ns);
00150                 if (has_missings(newinputs, S3get_variableID(ssplit))) {
00151                     if (INTEGER(get_missings(newinputs, 
00152                                              S3get_variableID(ssplit)))[i]) {
00153                         ns++;
00154                         continue;
00155                     }
00156                 }
00157 
00158                 cutpoint = REAL(S3get_splitpoint(ssplit))[0];
00159                 x = REAL(get_variable(newinputs, S3get_variableID(ssplit)))[i];
00160                      
00161                 if (S3get_toleft(ssplit)) {
00162                     if (x <= cutpoint) {
00163                         return(C_get_node(S3get_leftnode(subtree),
00164                                           newinputs, mincriterion, numobs));
00165                     } else {
00166                         return(C_get_node(S3get_rightnode(subtree),
00167                                newinputs, mincriterion, numobs));
00168                     }
00169                 } else {
00170                     if (x <= cutpoint) {
00171                         return(C_get_node(S3get_rightnode(subtree),
00172                                           newinputs, mincriterion, numobs));
00173                     } else {
00174                         return(C_get_node(S3get_leftnode(subtree),
00175                                newinputs, mincriterion, numobs));
00176                     }
00177                 }
00178                 break;
00179             }
00180 
00181             /* if this was not successful, we go with the majority */
00182             swleft = S3get_sumweights(S3get_leftnode(subtree));
00183             swright = S3get_sumweights(S3get_rightnode(subtree));
00184             if (swleft > swright) {
00185                 return(C_get_node(S3get_leftnode(subtree), 
00186                                   newinputs, mincriterion, numobs));
00187             } else {
00188                 return(C_get_node(S3get_rightnode(subtree), 
00189                                   newinputs, mincriterion, numobs));
00190             }
00191         }
00192     }
00193     
00194     if (S3is_ordered(split)) {
00195         cutpoint = REAL(S3get_splitpoint(split))[0];
00196         x = REAL(get_variable(newinputs, 
00197                      S3get_variableID(split)))[numobs];
00198         if (x <= cutpoint) {
00199             return(C_get_node(S3get_leftnode(subtree), 
00200                               newinputs, mincriterion, numobs));
00201         } else {
00202             return(C_get_node(S3get_rightnode(subtree), 
00203                               newinputs, mincriterion, numobs));
00204         }
00205     } else {
00206         levelset = INTEGER(S3get_splitpoint(split));
00207         level = INTEGER(get_variable(newinputs, 
00208                             S3get_variableID(split)))[numobs];
00209         /* level is in 1, ..., K */
00210         if (levelset[level - 1]) {
00211             return(C_get_node(S3get_leftnode(subtree), newinputs, 
00212                               mincriterion, numobs));
00213         } else {
00214             return(C_get_node(S3get_rightnode(subtree), newinputs, 
00215                               mincriterion, numobs));
00216         }
00217     }
00218 }
00219 
00220 
00229 SEXP R_get_node(SEXP subtree, SEXP newinputs, SEXP mincriterion, 
00230                 SEXP numobs) {
00231     return(C_get_node(subtree, newinputs, REAL(mincriterion)[0],
00232                       INTEGER(numobs)[0] - 1));
00233 }
00234 
00235 
00242 SEXP C_get_nodebynum(SEXP subtree, int nodenum) {
00243     
00244     if (nodenum == S3get_nodeID(subtree)) return(subtree);
00245 
00246     if (S3get_nodeterminal(subtree)) 
00247         error("no node with number %d\n", nodenum);
00248 
00249     if (nodenum < S3get_nodeID(S3get_rightnode(subtree))) {
00250         return(C_get_nodebynum(S3get_leftnode(subtree), nodenum));
00251     } else {
00252         return(C_get_nodebynum(S3get_rightnode(subtree), nodenum));
00253     }
00254 }
00255 
00256 
00263 SEXP R_get_nodebynum(SEXP subtree, SEXP nodenum) {
00264     return(C_get_nodebynum(subtree, INTEGER(nodenum)[0]));
00265 }
00266 
00267 
00276 SEXP C_get_prediction(SEXP subtree, SEXP newinputs, 
00277                       double mincriterion, int numobs) {
00278     return(S3get_prediction(C_get_node(subtree, newinputs, 
00279                             mincriterion, numobs)));
00280 }
00281 
00282 
00291 SEXP C_get_nodeweights(SEXP subtree, SEXP newinputs, 
00292                        double mincriterion, int numobs) {
00293     return(S3get_nodeweights(C_get_node(subtree, newinputs, 
00294                              mincriterion, numobs)));
00295 }
00296 
00297 
00306 int C_get_nodeID(SEXP subtree, SEXP newinputs,
00307                   double mincriterion, int numobs) {
00308      return(S3get_nodeID(C_get_node(subtree, newinputs, 
00309             mincriterion, numobs)));
00310 }
00311 
00312 
00320 SEXP R_get_nodeID(SEXP tree, SEXP newinputs, SEXP mincriterion) {
00321 
00322     SEXP ans;
00323     int nobs, i, *dans;
00324             
00325     nobs = get_nobs(newinputs);
00326     PROTECT(ans = allocVector(INTSXP, nobs));
00327     dans = INTEGER(ans);
00328     for (i = 0; i < nobs; i++)
00329          dans[i] = C_get_nodeID(tree, newinputs, REAL(mincriterion)[0], i);
00330     UNPROTECT(1);
00331     return(ans);
00332 }
00333 
00334 
00343 void C_predict(SEXP tree, SEXP newinputs, double mincriterion, SEXP ans) {
00344     
00345     int nobs, i;
00346     
00347     nobs = get_nobs(newinputs);    
00348     if (LENGTH(ans) != nobs) 
00349         error("ans is not of length %d\n", nobs);
00350         
00351     for (i = 0; i < nobs; i++)
00352         SET_VECTOR_ELT(ans, i, C_get_prediction(tree, newinputs, 
00353                        mincriterion, i));
00354 }
00355 
00356 
00364 SEXP R_predict(SEXP tree, SEXP newinputs, SEXP mincriterion) {
00365 
00366     SEXP ans;
00367     int nobs;
00368     
00369     nobs = get_nobs(newinputs);
00370     PROTECT(ans = allocVector(VECSXP, nobs));
00371     C_predict(tree, newinputs, REAL(mincriterion)[0], ans);
00372     UNPROTECT(1);
00373     return(ans);
00374 }
00375 
00376 
00384 void C_getpredictions(SEXP tree, SEXP where, SEXP ans) {
00385 
00386     int nobs, i, *iwhere;
00387     
00388     nobs = LENGTH(where);
00389     iwhere = INTEGER(where);
00390     if (LENGTH(ans) != nobs)
00391         error("ans is not of length %d\n", nobs);
00392         
00393     for (i = 0; i < nobs; i++)
00394         SET_VECTOR_ELT(ans, i, S3get_prediction(
00395             C_get_nodebynum(tree, iwhere[i])));
00396 }
00397 
00398 
00405 SEXP R_getpredictions(SEXP tree, SEXP where) {
00406 
00407     SEXP ans;
00408     int nobs;
00409             
00410     nobs = LENGTH(where);
00411     PROTECT(ans = allocVector(VECSXP, nobs));
00412     C_getpredictions(tree, where, ans);
00413     UNPROTECT(1);
00414     return(ans);
00415 }                        
00416 
00427 SEXP R_predictRF_weights(SEXP forest, SEXP where, SEXP weights, 
00428                          SEXP newinputs, SEXP mincriterion, SEXP oobpred) {
00429 
00430     SEXP ans, tree, bw;
00431     int ntrees, nobs, i, b, j, q, iwhere, oob = 0, count = 0, ntrain;
00432     
00433     if (LOGICAL(oobpred)[0]) oob = 1;
00434     
00435     nobs = get_nobs(newinputs);
00436     ntrees = LENGTH(forest);
00437     q = LENGTH(S3get_prediction(
00438                    C_get_nodebynum(VECTOR_ELT(forest, 0), 1)));
00439 
00440     if (oob) {
00441         if (LENGTH(VECTOR_ELT(weights, 0)) != nobs)
00442             error("number of observations don't match");
00443     }    
00444     
00445     tree = VECTOR_ELT(forest, 0);
00446     ntrain = LENGTH(VECTOR_ELT(weights, 0));
00447     
00448     PROTECT(ans = allocVector(VECSXP, nobs));
00449     
00450     for (i = 0; i < nobs; i++) {
00451         count = 0;
00452         SET_VECTOR_ELT(ans, i, bw = allocVector(REALSXP, ntrain));
00453         for (j = 0; j < ntrain; j++)
00454             REAL(bw)[j] = 0.0;
00455         for (b = 0; b < ntrees; b++) {
00456             tree = VECTOR_ELT(forest, b);
00457 
00458             if (oob && 
00459                 REAL(VECTOR_ELT(weights, b))[i] > 0.0) 
00460                 continue;
00461 
00462             iwhere = C_get_nodeID(tree, newinputs, REAL(mincriterion)[0], i);
00463             
00464             for (j = 0; j < ntrain; j++) {
00465                 if (iwhere == INTEGER(VECTOR_ELT(where, b))[j])
00466                     REAL(bw)[j] += REAL(VECTOR_ELT(weights, b))[j];
00467             }
00468             count++;
00469         }
00470         if (count == 0) 
00471             error("cannot compute out-of-bag predictions for obs ", i + 1);
00472     }
00473     UNPROTECT(1);
00474     return(ans);
00475 }
00476 
00477 
00483 SEXP R_proximity(SEXP where) {
00484 
00485     SEXP ans, bw, bin;
00486     int ntrees, nobs, i, b, j, iwhere;
00487     
00488     ntrees = LENGTH(where);
00489     nobs = LENGTH(VECTOR_ELT(where, 0));
00490     
00491     PROTECT(ans = allocVector(VECSXP, nobs));
00492     PROTECT(bin = allocVector(INTSXP, nobs));
00493      
00494     for (i = 0; i < nobs; i++) {
00495         SET_VECTOR_ELT(ans, i, bw = allocVector(REALSXP, nobs));
00496         for (j = 0; j < nobs; j++) {
00497             REAL(bw)[j] = 0.0;
00498             INTEGER(bin)[j] = 0;
00499         }
00500         for (b = 0; b < ntrees; b++) {
00501             /* don't look at out-of-bag observations */
00502             if (INTEGER(VECTOR_ELT(where, b))[i] == 0)
00503                 continue;
00504             iwhere = INTEGER(VECTOR_ELT(where, b))[i];
00505             for (j = 0; j < nobs; j++) {
00506                 if (iwhere == INTEGER(VECTOR_ELT(where, b))[j])
00507                     /* only count the number of trees; no weights */
00508                     REAL(bw)[j]++;
00509                 if (INTEGER(VECTOR_ELT(where, b))[j] > 0)
00510                     /* count the number of bootstrap samples
00511                     containing both i and j */
00512                     INTEGER(bin)[j]++;
00513             }
00514         }
00515         for (j = 0; j < nobs; j++)
00516             REAL(bw)[j] = REAL(bw)[j] / INTEGER(bin)[j];
00517     }
00518     UNPROTECT(2);
00519     return(ans);
00520 }

Generated on Thu Feb 28 15:14:51 2008 for party by  doxygen 1.5.3