BaselearnerPSpline | R Documentation |
BaselearnerPSpline creates a spline base learner object. The object calculates the B-spline basis functions and in the case of P-splines also the penalty. Instead of defining the penalty term directly, one should consider to restrict the flexibility by setting the degrees of freedom.
data_source |
(InMemoryData) |
blearner_type |
( |
degree |
( |
n_knots |
( |
penalty |
( |
differences |
( |
df |
( |
bin_root |
( |
S4 object.
BaselearnerPSpline$new(data_source, list(degree, n_knots, penalty, differences, df, bin_root)) BaselearnerPSpline$new(data_source, blearner_type, list(degree, n_knots, penalty, differences, df, bin_root))
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()
The data matrix is instantiated as transposed sparse matrix due to performance
reasons. The member function $getData()
accounts for that while $transformData()
returns the raw data matrix as p x n matrix.
# Sample data:
x = runif(100, 0, 10)
y = sin(x) + rnorm(100, 0, 0.2)
dat = data.frame(x, y)
# S4 wrapper
# Create new data object, a matrix is required as input:
data_mat = cbind(x)
data_source = InMemoryData$new(data_mat, "my_data_name")
# Create new linear base learner factory:
bl_sp_df2 = BaselearnerPSpline$new(data_source,
list(n_knots = 10, df = 2, bin_root = 2))
bl_sp_df5 = BaselearnerPSpline$new(data_source,
list(n_knots = 15, df = 5))
# Get the transformed data:
dim(bl_sp_df2$getData())
dim(bl_sp_df5$getData())
# Summarize factory:
bl_sp_df2$summarizeFactory()
# Get full meta data such as penalty term or matrix as well as knots:
str(bl_sp_df2$getMeta())
bl_sp_df2$getPenalty()
bl_sp_df5$getPenalty() # The penalty here is smaller due to more flexibility
# Transform "new data":
newdata = list(InMemoryData$new(cbind(rnorm(5)), "my_data_name"))
bl_sp_df2$transformData(newdata)
bl_sp_df5$transformData(newdata)
# R6 wrapper
cboost_df2 = Compboost$new(dat, "y")
cboost_df2$addBaselearner("x", "sp", BaselearnerPSpline,
n_knots = 10, df = 2, bin_root = 2)
cboost_df2$train(200, 0)
cboost_df5 = Compboost$new(dat, "y")
cboost_df5$addBaselearner("x", "sp", BaselearnerPSpline,
n_knots = 15, df = 5)
cboost_df5$train(200, 0)
# Access base learner directly from the API (n = sqrt(100) = 10 with binning):
str(cboost_df2$baselearner_list$x_sp$factory$getData())
str(cboost_df5$baselearner_list$x_sp$factory$getData())
gg_df2 = plotPEUni(cboost_df2, "x")
gg_df5 = plotPEUni(cboost_df5, "x")
library(ggplot2)
library(patchwork)
(gg_df2 | gg_df5) &
geom_point(data = dat, aes(x = x, y = y - c(cboost_df2$offset)), alpha = 0.2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.