File: helpers_fda.R

package info (click to toggle)
r-cran-mlr 2.19.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,264 kB
  • sloc: ansic: 65; sh: 13; makefile: 5
file content (45 lines) | stat: -rw-r--r-- 1,608 bytes parent folder | download | duplicates (2)
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
# Convert fd.features list to list of column indices and check for consitency.
fdFeatsToColumnIndex = function(df, fd.features = NULL, exclude.cols = NULL) {

  # If the data.frame already contains matricies, keep them
  fd.mats = which(vcapply(df, function(x) class(x)[1L]) == "matrix")

  # If fd.features is NULL, all numerics are a single functional feature
  # Already existing matricies are not converted
  if (is.null(fd.features)) {
    fd.features = list("fd1" = setdiff(which(vlapply(df, is.numeric)), c(exclude.cols, fd.mats)))
  }

  # Return the column index and check if indices/names refer to columns
  lapply(fd.features, function(fd.feature) {
    if (is.character(fd.feature)) {
      assertSubset(fd.feature, colnames(df), empty.ok = FALSE)
      setdiff(which(colnames(df) %in% fd.feature), exclude.cols)
    } else {
      assertSubset(fd.feature, seq_len(ncol(df)))
      setdiff(fd.feature, exclude.cols)
    }
  })
}

# Convert a data.frame containing functional features to a data.frame containing
# them as numerics.
functionalToNormalData = function(df) {
  if (hasFunctionalFeatures(df)) {
    df = do.call(data.frame, as.list(df))
    message("Functional features have been converted to numerics")
  }
  return(df)
}


# Helper function that checks functional data columns for consistency
# Takes a data.frame and a functional data column
# Returns the functional matrix-column
checkFDCols = function(data, col) {
  assertClass(data, "data.frame")
  assertChoice(col, choices = colnames(data))
  data = as.matrix(data[, col, drop = FALSE])
  assertNumeric(data)
  return(data)
}