Learn R Programming

rpart (version 3.0-2)

predict.rpart: Predictions from a Fitted Rpart Object

Description

Returns a vector of predicted responses from a fitted rpart object.

Usage

## S3 method for class 'rpart':
predict(object, newdata=list(),
       type=c("vector", "matrix", "class", "probs"))

Arguments

object
fitted model object of class rpart. This is assumed to be the result of some function that produces an object with the same named components as that returned by the rpart function.
newdata
data frame containing the values at which predictions are required. The predictors referred to in the right side of formula(object) must be present by name in newdata. If missing, the fitted values are returned.
type
character string denoting whether the predictions are returned as a vector (default except for classifcation trees) or as an rpart object, or as a matrix or (for classification trees) a factor of classes or a matrix of class

Value

  • A new object is obtained by dropping newdata down the object. For factor predictors, if an observation contains a level not used to grow the tree, it is left at the deepest possible node and frame$yval at the node is the prediction.

    If type="vector": vector of predicted responses. If type="matrix": a matrix of the full responses (frame$yval2 if this exists, otherwise frame$yval). For a Poisson tree this is (event rate, #events), and for a classification tree it is (predicted class, class counts, class probabilities).

    If type="class": (for a classification tree) a factor of classifications based on the responses.

    If type="probs": (for a classification tree) a matrix of class probabilities.

Details

This function is a method for the generic function predict for class rpart. It can be invoked by calling predict for an object of the appropriate class, or directly by calling predict.rpart regardless of the class of the object.

See Also

predict, rpart.object

Examples

Run this code
data(car.test.frame)
z.auto <- rpart(Mileage ~ Weight, car.test.frame)
predict(z.auto)

data(kyphosis)
fit <- rpart(Kyphosis ~ Age + Number + Start, data=kyphosis)
predict(fit, type="vector") # level numbers
predict(fit, type="class")  # factor
predict(fit, type="probs")  # class probabilities (default)
predict(fit, type="matrix") # level number, class frequencies, probabilities

data(iris)
sub <- c(sample(1:50, 25), sample(51:100, 25), sample(101:150, 25))
fit <- rpart(Species ~ ., data=iris, subset=sub)
fit
table(predict(fit, iris[-sub,], type="class"), iris[-sub, "Species"])

Run the code above in your browser using DataLab