1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
\name{randomForest}
\alias{randomForest}
\alias{randomForest.formula}
\alias{randomForest.default}
\alias{print.randomForest}
\title{Classification and Regression with Random Forest}
\description{
\code{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 assessing
proximities among data points.
}
\usage{
\method{randomForest}{formula}(formula, data=NULL, ..., subset, na.action=na.fail)
\method{randomForest}{default}(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))),
weights=NULL,
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, ...)
\method{print}{randomForest}(x, ...)
}
\arguments{
\item{data}{an optional data frame containing the variables in the model.
By default the variables are taken from the environment which
\code{randomForest} is called from.}
\item{subset}{an index vector indicating which rows should be used.
(NOTE: If given, this argument must be named.)}
\item{na.action}{A function to specify the action to be taken if NAs
are found. (NOTE: If given, this argument must be named.)}
\item{x, formula}{a data frame or a matrix of predictors, or a formula
describing the model to be fitted (for the
\code{print} method, an \code{randomForest} object).}
\item{y}{A response vector. If a factor, classification is assumed,
otherwise regression is assumed. If omitted, \code{randomForest}
will run in unsupervised mode.}
\item{xtest}{a data frame or matrix (like \code{x}) containing
predictors for the test set.}
\item{ytest}{response for the test set.}
\item{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. }
\item{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 \code{x})
and regression (p/3)}
\item{weights}{A vector of length same as \code{y} that are positive
weights used only in sampling data to grow each tree (not used in any
other calculation)}
\item{replace}{Should sampling of cases be done with or without
replacement?}
\item{classwt}{Priors of the classes. Need not add up to one.
Ignored for regression.}
\item{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).}
\item{strata}{A (factor) variable that is used for stratified sampling.}
\item{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.}
\item{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).}
\item{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 \code{nodesize}). If set larger than maximum
possible, a warning is issued.}
\item{importance}{Should importance of predictors be assessed? }
\item{localImp}{Should casewise importance measure be computed?
(Setting this to \code{TRUE} will override \code{importance}.) }
\item{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.}
\item{proximity}{Should proximity measure among the rows be
calculated?}
\item{oob.prox}{Should proximity be calculated only on ``out-of-bag''
data?}
\item{norm.votes}{If \code{TRUE} (default), the final result of votes
are expressed as fractions. If \code{FALSE}, raw vote counts are
returned (useful for combining results from different runs).
Ignored for regression.}
\item{do.trace}{If set to \code{TRUE}, give a more verbose output as
\code{randomForest} is run. If set to some integer, then running
output is printed for every \code{do.trace} trees.}
\item{keep.forest}{If set to \code{FALSE}, the forest will not be
retained in the output object. If \code{xtest} is given, defaults
to \code{FALSE}.}
\item{corr.bias}{perform bias correction for regression? Note:
Experimental. Use at your own risk.}
\item{keep.inbag}{Should an \code{n} by \code{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)}
\item{...}{optional parameters to be passed to the low level function
\code{randomForest.default}.}
}
\value{
An object of class \code{randomForest}, which is a list with the
following components:
\item{call}{the original call to \code{randomForest}}
\item{type}{one of \code{regression}, \code{classification}, or
\code{unsupervised}.}
\item{predicted}{the predicted values of the input data based on
out-of-bag samples.}
\item{importance}{a matrix with \code{nclass} + 2 (for classification)
or two (for regression) columns. For classification, the first
\code{nclass} columns are the class-specific measures computed as
mean descrease in accuracy. The \code{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 \code{importance=FALSE}, the last measure is still returned as a
vector.}
\item{importanceSD}{The ``standard errors'' of the permutation-based
importance measure. For classification, a \code{p} by \code{nclass
+ 1} matrix corresponding to the first \code{nclass + 1} columns
of the importance matrix. For regression, a length \code{p} vector.}
\item{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. \code{NULL} if \code{localImp=FALSE}.}
\item{ntree}{number of trees grown.}
\item{mtry}{number of predictors sampled for spliting at each node.}
\item{forest}{(a list that contains the entire forest; \code{NULL} if
\code{randomForest} is run in unsupervised mode or if
\code{keep.forest=FALSE}.}
\item{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.}
\item{confusion}{(classification only) the confusion matrix of the
prediction (based on OOB data).}
\item{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.}
\item{oob.times}{number of times cases are `out-of-bag' (and thus used
in computing OOB error estimate)}
\item{proximity}{if \code{proximity=TRUE} when
\code{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).}
\item{mse}{(regression only) vector of mean square errors: sum of squared
residuals divided by \code{n}.}
\item{rsq}{(regression only) ``pseudo R-squared'': 1 - \code{mse} /
Var(y).}
\item{test}{if test set is given (through the \code{xtest} or additionally
\code{ytest} arguments), this component is a list which contains the
corresponding \code{predicted}, \code{err.rate}, \code{confusion},
\code{votes} (for classification) or \code{predicted}, \code{mse} and
\code{rsq} (for regression) for the test set. If
\code{proximity=TRUE}, there is also a component, \code{proximity},
which contains the proximity among the test set as well as proximity
between test and training data.}
}
\note{
The \code{forest} structure is slightly different between
classification and regression. For details on how the trees are
stored, see the help page for \code{\link{getTree}}.
If \code{xtest} is given, prediction of the test set is done ``in
place'' as the trees are grown. If \code{ytest} is also given, and
\code{do.trace} is set to some positive integer, then for every
\code{do.trace} trees, the test set error is printed. Results for the
test set is returned in the \code{test} component of the resulting
\code{randomForest} object. For classification, the \code{votes}
component (for training or test set data) contain the votes the cases
received for the classes. If \code{norm.votes=TRUE}, the fraction is
given, which can be taken as predicted probabilities for the classes.
For large data sets, especially those with large number of variables,
calling \code{randomForest} via the formula interface is not advised:
There may be too much overhead in handling the formula.
The ``local'' (or casewise) variable importance is computed as
follows: For classification, it is the increase in percent of times a
case is OOB and misclassified when the variable is permuted. For
regression, it is the average increase in squared OOB residuals when
the variable is permuted.
}
\references{
Breiman, L. (2001), \emph{Random Forests}, Machine Learning 45(1),
5-32.
Breiman, L (2002), ``Manual On Setting Up, Using, And Understanding
Random Forests V3.1'', \url{https://www.stat.berkeley.edu/~breiman/Using_random_forests_V3.1.pdf}.
}
\author{Andy Liaw \email{andy_liaw@merck.com} and Matthew Wiener
\email{matthew_wiener@merck.com}, based on original Fortran code by
Leo Breiman and Adele Cutler.}
\seealso{\code{\link{predict.randomForest}}, \code{\link{varImpPlot}}}
\examples{
## Classification:
##data(iris)
set.seed(71)
iris.rf <- randomForest(Species ~ ., data=iris, importance=TRUE,
proximity=TRUE)
print(iris.rf)
## Look at variable importance:
round(importance(iris.rf), 2)
## Do MDS on 1 - proximity:
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")[as.numeric(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])
MDSplot(iris.urf, iris$Species)
## stratified sampling: draw 20, 30, and 20 of the species to grow each tree.
(iris.rf2 <- randomForest(iris[1:4], iris$Species,
sampsize=c(20, 30, 20)))
## Regression:
## data(airquality)
set.seed(131)
ozone.rf <- randomForest(Ozone ~ ., data=airquality, mtry=3,
importance=TRUE, na.action=na.omit)
print(ozone.rf)
## Show "importance" of variables: higher value mean more important:
round(importance(ozone.rf), 2)
## "x" can be a matrix instead of a data frame:
set.seed(17)
x <- matrix(runif(5e2), 100)
y <- gl(2, 50)
(myrf <- randomForest(x, y))
(predict(myrf, x))
## "complicated" formula:
(swiss.rf <- randomForest(sqrt(Fertility) ~ . - Catholic + I(Catholic < 50),
data=swiss))
(predict(swiss.rf, swiss))
## Test use of 32-level factor as a predictor:
set.seed(1)
x <- data.frame(x1=gl(53, 10), x2=runif(530), y=rnorm(530))
(rf1 <- randomForest(x[-3], x[[3]], ntree=10))
## Grow no more than 4 nodes per tree:
(treesize(randomForest(Species ~ ., data=iris, maxnodes=4, ntree=30)))
## test proximity in regression
iris.rrf <- randomForest(iris[-1], iris[[1]], ntree=101, proximity=TRUE, oob.prox=FALSE)
str(iris.rrf$proximity)
## Using weights: make versicolors having 3 times larger weights
iris_wt <- ifelse( iris$Species == "versicolor", 3, 1 )
set.seed(15)
iris.wcrf <- randomForest(iris[-5], iris[[5]], weights=iris_wt, keep.inbag=TRUE)
print(rowSums(iris.wcrf$inbag))
set.seed(15)
iris.wrrf <- randomForest(iris[-1], iris[[1]], weights=iris_wt, keep.inbag=TRUE)
print(rowSums(iris.wrrf$inbag))
}
\keyword{classif}% at least one, from doc/KEYWORDS
\keyword{regression}
\keyword{tree}
|