## 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