-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: functional principal component analysis (#45)
* feat: added functional principal component analysis * feat: implement train_dt and predict_dt methods for FPCA * test: use expect_pipeop test for all implemented pipeops * docs(readme): rebuild readme * docs: add missing tf packge for fpca pipeop * fix: remove todo_comment_linter from defaults in lintr confg * fix: missing stats import and fix tests * docs: param docs for pca * docs: more description * refactor: styling * refactor: replace pcr var name with pc * docs(fpca): correct title and name * docs(readme): build readme * docs(readme): build readme * docs: fix docs * fix(fpca): make .args in invoke a list * docs: remove format section for fpca * docs for fpca * readme [skip ci] --------- Co-authored-by: Sebastian Fischer <[email protected]>
- Loading branch information
1 parent
84fa4a4
commit 27dfc44
Showing
21 changed files
with
333 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
linters: linters_with_defaults( | ||
# lintr defaults: https://github.com/jimhester/lintr#available-linters | ||
# lintr defaults: https://lintr.r-lib.org/reference/default_linters.html | ||
# the following setup changes/removes certain linters | ||
assignment_linter = NULL, # do not force using <- for assignments | ||
object_name_linter = object_name_linter(c("snake_case", "CamelCase")), # only allow snake case and camel case object names | ||
cyclocomp_linter = NULL, # do not check function complexity | ||
commented_code_linter = NULL, # allow code in comments | ||
todo_comment_linter = NULL, # allow todo in comments | ||
line_length_linter = line_length_linter(120), | ||
object_length_linter = object_length_linter(40) | ||
line_length_linter = line_length_linter(120L), | ||
object_length_linter = object_length_linter(40L) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#' @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 irregualr data, convert it to a regular grid first using [`PipeOpFDAInterpol`]. | ||
#' | ||
#' For more details, see [`tfb_fpc()`][tf::tfb_fpc], which is called internally. | ||
#' | ||
#' | ||
#' @section Parameters: | ||
#' The parameters are the parameters inherited from [`PipeOpTaskPreproc`], 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 the there are three principcal components, the corresponding | ||
#' new columns will be called `"x_pc_1", "x_pc_2", "x_pc_3"`. | ||
#' | ||
#' @export | ||
#' @examples | ||
#' library(mlr3pipelines) | ||
#' | ||
#' task = tsk("fuel") | ||
#' po_fpca = po("fda.fpca") | ||
#' task_fpca = po_fpca$train(list(task))[[1L]] | ||
#' task_fpca$data() | ||
PipeOpFPCA = R6Class("PipeOpFPCA", | ||
inherit = mlr3pipelines::PipeOpTaskPreproc, | ||
public = list( | ||
#' @description Initializes a new instance of this Class. | ||
#' @param id (`character(1)`)\cr | ||
#' Identifier of resulting object, default is `"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 = "train"), | ||
n_components = p_int(1L, special_vals = list(Inf), tags = c("train", "required")) | ||
) | ||
param_set$set_values(n_components = Inf) | ||
|
||
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( | ||
.train_dt = function(dt, levels, target) { | ||
pars = self$param_set$get_values(tags = "train") | ||
|
||
dt = map_dtc(dt, function(x, nm) invoke(tf::tfb_fpc, data = x, .args = remove_named(pars, "n_components"))) | ||
self$state = list(fpc = dt) | ||
|
||
dt = imap_dtc(dt, function(col, nm) { | ||
map(col, function(x) { | ||
pc = as.list(x[2:min(pars$n_components + 1L, length(x))]) | ||
set_names(pc, sprintf("%s_pc_%d", nm, seq_along(pc))) | ||
}) | ||
}) | ||
unnest(dt, colnames(dt)) | ||
}, | ||
|
||
.predict_dt = function(dt, levels) { | ||
pars = self$param_set$get_values() | ||
|
||
dt = imap_dtc(dt, function(col, nm) { | ||
fpc = tf::tf_rebase(col, self$state$fpc[[nm]], arg = tf::tf_arg(col)) | ||
map(fpc, function(x) { | ||
pc = as.list(x[2:min(pars$n_components + 1L, length(x))]) | ||
set_names(pc, sprintf("%s_pc_%d", nm, seq_along(pc))) | ||
}) | ||
}) | ||
unnest(dt, colnames(dt)) | ||
} | ||
) | ||
) | ||
|
||
#' @include zzz.R | ||
register_po("fda.fpca", PipeOpFPCA) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.