predict.rpart             package:rpart             R Documentation

_P_r_e_d_i_c_t_i_o_n_s _f_r_o_m _a _F_i_t_t_e_d _R_p_a_r_t _O_b_j_e_c_t

_D_e_s_c_r_i_p_t_i_o_n:

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

_U_s_a_g_e:

     ## S3 method for class 'rpart':
     predict(object, newdata = list(),
            type = c("vector", "prob", "class", "matrix"),
            na.action = na.pass, ...)

_A_r_g_u_m_e_n_t_s:

  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 the type of predicted value
          returned.  If the 'rpart' object is a classification tree,
          then the default is to return 'prob' predictions, a matrix
          whose columns are the probability of the first, second, etc.
          class.  (This agrees with the default behavior of 'tree'). 
          Otherwise, a vector result is returned. 

na.action: a function to determine what should be done with missing
          values in 'newdata'.  The default is to pass them down the
          tree using surrogates in the way selected when the model was
          built. Other possibilities are 'na.omit' and 'na.fail'. 

     ...: further arguments passed to or from other methods. 

_D_e_t_a_i_l_s:

     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.

_V_a_l_u_e:

     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. For regression trees this is the
     mean response at the node, for Poisson trees it is the estimated
     response rate, and for classification trees it is the predicted
     class (as a number).

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

     If 'type="matrix"':
      a matrix of the full responses ('frame$yval2' if this exists,
     otherwise 'frame$yval'). For regression trees, this is the mean
     response, for Poisson trees it is the response rate and the number
     of events at that node in the fitted tree, and for classification
     trees it is the concatonation of the predicted class, the class
     counts at that node in the fitted tree, and the class
     probabilities.

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

_S_e_e _A_l_s_o:

     'predict',  'rpart.object'

_E_x_a_m_p_l_e_s:

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

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

     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"])

