Learn R Programming

compboost (version 0.1.0)

BaselearnerCustom: Create custom base-learner factory by using R functions.

Description

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, instantiateData, train,
  predict, extractParameter)

Arguments

data_source [Data Object]

Data object which contains the source data.

data_target [Data Object]

Data object which gets the transformed source data.

instantiateData [function]

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

train [function]

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

predict [function]

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

extractParameter [function]

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

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.

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.

Examples

Run this code
# NOT RUN {
# 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,
  instantiateDataFun, trainFun, predictFun, extractParameter)

# Get the transformed data:
custom.lin.factory$getData()

# Summarize factory:
custom.lin.factory$summarizeFactory()

# Transform data manually:
custom.lin.factory$transformData(data.mat)

# }

Run the code above in your browser using DataLab