predict.tree: Predictions from a Fitted Tree Object

Description Usage Arguments Details Value References See Also Examples

Description

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

Usage

1
2
3
4
## S3 method for class 'tree'
predict(object, newdata = list(),
        type = c("vector", "tree", "class", "where"), 
        split = FALSE, nwts, eps = 1e-3, ...)

Arguments

object

fitted model object of class tree. This is assumed to be the result of some function that produces an object with the same named components as that returned by the tree 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, fitted values are returned.

type

character string denoting whether the predictions are returned as a vector (default) or as a tree object.

split

governs the handling of missing values. If false, cases with missing values are dropped down the tree until a leaf is reached or a node for which the attribute is missing, and that node is used for prediction. If split = TRUE cases with missing attributes are split into fractional cases and dropped down each side of the split. The predicted values are averaged over the fractions to give the prediction.

nwts

weights for the newdata cases, used when predicting a tree.

eps

a lower bound for the probabilities, used if events of predicted probability zero occur in newdata when predicting a tree.

...

further arguments passed to or from other methods.

Details

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

Value

If type = "vector": vector of predicted responses or, if the response is a factor, matrix of predicted class probabilities. This new object is obtained by dropping newdata down 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 or frame$yprob at that node is the prediction.

If type = "tree": an object of class "tree" is returned with new values for frame$n and frame$dev. If newdata does not contain a column for the response in the formula the value of frame$dev will be NA, and if some values in the response are missing, the some of the deviances will be NA.

If type = "class": for a classification tree, a factor of the predicted classes (that with highest posterior probability, with ties split randomly).

If type = "where": the nodes the cases reach.

References

Ripley, B. D. (1996). Pattern Recognition and Neural Networks. Cambridge University Press, Cambridge. Chapter 7.

See Also

predict, tree.

Examples

1
2
3
4
5
6
data(shuttle, package="MASS")
shuttle.tr <- tree(use ~ ., shuttle, subset=1:253,
                   mindev=1e-6, minsize=2)
shuttle.tr
shuttle1 <- shuttle[254:256, ]  # 3 missing cases
predict(shuttle.tr, shuttle1)

Example output

node), split, n, deviance, yval, (yprob)
      * denotes terminal node

  1) root 253 345.300 auto ( 0.5731 0.4269 )  
    2) vis: no 128   0.000 auto ( 1.0000 0.0000 ) *
    3) vis: yes 125  99.410 noauto ( 0.1360 0.8640 )  
      6) stability: stab 61  72.190 noauto ( 0.2787 0.7213 )  
       12) error: MM,SS 29  39.340 auto ( 0.5862 0.4138 )  
         24) magn: Light,Medium,Strong 21  20.450 auto ( 0.8095 0.1905 )  
           48) error: MM 9  12.370 auto ( 0.5556 0.4444 )  
             96) sign: nn 3   0.000 noauto ( 0.0000 1.0000 ) *
             97) sign: pp 6   5.407 auto ( 0.8333 0.1667 )  
              194) magn: Light,Medium 4   0.000 auto ( 1.0000 0.0000 ) *
              195) magn: Strong 2   2.773 auto ( 0.5000 0.5000 )  
                390) wind: head 1   0.000 noauto ( 0.0000 1.0000 ) *
                391) wind: tail 1   0.000 auto ( 1.0000 0.0000 ) *
           49) error: SS 12   0.000 auto ( 1.0000 0.0000 ) *
         25) magn: Out 8   0.000 noauto ( 0.0000 1.0000 ) *
       13) error: LX,XL 32   0.000 noauto ( 0.0000 1.0000 ) *
      7) stability: xstab 64   0.000 noauto ( 0.0000 1.0000 ) *
    auto noauto
254    0      1
255    0      1
256    0      1

tree documentation built on May 2, 2019, 9:24 a.m.

Related to predict.tree in tree...