A Quick Start Guide for wsrf

He Zhao, Graham Williams

2021-10-10

Introduction

The wsrf package is a parallel implementation of the Weighted Subspace Random Forest algorithm (wsrf) of Xu et al. (2012). A novel variable weighting method is used for variable subspace selection in place of the traditional approach of random variable sampling. This new approach is particularly useful in building models for high dimensional data — often consisting of thousands of variables. Parallel computation is used to take advantage of multi-core machines and clusters of machines to build random forest models from high dimensional data with reduced elapsed times.

Requirements and Installation Notes

Currently, wsrf requires R (>= 3.3.0), Rcpp (>= 0.10.2) (Eddelbuettel and François 2011; Eddelbuettel 2013). For the use of multi-threading, a C++ compiler with C++11 standard support of threads is required. To install the latest stable version of the package, from within R run:

install.packages("wsrf")

or the latest development version:

devtools::install_github("simonyansenzhao/wsrf")

The version of R before 3.3.0 doesn’t provide fully support of C++11, thus we provided other options for installation of wsrf. From 1.6.0, we drop the support for those options. One can find the usage in the documentation from previous version if interested.

Usage

This section demonstrates how to use wsrf, especially on a cluster of machines.

The example uses a small dataset weather from rattle.data (Williams 2011). See the help page of rattle.data in R (?weather) for more details of weather. Below are the basic information of it.

library("rattle.data")
ds <- weather
dim(ds)
## [1] 366  24
names(ds)
##  [1] "Date"          "Location"      "MinTemp"       "MaxTemp"      
##  [5] "Rainfall"      "Evaporation"   "Sunshine"      "WindGustDir"  
##  [9] "WindGustSpeed" "WindDir9am"    "WindDir3pm"    "WindSpeed9am" 
## [13] "WindSpeed3pm"  "Humidity9am"   "Humidity3pm"   "Pressure9am"  
## [17] "Pressure3pm"   "Cloud9am"      "Cloud3pm"      "Temp9am"      
## [21] "Temp3pm"       "RainToday"     "RISK_MM"       "RainTomorrow"

Before building the model we need to prepare the training dataset. First we note the various roles played by the different variables, including identifying the irrelevant variables.

target <- "RainTomorrow"
ignore <- c("Date", "Location", "RISK_MM")
(vars <- setdiff(names(ds), ignore))
##  [1] "MinTemp"       "MaxTemp"       "Rainfall"      "Evaporation"  
##  [5] "Sunshine"      "WindGustDir"   "WindGustSpeed" "WindDir9am"   
##  [9] "WindDir3pm"    "WindSpeed9am"  "WindSpeed3pm"  "Humidity9am"  
## [13] "Humidity3pm"   "Pressure9am"   "Pressure3pm"   "Cloud9am"     
## [17] "Cloud3pm"      "Temp9am"       "Temp3pm"       "RainToday"    
## [21] "RainTomorrow"
dim(ds[vars])
## [1] 366  21

Next we deal with missing values, using na.roughfix() from randomForest to take care of them.

library("randomForest")
if (sum(is.na(ds[vars]))) ds[vars] <- na.roughfix(ds[vars])
ds[target] <- as.factor(ds[[target]])
(tt <- table(ds[target]))
## 
##  No Yes 
## 300  66

We construct the formula that describes the model which will predict the target based on all other variables.

(form <- as.formula(paste(target, "~ .")))
## RainTomorrow ~ .

Finally we create the randomly selected training and test datasets, setting a seed so that the results can be exactly replicated.

seed <- 42
set.seed(seed)
length(train <- sample(nrow(ds), 0.7*nrow(ds)))
## [1] 256
length(test <- setdiff(seq_len(nrow(ds)), train))
## [1] 110

The function to build a weighted random forest model in wsrf is:

wsrf(formula, data, ...)

and

wsrf(x,
     y,
     mtry=floor(log2(length(x))+1),
     ntree=500,
     weights=TRUE,
     parallel=TRUE,
     na.action=na.fail,
     importance=FALSE,
     nodesize=2,
     clusterlogfile,
     ...)

