From a769fb749d910db39dee7913a55df29ae3a023a0 Mon Sep 17 00:00:00 2001 From: nfrerebeau Date: Mon, 5 Aug 2024 17:06:57 +0200 Subject: [PATCH] Rename as_features() to augment() --- NAMESPACE | 3 +- R/AllGenerics.R | 17 +++--- R/coerce.R | 36 ++++++------ inst/examples/ex-augment.R | 11 ++++ inst/examples/ex-mutators.R | 2 +- .../{features_clr.rds => augment_clr.rds} | Bin .../_snaps/{features.rds => augment_coda.rds} | Bin inst/tinytest/test_coerce.R | 4 +- inst/tinytest/test_outliers.R | 1 - man/as_amounts.Rd | 2 +- man/as_composition.Rd | 2 +- man/as_features.Rd | 53 ------------------ man/augment.Rd | 46 +++++++++++++++ man/groups.Rd | 2 +- 14 files changed, 91 insertions(+), 88 deletions(-) create mode 100644 inst/examples/ex-augment.R rename inst/tinytest/_snaps/{features_clr.rds => augment_clr.rds} (100%) rename inst/tinytest/_snaps/{features.rds => augment_coda.rds} (100%) delete mode 100644 man/as_features.Rd create mode 100644 man/augment.Rd diff --git a/NAMESPACE b/NAMESPACE index 33025e8..b2e3032 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -36,8 +36,8 @@ exportMethods("set_totals<-") exportMethods(aggregate) exportMethods(as_amounts) exportMethods(as_composition) -exportMethods(as_features) exportMethods(as_graph) +exportMethods(augment) exportMethods(barplot) exportMethods(closure) exportMethods(condense) @@ -120,4 +120,5 @@ importFrom(utils,tail) importMethodsFrom(arkhe,describe) importMethodsFrom(arkhe,replace_NA) importMethodsFrom(arkhe,replace_zero) +importMethodsFrom(dimensio,augment) importMethodsFrom(dimensio,pca) diff --git a/R/AllGenerics.R b/R/AllGenerics.R index 7cb08f2..c874a13 100644 --- a/R/AllGenerics.R +++ b/R/AllGenerics.R @@ -10,6 +10,7 @@ setGeneric("mahalanobis", package = "stats") #' @importMethodsFrom arkhe describe #' @importMethodsFrom arkhe replace_NA #' @importMethodsFrom arkhe replace_zero +#' @importMethodsFrom dimensio augment #' @importMethodsFrom dimensio pca NULL @@ -54,22 +55,20 @@ setGeneric( valueClass = "matrix" ) -#' Coerce to Features +#' Augment Data with Extra Columns #' -#' Converts an object to a collection of features. -#' @param from A [`CompositionMatrix-class`] object. +#' Adds columns from the original data. +#' @param x A [`CompositionMatrix-class`] or [`LogRatio-class`] object. #' @param ... Currently not used. #' @return #' A [`data.frame`]. -#' @example inst/examples/ex-coerce.R +#' @example inst/examples/ex-augment.R #' @author N. Frerebeau #' @docType methods #' @family compositional data tools -#' @aliases as_features-method -setGeneric( - name = "as_features", - def = function(from, ...) standardGeneric("as_features") -) +#' @name augment +#' @rdname augment +NULL #' Data Description #' diff --git a/R/coerce.R b/R/coerce.R index 5a9d240..4993a8e 100644 --- a/R/coerce.R +++ b/R/coerce.R @@ -75,7 +75,7 @@ setMethod( } ) -# To Amounts =================================================================== +# To amounts =================================================================== #' @export #' @rdname as_amounts #' @aliases as_amounts,CompositionMatrix-method @@ -87,33 +87,33 @@ setMethod( } ) -# To Features ================================================================== +# To features ================================================================== #' @export -#' @rdname as_features -#' @aliases as_features,CompositionMatrix-method +#' @rdname augment +#' @aliases augment,CompositionMatrix-method setMethod( - f = "as_features", - signature = c(from = "CompositionMatrix"), - definition = function(from) { - if (has_extra(from)) { - data.frame(get_extra(from), from) + f = "augment", + signature = c(x = "CompositionMatrix"), + definition = function(x, ...) { + if (has_extra(x)) { + data.frame(get_extra(x), x) } else { - as.data.frame(from) + as.data.frame(x) } } ) #' @export -#' @rdname as_features -#' @aliases as_features,LogRatio-method +#' @rdname augment +#' @aliases augment,LogRatio-method setMethod( - f = "as_features", - signature = c(from = "LogRatio"), - definition = function(from) { - if (has_extra(from)) { - data.frame(get_extra(from), from) + f = "augment", + signature = c(x = "LogRatio"), + definition = function(x, ...) { + if (has_extra(x)) { + data.frame(get_extra(x), x) } else { - as.data.frame(from) + as.data.frame(x) } } ) diff --git a/inst/examples/ex-augment.R b/inst/examples/ex-augment.R new file mode 100644 index 0000000..a8c1e08 --- /dev/null +++ b/inst/examples/ex-augment.R @@ -0,0 +1,11 @@ +## Data from Aitchison 1986 +data("arctic") + +## Coerce to compositional data +coda <- as_composition(arctic, parts = 1:3) + +## Isometric log-ratio +ilr <- transform_ilr(coda) + +## Add extra column +augment(ilr) diff --git a/inst/examples/ex-mutators.R b/inst/examples/ex-mutators.R index 9198ee1..2f4c785 100644 --- a/inst/examples/ex-mutators.R +++ b/inst/examples/ex-mutators.R @@ -4,4 +4,4 @@ head(slides) ## Coerce to compositional data coda <- as_composition(slides) -head(as_features(coda)) +head(augment(coda)) diff --git a/inst/tinytest/_snaps/features_clr.rds b/inst/tinytest/_snaps/augment_clr.rds similarity index 100% rename from inst/tinytest/_snaps/features_clr.rds rename to inst/tinytest/_snaps/augment_clr.rds diff --git a/inst/tinytest/_snaps/features.rds b/inst/tinytest/_snaps/augment_coda.rds similarity index 100% rename from inst/tinytest/_snaps/features.rds rename to inst/tinytest/_snaps/augment_coda.rds diff --git a/inst/tinytest/test_coerce.R b/inst/tinytest/test_coerce.R index 2747a85..f6d02bd 100644 --- a/inst/tinytest/test_coerce.R +++ b/inst/tinytest/test_coerce.R @@ -1,10 +1,10 @@ # Data with groups ============================================================= data("slides") coda <- as_composition(slides) -expect_equal_to_reference(as_features(coda), file = "_snaps/features.rds") +expect_equal_to_reference(augment(coda), file = "_snaps/augment_coda.rds") clr <- transform_clr(coda, weights = FALSE) -expect_equal_to_reference(clr, file = "_snaps/features_clr.rds") +expect_equal_to_reference(clr, file = "_snaps/augment_clr.rds") # Back transform to count ====================================================== data("hongite") diff --git a/inst/tinytest/test_outliers.R b/inst/tinytest/test_outliers.R index fb74d19..48f475b 100644 --- a/inst/tinytest/test_outliers.R +++ b/inst/tinytest/test_outliers.R @@ -4,7 +4,6 @@ coda <- as_composition(hongite) # Detect outliers ============================================================== out <- outliers(coda) # expect_equal_to_reference(out, file = "_snaps/outliers.rds") -# expect_equal_to_reference(as_features(out), file = "_snaps/features_outliers.rds") # Plot ========================================================================= if (at_home()) { diff --git a/man/as_amounts.Rd b/man/as_amounts.Rd index 2a760f3..5ca89a9 100644 --- a/man/as_amounts.Rd +++ b/man/as_amounts.Rd @@ -42,7 +42,7 @@ head(X) \seealso{ Other compositional data tools: \code{\link{as_composition}()}, -\code{\link{as_features}()} +\code{\link{augment}()} } \author{ N. Frerebeau diff --git a/man/as_composition.Rd b/man/as_composition.Rd index 466e99f..9c93ce4 100644 --- a/man/as_composition.Rd +++ b/man/as_composition.Rd @@ -58,7 +58,7 @@ head(X) \seealso{ Other compositional data tools: \code{\link{as_amounts}()}, -\code{\link{as_features}()} +\code{\link{augment}()} } \author{ N. Frerebeau diff --git a/man/as_features.Rd b/man/as_features.Rd deleted file mode 100644 index 3f65a81..0000000 --- a/man/as_features.Rd +++ /dev/null @@ -1,53 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/AllGenerics.R, R/coerce.R -\docType{methods} -\name{as_features} -\alias{as_features} -\alias{as_features-method} -\alias{as_features,CompositionMatrix-method} -\alias{as_features,LogRatio-method} -\title{Coerce to Features} -\usage{ -as_features(from, ...) - -\S4method{as_features}{CompositionMatrix}(from) - -\S4method{as_features}{LogRatio}(from) -} -\arguments{ -\item{from}{A \code{\linkS4class{CompositionMatrix}} object.} - -\item{...}{Currently not used.} -} -\value{ -A \code{\link{data.frame}}. -} -\description{ -Converts an object to a collection of features. -} -\examples{ -## Create a count matrix -A1 <- matrix(data = sample(1:100, 100, TRUE), nrow = 20) - -## Coerce to compositions -B <- as_composition(A1) - -## Row sums are internally stored before coercing to relative frequencies -get_totals(B) - -## This allows to restore the source data -A2 <- as_amounts(B) - -## Coerce to a data.frame -X <- data.frame(B) -head(X) -} -\seealso{ -Other compositional data tools: -\code{\link{as_amounts}()}, -\code{\link{as_composition}()} -} -\author{ -N. Frerebeau -} -\concept{compositional data tools} diff --git a/man/augment.Rd b/man/augment.Rd new file mode 100644 index 0000000..accf0a8 --- /dev/null +++ b/man/augment.Rd @@ -0,0 +1,46 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/AllGenerics.R, R/coerce.R +\docType{methods} +\name{augment} +\alias{augment} +\alias{augment,CompositionMatrix-method} +\alias{augment,LogRatio-method} +\title{Augment Data with Extra Columns} +\usage{ +\S4method{augment}{CompositionMatrix}(x, ...) + +\S4method{augment}{LogRatio}(x, ...) +} +\arguments{ +\item{x}{A \code{\linkS4class{CompositionMatrix}} or \code{\linkS4class{LogRatio}} object.} + +\item{...}{Currently not used.} +} +\value{ +A \code{\link{data.frame}}. +} +\description{ +Adds columns from the original data. +} +\examples{ +## Data from Aitchison 1986 +data("arctic") + +## Coerce to compositional data +coda <- as_composition(arctic, parts = 1:3) + +## Isometric log-ratio +ilr <- transform_ilr(coda) + +## Add extra column +augment(ilr) +} +\seealso{ +Other compositional data tools: +\code{\link{as_amounts}()}, +\code{\link{as_composition}()} +} +\author{ +N. Frerebeau +} +\concept{compositional data tools} diff --git a/man/groups.Rd b/man/groups.Rd index b373b8a..582f540 100644 --- a/man/groups.Rd +++ b/man/groups.Rd @@ -30,7 +30,7 @@ head(slides) ## Coerce to compositional data coda <- as_composition(slides) -head(as_features(coda)) +head(augment(coda)) } \seealso{ Other mutators: