BaselearnerCustom | R Documentation |
R
functions.This class defines a custom base learner factory by
passing R
functions for instantiation, fitting, and predicting.
data_source |
(InMemoryData) |
instantiate_fun |
( |
train_fun |
( |
predict_fun |
( |
param_fun |
( |
S4 object.
BaselearnerCustom$new(data_source, list(instantiate_fun, train_fun, predict_fun, param_fun))
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 doesn't contain public fields.
$summarizeFactory()
: () -> ()
$transfromData(newdata)
: list(InMemoryData) -> matrix()
$getMeta()
: () -> list()
$getData()
: () -> matrix()
$getDF()
: () -> integer()
$getPenalty()
: () -> numeric()
$getPenaltyMat()
: () -> matrix()
$getFeatureName()
: () -> character()
$getModelName()
: () -> character()
$getBaselearnerId()
: () -> character()
# 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")
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,
list(instantiate_fun = instantiateDataFun, train_fun = trainFun,
predict_fun = predictFun, param_fun = extractParameter))
# Get the transformed data:
custom_lin_factory$getData()
# Summarize factory:
custom_lin_factory$summarizeFactory()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.