Skip to content

Commit

Permalink
Merge pull request #3 from hendersontrent/trent-dev
Browse files Browse the repository at this point in the history
maxabs scaling
  • Loading branch information
hendersontrent committed Aug 20, 2023
2 parents 0d19bd4 + 2d33bfd commit 2befa54
Show file tree
Hide file tree
Showing 17 changed files with 173 additions and 15 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(maxabs_scaler)
export(minmax_scaler)
export(normalise)
export(normalize)
Expand Down
13 changes: 11 additions & 2 deletions R/normalise.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
#' @importFrom scales rescale
#' @importFrom dplyr %>% group_by mutate ungroup
#' @param data either a \code{feature_calculations} object containing the raw feature matrix produced by \code{calculate_features} or a \code{vector} of class \code{numeric} containing values to be rescaled
#' @param norm_method \code{character} denoting the rescaling/normalising method to apply. Can be one of \code{"zScore"}, \code{"Sigmoid"}, \code{"RobustSigmoid"}, or \code{"MinMax"}. Defaults to \code{"zScore"}
#' @param norm_method \code{character} denoting the rescaling/normalising method to apply. Can be one of \code{"zScore"}, \code{"Sigmoid"}, \code{"RobustSigmoid"}, \code{"MinMax"}, or \code{"MaxAbs"}. Defaults to \code{"zScore"}
#' @param unit_int \code{Boolean} whether to rescale into unit interval \code{[0,1]} after applying normalisation method. Defaults to \code{FALSE}
#' @return either an object of class \code{data.frame} or a \code{numeric} vector
#' @author Trent Henderson
#' @export
#'

normalise <- function(data, norm_method = c("zScore", "Sigmoid", "RobustSigmoid", "MinMax"), unit_int = FALSE){
normalise <- function(data, norm_method = c("zScore", "Sigmoid", "RobustSigmoid", "MinMax", "MaxAbs"), unit_int = FALSE){

norm_method <- match.arg(norm_method)

Expand Down Expand Up @@ -46,6 +46,11 @@ normalise <- function(data, norm_method = c("zScore", "Sigmoid", "RobustSigmoid"
dplyr::mutate(values = minmax_scaler(.data$values))
}

if(norm_method == "MaxAbs"){
normed <- normed %>%
dplyr::mutate(values = maxabs_scaler(.data$values))
}

if(unit_int){
normed <- normed %>%
dplyr::mutate(values = scales::rescale(.data$values, to = c(0, 1)))
Expand Down Expand Up @@ -74,6 +79,10 @@ normalise <- function(data, norm_method = c("zScore", "Sigmoid", "RobustSigmoid"
normed <- minmax_scaler(data)
}

if(norm_method == "MaxAbs"){
normed <- maxabs_scaler(data)
}

if(unit_int){
normed <- scales::rescale(normed, to = c(0, 1))
}
Expand Down
16 changes: 16 additions & 0 deletions R/rescalers.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,19 @@ robustsigmoid_scaler <- function(x){
x_new <- 1 / (1 + exp(-((x1 - stats::median(x1, na.rm = TRUE)) / (stats::IQR(x1, na.rm = TRUE) / 1.35))))
return(x_new)
}

#' Rescales a numeric vector using maximum absolute scaling
#'
#' \eqn{z_{i} = \frac{x_{i}}{\text{max}(\mathbf{x})}}
#'
#' @param x \code{numeric} vector
#' @return \code{numeric} vector
#' @author Trent Henderson
#' @export
#'

maxabs_scaler <- function(x){
x1 <- as.vector(x) # Catches class "ts" cases
x_new <- x1 / max(x1, na.rm = TRUE)
return(x_new)
}
3 changes: 2 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ devtools::install_github("hendersontrent/normaliseR")

`normaliseR` is a software package for R for rescaling numerical vectors or `feature_calculations` objects produced by the [`purloiner`](https://github.com/hendersontrent/purloiner) package. It was specifically designed to be the rescaling module of the broader feature-based time-series 'mothership' package [`theft`](https://github.com/hendersontrent/theft).

Putting calculated feature vectors on an equal scale is crucial for any statistical or machine learning model as variables with high variance can adversely impact the model's capacity to fit the data appropriately, learn appropriate weight values, or minimise a loss function. `normaliseR` includes function `normalise` (or `normalize`) to rescale either a whole `feature_calculations` object, or a single vector of values. Four normalisation methods are currently offered:
Putting calculated feature vectors on an equal scale is crucial for any statistical or machine learning model as variables with high variance can adversely impact the model's capacity to fit the data appropriately, learn appropriate weight values, or minimise a loss function. `normaliseR` includes function `normalise` (or `normalize`) to rescale either a whole `feature_calculations` object, or a single vector of values. The following normalisation methods are currently offered:

* z-score---`"zScore"`
* Sigmoid---`"Sigmoid"`
* Outlier-robust Sigmoid (credit to Ben Fulcher for creating the original [MATLAB version](https://github.com/benfulcher/hctsa)) -- `"RobustSigmoid"`
* Min-max---`"MinMax"`
* Maximum absolute---`"MaxAbs"`
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ can adversely impact the model’s capacity to fit the data appropriately,
learn appropriate weight values, or minimise a loss function.
`normaliseR` includes function `normalise` (or `normalize`) to rescale
either a whole `feature_calculations` object, or a single vector of
values. Four normalisation methods are currently offered:
values. The following normalisation methods are currently offered:

- z-score—`"zScore"`
- Sigmoid—`"Sigmoid"`
- Outlier-robust Sigmoid (credit to Ben Fulcher for creating the
original [MATLAB version](https://github.com/benfulcher/hctsa)) –
`"RobustSigmoid"`
- Min-max—`"MinMax"`
- Maximum absolute—`"MaxAbs"`
4 changes: 3 additions & 1 deletion docs/articles/normaliseR.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion docs/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pkgdown: 2.0.7
pkgdown_sha: ~
articles:
normaliseR: normaliseR.html
last_built: 2023-08-20T10:34Z
last_built: 2023-08-20T11:40Z
urls:
reference: https://hendersontrent.github.io/normaliseR/reference
article: https://hendersontrent.github.io/normaliseR/articles
Expand Down
5 changes: 5 additions & 0 deletions docs/reference/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

93 changes: 93 additions & 0 deletions docs/reference/maxabs_scaler.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions docs/reference/normalise.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2befa54

Please sign in to comment.