cinbag: Modified Classification and Regression with Random Forest

Description Usage Arguments Value Note Author(s) References See Also Examples

Description

cinbag implements a modified random forest algorithm (based on the source code from the randomForest package by Andy Liaw and Matthew Wiener and on the original Fortran code by Leo Breiman and Adele Cutler) to return the number of times a row appears in a tree's bag. cinbag returns a randomForest object, e.g., rfobj, with an additional output, a matrix with inbag counts (rows) for each tree (columns). For instance, rfobj$inbagCount is similar to rfobj$inbag, but with inbag counts instead of inbag indicators.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
      cinbag(x, y=NULL,  xtest=NULL, ytest=NULL, ntree=500,
             mtry=if (!is.null(y) && !is.factor(y))
             max(floor(ncol(x)/3), 1) else floor(sqrt(ncol(x))),
             replace=TRUE, classwt=NULL, cutoff, strata,
             sampsize = if (replace) nrow(x) else ceiling(.632*nrow(x)),
             nodesize = if (!is.null(y) && !is.factor(y)) 5 else 1,
             maxnodes = NULL,
             importance=FALSE, localImp=FALSE, nPerm=1,
             proximity, oob.prox=proximity,
             norm.votes=TRUE, do.trace=FALSE,
             keep.forest=!is.null(y) && is.null(xtest), corr.bias=FALSE,
             keep.inbag=FALSE, ...)

Arguments

x

a data frame or a matrix of predictors, or a formula describing the model to be fitted (for the print method, an randomForest object).

y

A response vector. If a factor, classification is assumed, otherwise regression is assumed. If omitted, randomForest will run in unsupervised mode.

xtest

a data frame or matrix (like x) containing predictors for the test set.

ytest

response for the test set.

ntree

Number of trees to grow. This should not be set to too small a number, to ensure that every input row gets predicted at least a few times.

mtry

Number of variables randomly sampled as candidates at each split. Note that the default values are different for classification (sqrt(p) where p is number of variables in x) and regression (p/3).

replace

Should sampling of cases be done with or without replacement?

classwt

Priors of the classes. Need not add up to one. Ignored for regression.

cutoff

(Classification only) A vector of length equal to number of classes. The ‘winning’ class for an observation is the one with the maximum ratio of proportion of votes to cutoff. Default is 1/k where k is the number of classes (i.e., majority vote wins).

strata

A (factor) variable that is used for stratified sampling.

sampsize

Size(s) of sample to draw. For classification, if sampsize is a vector of the length the number of strata, then sampling is stratified by strata, and the elements of sampsize indicate the numbers to be drawn from the strata.

nodesize

Minimum size of terminal nodes. Setting this number larger causes smaller trees to be grown (and thus take less time). Note that the default values are different for classification (1) and regression (5).

maxnodes

Maximum number of terminal nodes trees in the forest can have. If not given, trees are grown to the maximum possible (subject to limits by nodesize). If set larger than maximum possible, a warning is issued.

importance

Should importance of predictors be assessed?

localImp

Should casewise importance measure be computed? (Setting this to TRUE will override importance.)

nPerm

Number of times the OOB data are permuted per tree for assessing variable importance. Number larger than 1 gives slightly more stable estimate, but not very effective. Currently only implemented for regression.

proximity

Should proximity measure among the rows be calculated?

oob.prox

Should proximity be calculated only on “out-of-bag” data?

norm.votes

If TRUE (default), the final result of votes are expressed as fractions. If FALSE, raw vote counts are returned (useful for combining results from different runs). Ignored for regression.

do.trace

If set to TRUE, give a more verbose output as randomForest is run. If set to some integer, then running output is printed for every do.trace trees.

keep.forest

If set to FALSE, the forest will not be retained in the output object. If xtest is given, defaults to FALSE.

corr.bias

perform bias correction for regression? Note: Experimental. Use at your own risk.

keep.inbag

Should an n by ntree matrix be returned that keeps track of which samples are “in-bag” in which trees (but not how many times, if sampling with replacement)

...

optional parameters to be passed to the low level function cinbag.default.

Value

An object of class randomForest, which is a list with the following components:

call

the original call to randomForest

type

one of regression, classification, or unsupervised.

predicted

the predicted values of the input data based on out-of-bag samples.

importance

a matrix with nclass + 2 (for classification) or two (for regression) columns. For classification, the first nclass columns are the class-specific measures computed as mean descrease in accuracy. The nclass + 1st column is the mean descrease in accuracy over all classes. The last column is the mean decrease in Gini index. For Regression, the first column is the mean decrease in accuracy and the second the mean decrease in MSE. If importance=FALSE, the last measure is still returned as a vector.

importanceSD

The “standard errors” of the permutation-based importance measure. For classification, a p by nclass + 1 matrix corresponding to the first nclass + 1 columns of the importance matrix. For regression, a length p vector.

localImp

a p by n matrix containing the casewise importance measures, the [i,j] element of which is the importance of i-th variable on the j-th case. NULL if localImp=FALSE.