We use the training dataset to build a random forest model. All parameters, except formula and data, use their default values: 500 for ntree — the number of trees; TRUE for weights — weighted subspace random forest or random forest; TRUE for parallel — use multi-thread or other options, etc.

library("wsrf")
model.wsrf.1 <- wsrf(form, data=ds[train, vars], parallel=FALSE)
print(model.wsrf.1)
## A Weighted Subspace Random Forest model with 500 trees.
## 
##   No. of variables tried at each split: 5
##         Minimum size of terminal nodes: 2
##                  Out-of-Bag Error Rate: 0.13
##                               Strength: 0.61
##                            Correlation: 0.18
## 
## Confusion matrix:
##      No Yes class.error
## No  208   6        0.03
## Yes  28  14        0.67
print(model.wsrf.1, 1)  # Print tree 1.
## Tree 1 has 23 tests (internal nodes), with OOB error rate 0.1596:
## 
##  1) Humidity3pm <= 72
##  .. 2) WindGustSpeed <= 61
##  .. .. 3) Cloud3pm <= 5
##  .. .. .. 4) Humidity9am <= 92
##  .. .. .. .. 5) MinTemp <= -0.4
##  .. .. .. .. .. 6) WindSpeed3pm <= 17   [No] (1 0) *
##  .. .. .. .. .. 6) WindSpeed3pm >  17   [Yes] (0.33 0.67) *
##  .. .. .. .. 5) MinTemp >  -0.4
##  .. .. .. .. .. 7) Pressure9am <= 1016.1
##  .. .. .. .. .. .. 8) Sunshine <= 10.5   [No] (1 0) *
##  .. .. .. .. .. .. 8) Sunshine >  10.5
##  .. .. .. .. .. .. .. 9) Temp9am <= 15   [Yes] (0 1) *
##  .. .. .. .. .. .. .. 9) Temp9am >  15   [No] (1 0) *
##  .. .. .. .. .. 7) Pressure9am >  1016.1   [No] (1 0) *
##  .. .. .. 4) Humidity9am >  92   [Yes] (0.25 0.75) *
##  .. .. 3) Cloud3pm >  5
##  .. .. .. 10) WindSpeed3pm <= 9
##  .. .. .. .. 11) Pressure9am <= 1023.7
##  .. .. .. .. .. 12) WindGustDir == N   [Yes] (0.12 0.88) *
##  .. .. .. .. .. 12) WindGustDir == NNE   [Yes] (0.12 0.88) *
##  .. .. .. .. .. 12) WindGustDir == NE   [Yes] (0 1) *
##  .. .. .. .. .. 12) WindGustDir == ENE   [No] (1 0) *
##  .. .. .. .. .. 12) WindGustDir == E   [Yes] (0 1) *
##  .. .. .. .. .. 12) WindGustDir == ESE   [Yes] (0 1) *
##  .. .. .. .. .. 12) WindGustDir == SE   [Yes] (0.12 0.88) *
##  .. .. .. .. .. 12) WindGustDir == SSE   [Yes] (0.12 0.88) *
##  .. .. .. .. .. 12) WindGustDir == S   [Yes] (0 1) *
##  .. .. .. .. .. 12) WindGustDir == SSW   [Yes] (0.12 0.88) *
##  .. .. .. .. .. 12) WindGustDir == SW   [Yes] (0.12 0.88) *
##  .. .. .. .. .. 12) WindGustDir == WSW   [Yes] (0.12 0.88) *
##  .. .. .. .. .. 12) WindGustDir == W   [Yes] (0.12 0.88) *
##  .. .. .. .. .. 12) WindGustDir == WNW   [Yes] (0.12 0.88) *
##  .. .. .. .. .. 12) WindGustDir == NW   [Yes] (0.12 0.88) *
##  .. .. .. .. .. 12) WindGustDir == NNW   [Yes] (0 1) *
##  .. .. .. .. 11) Pressure9am >  1023.7   [No] (1 0) *
##  .. .. .. 10) WindSpeed3pm >  9
##  .. .. .. .. 13) MinTemp <= 16
##  .. .. .. .. .. 14) Temp9am <= 1   [Yes] (0 1) *
##  .. .. .. .. .. 14) Temp9am >  1
##  .. .. .. .. .. .. 15) Rainfall <= 2.8
##  .. .. .. .. .. .. .. 16) WindSpeed9am <= 6
##  .. .. .. .. .. .. .. .. 17) WindGustSpeed <= 30
##  .. .. .. .. .. .. .. .. .. 18) WindGustDir == N   [No] (0.5 0.5) *
##  .. .. .. .. .. .. .. .. .. 18) WindGustDir == NNE   [Yes] (0 1) *
##  .. .. .. .. .. .. .. .. .. 18) WindGustDir == NE   [No] (0.5 0.5) *
##  .. .. .. .. .. .. .. .. .. 18) WindGustDir == ENE   [No] (0.5 0.5) *
##  .. .. .. .. .. .. .. .. .. 18) WindGustDir == E   [No] (1 0) *
##  .. .. .. .. .. .. .. .. .. 18) WindGustDir == ESE   [No] (0.5 0.5) *
##  .. .. .. .. .. .. .. .. .. 18) WindGustDir == SE   [No] (0.5 0.5) *
##  .. .. .. .. .. .. .. .. .. 18) WindGustDir == SSE   [No] (0.5 0.5) *
##  .. .. .. .. .. .. .. .. .. 18) WindGustDir == S   [No] (0.5 0.5) *
##  .. .. .. .. .. .. .. .. .. 18) WindGustDir == SSW   [No] (0.5 0.5) *
##  .. .. .. .. .. .. .. .. .. 18) WindGustDir == SW   [No] (0.5 0.5) *
##  .. .. .. .. .. .. .. .. .. 18) WindGustDir == WSW   [No] (0.5 0.5) *
##  .. .. .. .. .. .. .. .. .. 18) WindGustDir == W   [No] (0.5 0.5) *
##  .. .. .. .. .. .. .. .. .. 18) WindGustDir == WNW   [No] (0.5 0.5) *
##  .. .. .. .. .. .. .. .. .. 18) WindGustDir == NW   [Yes] (0 1) *
##  .. .. .. .. .. .. .. .. .. 18) WindGustDir == NNW   [No] (0.5 0.5) *
##  .. .. .. .. .. .. .. .. 17) WindGustSpeed >  30   [No] (1 0) *
##  .. .. .. .. .. .. .. 16) WindSpeed9am >  6   [No] (1 0) *
##  .. .. .. .. .. .. 15) Rainfall >  2.8
##  .. .. .. .. .. .. .. 19) Temp3pm <= 8.8   [No] (1 0) *
##  .. .. .. .. .. .. .. 19) Temp3pm >  8.8   [Yes] (0 1) *
##  .. .. .. .. 13) MinTemp >  16
##  .. .. .. .. .. 20) WindDir3pm == N   [Yes] (0.14 0.86) *
##  .. .. .. .. .. 20) WindDir3pm == NNE   [Yes] (0.14 0.86) *
##  .. .. .. .. .. 20) WindDir3pm == NE   [Yes] (0.14 0.86) *
##  .. .. .. .. .. 20) WindDir3pm == ENE   [Yes] (0.14 0.86) *
##  .. .. .. .. .. 20) WindDir3pm == E   [Yes] (0.14 0.86) *
##  .. .. .. .. .. 20) WindDir3pm == ESE   [Yes] (0 1) *
##  .. .. .. .. .. 20) WindDir3pm == SE   [Yes] (0.14 0.86) *
##  .. .. .. .. .. 20) WindDir3pm == SSE   [No] (1 0) *
##  .. .. .. .. .. 20) WindDir3pm == S   [Yes] (0.14 0.86) *
##  .. .. .. .. .. 20) WindDir3pm == SSW   [Yes] (0.14 0.86) *
##  .. .. .. .. .. 20) WindDir3pm == SW   [Yes] (0.14 0.86) *
##  .. .. .. .. .. 20) WindDir3pm == WSW   [Yes] (0.14 0.86) *
##  .. .. .. .. .. 20) WindDir3pm == W   [Yes] (0.14 0.86) *
##  .. .. .. .. .. 20) WindDir3pm == WNW   [Yes] (0.14 0.86) *
##  .. .. .. .. .. 20) WindDir3pm == NW   [Yes] (0 1) *
##  .. .. .. .. .. 20) WindDir3pm == NNW   [Yes] (0.14 0.86) *
##  .. 2) WindGustSpeed >  61
##  .. .. 21) Temp3pm <= 11.1   [No] (1 0) *
##  .. .. 21) Temp3pm >  11.1
##  .. .. .. 22) WindGustSpeed <= 65   [No] (0.5 0.5) *
##  .. .. .. 22) WindGustSpeed >  65   [Yes] (0 1) *
##  1) Humidity3pm >  72
##  .. 23) WindGustSpeed <= 30   [No] (0.75 0.25) *
##  .. 23) WindGustSpeed >  30   [Yes] (0 1) *

