Skip to contents

This class is the raw C++ pendant and still at a very high-level. It is the base for the Compboost R6 class and provides many convenient wrapper to access data and execute methods by calling the C++ methods.

Format

S4 object.

Arguments

oob_response

(ResponseRegr | ResponseBinaryClassif)
The response object containing the target variable.

learning_rate

(numeric(1))
The learning rate.

stop_if_all_stopper_fulfilled

(logical(1))
Boolean to indicate which stopping strategy is used. If TRUE, the algorithm stops if the conditions of all loggers for stopping apply.

factory_list

(BlearnerFactoryList)
List of base learner factories from which one base learner is selected in each iteration by using the

loss

(LossQuadratic | LossBinomial | LossHuber | LossAbsolute | LossQuantile)
An initialized S4 loss object (requires to call Loss*$new(...)). See the respective help page for further information.

logger_list

(LoggerList)
The LoggerList object with all loggers.

optimizer

(OptimizerCoordinateDescent | OptimizerCoordinateDescentLineSearch | OptimizerAGBM | OptimizerCosineAnnealing)
An initialized S4 optimizer object (requires to call Optimizer*.new(..). See the respective help page for further information.

Usage


Compboost$new(response, learning_rate, stop_if_all_stopper_fulfilled,
  factory_list, loss, logger_list, optimizer)

Fields

This class doesn't contain public fields.

Methods

  • $train(): () -> ()

  • $continueTraining(): () -> ()

  • $getLearningRate(): () -> numeric(1)

  • $getPrediction(): () -> matrix()

  • $getSelectedBaselearner(): () -> character()

  • $getLoggerData(): () -> list(character(), matrix())

  • $getEstimatedParameter(): () -> list(matrix())

  • $getParameterAtIteration(): () -> list(matrix())

  • $getParameterMatrix(): () -> matrix()

  • $predictFactoryTrainData(): () -> matrix()

  • $predictFactoryNewData(): list(Data*) -> matrix()

  • $predictIndividualTrainData(): () -> list(matrix()) Get the linear contribution of each base learner for the training data.

  • $predictIndividual(): list(Data*) -> list(matrix()) Get the linear contribution of each base learner for new data.

  • $predict(): list(Data*), logical(1) -> matrix()

  • $summarizeCompboost(): () -> ()

  • $isTrained(): () -> logical(1)

  • $setToIteration(): () -> ()

  • $saveJson(): () -> ()

  • $getOffset(): () -> numeric(1) | matrix()

  • $getRiskVector(): () -> numeric()

  • $getResponse(): () -> Response*

  • $getOptimizer(): () -> Optimizer*

  • $getLoss(): () -> Loss*

  • $getLoggerList(): () -> LoggerList

  • $getBaselearnerList(): () -> BlearnerFactoryList

  • $useGlobalStopping(): () -> logical(1)*

  • $getFactoryMap(): () -> list(Baselearner*)

  • $getDataMap(): () -> list(Data*)

Examples


# Some data:
df = mtcars
df$mpg_cat = ifelse(df$mpg > 20, "high", "low")

# # Create new variable to check the polynomial base learner with degree 2:
# df$hp2 = df[["hp"]]^2

# Data for the baselearner are matrices:
X_hp = as.matrix(df[["hp"]])
X_wt = as.matrix(df[["wt"]])

# Target variable:
response = ResponseBinaryClassif$new("mpg_cat", "high", df[["mpg_cat"]])

data_source_hp = InMemoryData$new(X_hp, "hp")
data_source_wt = InMemoryData$new(X_wt, "wt")

# List for oob logging:
oob_data = list(data_source_hp, data_source_wt)

# List to test prediction on newdata:
test_data = oob_data

# Factories:
linear_factory_hp = BaselearnerPolynomial$new(data_source_hp,
  list(degree = 1, intercept = TRUE))
linear_factory_wt = BaselearnerPolynomial$new(data_source_wt,
  list(degree = 1, intercept = TRUE))
quadratic_factory_hp = BaselearnerPolynomial$new(data_source_hp,
  list(degree = 2, intercept = TRUE))
spline_factory_wt = BaselearnerPSpline$new(data_source_wt,
  list(degree = 3, n_knots = 10, penalty = 2, differences = 2))

# Create new factory list:
factory_list = BlearnerFactoryList$new()

# Register factories:
factory_list$registerFactory(linear_factory_hp)
factory_list$registerFactory(linear_factory_wt)
factory_list$registerFactory(quadratic_factory_hp)
factory_list$registerFactory(spline_factory_wt)

# Define loss:
loss_bin = LossBinomial$new()

# Define optimizer:
optimizer = OptimizerCoordinateDescent$new()

## Logger

# Define logger. We want just the iterations as stopper but also track the
# time, inbag risk and oob risk:
log_iterations  = LoggerIteration$new(" iteration_logger", TRUE, 500)
log_time        = LoggerTime$new("time_logger", FALSE, 500, "microseconds")

# Define new logger list:
logger_list = LoggerList$new()

# Register the logger:
logger_list$registerLogger(log_iterations)
logger_list$registerLogger(log_time)

# Run compboost:
# --------------

# Initialize object:
cboost = Compboost_internal$new(
  response      = response,
  learning_rate = 0.05,
  stop_if_all_stopper_fulfilled = FALSE,
  factory_list = factory_list,
  loss         = loss_bin,
  logger_list  = logger_list,
  optimizer    = optimizer
)

# Train the model (we want to print the trace):
cboost$train(trace = 50)
#>   1/500   risk = 0.68  time_logger = 0   
#>  50/500   risk = 0.42  time_logger = 635   
#> 100/500   risk = 0.31  time_logger = 1288   
#> 150/500   risk = 0.25  time_logger = 2056   
#> 200/500   risk = 0.21  time_logger = 2914   
#> 250/500   risk = 0.19  time_logger = 3897   
#> 300/500   risk = 0.17  time_logger = 4933   
#> 350/500   risk = 0.16  time_logger = 6079   
#> 400/500   risk = 0.15  time_logger = 7334   
#> 450/500   risk = 0.14  time_logger = 8631   
#> 500/500   risk = 0.13  time_logger = 10002   
#> 
#> 
#> Train 500 iterations in 0 Seconds.
#> Final risk based on the train set: 0.13
#> 
cboost
#> 
#> Compboost object with:
#> 	- Learning Rate: 0.05
#> 	- Are all logger used as stopper: 0
#> 	- Model is already trained with 500 iterations/fitted baselearner
#> 	- Actual state is at iteration 500
#> 
#> 
#> 

# Get estimated parameter:
cboost$getEstimatedParameter()
#> $hp_poly2
#>               [,1]
#> [1,]  6.0660349854
#> [2,] -0.0645193461
#> [3,]  0.0001267875
#> 
#> $wt_spline_degree_3
#>             [,1]
#>  [1,]  1.9370515
#>  [2,]  1.8485620
#>  [3,]  1.8081122
#>  [4,]  1.8647942
#>  [5,]  1.7149991
#>  [6,]  1.0827258
#>  [7,] -0.4203328
#>  [8,] -1.8130157
#>  [9,] -2.0641036
#> [10,] -2.0050272
#> [11,] -1.8182042
#> [12,] -1.5569128
#> [13,] -1.2730290
#> [14,] -0.9904981
#> 

# Get trace of selected base learner:
cboost$getSelectedBaselearner()
#>   [1] "wt_spline_degree_3" "wt_spline_degree_3" "wt_spline_degree_3"
#>   [4] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#>   [7] "hp_poly2"           "wt_spline_degree_3" "wt_spline_degree_3"
#>  [10] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#>  [13] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#>  [16] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#>  [19] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#>  [22] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#>  [25] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#>  [28] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#>  [31] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#>  [34] "wt_spline_degree_3" "wt_spline_degree_3" "hp_poly2"          
#>  [37] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#>  [40] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#>  [43] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#>  [46] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#>  [49] "wt_spline_degree_3" "wt_spline_degree_3" "hp_poly2"          
#>  [52] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#>  [55] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#>  [58] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#>  [61] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#>  [64] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#>  [67] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#>  [70] "wt_spline_degree_3" "wt_spline_degree_3" "hp_poly2"          
#>  [73] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#>  [76] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#>  [79] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#>  [82] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#>  [85] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#>  [88] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#>  [91] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#>  [94] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#>  [97] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [100] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [103] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [106] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [109] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [112] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [115] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [118] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [121] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [124] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [127] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [130] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [133] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [136] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [139] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [142] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [145] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [148] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [151] "wt_spline_degree_3" "hp_poly2"           "hp_poly2"          
#> [154] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [157] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [160] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [163] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [166] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [169] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [172] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [175] "hp_poly2"           "hp_poly2"           "wt_spline_degree_3"
#> [178] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [181] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [184] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [187] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [190] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [193] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [196] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [199] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [202] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [205] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [208] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [211] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [214] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [217] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [220] "hp_poly2"           "hp_poly2"           "wt_spline_degree_3"
#> [223] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [226] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [229] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [232] "wt_spline_degree_3" "hp_poly2"           "hp_poly2"          
#> [235] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [238] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [241] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [244] "hp_poly2"           "hp_poly2"           "wt_spline_degree_3"
#> [247] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [250] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [253] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [256] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [259] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [262] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [265] "wt_spline_degree_3" "hp_poly2"           "hp_poly2"          
#> [268] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [271] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [274] "wt_spline_degree_3" "hp_poly2"           "hp_poly2"          
#> [277] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [280] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [283] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [286] "hp_poly2"           "hp_poly2"           "wt_spline_degree_3"
#> [289] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [292] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [295] "hp_poly2"           "hp_poly2"           "wt_spline_degree_3"
#> [298] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [301] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [304] "hp_poly2"           "hp_poly2"           "wt_spline_degree_3"
#> [307] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [310] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [313] "hp_poly2"           "hp_poly2"           "wt_spline_degree_3"
#> [316] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [319] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [322] "hp_poly2"           "hp_poly2"           "wt_spline_degree_3"
#> [325] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [328] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [331] "hp_poly2"           "hp_poly2"           "wt_spline_degree_3"
#> [334] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [337] "wt_spline_degree_3" "hp_poly2"           "hp_poly2"          
#> [340] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [343] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [346] "wt_spline_degree_3" "hp_poly2"           "hp_poly2"          
#> [349] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [352] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [355] "wt_spline_degree_3" "hp_poly2"           "hp_poly2"          
#> [358] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [361] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [364] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [367] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [370] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [373] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [376] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [379] "hp_poly2"           "hp_poly2"           "wt_spline_degree_3"
#> [382] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [385] "wt_spline_degree_3" "hp_poly2"           "hp_poly2"          
#> [388] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [391] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [394] "wt_spline_degree_3" "hp_poly2"           "hp_poly2"          
#> [397] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [400] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [403] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [406] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [409] "hp_poly2"           "hp_poly2"           "wt_spline_degree_3"
#> [412] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [415] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [418] "hp_poly2"           "hp_poly2"           "wt_spline_degree_3"
#> [421] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [424] "wt_spline_degree_3" "hp_poly2"           "hp_poly2"          
#> [427] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [430] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [433] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [436] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [439] "hp_poly2"           "hp_poly2"           "wt_spline_degree_3"
#> [442] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [445] "wt_spline_degree_3" "hp_poly2"           "hp_poly2"          
#> [448] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [451] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [454] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [457] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [460] "hp_poly2"           "hp_poly2"           "wt_spline_degree_3"
#> [463] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [466] "wt_spline_degree_3" "hp_poly2"           "hp_poly2"          
#> [469] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [472] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [475] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [478] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [481] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [484] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [487] "wt_spline_degree_3" "hp_poly2"           "wt_spline_degree_3"
#> [490] "hp_poly2"           "hp_poly2"           "wt_spline_degree_3"
#> [493] "hp_poly2"           "wt_spline_degree_3" "hp_poly2"          
#> [496] "wt_spline_degree_3" "hp_poly2"           "hp_poly2"          
#> [499] "wt_spline_degree_3" "hp_poly2"          

# Set to iteration 200:
cboost$setToIteration(200, 30)

# Get new parameter values:
cboost$getEstimatedParameter()
#> $hp_poly2
#>               [,1]
#> [1,]  3.649127e+00
#> [2,] -3.820112e-02
#> [3,]  7.376905e-05
#> 
#> $wt_spline_degree_3
#>             [,1]
#>  [1,]  1.3479210
#>  [2,]  1.3418629
#>  [3,]  1.3528398
#>  [4,]  1.3714923
#>  [5,]  1.2096167
#>  [6,]  0.7153452
#>  [7,] -0.2712226
#>  [8,] -1.1840205
#>  [9,] -1.4335470
#> [10,] -1.4475909
#> [11,] -1.3450002
#> [12,] -1.1727850
#> [13,] -0.9769323
#> [14,] -0.7809478
#>