predict.mml {ocf}R Documentation

Prediction Method for mml Objects

Description

Prediction method for class mml.

Usage

## S3 method for class 'mml'
predict(object, data = NULL, ...)

Arguments

object

An mml object.

data

Data set of class data.frame. It must contain the same covariates used to train the base learners. If data is NULL, then object$X is used.

...

Further arguments passed to or from other methods.

Details

If object$learner == "l1", then model.matrix is used to handle non-numeric covariates. If we also have object$scaling == TRUE, then data is scaled to have zero mean and unit variance.

Value

Matrix of predictions.

Author(s)

Riccardo Di Francesco

See Also

multinomial_ml, ordered_ml

Examples


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

library(orf)
data(odata)
odata <- odata[1:100, ] # 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 multinomial machine learning on training sample using two different learners.
multinomial_forest <- multinomial_ml(y_tr, X_tr, learner = "forest")
multinomial_l1 <- multinomial_ml(y_tr, X_tr, learner = "l1")

## Predict out of sample.
predictions_forest <- predict(multinomial_forest, X_test)
predictions_l1 <- predict(multinomial_l1, X_test)

## Compare predictions.
cbind(head(predictions_forest), head(predictions_l1))


[Package ocf version 1.0.0 Index]