Then, predict the classes of test data.

cl <- predict(model.wsrf.1, newdata=ds[test, vars], type="class")$class
actual <- ds[test, target]
(accuracy.wsrf <- mean(cl == actual, na.rm=TRUE))
## [1] 0.8636364

Thus, we have built a model that is around 86% accurate on unseen testing data.

Using different random seed, we obtain another model.

# Here we build another model without weighting.
model.wsrf.2 <- wsrf(form, data=ds[train, vars], weights=FALSE, parallel=FALSE)
print(model.wsrf.2)
## A Weighted Subspace Random Forest model with 500 trees.
## 
##   No. of variables tried at each split: 5
##         Minimum size of terminal nodes: 2
##                  Out-of-Bag Error Rate: 0.15
##                               Strength: 0.59
##                            Correlation: 0.19
## 
## Confusion matrix:
##      No Yes class.error
## No  210   4        0.02
## Yes  34   8        0.81

We can also derive a subset of the forest from the model or a combination of multiple forests.

submodel.wsrf <- subset.wsrf(model.wsrf.1, 1:150)
print(submodel.wsrf)
## A Weighted Subspace Random Forest model with 150 trees.
## 
##   No. of variables tried at each split: 5
##         Minimum size of terminal nodes: 2
##                  Out-of-Bag Error Rate: 0.14
##                               Strength: 0.62
##                            Correlation: 0.19
## 
## Confusion matrix:
##      No Yes class.error
## No  207   7        0.03
## Yes  28  14        0.67
bigmodel.wsrf <- combine.wsrf(model.wsrf.1, model.wsrf.2)
print(bigmodel.wsrf)
## A Weighted Subspace Random Forest model with 1000 trees.
## 
##   No. of variables tried at each split: 5
##         Minimum size of terminal nodes: 2
##                  Out-of-Bag Error Rate: 0.14
##                               Strength: 0.60
##                            Correlation: 0.18
## 
## Confusion matrix:
##      No Yes class.error
## No  210   4        0.02
## Yes  31  11        0.74

