rFerns: Classification with random ferns

Description Usage Arguments Value Note References Examples

View source: R/ferns.R

Description

This function builds a random ferns model on the given training data.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
rFerns(x, ...)

## S3 method for class 'formula'
rFerns(formula, data = .GlobalEnv, ...)

## S3 method for class 'matrix'
rFerns(x, y, ...)

## Default S3 method:
rFerns(
  x,
  y,
  depth = 5,
  ferns = 1000,
  importance = "none",
  saveForest = TRUE,
  consistentSeed = NULL,
  threads = 0,
  ...
)

Arguments

x

Data frame containing attributes; must have unique names and contain only numeric, integer or (ordered) factor columns. Factors must have less than 31 levels. No NA values are permitted.

...

For formula and matrix methods, a place to state parameters to be passed to default method. For the print method, arguments to be passed to print.

formula

alternatively, formula describing model to be analysed.

data

in which to interpret formula.

y

A decision vector. Must a factor of the same length as nrow(X) for ordinary many-label classification, or a logical matrix with each column corresponding to a class for multi-label classification.

depth

The depth of the ferns; must be in 1–16 range. Note that time and memory requirements scale with 2^depth.

ferns

Number of ferns to be build.

importance

Set to calculate attribute importance measure (VIM); "simple" will calculate the default mean decrease of true class score (MDTS, something similar to Random Forest's MDA/MeanDecreaseAccuracy), "shadow" will calculate MDTS and additionally MDTS of this attribute shadow, an implicit feature build by shuffling values within it, thus stripping it from information (which is slightly slower). Shadow importance is useful as a reference to judge significance of a regular importance. "none" turns importance calculation off, for a slightly faster execution. For compatibility with pre-1.2 rFerns, TRUE will resolve to "simple" and FALSE to "none". Abbreviation can be used instead of a full value.

saveForest

Should the model be saved? It must be TRUE if you want to use the model for prediction; however, if you are interested in importance or OOB error only, setting it to FALSE significantly improves memory requirements, especially for large depth and ferns.

consistentSeed

PRNG seed used for shadow importance only. Must be either a 2-element integer vector or NULL, which corresponds to seeding from the default PRNG.

threads

Number or OpenMP threads to use. The default value of 0 means all available to OpenMP. It should be set to the same value in two merged models to make shadow importance meaningful.

Value

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

model

The built model; NULL if saveForest was FALSE.

oobErr

OOB approximation of accuracy. Ignores never-OOB-tested objects (see oobScores element).

importance

The importance scores or NULL if importance was set to "none". In a first case it is a data.frame with two or three columns: MeanScoreLoss which is a mean decrease of a score of a correct class when a certain attribute is permuted, Tries which is number of ferns which utilised certain attribute, and, only when importance was set to "shadow", Shadow, which is a mean decrease of accuracy for the correct class for a permuted copy of an attribute (useful as a baseline for normal importance). The rownames are set and equal to the names(x).

oobScores

A matrix of OOB scores of each class for each object in training set. Rows correspond to classes in the same order as in levels(Y). If the ferns is too small, some columns may contain NAs, what means that certain objects were never in test set.

oobPreds

A vector of OOB predictions of class for each object in training set. Never-OOB-tested objects (see above) have predictions equal to NA.

oobConfusionMatrix

Confusion matrix build from oobPreds and y.

timeTaken

Time used to train the model (smaller than wall time because data preparation and model final touches are excluded; however it includes the time needed to compute importance, if it applies). An object of difftime class.

parameters

Numerical vector of three elements: classes, depth and ferns, containing respectively the number of classes in decision and copies of depth and ferns parameters.

classLabels

Copy of levels(Y) after purging unused levels.

consistentSeed

Consistent seed used; only present for importance="shadow". Can be used to seed a new model via consistentSeed argument.

isStruct

Copy of the train set structure, required internally by predict method.

Note

The unused levels of the decision will be removed; on the other hand unused levels of categorical attributes will be preserved, so that they could be present in the data later predicted with the model. The levels of ordered factors in training and predicted data must be identical.

Do not use formula interface for a data with large number of attributes; the overhead from handling the formula may be significant.

References

Ozuysal M, Calonder M, Lepetit V & Fua P. (2009). Fast Keypoint Recognition using Random Ferns, IEEE Transactions on Pattern Analysis and Machine Intelligence, 32(3), 448-461.

Kursa MB (2014). rFerns: An Implementation of the Random Ferns Method for General-Purpose Machine Learning, Journal of Statistical Software, 61(10), 1-13.

Examples

1
2
3
4
5
6
7
8
set.seed(77)
#Fetch Iris data
data(iris)
#Build model
rFerns(Species~.,data=iris)
##Importance
rFerns(Species~.,data=iris,importance="shadow")->model
print(model$imp)

Example output

 Forest of 1000 ferns of a depth 5.

 OOB error 5.33%; OOB confusion matrix:
            True
Predicted    setosa versicolor virginica
  setosa         50          0         0
  versicolor      0         45         3
  virginica       0          5        47
             MeanScoreLoss     Shadow Tries
Sepal.Length     0.2630551 0.05195148   773
Sepal.Width      0.1811833 0.05247208   752
Petal.Length     0.5268230 0.04527552   777
Petal.Width      0.5471982 0.04377684   752

rFerns documentation built on Sept. 22, 2021, 5:10 p.m.

Related to rFerns in rFerns...