Skip to content

Fits a random forest on a 'tidyFit' R6 class. The function can be used with regress and classify.

Usage

# S3 method for rf
.fit(self, data = NULL)

Arguments

self

a 'tidyFit' R6 class.

data

a data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr).

Value

A fitted 'tidyFit' class model.

Details

Hyperparameters:

  • ntree (number of trees)

  • mtry (number of variables randomly sampled at each split)

Important method arguments (passed to m)

The function provides a wrapper for randomForest::randomForest. See ?randomForest for more details.

Implementation

The random forest is always fit with importance = TRUE. The feature importance values are extracted using coef().

References

Liaw, A. and Wiener, M. (2002). Classification and Regression by randomForest. R News 2(3), 18--22.

See also

.fit.svm, .fit.boost and m methods

Author

Johann Pfitzinger

Examples

# Load data
data <- tidyfit::Factor_Industry_Returns
data <- dplyr::filter(data, Industry == "HiTec")
data <- dplyr::select(data, -Date, -Industry)

# Stand-alone function
fit <- m("rf", Return ~ ., data)
fit
#> # A tibble: 1 × 5
#>   estimator_fct              `size (MB)` grid_id  model_object settings
#>   <chr>                            <dbl> <chr>    <list>       <list>  
#> 1 randomForest::randomForest        8.56 #0010000 <tidyFit>    <tibble>

# Within 'regress' function
fit <- regress(data, Return ~ ., m("rf"))
tidyr::unnest(coef(fit), model_info)
#> # A tibble: 7 × 6
#> # Groups:   model [1]
#>   model term        estimate `%IncMSE` IncNodePurity importanceSD
#>   <chr> <chr>       <lgl>        <dbl>         <dbl>        <dbl>
#> 1 rf    (Intercept) NA           0                0        0     
#> 2 rf    Mkt-RF      NA          35.9          13680.       0.451 
#> 3 rf    SMB         NA           1.20          2209.       0.0978
#> 4 rf    HML         NA           4.01          3340.       0.184 
#> 5 rf    RMW         NA           2.33          2751.       0.140 
#> 6 rf    CMA         NA           5.94          4509.       0.246 
#> 7 rf    RF          NA           0.704         1217.       0.0886