BaselearnerCustom creates a custom base-learner factory by setting custom R functions. This factory object can be registered within a base-learner list and then used for training.

Format

S4 object.

Usage

BaselearnerCustom$new(data_source, data_target, list(instantiate_fun,
  train_fun, predict_fun, param_fun))

Arguments

data_source [Data Object]

Data object which contains the source data.

data_target [Data Object]

Data object which gets the transformed source data.

instantiate_fun [function]

R function to transform the source data. For details see the Details.

train_fun [function]

R function to train the base-learner on the target data. For details see the Details.

predict_fun [function]

R function to predict on the object returned by train. For details see the Details.

param_fun [function]

R function to extract the parameter of the object returned by train. For details see the Details.

Details

The function must have the following structure:

instantiateData(X) { ... return (X_trafo) } With a matrix argument X and a matrix as return object.

train(y, X) { ... return (SEXP) } With a vector argument y and a matrix argument X. The target data is used in X while y contains the response. The function can return any R object which is stored within a SEXP.

predict(model, newdata) { ... return (prediction) } The returned object of the train function is passed to the model argument while newdata contains a new matrix used for predicting.

extractParameter() { ... return (parameters) } Again, model contains the object returned by train. The returned object must be a matrix containing the estimated parameter. If no parameter should be estimated one can return NA.

For an example see the Examples.

This class is a wrapper around the pure C++ implementation. To see the functionality of the C++ class visit https://schalkdaniel.github.io/compboost/cpp_man/html/classblearnerfactory_1_1_custom_blearner_factory.html.

Fields

This class doesn't contain public fields.

Methods

getData()

Get the data matrix of the target data which is used for modeling.

transformData(X)

Transform a data matrix as defined within the factory. The argument has to be a matrix with one column.

summarizeFactory()

Summarize the base-learner factory object.

Examples

# Sample data: data_mat = cbind(1, 1:10) y = 2 + 3 * 1:10 # Create new data object: data_source = InMemoryData$new(data_mat, "my_data_name") data_target = InMemoryData$new() instantiateDataFun = function (X) { return(X) } # Ordinary least squares estimator: trainFun = function (y, X) { return(solve(t(X) %*% X) %*% t(X) %*% y) } predictFun = function (model, newdata) { return(as.matrix(newdata %*% model)) } extractParameter = function (model) { return(as.matrix(model)) } # Create new custom linear base-learner factory: custom_lin_factory = BaselearnerCustom$new(data_source, data_target, list(instantiate_fun = instantiateDataFun, train_fun = trainFun, predict_fun = predictFun, param_fun = extractParameter)) # Get the transformed data: custom_lin_factory$getData()
#> [,1] [,2] #> [1,] 1 1 #> [2,] 1 2 #> [3,] 1 3 #> [4,] 1 4 #> [5,] 1 5 #> [6,] 1 6 #> [7,] 1 7 #> [8,] 1 8 #> [9,] 1 9 #> [10,] 1 10
# Summarize factory: custom_lin_factory$summarizeFactory()
#> Custom base-learner Factory: #> - Name of the used data: my_data_name #> - Factory creates the following base-learner: custom
# Transform data manually: custom_lin_factory$transformData(data_mat)
#> [,1] [,2] #> [1,] 1 1 #> [2,] 1 2 #> [3,] 1 3 #> [4,] 1 4 #> [5,] 1 5 #> [6,] 1 6 #> [7,] 1 7 #> [8,] 1 8 #> [9,] 1 9 #> [10,] 1 10