randomForest {randomForest} | R Documentation |
randomForest
implements Breiman's random forest algorithm (based on
Breiman and Cutler's original Fortran code) for classification and
regression. It can also be used in unsupervised mode for locating
outliers or assessing proximities among data points.
randomForest(x, data=NULL, ..., subset, na.action=na.fail) randomForest(x, y = NULL, xtest = NULL, ytest = NULL, addclass = 0, ntree = 500, mtry = ifelse(!is.null(y) && !is.factor(y), max(floor(ncol(x)/3), 1), floor(sqrt(ncol(x)))), classwt = NULL, cutoff, nodesize = ifelse(!is.null(y) && !is.factor(y), 5, 1), importance = FALSE, proximity = FALSE, outscale = FALSE, norm.votes = TRUE, do.trace = FALSE, keep.forest = is.null(xtest), corr.bias=FALSE, ...) NULL print(x, ...)
data |
an optional data frame containing the variables in the model.
By default the variables are taken from the environment which
randomForest is called from. |
subset |
an index vector indicating which rows should be used. (NOTE: If given, this argument must be named.) |
na.action |
A function to specify the action to be taken if NAs are found. (NOTE: If given, this argument must be named.) |
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 with addclass=1 (unless
explicitly set otherwise). |
xtest |
a data frame or matrix (like x ) containing
predictors for the test set. |
ytest |
response for the test set. |
addclass |
=0 (default) do not add a synthetic class to
the data. =1 label the input data as class 1 and add a
synthetic class by randomly sampling from the product of empirical
marginal distributions of the input. =2 is similar to
=1 , but the synthetic data are sampled from the uniform
hyperrectangle that contain the input. Ignored for regression. |
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 and regression |
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 (positive) difference between proportion of votes and cutoff. Default is 1/k where k is the number of classes (i.e., majority vote wins). |
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 and regression. |
importance |
Should importance of predictors be assessed? |
proximity |
Should proximity measure among the rows be calculated? |
outscale |
Should outlyingness of rows be assessed? Ignored for regression. |
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. |
... |
optional parameters to be passed to the low level function
randomForest.default . |
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 two columns, each a different measure
of importance of the predictors. If
importance=FALSE , the last measure is still returned. This
measure corresponds to average decrease in node impurity caused by
splitting on the variables, and is always computed. |
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 error rate for all trees up to the i-th. |
confusion |
(classification only) the confusion matrix of the prediction. |
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 `votes' from the random forest. |
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). |
outlier |
(classification only) if outscale=TRUE when
randomForest is called, a vector indicating how outlying the
data points are (based on the proximity measures). |
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. |
The forest
structure is slightly different between
classification and regression.
If xtest
is given, prediction of the test set is done ``in
place'' as the trees are grown. If ytest
is also given, and
do.trace
is set to some positive integer, then for every
do.trace
trees, the test set error is printed. Results for the
test set is returned in the test
component of the resulting
randomForest
object.
Here are the definitions of the variable importance measures. For classification, define the margin of a case as the proportion of votes for the correct class minus the maximum proportion of votes for the other classes. The first measure is the percent decrease in average margin when the out-of-bag portion of the data are permuted one variable at a time. The second measure is the total decrease in Gini index in nodes split by the variable, averaged over all trees. For regression, the first measure is the percent increase in mean squared residuals when the out-of-bag portion of the data are permuted one variable at a time. The second measure is the total decrease in sum of squares in nodes split by the variable, averaged over all trees.
Andy Liaw andy_liaw@merck.com and Matthew Wiener matthew_wiener@merck.com, based on original Fortran code by Leo Breiman and Adele Cutler.
Breiman, L. (2001), Random Forests, Machine Learning 45(1), 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.
predict.randomForest
, var.imp.plot
## Classification: data(iris) set.seed(71) iris.rf <- randomForest(Species ~ ., data=iris, importance=TRUE, proximity=TRUE) print(iris.rf) ## Look at variable importance: print(round(iris.rf$importance, 2)) ## Do MDS on 1 - proximity: require(mva) iris.mds <- cmdscale(1 - iris.rf$proximity, eig=TRUE) op <- par(pty="s") pairs(cbind(iris[,1:4], iris.mds$points), cex=0.6, gap=0, col=c("red", "green", "blue")[codes(iris$Species)], main="Iris Data: Predictors and MDS of Proximity Based on RandomForest") par(op) print(iris.mds$GOF) ## The `unsupervised' case: set.seed(17) iris.urf <- randomForest(iris[, -5], proximity=TRUE, outscale=TRUE) ## Look for Outliers: plot(iris.urf$out, type="h", ylab="", main="Measure of Outlyingness for Iris Data") ## Regression: data(airquality) set.seed(131) ozone.rf <- randomForest(Ozone ~ ., data=airquality, mtry=3, importance=TRUE) print(ozone.rf) ## Show "importance" of variables: higher value mean more important: print(round(ozone.rf$importance, 2))