Repository navigation
Expand file tree
/
Copy pathPipeOpFPCA.R
More file actions
89 lines (85 loc) · 3.47 KB
/
Copy pathPipeOpFPCA.R
File metadata and controls
89 lines (85 loc) · 3.47 KB
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
#' @title Functional Principal Component Analysis
#'
#' @name mlr_pipeops_fda.fpca
#'
#' @description
#' This `PipeOp` applies a functional principal component analysis (FPCA) to functional columns and then
#' extracts the principal components as features. This is done using a (truncated) weighted SVD.
#'
#' To apply this `PipeOp` to irregular data, convert it to a regular grid first using [`PipeOpFDAInterpol`].
#'
#' For more details, see [tf::tfb_fpc()], which is called internally.
#'
#' @section State:
#' The `$state` is a named `list` with the `$state` elements inherited from
#' [`PipeOpTaskPreproc`][mlr3pipelines::PipeOpTaskPreproc], as well as:
#' * `fpc` :: [`data.table`][data.table::data.table]\cr
#' The fitted [tf::tfb_fpc()] object for each functional column, used as basis to project new data during prediction.
#' * `n_components` :: `integer(1)`\cr
#' The value of the `n_components` parameter during training.
#'
#' @section Parameters:
#' The parameters are the parameters inherited from [`PipeOpTaskPreprocSimple`][mlr3pipelines::PipeOpTaskPreprocSimple],
#' as well as the following parameters:
#' * `pve` :: `numeric(1)`\cr
#' The percentage of variance explained that should be retained. Default is `0.995`.
#' * `n_components` :: `integer(1)`\cr
#' The number of principal components to extract. This parameter is initialized to `Inf`.
#'
#' @section Naming:
#' The new names generally append a `_pc_{number}` to the corresponding column name.
#' If a column was called `"x"` and there are three principal components, the corresponding
#' new columns will be called `"x_pc_1", "x_pc_2", "x_pc_3"`.
#'
#' @export
#' @examples
#' task = tsk("fuel")
#' po_fpca = po("fda.fpca", n_components = 3L)
#' task_fpca = po_fpca$train(list(task))[[1L]]
#' task_fpca$data()
PipeOpFPCA = R6Class(
"PipeOpFPCA",
inherit = PipeOpTaskPreprocSimple,
public = list(
#' @description Initializes a new instance of this Class.
#' @param id (`character(1)`)\cr
#' Identifier of resulting object, default `"fda.fpca"`.
#' @param param_vals (named `list()`)\cr
#' List of hyperparameter settings, overwriting the hyperparameter settings that would
#' otherwise be set during construction. Default `list()`.
initialize = function(id = "fda.fpca", param_vals = list()) {
param_set = ps(
pve = p_dbl(default = 0.995, lower = 0, upper = 1, tags = c("train", "fpc")),
n_components = p_int(1L, special_vals = list(Inf), init = Inf, tags = c("train", "required"))
)
super$initialize(
id = id,
param_set = param_set,
param_vals = param_vals,
packages = c("mlr3fda", "mlr3pipelines", "tf"),
feature_types = "tfd_reg",
tags = "fda"
)
}
),
private = list(
.get_state_dt = function(dt, levels, target) {
pars = self$param_set$get_values(tags = "fpc")
fpc = map_dtc(dt, \(x) invoke(tf::tfb_fpc, data = x, .args = pars))
list(fpc = fpc, n_components = self$param_set$values$n_components)
},
.transform_dt = function(dt, levels) {
n_components = self$state$n_components
dt = imap_dtc(dt, function(col, nm) {
fpc = invoke(tf::tf_rebase, object = col, basis_from = self$state$fpc[[nm]], arg = tf::tf_arg(col))
map(fpc, function(x) {
pc = as.list(head(x[-1L], n_components))
set_names(pc, sprintf("%s_pc_%i", nm, seq_along(pc)))
})
})
unnest(dt, names(dt))
}
)
)
#' @include zzz.R
register_po("fda.fpca", PipeOpFPCA)