-
Notifications
You must be signed in to change notification settings - Fork 3
/
RcppExports.R
1446 lines (1424 loc) · 45.9 KB
/
RcppExports.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
#' In memory data class to store data in RAM
#'
#' \code{InMemoryData} creates an data object which can be used as source or
#' target object within the base-learner factories of \code{compboost}. The
#' convention to initialize target data is to call the constructor without
#' any arguments.
#'
#' @format \code{\link{S4}} object.
#' @name InMemoryData
#'
#' @section Usage:
#' \preformatted{
#' InMemoryData$new()
#' InMemoryData$new(data_mat, data_identifier)
#' }
#'
#' @section Arguments:
#' \describe{
#' \item{\code{data_mat} [\code{matrix}]}{
#' Matrix containing the source data. This source data is later transformed
#' to obtain the design matrix a base-learner uses for training.
#' }
#' \item{\code{data_identifier} [\code{character(1)}]}{
#' The name for the data specified in \code{data_mat}. Note that it is
#' important to have the same data names for train and evaluation data.
#' }
#' }
#'
#'
#' @section Details:
#' The \code{data_mat} needs to suits the base-learner. For instance, the
#' spline base-learner does just take a one column matrix since there are
#' just one dimensional splines till now. Additionally, using the polynomial
#' base-learner the \code{data_mat} is used to control if a intercept should
#' be fitted or not by adding a column containing just ones. It is also
#' possible to add other columns to estimate multiple features
#' simultaneously. Anyway, this is not recommended in terms of unbiased
#' features selection.
#'
#' The \code{data_mat} and \code{data_identifier} of a target data object
#' is set automatically by passing the source and target object to the
#' desired factory. \code{getData()} can then be used to access the
#' transformed data of the target object.
#'
#' This class is a wrapper around the pure \code{C++} implementation. To see
#' the functionality of the \code{C++} class visit
#' \url{https://schalkdaniel.github.io/compboost/cpp_man/html/classdata_1_1_in_memory_data.html}.
#'
#' @section Fields:
#' This class doesn't contain public fields.
#'
#' @section Methods:
#' \describe{
#' \item{\code{getData()}}{method extract the \code{data_mat} from the data object.}
#' \item{\code{getIdentifier()}}{method to extract the used name from the data object.}
#' }
#' @examples
#' # Sample data:
#' data_mat = cbind(1:10)
#'
#' # Create new data object:
#' data_obj = InMemoryData$new(data_mat, "my_data_name")
#'
#' # Get data and identifier:
#' data_obj$getData()
#' data_obj$getIdentifier()
#'
#' @export InMemoryData
NULL
#' Base-learner factory to make polynomial regression
#'
#' \code{BaselearnerPolynomial} creates a polynomial base-learner factory
#' object which can be registered within a base-learner list and then used
#' for training.
#'
#' @format \code{\link{S4}} object.
#' @name BaselearnerPolynomial
#'
#' @section Usage:
#' \preformatted{
#' BaselearnerPolynomial$new(data_source, data_target, list(degree, intercept))
#' BaselearnerPolynomial$new(data_source, data_target, blearner_type, list(degree, intercept))
#' }
#'
#' @section Arguments:
#' \describe{
#' \item{\code{data_source} [\code{Data} Object]}{
#' Data object which contains the source data.
#' }
#' \item{\code{data_target} [\code{Data} Object]}{
#' Data object which gets the transformed source data.
#' }
#' \item{\code{degree} [\code{integer(1)}]}{
#' This argument is used for transforming the source data. Each element is
#' taken to the power of the \code{degree} argument.
#' }
#' \item{\code{intercept} [\code{logical(1)}]}{
#' Indicating whether an intercept should be added or not. Default is set to TRUE.
#' }
#' }
#'
#'
#' @section Details:
#' The polynomial base-learner factory takes any matrix which the user wants
#' to pass the number of columns indicates how much parameter are estimated.
#' Note that the intercept isn't added by default. To get an intercept add a
#' column of ones to the source data matrix.
#'
#' This class is a wrapper around the pure \code{C++} implementation. To see
#' the functionality of the \code{C++} class visit
#' \url{https://schalkdaniel.github.io/compboost/cpp_man/html/classblearnerfactory_1_1_polynomial_blearner_factory.html}.
#'
#' @section Fields:
#' This class doesn't contain public fields.
#'
#' @section Methods:
#' \describe{
#' \item{\code{getData()}}{Get the data matrix of the target data which is used
#' for modeling.}
#' \item{\code{transformData(X)}}{Transform a data matrix as defined within the
#' factory. The argument has to be a matrix with one column.}
#' \item{\code{summarizeFactory()}}{Summarize the base-learner factory object.}
#' }
#' @examples
#' # Sample data:
#' data_mat = cbind(1:10)
#'
#' # Create new data object:
#' data_source = InMemoryData$new(data_mat, "my_data_name")
#' data_target1 = InMemoryData$new()
#' data_target2 = InMemoryData$new()
#'
#' # Create new linear base-learner factory:
#' lin_factory = BaselearnerPolynomial$new(data_source, data_target1,
#' list(degree = 2, intercept = FALSE))
#' lin_factory_int = BaselearnerPolynomial$new(data_source, data_target2,
#' list(degree = 2, intercept = TRUE))
#'
#' # Get the transformed data:
#' lin_factory$getData()
#' lin_factory_int$getData()
#'
#' # Summarize factory:
#' lin_factory$summarizeFactory()
#'
#' # Transform data manually:
#' lin_factory$transformData(data_mat)
#' lin_factory_int$transformData(data_mat)
#'
#' @export BaselearnerPolynomial
NULL
#' Base-learner factory to do non-parametric B or P-spline regression
#'
#' \code{BaselearnerPSpline} creates a spline base-learner factory
#' object which can be registered within a base-learner list and then used
#' for training.
#'
#' @format \code{\link{S4}} object.
#' @name BaselearnerPSpline
#'
#' @section Usage:
#' \preformatted{
#' BaselearnerPSpline$new(data_source, data_target, list(degree, n_knots, penalty,
#' differences))
#' }
#'
#' @section Arguments:
#' \describe{
#' \item{\code{data_source} [\code{Data} Object]}{
#' Data object which contains the source data.
#' }
#' \item{\code{data_target} [\code{Data} Object]}{
#' Data object which gets the transformed source data.
#' }
#' \item{\code{degree} [\code{integer(1)}]}{
#' Degree of the spline functions to interpolate the knots.
#' }
#' \item{\code{n_knots} [\code{integer(1)}]}{
#' Number of \strong{inner knots}. To prevent weird behavior on the edges
#' the inner knots are expanded by \eqn{\mathrm{degree} - 1} additional knots.
#' }
#' \item{\code{penalty} [\code{numeric(1)}]}{
#' Positive numeric value to specify the penalty parameter. Setting the
#' penalty to 0 ordinary B-splines are used for the fitting.
#' }
#' \item{\code{differences} [\code{integer(1)}]}{
#' The number of differences which are penalized. A higher value leads to
#' smoother curves.
#' }
#' }
#'
#' @section Details:
#' The data matrix of the source data is restricted to have just one column.
#' The spline bases are created for this single feature. Multidimensional
#' splines are not supported at the moment.
#'
#' This class is a wrapper around the pure \code{C++} implementation. To see
#' the functionality of the \code{C++} class visit
#' \url{https://schalkdaniel.github.io/compboost/cpp_man/html/classblearnerfactory_1_1_p_spline_blearner_factory.html}.
#'
#' @section Fields:
#' This class doesn't contain public fields.
#'
#' @section Methods:
#' \describe{
#' \item{\code{getData()}}{Get the data matrix of the target data which is used
#' for modeling.}
#' \item{\code{transformData(X)}}{Transform a data matrix as defined within the
#' factory. The argument has to be a matrix with one column.}
#' \item{\code{summarizeFactory()}}{Summarize the base-learner factory object.}
#' }
#' @examples
#' # Sample data:
#' data_mat = cbind(1:10)
#' y = sin(1:10)
#'
#' # Create new data object:
#' data_source = InMemoryData$new(data_mat, "my_data_name")
#' data_target = InMemoryData$new()
#'
#' # Create new linear base-learner:
#' spline_factory = BaselearnerPSpline$new(data_source, data_target,
#' list(degree = 3, n_knots = 4, penalty = 2, differences = 2))
#'
#' # Get the transformed data:
#' spline_factory$getData()
#'
#' # Summarize factory:
#' spline_factory$summarizeFactory()
#'
#' # Transform data manually:
#' spline_factory$transformData(data_mat)
#'
#' @export BaselearnerPSpline
NULL
#' Create custom base-learner factory by using R functions.
#'
#' \code{BaselearnerCustom} creates a custom base-learner factory by
#' setting custom \code{R} functions. This factory object can be registered
#' within a base-learner list and then used for training.
#'
#' @format \code{\link{S4}} object.
#' @name BaselearnerCustom
#'
#' @section Usage:
#' \preformatted{
#' BaselearnerCustom$new(data_source, data_target, list(instantiate_fun,
#' train_fun, predict_fun, param_fun))
#' }
#'
#' @section Arguments:
#' \describe{
#' \item{\code{data_source} [\code{Data} Object]}{
#' Data object which contains the source data.
#' }
#' \item{\code{data_target} [\code{Data} Object]}{
#' Data object which gets the transformed source data.
#' }
#' \item{\code{instantiate_fun} [\code{function}]}{
#' \code{R} function to transform the source data. For details see the
#' \code{Details}.
#' }
#' \item{\code{train_fun} [\code{function}]}{
#' \code{R} function to train the base-learner on the target data. For
#' details see the \code{Details}.
#' }
#' \item{\code{predict_fun} [\code{function}]}{
#' \code{R} function to predict on the object returned by \code{train}.
#' For details see the \code{Details}.
#' }
#' \item{\code{param_fun} [\code{function}]}{
#' \code{R} function to extract the parameter of the object returned by
#' \code{train}. For details see the \code{Details}.
#' }
#' }
#'
#' @section Details:
#' The function must have the following structure:
#'
#' \code{instantiateData(X) { ... return (X_trafo) }} With a matrix argument
#' \code{X} and a matrix as return object.
#'
#' \code{train(y, X) { ... return (SEXP) }} With a vector argument \code{y}
#' and a matrix argument \code{X}. The target data is used in \code{X} while
#' \code{y} contains the response. The function can return any \code{R}
#' object which is stored within a \code{SEXP}.
#'
#' \code{predict(model, newdata) { ... return (prediction) }} The returned
#' object of the \code{train} function is passed to the \code{model}
#' argument while \code{newdata} contains a new matrix used for predicting.
#'
#' \code{extractParameter() { ... return (parameters) }} Again, \code{model}
#' contains the object returned by \code{train}. The returned object must be
#' a matrix containing the estimated parameter. If no parameter should be
#' estimated one can return \code{NA}.
#'
#' For an example see the \code{Examples}.
#'
#' This class is a wrapper around the pure \code{C++} implementation. To see
#' the functionality of the \code{C++} class visit
#' \url{https://schalkdaniel.github.io/compboost/cpp_man/html/classblearnerfactory_1_1_custom_blearner_factory.html}.
#'
#' @section Fields:
#' This class doesn't contain public fields.
#'
#' @section Methods:
#' \describe{
#' \item{\code{getData()}}{Get the data matrix of the target data which is used
#' for modeling.}
#' \item{\code{transformData(X)}}{Transform a data matrix as defined within the
#' factory. The argument has to be a matrix with one column.}
#' \item{\code{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()
#'
#' # Summarize factory:
#' custom_lin_factory$summarizeFactory()
#'
#' # Transform data manually:
#' custom_lin_factory$transformData(data_mat)
#'
#' @export BaselearnerCustom
NULL
#' Create custom cpp base-learner factory by using cpp functions and external
#' pointer.
#'
#' \code{BaselearnerCustomCpp} creates a custom base-learner factory by
#' setting custom \code{C++} functions. This factory object can be registered
#' within a base-learner list and then used for training.
#'
#' @format \code{\link{S4}} object.
#' @name BaselearnerCustomCpp
#'
#' @section Usage:
#' \preformatted{
#' BaselearnerCustomCpp$new(data_source, data_target, list(instantiate_ptr,
#' train_ptr, predict_ptr))
#' }
#'
#' @section Arguments:
#' \describe{
#' \item{\code{data_source} [\code{Data} Object]}{
#' Data object which contains the source data.
#' }
#' \item{\code{data_target} [\code{Data} Object]}{
#' Data object which gets the transformed source data.
#' }
#' \item{\code{instantiate_ptr} [\code{externalptr}]}{
#' External pointer to the \code{C++} instantiate data function.
#' }
#' \item{\code{train_ptr} [\code{externalptr}]}{
#' External pointer to the \code{C++} train function.
#' }
#' \item{\code{predict_ptr} [\code{externalptr}]}{
#' External pointer to the \code{C++} predict function.
#' }
#' }
#'
#' @section Details:
#' For an example see the extending compboost vignette or the function
#' \code{getCustomCppExample}.
#'
#' This class is a wrapper around the pure \code{C++} implementation. To see
#' the functionality of the \code{C++} class visit
#' \url{https://schalkdaniel.github.io/compboost/cpp_man/html/classblearnerfactory_1_1_custom_cpp_blearner_factory.html}.
#'
#' @section Fields:
#' This class doesn't contain public fields.
#'
#' @section Methods:
#' \describe{
#' \item{\code{getData()}}{Get the data matrix of the target data which is used
#' for modeling.}
#' \item{\code{transformData(X)}}{Transform a data matrix as defined within the
#' factory. The argument has to be a matrix with one column.}
#' \item{\code{summarizeFactory()}}{Summarize the base-learner factory object.}
#' }
#' @examples
#' \donttest{
#' # 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()
#'
#' # Source the external pointer exposed by using XPtr:
#' Rcpp::sourceCpp(code = getCustomCppExample(silent = TRUE))
#'
#' # Create new linear base-learner:
#' custom_cpp_factory = BaselearnerCustomCpp$new(data_source, data_target,
#' list(instantiate_ptr = dataFunSetter(), train_ptr = trainFunSetter(),
#' predict_ptr = predictFunSetter()))
#'
#' # Get the transformed data:
#' custom_cpp_factory$getData()
#'
#' # Summarize factory:
#' custom_cpp_factory$summarizeFactory()
#'
#' # Transform data manually:
#' custom_cpp_factory$transformData(data_mat)
#' }
#' @export BaselearnerCustomCpp
NULL
#' Base-learner factory list to define the set of base-learners
#'
#' \code{BlearnerFactoryList} creates an object in which base-learner factories
#' can be registered. This object can then be passed to compboost as set of
#' base-learner which is used by the optimizer to get the new best
#' base-learner.
#'
#' @format \code{\link{S4}} object.
#' @name BlearnerFactoryList
#'
#' @section Usage:
#' \preformatted{
#' BlearnerFactoryList$new()
#' }
#'
#' @section Details:
#'
#' This class is a wrapper around the pure \code{C++} implementation. To see
#' the functionality of the \code{C++} class visit
#' \url{https://schalkdaniel.github.io/compboost/cpp_man/html/classblearnerlist_1_1_baselearner_factory_list.html}.
#'
#' @section Fields:
#' This class doesn't contain public fields.
#'
#' @section Methods:
#' \describe{
#' \item{\code{registerFactory(BaselearnerFactory)}}{Takes a object of the
#' class \code{BaseLearnerFactory} and adds this factory to the set of
#' base-learner.}
#' \item{\code{printRegisteredFactories()}}{Get all registered factories.}
#' \item{\code{clearRegisteredFactories()}}{Remove all registered factories.
#' Note that the factories are not deleted, just removed from the map.}
#' \item{\code{getModelFrame()}}{Get each target data matrix parsed to one
#' big matrix.}
#' \item{\code{getNumberOfRegisteredFactories()}}{Get the number of registered
#' factories.}
#' }
#' @examples
#' # Sample data:
#' data_mat = cbind(1:10)
#'
#' # Create new data object:
#' data_source = InMemoryData$new(data_mat, "my_data_name")
#' data_target1 = InMemoryData$new()
#' data_target2 = InMemoryData$new()
#'
#' lin_factory = BaselearnerPolynomial$new(data_source, data_target1,
#' list(degree = 1, intercept = TRUE))
#' poly_factory = BaselearnerPolynomial$new(data_source, data_target2,
#' list(degree = 2, intercept = TRUE))
#'
#' # Create new base-learner list:
#' my_bl_list = BlearnerFactoryList$new()
#'
#' # Register factories:
#' my_bl_list$registerFactory(lin_factory)
#' my_bl_list$registerFactory(poly_factory)
#'
#' # Get registered factories:
#' my_bl_list$printRegisteredFactories()
#'
#' # Get all target data matrices in one big matrix:
#' my_bl_list$getModelFrame()
#'
#' # Clear list:
#' my_bl_list$clearRegisteredFactories()
#'
#' # Get number of registered factories:
#' my_bl_list$getNumberOfRegisteredFactories()
#'
#' @export BlearnerFactoryList
NULL
#' Quadratic loss for regression tasks.
#'
#' This loss can be used for regression with \eqn{y \in \mathrm{R}}.
#'
#' \strong{Loss Function:}
#' \deqn{
#' L(y, f(x)) = \frac{1}{2}( y - f(x))^2
#' }
#' \strong{Gradient:}
#' \deqn{
#' \frac{\delta}{\delta f(x)}\ L(y, f(x)) = f(x) - y
#' }
#' \strong{Initialization:}
#' \deqn{
#' \hat{f}^{[0]}(x) = \mathrm{arg~min}{c\in\mathrm{R}}{\mathrm{arg~min}}\ \frac{1}{n}\sum\limits_{i=1}^n
#' L\left(y^{(i)}, c\right) = \bar{y}
#' }
#'
#' @format \code{\link{S4}} object.
#' @name LossQuadratic
#'
#' @section Usage:
#' \preformatted{
#' LossQuadratic$new()
#' LossQuadratic$new(offset)
#' }
#'
#' @section Arguments:
#' \describe{
#' \item{\code{offset} [\code{numeric(1)}]}{
#' Numerical value which can be used to set a custom offset. If so, this
#' value is returned instead of the loss optimal initialization.
#' }
#' }
#'
#' @section Details:
#'
#' This class is a wrapper around the pure \code{C++} implementation. To see
#' the functionality of the \code{C++} class visit
#' \url{https://schalkdaniel.github.io/compboost/cpp_man/html/classloss_1_1_quadratic_loss.html}.
#'
#' @examples
#'
#' # Create new loss object:
#' quadratic_loss = LossQuadratic$new()
#' quadratic_loss
#'
#' @export LossQuadratic
NULL
#' Absolute loss for regression tasks.
#'
#' This loss can be used for regression with \eqn{y \in \mathrm{R}}.
#'
#' \strong{Loss Function:}
#' \deqn{
#' L(y, f(x)) = | y - f(x)|
#' }
#' \strong{Gradient:}
#' \deqn{
#' \frac{\delta}{\delta f(x)}\ L(y, f(x)) = \mathrm{sign}( f(x) - y)
#' }
#' \strong{Initialization:}
#' \deqn{
#' \hat{f}^{[0]}(x) = \mathrm{arg~min}_{c\in R}\ \frac{1}{n}\sum\limits_{i=1}^n
#' L(y^{(i)}, c) = \mathrm{median}(y)
#' }
#'
#' @format \code{\link{S4}} object.
#' @name LossAbsolute
#'
#' @section Usage:
#' \preformatted{
#' LossAbsolute$new()
#' LossAbsolute$new(offset)
#' }
#'
#' @section Arguments:
#' \describe{
#' \item{\code{offset} [\code{numeric(1)}]}{
#' Numerical value which can be used to set a custom offset. If so, this
#' value is returned instead of the loss optimal initialization.
#' }
#' }
#'
#' @section Details:
#'
#' This class is a wrapper around the pure \code{C++} implementation. To see
#' the functionality of the \code{C++} class visit
#' \url{https://schalkdaniel.github.io/compboost/cpp_man/html/classloss_1_1_absolute_loss.html}.
#'
#' @examples
#'
#' # Create new loss object:
#' absolute_loss = LossAbsolute$new()
#' absolute_loss
#'
#' @export LossAbsolute
NULL
#' 0-1 Loss for binary classification derived of the binomial distribution
#'
#' This loss can be used for binary classification. The coding we have chosen
#' here acts on
#' \eqn{y \in \{-1, 1\}}.
#'
#' \strong{Loss Function:}
#' \deqn{
#' L(y, f(x)) = \log(1 + \mathrm{exp}(-2yf(x)))
#' }
#' \strong{Gradient:}
#' \deqn{
#' \frac{\delta}{\delta f(x)}\ L(y, f(x)) = - \frac{y}{1 + \mathrm{exp}(2yf)}
#' }
#' \strong{Initialization:}
#' \deqn{
#' \hat{f}^{[0]}(x) = \frac{1}{2}\mathrm{log}(p / (1 - p))
#' }
#' with
#' \deqn{
#' p = \frac{1}{n}\sum\limits_{i=1}^n\mathrm{1}_{\{y^{(i)} = 1\}}
#' }
#'
#' @format \code{\link{S4}} object.
#' @name LossBinomial
#'
#' @section Usage:
#' \preformatted{
#' LossBinomial$new()
#' LossBinomial$new(offset)
#' }
#'
#' @section Arguments:
#' \describe{
#' \item{\code{offset} [\code{numeric(1)}]}{
#' Numerical value which can be used to set a custom offset. If so, this
#' value is returned instead of the loss optimal initialization.
#' }
#' }
#'
#' @section Details:
#'
#' This class is a wrapper around the pure \code{C++} implementation. To see
#' the functionality of the \code{C++} class visit
#' \url{https://schalkdaniel.github.io/compboost/cpp_man/html/classloss_1_1_binomial_loss.html}.
#'
#' @examples
#'
#' # Create new loss object:
#' bin_loss = LossBinomial$new()
#' bin_loss
#'
#' @export LossBinomial
NULL
#' Create LossCustom by using R functions.
#'
#' \code{LossCustom} creates a custom loss by using
#' \code{Rcpp::Function} to set \code{R} functions.
#'
#' @format \code{\link{S4}} object.
#' @name LossCustom
#'
#' @section Usage:
#' \preformatted{
#' LossCustom$new(lossFun, gradientFun, initFun)
#' }
#'
#' @section Arguments:
#' \describe{
#' \item{\code{lossFun} [\code{function}]}{
#' \code{R} function to calculate the loss. For details see the
#' \code{Details}.
#' }
#' \item{\code{gradientFun} [\code{function}]}{
#' \code{R} function to calculate the gradient. For details see the
#' \code{Details}.
#' }
#' \item{\code{initFun} [\code{function}]}{
#' \code{R} function to calculate the constant initialization. For
#' details see the \code{Details}.
#' }
#' }
#'
#' @section Details:
#' The functions must have the following structure:
#'
#' \code{lossFun(truth, prediction) { ... return (loss) }} With a vector
#' argument \code{truth} containing the real values and a vector of
#' predictions \code{prediction}. The function must return a vector
#' containing the loss for each component.
#'
#' \code{gradientFun(truth, prediction) { ... return (grad) }} With a vector
#' argument \code{truth} containing the real values and a vector of
#' predictions \code{prediction}. The function must return a vector
#' containing the gradient of the loss for each component.
#'
#' \code{initFun(truth) { ... return (init) }} With a vector
#' argument \code{truth} containing the real values. The function must
#' return a numeric value containing the offset for the constant
#' initialization.
#'
#' For an example see the \code{Examples}.
#'
#' This class is a wrapper around the pure \code{C++} implementation. To see
#' the functionality of the \code{C++} class visit
#' \url{https://schalkdaniel.github.io/compboost/cpp_man/html/classloss_1_1_custom_loss.html}.
#'
#' @examples
#'
#' # Loss function:
#' myLoss = function (true_values, prediction) {
#' return (0.5 * (true_values - prediction)^2)
#' }
#' # Gradient of loss function:
#' myGradient = function (true_values, prediction) {
#' return (prediction - true_values)
#' }
#' # Constant initialization:
#' myConstInit = function (true_values) {
#' return (mean(true_values))
#' }
#'
#' # Create new custom quadratic loss:
#' my_loss = LossCustom$new(myLoss, myGradient, myConstInit)
#'
#' @export LossCustom
NULL
#' Create custom cpp losses by using cpp functions and external pointer.
#'
#' \code{LossCustomCpp} creates a custom loss by using
#' \code{Rcpp::XPtr} to set \code{C++} functions.
#'
#' @format \code{\link{S4}} object.
#' @name LossCustomCpp
#'
#' @section Usage:
#' \preformatted{
#' LossCustomCpp$new(loss_ptr, grad_ptr, const_init_ptr)
#' }
#'
#' @section Arguments:
#' \describe{
#' \item{\code{loss_ptr} [\code{externalptr}]}{
#' External pointer to the \code{C++} loss function.
#' }
#' \item{\code{grad_ptr} [\code{externalptr}]}{
#' External pointer to the \code{C++} gradient function.
#' }
#' \item{\code{const_init_ptr} [\code{externalptr}]}{
#' External pointer to the \code{C++} constant initialization function.
#' }
#' }
#'
#' @section Details:
#' For an example see the extending compboost vignette or the function
#' \code{getCustomCppExample(example = "loss")}.
#'
#' This class is a wrapper around the pure \code{C++} implementation. To see
#' the functionality of the \code{C++} class visit
#' \url{https://schalkdaniel.github.io/compboost/cpp_man/html/classloss_1_1_custom_cpp_loss.html}.
#'
#' @examples
#' \donttest{
#' # Load loss functions:
#' Rcpp::sourceCpp(code = getCustomCppExample(example = "loss", silent = TRUE))
#'
#' # Create new custom quadratic loss:
#' my_cpp_loss = LossCustomCpp$new(lossFunSetter(), gradFunSetter(), constInitFunSetter())
#' }
#' @export LossCustomCpp
NULL
#' Create response object for regression.
#'
#' \code{ResponseRegr} creates a response object that are used as target during the
#' fitting process.
#'
#' @format \code{\link{S4}} object.
#' @name ResponseRegr
#'
#' @section Usage:
#' \preformatted{
#' ResponseRegr$new(target_name, response)
#' ResponseRegr$new(target_name, response, weights)
#' }
#'
#' @export ResponseRegr
NULL
#' Create response object for binary classification.
#'
#' \code{ResponseBinaryClassif} creates a response object that are used as target during the
#' fitting process.
#'
#' @format \code{\link{S4}} object.
#' @name ResponseBinaryClassif
#'
#' @section Usage:
#' \preformatted{
#' ResponseBinaryClassif$new(target_name, response)
#' ResponseBinaryClassif$new(target_name, response, weights)
#' }
#'
#' @export ResponseBinaryClassif
NULL
#' Logger class to log the current iteration
#'
#' @format \code{\link{S4}} object.
#' @name LoggerIteration
#'
#' @section Usage:
#' \preformatted{
#' LoggerIterationWrapper$new(logger_id, use_as_stopper, max_iterations)
#' }
#'
#' @section Arguments:
#' \describe{
#' \item{\code{logger_id} [\code{character(1)}]}{
#' Unique identifier of the logger.
#' }
#' \item{\code{use_as_stopper} [\code{logical(1)}]}{
#' Boolean to indicate if the logger should also be used as stopper.
#' }
#' \item{\code{max_iterations} [\code{integer(1)}]}{
#' If the logger is used as stopper this argument defines the maximal
#' iterations.
#' }
#' }
#'
#' @section Details:
#'
#' This class is a wrapper around the pure \code{C++} implementation. To see
#' the functionality of the \code{C++} class visit
#' \url{https://schalkdaniel.github.io/compboost/cpp_man/html/classlogger_1_1_iteration_logger.html}.
#'
#' @section Fields:
#' This class doesn't contain public fields.
#'
#' @section Methods:
#' \describe{
#' \item{\code{summarizeLogger()}}{Summarize the logger object.}
#' }
#' @examples
#' # Define logger:
#' log_iters = LoggerIteration$new("iterations", FALSE, 100)
#'
#' # Summarize logger:
#' log_iters$summarizeLogger()
#'
#' @export LoggerIteration
NULL
#' Logger class to log the inbag risk
#'
#' This class logs the inbag risk for a specific loss function. It is also
#' possible to use custom losses to log performance measures. For details
#' see the use case or extending compboost vignette.
#'
#' @format \code{\link{S4}} object.
#' @name LoggerInbagRisk
#'
#' @section Usage:
#' \preformatted{
#' LoggerInbagRisk$new(logger_id, use_as_stopper, used_loss, eps_for_break)
#' }
#'
#' @section Arguments:
#' \describe{
#' \item{\code{logger_id} [\code{character(1)}]}{
#' Unique identifier of the logger.
#' }
#' \item{\code{use_as_stopper} [\code{logical(1)}]}{
#' Boolean to indicate if the logger should also be used as stopper.
#' }
#' \item{\code{used_loss} [\code{Loss} object]}{
#' The loss used to calculate the empirical risk by taking the mean of the
#' returned defined loss within the loss object.
#' }
#' \item{\code{eps_for_break} [\code{numeric(1)}]}{
#' This argument is used if the loss is also used as stopper. If the relative
#' improvement of the logged inbag risk falls above this boundary the stopper
#' returns \code{TRUE}.
#' }
#' }
#'
#' @section Details:
#'
#' This logger computes the risk for the given training data
#' \eqn{\mathcal{D} = \{(x^{(i)},\ y^{(i)})\ |\ i \in \{1, \dots, n\}\}}
#' and stores it into a vector. The empirical risk \eqn{\mathcal{R}} for
#' iteration \eqn{m} is calculated by:
#' \deqn{
#' \mathcal{R}_\mathrm{emp}^{[m]} = \frac{1}{n}\sum\limits_{i = 1}^n L(y^{(i)}, \hat{f}^{[m]}(x^{(i)}))
#' }
#'
#' \strong{Note:}
#' \itemize{
#' \item
#' If \eqn{m=0} than \eqn{\hat{f}} is just the offset.
#'
#' \item
#' The implementation to calculate \eqn{\mathcal{R}_\mathrm{emp}^{[m]}} is
#' done in two steps:
#' \enumerate{
#' \item
#' Calculate vector \code{risk_temp} of losses for every observation for
#' given response \eqn{y^{(i)}} and prediction \eqn{\hat{f}^{[m]}(x^{(i)})}.
#'
#' \item
#' Average over \code{risk_temp}.
#' }
#' }
#' This procedure ensures, that it is possible to e.g. use the AUC or any
#' arbitrary performance measure for risk logging. This gives just one
#' value for \code{risk_temp} and therefore the average equals the loss
#' function. If this is just a value (like for the AUC) then the value is
#' returned.
#'
#' This class is a wrapper around the pure \code{C++} implementation. To see
#' the functionality of the \code{C++} class visit
#' \url{https://schalkdaniel.github.io/compboost/cpp_man/html/classlogger_1_1_inbag_risk_logger.html}.
#'
#' @section Fields:
#' This class doesn't contain public fields.
#'
#' @section Methods:
#' \describe{
#' \item{\code{summarizeLogger()}}{Summarize the logger object.}
#' }
#' @examples
#' # Used loss:
#' log_bin = LossBinomial$new()
#'
#' # Define logger:
#' log_inbag_risk = LoggerInbagRisk$new("inbag", FALSE, log_bin, 0.05)
#'
#' # Summarize logger:
#' log_inbag_risk$summarizeLogger()
#'
#' @export LoggerInbagRisk
NULL
#' Logger class to log the out of bag risk
#'
#' This class logs the out of bag risk for a specific loss function. It is
#' also possible to use custom losses to log performance measures. For details
#' see the use case or extending compboost vignette.
#'
#' @format \code{\link{S4}} object.
#' @name LoggerOobRisk
#'
#' @section Usage:
#' \preformatted{
#' LoggerOobRisk$new(logger_id, use_as_stopper, used_loss, eps_for_break,
#' oob_data, oob_response)
#' }
#'
#' @section Arguments:
#' \describe{
#' \item{\code{logger_id} [\code{character(1)}]}{
#' Unique identifier of the logger.
#' }
#' \item{\code{use_as_stopper} [\code{logical(1)}]}{
#' Boolean to indicate if the logger should also be used as stopper.
#' }
#' \item{\code{used_loss} [\code{Loss} object]}{
#' The loss used to calculate the empirical risk by taking the mean of the
#' returned defined loss within the loss object.
#' }
#' \item{\code{eps_for_break} [\code{numeric(1)}]}{
#' This argument is used if the loss is also used as stopper. If the relative
#' improvement of the logged inbag risk falls above this boundary the stopper
#' returns \code{TRUE}.
#' }
#' \item{\code{oob_data} [\code{list}]}{
#' A list which contains data source objects which corresponds to the
#' source data of each registered factory. The source data objects should
#' contain the out of bag data. This data is then used to calculate the
#' prediction in each step.
#' }
#' \item{\code{oob_response} [\code{numeric}]}{