DataLab is a compact statistics package aiming at exploratory data analysis. Please visit the DataLab Web site for more information....


TrainRF

Declaration: TRndForest.TrainRF: integer;
The method TrainRF creates a random forest model using the following data and parameters: the properties TrnDataX and TrnDataY contain the training data X- and Y-block, respectively. The property IsClassifier controls whether the RF model should be used as a classifier or a regression model. The properties NTrees and RPar specify the number of trees and the resampling factor.

On return the random forest model has been created (if the returned error code is 0) and can be stored by using the method SaveOnDisk. The array property YHat contains the estimated y values, the array property VarImp contains the variable importance.

The function returns the following error codes:

 0 ... everything is OK
-1 ... TrnDataY must be dichotomous in classifier mode

Example: The following code shows a simple example how to train a random decision forest. It takes the first three columns (constant NXVARS) as the independent variables and the 12th column (constant TARGETCOL) as the y variable for the model. After training the random forest model the estimated y values are stored in an extra column appended to the data matrix:

program RFRegression;

const
  NXVARS = 11;
  TARGETCOL = 13;

var
  RF      : TRndForest;
  i       : integer;
  LastC   : integer;
  obj     : integer;

begin
RF := TRndForest.Create(nil);                      // create random forest model
RF.NTrees := 70;                                   // set the parameters
RF.RPar := 0.65;
RF.ResizeTrnData (NXVARS, DStore.NrOfRows);        // resize the trainíng data matrices
for i:=1 to NXVARS do                              // and fill them
  RF.TrnDataX.CopyColumnFrom (DStore,i,i);         // X data block
for obj:=1 to DStore.NrOfRows do
  RF.TrnDataY[obj] := DStore.Elem[TARGETCOL,obj];  // Y data
RF.TrainRF;                                        // perform the training
DStore.NrOfColumns := DStore.NrOfColumns + 1;      // extend data matrix by one column
LastC := DStore.NrOfColumns;
DStore.ColName[LastC] := 'Estimated';
for obj:=1 to DStore.NrOfRows do                   // copy estimated values into last column
  DStore.Elem[LastC,obj] := RF.YHat[obj];
RF.SaveOnDisk (GetDLabDir(ddWork)+'test_rf', true);// save trained random forest
RF.Free;
end.