[fbf06f]: / partyMod / R / Classes.R

Download this file

303 lines (276 with data), 9.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# $Id$
### Linear statistic with expectation and covariance
setClass(Class = "LinStatExpectCovar",
representation = representation(
linearstatistic = "numeric",
expcovinf = "ExpectCovarInfluence"
),
contains = "ExpectCovar"
)
### Memory for C_svd
setClass(Class = "svd_mem",
representation = representation(
method = "character",
jobu = "character",
jobv = "character",
u = "matrix",
v = "matrix",
s = "numeric",
p = "integer"
)
)
### with Moore-Penrose inverse of the covariance matrix
setClass(Class = "LinStatExpectCovarMPinv",
representation = representation(
MPinv = "matrix",
rank = "numeric",
svdmem = "svd_mem"
),
contains = "LinStatExpectCovar"
)
################ Memory Classes #####################
setClass(Class = "TreeFitMemory",
representation = representation(
expcovinf = "ExpectCovarInfluence",
expcovinfss = "ExpectCovarInfluence",
linexpcov2sample = "LinStatExpectCovar",
weights = "numeric",
varmemory = "list",
dontuse = "logical",
dontusetmp = "logical",
splitstatistics = "numeric"
),
validity = function(object) {
ni <- length(dontuse)
length(varmemory) == ni && length(dontusetmp) == ni
}
)
############## Tree Classes ######################
setClassUnion("df_OR_list", c("data.frame", "list"))
setClass(Class = "VariableControl",
representation = representation(
teststat = "factor",
pvalue = "logical",
tol = "numeric",
maxpts = "integer",
abseps = "numeric",
releps = "numeric"
),
prototype = list(
teststat = factor("max", levels = c("max", "quad")),
pvalue = as.logical(TRUE),
tol = as.double(1e-10),
maxpts = as.integer(25000),
abseps = as.double(1e-4),
releps = as.double(0.0)
)
)
setClass(Class = "SplitControl",
representation = representation(
minprob = "numeric",
minsplit = "numeric",
minbucket = "numeric",
tol = "numeric",
maxsurrogate = "integer"
),
prototype = list(minprob = as.double(0.01),
minsplit = as.double(20),
minbucket = as.double(7),
tol = as.double(1e-10),
maxsurrogate = as.integer(0)
),
validity = function(object) {
if (any(c(object@minsplit, object@minbucket,
object@tol, object@maxsurrogate) < 0)) {
warning("no negative values allowed in objects of class ",
sQuote("SplitControl"))
return(FALSE)
}
if (object@minprob < 0.01 || object@minprob > 0.99) {
warning(sQuote("minprob"), " must be in (0.01, 0.99)")
return(FALSE)
}
return(TRUE)
}
)
setClass(Class = "GlobalTestControl",
representation = representation(
testtype = "factor",
nresample = "integer",
randomsplits = "logical",
mtry = "integer",
mincriterion = "numeric"
),
prototype = list(
testtype = factor("Bonferroni",
levels = c("Bonferroni", "MonteCarlo", "Aggregated",
"Univariate", "Teststatistic")),
nresample = as.integer(9999),
randomsplits = as.logical(FALSE),
mtry = as.integer(0),
mincriterion = as.double(0.95)
),
validity = function(object) {
if (object@mincriterion < 0) {
warning(sQuote("mincriterion"), " must not be negative")
return(FALSE)
}
if (any(object@mtry < 0)) {
warning(sQuote("mtry"), " must be positive")
return(FALSE)
}
if (object@nresample < 100) {
warning(sQuote("nresample"), " must be larger than 100")
return(FALSE)
}
return(TRUE)
},
)
setClass(Class = "TreeGrowControl",
representation = representation(
stump = "logical",
varOnce = "logical",
maxdepth = "integer",
savesplitstats = "logical"
),
prototype = list(stump = as.logical(FALSE),
varOnce = as.logical(FALSE),
maxdepth = as.integer(0),
savesplitstats = as.logical(TRUE)),
validity = function(object) {
if (object@maxdepth < 0) {
warning(sQuote("maxdepth"), " must be positive")
return(FALSE)
}
return(TRUE)
}
)
setClass(Class = "TreeControl",
representation = representation(
varctrl = "VariableControl",
splitctrl = "SplitControl",
gtctrl = "GlobalTestControl",
tgctrl = "TreeGrowControl"
),
prototype = list(varctrl = new("VariableControl"),
splitctrl = new("SplitControl"),
gtctrl = new("GlobalTestControl"),
tgctrl = new("TreeGrowControl")
),
validity = function(object) {
(validObject(object@varctrl) &&
validObject(object@splitctrl)) &&
(validObject(object@gtctrl) &&
validObject(object@tgctrl))
}
)
setClass(Class = "ForestControl",
representation = representation(
ntree = "integer",
replace = "logical",
fraction = "numeric",
trace = "logical",
dropcriterion = "logical",
compress = "function",
expand = "function"),
contains = "TreeControl",
validity = function(object) {
if (object@ntree < 1) {
warning(sQuote("ntree"), " must be equal or greater 1")
return(FALSE)
}
if (object@fraction < 0.01 || object@fraction > 0.99) {
warning(sQuote("fraction"), " must be in (0.01, 0.99)")
return(FALSE)
}
return(TRUE)
}
)
setClass(Class = "VariableFrame",
representation = representation(
variables = "df_OR_list",
transformations = "list",
is_nominal = "logical",
is_ordinal = "logical",
is_censored = "logical",
ordering = "list",
levels = "list",
scores = "list",
has_missings = "logical",
whichNA = "list",
nobs = "integer",
ninputs = "integer")
)
setClass(Class = "ResponseFrame",
representation = representation(
test_trafo = "matrix",
predict_trafo = "matrix"
), contains = "VariableFrame"
)
setClass(Class = "LearningSample",
representation = representation(
responses = "ResponseFrame",
inputs = "VariableFrame",
weights = "numeric",
nobs = "integer",
ninputs = "integer"
)
)
setClass(Class = "LearningSampleFormula",
representation = representation(
menv = "ModelEnv"
), contains = "LearningSample"
)
### the tree structure itself is a list,
### and we need to make sure that the tree slot excepts
### the S3 classes.
setClass(Class = "SplittingNode", contains = "list")
setClass(Class = "TerminalNode", contains = "list")
setClass(Class = "TerminalModelNode", contains = "list")
setClass(Class = "orderedSplit", contains = "list")
setClass(Class = "nominalSplit", contains = "list")
### and we don't want to see warnings that class `Surv'
### (S3 method in `survival') is unknown
setClass(Class = "Surv", contains = "list")
### A class for partitions induced by recursive binary splits
setClass(Class = "BinaryTreePartition",
representation = representation(
tree = "list", # the basic tree structure as (named or
# unnamed) list
where = "integer", # the nodeID of the observations in the
# learning sample
weights = "numeric" # the weights in the root node
),
)
### A class for binary trees
setClass(Class = "BinaryTree",
representation = representation(
data = "ModelEnv",
responses = "VariableFrame", # a list of response `variables'
# for computing predictions
cond_distr_response = "function", # predict distribtion
predict_response = "function", # predict responses
prediction_weights = "function", # prediction weights
get_where = "function", # node numbers
update = "function" # update weights
),
contains = "BinaryTreePartition"
)
### A class for random forest
setClass(Class = "RandomForest",
representation = representation(
ensemble = "list",
where = "list",
weights = "list",
initweights = "numeric",
data = "ModelEnv",
responses = "VariableFrame", # a list of response `variables'
# for computing predictions
cond_distr_response = "function", # predict distribtion
predict_response = "function", # predict responses
prediction_weights = "function", # prediction weights
get_where = "function", # node numbers
update = "function", # update weights
expand = "function" # function to invert compress operation
)
)