ntree

number of trees grown.

mtry

number of predictors sampled for spliting at each node.

forest

(a list that contains the entire forest; NULL if randomForest is run in unsupervised mode or if keep.forest=FALSE.

err.rate

(classification only) vector error rates of the prediction on the input data, the i-th element being the (OOB) error rate for all trees up to the i-th.

confusion

(classification only) the confusion matrix of the prediction (based on OOB data).

votes

(classification only) a matrix with one row for each input data point and one column for each class, giving the fraction or number of (OOB) ‘votes’ from the random forest.

oob.times

number of times cases are ‘out-of-bag’ (and thus used in computing OOB error estimate)

proximity

if proximity=TRUE when randomForest is called, a matrix of proximity measures among the input (based on the frequency that pairs of data points are in the same terminal nodes).

mse

(regression only) vector of mean square errors: sum of squared residuals divided by n.

rsq

(regression only) “pseudo R-squared”: 1 - mse / Var(y).

test

if test set is given (through the xtest or additionally ytest arguments), this component is a list which contains the corresponding predicted, err.rate, confusion, votes (for classification) or predicted, mse and rsq (for regression) for the test set. If proximity=TRUE, there is also a component, proximity, which contains the proximity among the test set as well as proximity between test and training data.

inbag

An indicator (1 or 0) for each training set row and each tree. The indicator is 1 if the training set row is in the tree's bag and is 0 otherwise. Note that this value is not listed in the original randomForest function's output, although it is implemented.

inbagCount

A count for each training set row and each tree. The count is the number of times the training set row is in the tree's bag. This output is not available in the original randomForest package. The purpose of the cinbag function is to augment the randomForest function so that it returns inbag counts. These counts are necessary for computing and ensembling the trees' empirical cumulative distribution functions.

Note

cinbag's source files call the C functions classRFmod.c and regRFmod.c, which are slightly modified versions of the randomForest's source files classRF.c and regRF.c, respectively.

Author(s)

Yael Grushka-Cockayne, Victor Richmond R. Jose, Kenneth C. Lichtendahl Jr. and Huanghui Zeng, based on the source code from the randomForest package by Andy Liaw and Matthew Wiener and on the original Fortran code by Leo Breiman and Adele Cutler.

References

Breiman L (2001). Random forests. Machine Learning 45 5-32.

Breiman L (2002). Manual on setting up, using, and understanding random forests V3.1. http://oz.berkeley.edu/users/breiman/Using_random_forests_V3.1.pdf.

See Also

trimTrees, hitRate

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# Load the data
set.seed(201) # Can be removed; useful for replication
data <- as.data.frame(mlbench.friedman1(500, sd=1))
summary(data)

# Prepare data for trimming
train <- data[1:400, ]
test <- data[401:500, ]
xtrain <- train[,-11]  
ytrain <- train[,11]
xtest <- test[,-11]
ytest <- test[,11]
      
# Run cinbag
set.seed(201) # Can be removed; useful for replication
rf <- cinbag(xtrain, ytrain, ntree=500, nodesize=5, mtry=3, keep.inbag=TRUE)
rf$inbag[,1] # First tree's inbag indicators 
rf$inbagCount[,1] # First tree's inbag counts

Example output

Loading required package: randomForest
randomForest 4.6-12
Type rfNews() to see new features/changes/bug fixes.
Loading required package: mlbench
      x.1                x.2                 x.3               x.4         
 Min.   :0.001093   Min.   :0.0009745   Min.   :0.00216   Min.   :0.00143  
 1st Qu.:0.263601   1st Qu.:0.2552209   1st Qu.:0.26539   1st Qu.:0.25687  
 Median :0.516836   Median :0.4793052   Median :0.51228   Median :0.53476  
 Mean   :0.505285   Mean   :0.4960016   Mean   :0.50927   Mean   :0.51471  
 3rd Qu.:0.748586   3rd Qu.:0.7622338   3rd Qu.:0.75783   3rd Qu.:0.77337  
 Max.   :0.995597   Max.   :0.9995513   Max.   :0.99795   Max.   :0.99771  
      x.5                x.6                 x.7                x.8          
 Min.   :0.002658   Min.   :0.0007875   Min.   :0.001599   Min.   :0.002436  
 1st Qu.:0.263081   1st Qu.:0.2210349   1st Qu.:0.243378   1st Qu.:0.266648  
 Median :0.519615   Median :0.4380409   Median :0.471095   Median :0.493080  
 Mean   :0.514470   Mean   :0.4694873   Mean   :0.485703   Mean   :0.509182  
 3rd Qu.:0.766236   3rd Qu.:0.7247891   3rd Qu.:0.746310   3rd Qu.:0.786979  
 Max.   :0.999698   Max.   :0.9996735   Max.   :0.999112   Max.   :0.995875  
      x.9                x.10                y          
 Min.   :0.000695   Min.   :0.005041   Min.   : 0.1456  
 1st Qu.:0.247458   1st Qu.:0.291609   1st Qu.:11.0023  
 Median :0.509853   Median :0.500964   Median :14.7441  
 Mean   :0.495805   Mean   :0.500816   Mean   :14.5685  
 3rd Qu.:0.744925   3rd Qu.:0.728643   3rd Qu.:18.1207  
 Max.   :0.998112   Max.   :0.998811   Max.   :27.1957  
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
  1   1   1   1   1   1   0   1   1   1   0   0   1   1   1   1   1   1   1   1 
 21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40 
  0   1   0   0   0   0   1   0   0   1   0   1   1   1   0   1   1   1   0   1 
 41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60 
  0   0   1   0   0   1   0   0   1   1   1   1   1   0   1   1   1   1   0   1 
 61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80 
  0   0   1   0   1   1   1   1   1   1   0   1   1   1   1   0   1   1   0   0 
 81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 
  0   1   0   1   1   1   0   1   1   0   1   1   0   1   0   1   1   1   0   1 
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 
  1   1   0   1   1   1   1   1   1   1   0   1   0   0   1   1   0   1   1   1 
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 
  1   0   1   1   1   1   1   1   1   0   0   0   0   0   0   1   1   1   0   0 
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 
  1   1   1   1   1   1   1   0   0   1   0   1   1   1   1   0   0   1   1   1 
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 
  0   1   1   0   1   0   1   0   1   1   1   0   0   1   0   1   1   0   0   0 
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 
  0   1   1   0   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 
  0   1   0   1   1   0   1   1   1   1   1   1   1   1   1   1   1   1   1   0 
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 
  0   1   0   0   0   1   1   1   0   1   1   1   1   0   1   1   1   1   1   1 
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 
  0   0   1   1   0   1   1   0   1   0   1   1   1   1   0   0   1   1   0   0 
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 
  0   1   0   1   1   1   0   1   1   0   1   1   1   0   1   0   0   0   1   0 
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 
  1   0   1   1   0   1   0   1   0   1   0   0   0   0   1   1   1   1   1   1 
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 
  1   0   1   1   1   1   1   1   0   0   0   1   0   1   1   0   0   0   1   1 
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 
  1   0   0   0   1   0   0   1   1   1   1   0   0   1   1   0   0   1   1   1 
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 
  0   1   1   1   1   0   1   1   1   1   0   1   1   0   1   0   0   1   1   0 
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 
  1   1   0   1   1   0   1   1   1   1   1   0   1   1   1   1   0   0   1   1 
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 
  1   1   0   0   1   1   1   0   1   1   1   1   1   0   0   0   1   0   1   0 
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
  1   1   1   3   1   2   0   1   1   1   0   0   2   2   2   2   1   2   1   1 
 21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40 
  0   2   0   0   0   0   1   0   0   1   0   1   2   1   0   1   2   3   0   1 
 41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60 
  0   0   2   0   0   2   0   0   1   3   1   1   5   0   1   2   1   1   0   2 
 61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80 
  0   0   1   0   1   1   2   1   1   1   0   2   1   2   1   0   2   1   0   0 
 81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 
  0   2   0   2   1   1   0   2   1   0   1   2   0   3   0   1   2   1   0   1 
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 
  1   1   0   2   1   2   1   1   2   2   0   2   0   0   1   1   0   1   3   1 
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 
  2   0   1   1   2   1   2   2   3   0   0   0   0   0   0   1   1   1   0   0 
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 
  1   2   1   2   2   2   3   0   0   1   0   3   1   3   1   0   0   2   1   1 
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 
  0   4   1   0   1   0   1   0   1   3   1   0   0   1   0   1   2   0   0   0 
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 
  0   2   2   0   1   2   1   2   4   1   1   1   1   1   1   1   1   1   1   2 
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 
  0   1   0   1   1   0   1   3   1   1   1   1   3   3   1   1   2   1   1   0 
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 
  0   2   0   0   0   1   1   2   0   3   1   1   1   0   1   2   1   2   1   3 
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 
  0   0   2   1   0   1   2   0   3   0   1   2   1   2   0   0   1   1   0   0 
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 
  0   3   0   1   2   1   0   1   3   0   2   2   3   0   1   0   0   0   1   0 
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 
  2   0   1   1   0   3   0   1   0   1   0   0   0   0   2   1   1   1   1   2 
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 
  2   0   2   3   1   1   2   1   0   0   0   2   0   1   1   0   0   0   3   1 
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 
  1   0   0   0   2   0   0   1   1   1   1   0   0   1   1   0   0   3   1   1 
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 
  0   1   1   1   1   0   1   3   1   2   0   1   1   0   1   0   0   2   1   0 
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 
  2   2   0   2   3   0   1   1   2   1   2   0   2   1   3   1   0   0   1   1 
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 
  1   1   0   0   1   1   2   0   1   2   2   2   3   0   0   0   2   0   1   0 

trimTrees documentation built on May 29, 2017, 7:14 p.m.