Skip to content

Commit

Permalink
Add scaling and centering
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed Nov 22, 2023
1 parent ea8dc16 commit f0ec0db
Show file tree
Hide file tree
Showing 17 changed files with 143 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ S3method(mean,CompositionMatrix)
S3method(plot,CompositionMatrix)
S3method(plot,LogRatio)
S3method(plot,OutlierIndex)
S3method(scale,CompositionMatrix)
export(pca)
export(remove_NA)
export(remove_zero)
Expand Down Expand Up @@ -56,6 +57,7 @@ exportMethods(powering)
exportMethods(replace_NA)
exportMethods(replace_zero)
exportMethods(scalar)
exportMethods(scale)
exportMethods(transform_alr)
exportMethods(transform_clr)
exportMethods(transform_ilr)
Expand Down
23 changes: 23 additions & 0 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,29 @@ setGeneric(
# valueClass = "matrix"
# )

#' Scaling and Centering of Compositional Data
#'
#' @param x A [`CompositionMatrix-class`] object.
#' @param center A [`logical`] scalar or a [`numeric`] vector giving the center
#' to be substracted.
#' @param scale A [`logical`] scalar or a length-one [`numeric`] vector giving a
#' scaling factor for multiplication.
#' @return A [`CompositionMatrix-class`] object.
#' @references
#' Aitchison, J. (1986). *The Statistical Analysis of Compositional Data*.
#' London: Chapman and Hall, p. 64-91. \doi{10.1007/978-94-009-4109-0}.
#'
#' Boogaart, K. G. van den & Tolosana-Delgado, R. (2013). *Analyzing
#' Compositional Data with R*. Berlin Heidelberg: Springer-Verlag.
#' \doi{10.1007/978-3-642-36809-7}.
#' @example inst/examples/ex-scale.R
#' @author N. Frerebeau
#' @docType methods
#' @family statistics
#' @name scale
#' @rdname scale
NULL

# Distances ====================================================================
#' Distances
#'
Expand Down
5 changes: 5 additions & 0 deletions R/simplex.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ setMethod(
signature = c(x = "CompositionMatrix", y = "CompositionMatrix"),
definition = function(x, y) {
arkhe::assert_dimensions(y, dim(x))

if (all(x <= 0)) x <- 1 / x
if (all(y <= 0)) y <- 1 / y

z <- x * y
z <- as_composition(z)

set_samples(z) <- get_samples(x)
set_groups(z) <- get_groups(x)

z
}
)
Expand All @@ -63,6 +67,7 @@ setMethod(
f = "perturbation",
signature = c(x = "CompositionMatrix", y = "numeric"),
definition = function(x, y) {
y <- matrix(data = y, nrow = nrow(x), ncol = length(y), byrow = TRUE)
x %perturbe% as_composition(y)
}
)
Expand Down
30 changes: 30 additions & 0 deletions R/statistics.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,36 @@ gmean <- function(x, trim = 0, na.rm = FALSE) {
exp(mean(log(unclass(x)[index]), trim = trim, na.rm = na.rm))
}

# Scale ========================================================================
#' @export
#' @method scale CompositionMatrix
scale.CompositionMatrix <- function(x, center = TRUE, scale = TRUE) {
if (isFALSE(center) & isFALSE(scale)) return(x)

y <- x
if (!isFALSE(center)) {
if (isTRUE(center)) center <- mean(x)
arkhe::assert_type(center, "numeric")
arkhe::assert_length(center, NCOL(x))

y <- perturbation(y, 1 / center)
}

if (!isFALSE(scale)) {
if (isTRUE(scale)) scale <- sqrt(mean(diag(covariance(x, center = TRUE))))
arkhe::assert_type(scale, "numeric")

y <- powering(y, 1 / scale)
}

y
}

#' @export
#' @rdname scale
#' @aliases scale,CompositionMatrix-method
setMethod("scale", "CompositionMatrix", scale.CompositionMatrix)

# Metric variance ==============================================================
#' @export
#' @rdname metric_var
Expand Down
8 changes: 8 additions & 0 deletions inst/examples/ex-scale.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Coerce to compositional data
data("hongite")
coda <- as_composition(hongite)

## Center and scale
scaled <- scale(coda, center = TRUE, scale = TRUE)
mean(scaled)
head(scaled)
Binary file added inst/tinytest/_snaps/mean.rds
Binary file not shown.
Binary file added inst/tinytest/_snaps/scale.rds
Binary file not shown.
8 changes: 8 additions & 0 deletions inst/tinytest/test_statistics.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
data("hongite")
coda <- as_composition(hongite)

# Mean =========================================================================
expect_equal_to_reference(mean(coda), file = "_snaps/mean.rds")

# Scale ========================================================================
z <- scale(coda, center = TRUE, scale = TRUE)
expect_equal(mean(z), c(A = 0.2, B = 0.2, C = 0.2, D = 0.2, E = 0.2))
expect_equal_to_reference(z, file = "_snaps/scale.rds")

# Margin =======================================================================
expect_equal_to_reference(margin(coda, parts = c("B", "D")), file = "_snaps/margin.rds")

Expand Down
1 change: 1 addition & 0 deletions man/aggregate.Rd

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

1 change: 1 addition & 0 deletions man/covariance.Rd

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

1 change: 1 addition & 0 deletions man/dist.Rd

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

1 change: 1 addition & 0 deletions man/mahalanobis.Rd

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

1 change: 1 addition & 0 deletions man/margin.Rd

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

1 change: 1 addition & 0 deletions man/mean.Rd

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

1 change: 1 addition & 0 deletions man/metric_var.Rd

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

58 changes: 58 additions & 0 deletions man/scale.Rd

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

3 changes: 2 additions & 1 deletion man/variation.Rd

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

0 comments on commit f0ec0db

Please sign in to comment.