Skip to contents

Generate predictions on new data using a fitted Treee model.

Usage

# S3 method for class 'Treee'
predict(object, newdata, type = c("response", "prob", "all"), ...)

Arguments

object

A fitted model object of class Treee, typically the result of the Treee() function.

newdata

A data frame containing the predictor variables. Missing values are allowed and will be handled according to the fitted tree's method for handling missing data.

type

A character string specifying the type of prediction to return. Options are:

  • 'response': returns the predicted class for each observation (default).

  • 'prob': returns a data frame of posterior probabilities for each class.

  • 'all': returns a data frame containing predicted classes, posterior probabilities, and the predicted node indices.

...

Additional arguments passed to or from other methods.

Value

Depending on the value of type, the function returns:

  • If type = 'response': A character vector of predicted class labels.

  • If type = 'prob': A data frame of posterior probabilities, where each class has its own column.

  • If type = 'all': A data frame containing predicted class labels, posterior probabilities, and the predicted node indices.

Note: For factor predictors, if a level not present in the training data is found in newdata, it will be treated as missing and handled according to the missingMethod specified in the fitted tree.

Examples

fit <- Treee(datX = iris[, -5], response = iris[, 5], verbose = FALSE)
head(predict(fit, iris)) # Predicted classes
#> [1] "setosa" "setosa" "setosa" "setosa" "setosa" "setosa"
head(predict(fit, iris[, -5], type = "prob")) # Posterior probabilities
#>   setosa   versicolor    virginica
#> 1      1 1.425464e-20 2.261436e-39
#> 2      1 3.835613e-16 7.404471e-34
#> 3      1 1.428019e-18 6.243052e-37
#> 4      1 2.264688e-16 4.310477e-34
#> 5      1 1.853449e-21 1.783867e-40
#> 6      1 7.337925e-20 1.553330e-37
head(predict(fit, iris[, -5], type = "all")) # Full details
#>   response node setosa   versicolor    virginica
#> 1   setosa    1      1 1.425464e-20 2.261436e-39
#> 2   setosa    1      1 3.835613e-16 7.404471e-34
#> 3   setosa    1      1 1.428019e-18 6.243052e-37
#> 4   setosa    1      1 2.264688e-16 4.310477e-34
#> 5   setosa    1      1 1.853449e-21 1.783867e-40
#> 6   setosa    1      1 7.337925e-20 1.553330e-37