Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

xpred.rpart

Return Cross-Validated Predictions


Description

Gives the predicted values for an rpart fit, under cross validation, for a set of complexity parameter values.

Usage

xpred.rpart(fit, xval = 10, cp, return.all = FALSE)

Arguments

fit

a object of class "rpart".

xval

number of cross-validation groups. This may also be an explicit list of integers that define the cross-validation groups.

cp

the desired list of complexity values. By default it is taken from the cptable component of the fit.

return.all

if FALSE return only the first element of the prediction

Details

Complexity penalties are actually ranges, not values. If the cp values found in the table were .36, .28, and .13, for instance, this means that the first row of the table holds for all complexity penalties in the range [.36, 1], the second row for cp in the range [.28, .36) and the third row for [.13,.28). By default, the geometric mean of each interval is used for cross validation.

Value

A matrix with one row for each observation and one column for each complexity value. If return.all is TRUE and the prediction for each node is a vector, then the result will be an array containing all of the predictions. When the response is categorical, for instance, the result contains the predicted class followed by the class probabilities of the selected terminal node; result[1,,] will be the matrix of predicted classes, result[2,,] the matrix of class 1 probabilities, etc.

See Also

Examples

fit <- rpart(Mileage ~ Weight, car.test.frame)
xmat <- xpred.rpart(fit)
xerr <- (xmat - car.test.frame$Mileage)^2
apply(xerr, 2, sum)   # cross-validated error estimate

# approx same result as rel. error from printcp(fit)
apply(xerr, 2, sum)/var(car.test.frame$Mileage) 
printcp(fit)

rpart

Recursive Partitioning and Regression Trees

v4.1-15
GPL-2 | GPL-3
Authors
Terry Therneau [aut], Beth Atkinson [aut, cre], Brian Ripley [trl] (producer of the initial R port, maintainer 1999-2017)
Initial release
2019-04-10

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.