Builds a YARF Model. There are many customizations available.
Usage
YARF(
X = NULL,
y = NULL,
Xy = NULL,
Xother = NULL,
allow_missingness_in_y = FALSE,
num_trees = NULL,
bootstrap_indices = NULL,
n_max_per_tree = NULL,
other_indices = NULL,
mtry = NULL,
nodesize = NULL,
mtry_script = NULL,
split_vals_script = NULL,
make_node_to_leaf_script = NULL,
cost_single_node_calc_script = NULL,
cost_both_children_calc_script = NULL,
node_assign_script = NULL,
after_node_birth_function_script = NULL,
shared_scripts = NULL,
use_missing_data = TRUE,
replace_missing_data_with_x_j_bar = FALSE,
no_missing_data_split_rule = "RANDOM",
serialize = FALSE,
seed = NULL,
wait = TRUE,
calculate_oob_error = TRUE,
fit_until_convergence = FALSE,
oob_cost_calculation_script = NULL,
tolerance = 0.01,
verbose = TRUE,
debug_log = FALSE
)Arguments
- X
The data frame of training data
- y
The vector of training responses which is either numeric (for regression or factor (for classification).
- Xy
The data frame of training data where the last column is responses
- Xother
Other data that is used in the training but the RF doesn't split on it
- allow_missingness_in_y
If
TRUE, missingness in the response variable,y, is allowed. If the missings are not handled (somehow) in the custom functions, YARF will crash!! Default isFALSE.- num_trees
The number of trees in the RF. Default is
NULLwhich sets the value to500iffit_until_convergenceis set toFALSEand 10,000 iffit_until_convergenceis set toTRUE.If the model is fit asynchronously via the
waitparameter being set toTRUE, this number represents a vague contract between the user and the software as the model fitting can be (a) halted by the user or the model can be (b) set to converge automatically (by setting thefit_until_convergenceparameter toTRUE). In the latter case, this parameter functions as the maximum number of trees that can be created and thus, it is recommended that this number is very large, hence the default of 10,000.- bootstrap_indices
A list with keys 1, 2, ..., num_trees where each value is the indices of the training data you wish to use for each tree. The default is
NULLindicating the default algorithm of sampling1, ..., nwith replacement (i.e. the non-parametric bootstrap default). Needless to say indices specified here will not be part of the out-of-bag collection of indices. You may specify more than enough (i.e. more thannum_trees) elements in this list. Only the firstnum_treeswill be used. No warning message will be displayed if you include too many. If this parameter is specified, there is no need to specifyn_max_per_tree.- n_max_per_tree
An upper limit on the number of observations used to build each tree. If this parameter is specified, there is no need to specify
bootstrap_indices. Default isNULLfor the entire dataset,n.- other_indices
An optional list with keys 1,2,..., num_trees where each value is indices of the training data you wish to use in some custom way for each tree using a custom function. If not custom function is specified which makes use of this, it will be ignored. However, indices specified here will not be part of the out-of-bag collection of indices. If this is not your wish, the elements in the vectors specified in this list's values should be a subset of those in the values of
bootstrap_indices. The default isNULLindicating you do not wish to specify any "other" data records.- mtry
The number of variables tried at every split. The default is
NULLwhich indicates the out-of-box RF default which is floor(p / 3) for regression and floor(sqrt(p)) for classification. If you want to use all possible features, set this parameter to "all". If you want a custom function, leave this NULL and see next parameter.- nodesize
The minimum number of observations in a node. YARF will stop splitting at this point. If
NULLthe out-of-the-box default of 5 for regression and 1 for classification will be used.- mtry_script
A custom javascript function which selects the variables to be greedily searched (see below) The default is
NULLwhich employs themtryargument. If you specify your function please randomize the order of the returned attributes to arbitrate ties.function tryVars(node){ //node is of type YARF.YARFNode
...
return int_array //a subset of 0,...,p-1, indices indicating the variables to perform the exhaustive search on
}
- split_vals_script
A custom javascript function which selects the split values to be greedily searched in feature j. The default is
NULLwhich employs the midpoints of all sorted values.function tryVals(node, j){ //node is of type YARF.YARFNode and j is the feature number in 0,...,p-1
...
return double_array //a vector of split vals to greedily assess
}
- make_node_to_leaf_script
A custom javascript function to be used to calculate nodesize (see below). The default is
NULLwhere nodesize will be calculated as a static constant (see thenodesizeargument).function makeNodeIntoLeaf(node){ //node is of type YARF.YARFNode
...
return boolean //where true makes this node into a leaf
}
- cost_single_node_calc_script
A custom cost calculation for a potential node (when considering a split) in Javascript (see below). The default is
NULLwhich means the out-of-the-box default of sum of squared error relative to the sample average (if regression) and sum of entropy (if classification). You may find it convenient to also made a node assignment here. If so, make sure you specify the node assignment function as a blank function (notNULL).function nodeCost(node){ //node is of type YARF.YARFNode
...
return double //where a higher number indicates a higher cost
}
- cost_both_children_calc_script
A custom cost calculation in Javascript for an entire split considering both the putative left and right children nodes (see below). The default is
NULLwhich means the out-of-the-box default for Random Forests which is sum of left and right nodes' costs for regression and average of left and right nodes' cost (relative to the number of observations in each node).function totalChildrenCost(leftNode, rightNode){ //both nodes are of type YARF.YARFNode
...
return double //where a higher number indicates a higher cost
}
- node_assign_script
A custom node assignment function in Javascript (see below). This function is run after RF greedily finds the "lowest cost" split. The default is
NULLcorresponding to the sample average of the node responses in regression or the modal class during classification.function assignYhatToNode(node){ //node is of type YARF.YARFNode
...
return double //assigned as this node's predicted value ("y_hat")
}
- after_node_birth_function_script
A custom function in Javascript which is executed after a node is given birth to. The default is
NULLwhich implies nothing special is done, the Random Forest default. This is particularly useful to record extra information in the node (e.g. by writing a hash to theother_infofield in the node).function nodeAfterNodeBirth(node){ //node is of type YARF.YARFNode
...
}
Custom Javascript code that are always in scope when running all your custom methods. The default is
NULLfor no shared scripts.- use_missing_data
Use the "missing-incorporated-in-attributes" strategy to fit data with missingness. The default is
TRUE.- replace_missing_data_with_x_j_bar
Replace missing predictor values with their training-column means before fitting.
- no_missing_data_split_rule
What rule should be used for missing data seen in testing for nodes that did not see missing data in training? Choices:
"RANDOM"and"CONDITIONAL_ON_QUANTILE"."RANDOM"will assign a split rule for missing data at such nodes purely arbitrarily and with equal probability, i.e. 50 will condition this probability on the quantile of the split value amongst the observed (non-missing) data. For example, if the split value lies at the 75th percentile, then with probability .75, this node's rule for missing data will be to go left, and with probability .25 to go right. Note that for either of these options, the randomization happens at training time and thus during prediction the rule will be deterministic. The default is"RANDOM".- serialize
Should the YARF model be saved? The default is
FALSEas this is costly in processing time and memory. This can only be set toTRUEifwait = TRUE. IfTRUE, we will automatically serialize after other operations that add data (such as the OOB evaluation).- seed
Set a random seed for reproducibility.
- wait
Should we hang R to wait for the YARF model to complete? The default is
TRUE.- calculate_oob_error
Should we also calculate the OOB error? Default is
TRUE. Automatically is turned off ifwaitbecomesFALSE.- fit_until_convergence
Default is
FALSE. IfTRUE, then thewaitparameter is set toFALSEregardless of the user-specified value. Then, the Random Forest model is fit until "convergence" as defined below:After each tree is completed, the OOB cost is computed. If the cost decreases the algorithm does not engage. However, upon the first increase in cost, the model checks for convergence by calculating the average change in cost and the standard deviation of change in cost. If a 95% confidence interval of the average cost is within the window created by plus or minus
tolerance(i.e. from zero), then the algorithm has "converged". Upon convergence, the model is stopped and a message is printed to the console. Note that specification of theoob_cost_calculation_scriptand a propertolerancelevel is essential.It is strongly recommended to set the
num_treesparameter large, otherwise the model can halt before it has converged sincenum_treesrepresents an upper limit. If left unspecified,num_treesis set to 10,000 whenfit_until_convergenceset toTRUE.If the user wishes to view the convergence in real time, we recommend the
YARF_progressfunction. However, this function will lock the console.- oob_cost_calculation_script
This parameter will determine the out-of-bag cost of the RF forest model. Default is
NULL. If it isNULL, then the cost will be 1-R^2 for regression models and misclassification error for classification models.If non-null, an optional custom Javascript function which calculates the cost of a prediction given the true value of the prediction (see below). If is likely similar to
cost_single_node_calc_script. It is recommended to share code between them by writing a function included in theshared_scriptsargument which can be referenced when calculating OOB results.function oobCost(y_hat, y){ //y_hat is the predicted value and y is the true value (both are of type double)
...
return double //where a larger number indicates a higher cost to the error between y and y_hat.
}
If this script is specified (i.e. the argument is non-
NULL), this will also affect the output of theYARF_update_with_oob_resultsfunction. This script can be reset after the model is built if necessary.- tolerance
This parameter is ignored unless
fit_until_convergenceis set toTRUE. If so, this controls the tolerance used when assessing the convergence of the RF model. Default is0.1. Ifoob_cost_calculation_scriptis specified, this should also be specified as 0.1 may not be appropriate for a custom oob cost function.- verbose
Should we print out messages verbosely during construction? Default is
FALSE.- debug_log
Should we print out messages from Java? Default is
FALSE.
Value
A list that (a) reiterates all ob the above arguments passed in and (b) pred_type is the
guess as to modeling type: regression or classification (c) java_YARF is the Java object
(d) y_levels is the unique values of the response variable (e) model_matrix_training_data
is the data matrix with factor variables converted to dummies that is fed into YARF as the X variables
(f) training_data_features is the names of the features (g) predictors_which_are_factors
is a list of the variables that are factors and (h) n and p which are the dimensions of
the training data model matrix and (i) various other convenient data and internal data of use to
other functions in this package.