Next, we will specify building the model on a cluster of servers.

servers <- paste0("node", 31:40)
model.wsrf.3 <- wsrf(form, data=ds[train, vars], parallel=servers)

All we need is a character verctor specifying the hostnames of which nodes to use, or a named integer vector, whose values of the elements give how many threads to use for model building, in other words, how many trees built simultaneously. More detail descriptions about wsrf are presented in the manual.

References

Eddelbuettel, Dirk. 2013. Seamless R and C++ Integration with Rcpp. New York: Springer.

Eddelbuettel, Dirk, and Romain François. 2011. “Rcpp: Seamless R and C++ Integration.” Journal of Statistical Software 40 (8): 1–18. https://doi.org/10.18637/jss.v040.i08.

Williams, Graham J. 2011. Data Mining with Rattle and R: The Art of Excavating Data for Knowledge Discovery. Use R! Springer. https://www.amazon.com/gp/product/1441998896/ref=as_li_qf_sp_asin_tl?ie=UTF8&tag=togaware-20&linkCode=as2&camp=217145&creative=399373&creativeASIN=1441998896.

Xu, Baoxun, Joshua Zhexue Huang, Graham Williams, Qiang Wang, and Yunming Ye. 2012. “Classifying Very High-Dimensional Data with Random Forests Built from Small Subspaces.” International Journal of Data Warehousing and Mining (IJDWM) 8 (2): 44–63.