|
a |
|
b/partyMod/src/S3Classes.c |
|
|
1 |
|
|
|
2 |
/** |
|
|
3 |
S3 classes for dealing with nodes and splits |
|
|
4 |
*\file S3Classes.c |
|
|
5 |
*\author $Author$ |
|
|
6 |
*\date $Date$ |
|
|
7 |
*/ |
|
|
8 |
|
|
|
9 |
#include "party.h" |
|
|
10 |
|
|
|
11 |
void C_init_node(SEXP node, int nobs, int ninputs, int nsurr, int q) { |
|
|
12 |
|
|
|
13 |
SEXP nodeID, weights, criterion, primarysplit, surrogatesplits, |
|
|
14 |
terminal, prediction; |
|
|
15 |
|
|
|
16 |
if (LENGTH(node) < NODE_LENGTH) |
|
|
17 |
error("node is not a list with at least %d elements", NODE_LENGTH); |
|
|
18 |
|
|
|
19 |
SET_VECTOR_ELT(node, S3_NODEID, nodeID = allocVector(INTSXP, 1)); |
|
|
20 |
if (nobs > 0) |
|
|
21 |
SET_VECTOR_ELT(node, S3_WEIGHTS, weights = allocVector(REALSXP, nobs)); |
|
|
22 |
else |
|
|
23 |
SET_VECTOR_ELT(node, S3_WEIGHTS, R_NilValue); |
|
|
24 |
SET_VECTOR_ELT(node, S3_SUMWEIGHTS, allocVector(REALSXP, 1)); |
|
|
25 |
SET_VECTOR_ELT(node, S3_CRITERION, |
|
|
26 |
criterion = allocVector(VECSXP, CRITERION_LENGTH)); |
|
|
27 |
/* teststats */ |
|
|
28 |
SET_VECTOR_ELT(criterion, S3_STATISTICS, allocVector(REALSXP, ninputs)); |
|
|
29 |
/* criterion, aka pvalues */ |
|
|
30 |
SET_VECTOR_ELT(criterion, S3_iCRITERION, allocVector(REALSXP, ninputs)); |
|
|
31 |
/* max(criterion) */ |
|
|
32 |
SET_VECTOR_ELT(criterion, S3_MAXCRITERION, allocVector(REALSXP, 1)); |
|
|
33 |
SET_VECTOR_ELT(node, S3_TERMINAL, terminal = allocVector(LGLSXP, 1)); |
|
|
34 |
INTEGER(terminal)[0] = 0; |
|
|
35 |
SET_VECTOR_ELT(node, S3_PSPLIT, |
|
|
36 |
primarysplit = allocVector(VECSXP, SPLIT_LENGTH)); |
|
|
37 |
SET_VECTOR_ELT(node, S3_SSPLIT, |
|
|
38 |
surrogatesplits = allocVector(VECSXP, nsurr)); |
|
|
39 |
SET_VECTOR_ELT(node, S3_PREDICTION, prediction = allocVector(REALSXP, q)); |
|
|
40 |
|
|
|
41 |
} |
|
|
42 |
|
|
|
43 |
void S3set_nodeID(SEXP node, int nodeID) { |
|
|
44 |
INTEGER(VECTOR_ELT(node, S3_NODEID))[0] = nodeID; |
|
|
45 |
} |
|
|
46 |
|
|
|
47 |
int S3get_nodeID(SEXP node) { |
|
|
48 |
return(INTEGER(VECTOR_ELT(node, S3_NODEID))[0]); |
|
|
49 |
} |
|
|
50 |
|
|
|
51 |
SEXP S3get_nodeweights(SEXP node) { |
|
|
52 |
SEXP ans; |
|
|
53 |
|
|
|
54 |
ans = VECTOR_ELT(node, S3_WEIGHTS); |
|
|
55 |
if (ans == R_NilValue) |
|
|
56 |
error("node has no weights element"); |
|
|
57 |
return(VECTOR_ELT(node, S3_WEIGHTS)); |
|
|
58 |
} |
|
|
59 |
|
|
|
60 |
double S3get_sumweights(SEXP node) { |
|
|
61 |
return(REAL(VECTOR_ELT(node, S3_SUMWEIGHTS))[0]); |
|
|
62 |
} |
|
|
63 |
|
|
|
64 |
SEXP S3get_teststat(SEXP node) { |
|
|
65 |
return(VECTOR_ELT(VECTOR_ELT(node, S3_CRITERION), S3_STATISTICS)); |
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
SEXP S3get_criterion(SEXP node) { |
|
|
69 |
return(VECTOR_ELT(VECTOR_ELT(node, S3_CRITERION), S3_iCRITERION)); |
|
|
70 |
} |
|
|
71 |
|
|
|
72 |
SEXP S3get_maxcriterion(SEXP node) { |
|
|
73 |
return(VECTOR_ELT(VECTOR_ELT(node, S3_CRITERION), S3_MAXCRITERION)); |
|
|
74 |
} |
|
|
75 |
|
|
|
76 |
void S3set_nodeterminal(SEXP node) { |
|
|
77 |
INTEGER(VECTOR_ELT(node, S3_TERMINAL))[0] = 1; |
|
|
78 |
} |
|
|
79 |
|
|
|
80 |
int S3get_nodeterminal(SEXP node) { |
|
|
81 |
return(INTEGER(VECTOR_ELT(node, S3_TERMINAL))[0]); |
|
|
82 |
} |
|
|
83 |
|
|
|
84 |
SEXP S3get_primarysplit(SEXP node) { |
|
|
85 |
return(VECTOR_ELT(node, S3_PSPLIT)); |
|
|
86 |
} |
|
|
87 |
|
|
|
88 |
SEXP S3get_surrogatesplits(SEXP node) { |
|
|
89 |
return(VECTOR_ELT(node, S3_SSPLIT)); |
|
|
90 |
} |
|
|
91 |
|
|
|
92 |
SEXP S3get_prediction(SEXP node) { |
|
|
93 |
return(VECTOR_ELT(node, S3_PREDICTION)); |
|
|
94 |
} |
|
|
95 |
|
|
|
96 |
SEXP S3get_leftnode(SEXP node) { |
|
|
97 |
return(VECTOR_ELT(node, S3_LEFT)); |
|
|
98 |
} |
|
|
99 |
|
|
|
100 |
SEXP S3get_rightnode(SEXP node) { |
|
|
101 |
return(VECTOR_ELT(node, S3_RIGHT)); |
|
|
102 |
} |
|
|
103 |
|
|
|
104 |
void C_init_orderedsplit(SEXP split, int nobs) { |
|
|
105 |
|
|
|
106 |
SEXP variableID, splitpoint, splitstatistics, ordered, toleft; |
|
|
107 |
|
|
|
108 |
if (LENGTH(split) < SPLIT_LENGTH) |
|
|
109 |
error("split is not a list with at least %d elements", SPLIT_LENGTH); |
|
|
110 |
|
|
|
111 |
SET_VECTOR_ELT(split, S3_VARIABLEID, |
|
|
112 |
variableID = allocVector(INTSXP, 1)); |
|
|
113 |
SET_VECTOR_ELT(split, S3_ORDERED, |
|
|
114 |
ordered = allocVector(LGLSXP, 1)); |
|
|
115 |
INTEGER(ordered)[0] = 1; |
|
|
116 |
SET_VECTOR_ELT(split, S3_SPLITPOINT, |
|
|
117 |
splitpoint = allocVector(REALSXP, 1)); |
|
|
118 |
if (nobs > 0) |
|
|
119 |
SET_VECTOR_ELT(split, S3_SPLITSTATISTICS, |
|
|
120 |
splitstatistics = allocVector(REALSXP, nobs)); |
|
|
121 |
else |
|
|
122 |
SET_VECTOR_ELT(split, S3_SPLITSTATISTICS, R_NilValue); |
|
|
123 |
SET_VECTOR_ELT(split, S3_TOLEFT, toleft = allocVector(INTSXP, 1)); |
|
|
124 |
INTEGER(toleft)[0] = 1; |
|
|
125 |
SET_VECTOR_ELT(split, S3_TABLE, R_NilValue); |
|
|
126 |
} |
|
|
127 |
|
|
|
128 |
void C_init_nominalsplit(SEXP split, int nlevels, int nobs) { |
|
|
129 |
|
|
|
130 |
SEXP variableID, splitpoint, splitstatistics, ordered, toleft, table; |
|
|
131 |
|
|
|
132 |
if (LENGTH(split) < SPLIT_LENGTH) |
|
|
133 |
error("split is not a list with at least %d elements", SPLIT_LENGTH); |
|
|
134 |
|
|
|
135 |
SET_VECTOR_ELT(split, S3_VARIABLEID, variableID = allocVector(INTSXP, 1)); |
|
|
136 |
SET_VECTOR_ELT(split, S3_ORDERED, ordered = allocVector(LGLSXP, 1)); |
|
|
137 |
INTEGER(ordered)[0] = 0; |
|
|
138 |
SET_VECTOR_ELT(split, S3_SPLITPOINT, |
|
|
139 |
splitpoint = allocVector(INTSXP, nlevels)); |
|
|
140 |
if (nobs > 0) |
|
|
141 |
SET_VECTOR_ELT(split, S3_SPLITSTATISTICS, |
|
|
142 |
splitstatistics = allocVector(REALSXP, nobs)); |
|
|
143 |
else |
|
|
144 |
SET_VECTOR_ELT(split, S3_SPLITSTATISTICS, R_NilValue); |
|
|
145 |
SET_VECTOR_ELT(split, S3_TOLEFT, toleft = allocVector(INTSXP, 1)); |
|
|
146 |
INTEGER(toleft)[0] = 1; |
|
|
147 |
SET_VECTOR_ELT(split, S3_TABLE, table = allocVector(INTSXP, nlevels)); |
|
|
148 |
} |
|
|
149 |
|
|
|
150 |
void S3set_variableID(SEXP split, int variableID) { |
|
|
151 |
INTEGER(VECTOR_ELT(split, S3_VARIABLEID))[0] = variableID; |
|
|
152 |
} |
|
|
153 |
|
|
|
154 |
int S3get_variableID(SEXP split) { |
|
|
155 |
return(INTEGER(VECTOR_ELT(split, S3_VARIABLEID))[0]); |
|
|
156 |
} |
|
|
157 |
|
|
|
158 |
int S3is_ordered(SEXP split) { |
|
|
159 |
return(INTEGER(VECTOR_ELT(split, S3_ORDERED))[0]); |
|
|
160 |
} |
|
|
161 |
|
|
|
162 |
void S3set_ordered(SEXP split) { |
|
|
163 |
INTEGER(VECTOR_ELT(split, S3_ORDERED))[0] = 1; |
|
|
164 |
} |
|
|
165 |
|
|
|
166 |
void S3set_nominal(SEXP split) { |
|
|
167 |
INTEGER(VECTOR_ELT(split, S3_ORDERED))[0] = 0; |
|
|
168 |
} |
|
|
169 |
|
|
|
170 |
int S3get_toleft(SEXP split) { |
|
|
171 |
return(INTEGER(VECTOR_ELT(split, S3_TOLEFT))[0]); |
|
|
172 |
} |
|
|
173 |
|
|
|
174 |
void S3set_toleft(SEXP split, int left) { |
|
|
175 |
/* <FIXME> use LOGICAL here? </FIXME> */ |
|
|
176 |
INTEGER(VECTOR_ELT(split, S3_TOLEFT))[0] = left; |
|
|
177 |
} |
|
|
178 |
|
|
|
179 |
SEXP S3get_splitpoint(SEXP split) { |
|
|
180 |
return(VECTOR_ELT(split, S3_SPLITPOINT)); |
|
|
181 |
} |
|
|
182 |
|
|
|
183 |
SEXP S3get_splitstatistics(SEXP split) { |
|
|
184 |
SEXP ans; |
|
|
185 |
|
|
|
186 |
ans = VECTOR_ELT(split, S3_SPLITSTATISTICS); |
|
|
187 |
if (ans == R_NilValue) |
|
|
188 |
error("split does not have a splitstatistics element"); |
|
|
189 |
return(ans); |
|
|
190 |
} |
|
|
191 |
|
|
|
192 |
SEXP S3get_table(SEXP split) { |
|
|
193 |
SEXP ans; |
|
|
194 |
|
|
|
195 |
ans = VECTOR_ELT(split, S3_TABLE); |
|
|
196 |
if (ans == R_NilValue) |
|
|
197 |
error("split does not have a table element"); |
|
|
198 |
return(ans); |
|
|
199 |
} |