Learn R Programming

morf (version 1.0.0)

predict.morf: Prediction Method for morf Objects

Description

Prediction method for class morf.

Usage

# S3 method for morf
predict(object, data = NULL, type = "response", ...)

Value

Desired predictions.

Arguments

object

An morf object.

data

Data set of class data.frame. It must contain at least the same covariates used to train the forests. If data is NULL, then object$full_data is used.

type

Type of prediction. Either "response" or "terminalNodes".

...

Further arguments passed to or from other methods.

Author

Riccardo Di Francesco

Details

If type == "response", the routine returns the predicted conditional class probabilities and the predicted class labels. If forests are honest, the predicted probabilities are honest.

If type == "terminalNodes", the IDs of the terminal node in each tree for each observation in data are returned.

See Also

morf, marginal_effects

Examples

Run this code
## Load data from orf package.
set.seed(1986)

library(orf)
data(odata)
odata <- odata[1:200, ] # Subset to reduce elapsed time.

y <- as.numeric(odata[, 1])
X <- as.matrix(odata[, -1])

## Training-test split.
train_idx <- sample(seq_len(length(y)), floor(length(y) * 0.5))

y_tr <- y[train_idx]
X_tr <- X[train_idx, ]

y_test <- y[-train_idx]
X_test <- X[-train_idx, ]

## Fit morf on training sample.
forests <- morf(y_tr, X_tr)

## Predict on test sample.
predictions <- predict(forests, X_test)
head(predictions$probabilities)
predictions$classification

## Get terminal nodes.
predictions <- predict(forests, X_test, type = "terminalNodes")
predictions$forest.1[1:10, 1:20] # Rows are observations, columns are forests.

Run the code above in your browser using DataLab