Skip to content

Commit

Permalink
Add as_seriation() to coerce objects to seriation orders
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed Nov 22, 2024
1 parent 596f81c commit d03ab65
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 27 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Collate:
'reexport.R'
'seriation_assess.R'
'seriation_average.R'
'seriation_coerce.R'
'seriation_permute.R'
'seriation_rank.R'
'seriation_refine.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ exportMethods("[[")
exportMethods(aoristic)
exportMethods(apportion)
exportMethods(as.data.frame)
exportMethods(as_seriation)
exportMethods(assess)
exportMethods(bootstrap)
exportMethods(coef)
Expand Down
21 changes: 21 additions & 0 deletions R/AllGenerics.R
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,27 @@ setGeneric(
# valueClass = "PermutationOrder"
# )

#' Coerce an \R Object to a Seriation Order
#'
#' @param object An \R object.
#' @param margin A [`numeric`] vector giving the subscripts which the
#' rearrangement will be applied over: `1` indicates rows, `2` indicates
#' columns, `c(1, 2)` indicates rows then columns, `c(2, 1)` indicates columns
#' then rows.
#' @param axes An [`integer`] vector giving the subscripts of the CA axes to be
#' used.
#' @param ... Currently not used.
#' @return
#' A [`PermutationOrder-class`] object.
#' @author N. Frerebeau
#' @family seriation methods
#' @docType methods
#' @aliases as_seriation-method
setGeneric(
name = "as_seriation",
def = function(object, ...) standardGeneric("as_seriation")
)

## Refine ----------------------------------------------------------------------
#' Refine CA-based Seriation
#'
Expand Down
28 changes: 1 addition & 27 deletions R/seriation_average.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,10 @@ setMethod(
signature = c(object = "matrix"),
definition = function(object, margin = c(1, 2), axes = 1,
sup_row = NULL, sup_col = NULL, ...) {
## Validation
arkhe::assert_length(axes, 1)

margin <- as.integer(margin)
axes <- as.integer(axes)

## Original sequences
i <- seq_len(nrow(object))
j <- seq_len(ncol(object))

## Correspondence analysis
corresp <- dimensio::ca(object, sup_row = sup_row, sup_col = sup_col)
coords <- list(
rows = dimensio::get_coordinates(corresp, margin = 1),
columns = dimensio::get_coordinates(corresp, margin = 2)
)

## Reorder in case if supplementary observations
coords$rows <- coords$rows[order(corresp@rows@order), ]
coords$columns <- coords$columns[order(corresp@columns@order), ]

## Seriation order
row_coords <- if (any(margin == 1)) order(coords$rows[, axes]) else i
col_coords <- if (any(margin == 2)) order(coords$columns[, axes]) else j

## New PermutationOrder object
.AveragePermutationOrder(
corresp,
rows_order = as.integer(row_coords),
columns_order = as.integer(col_coords)
)
as_seriation(corresp, margin = margin, axes = axes)
}
)
41 changes: 41 additions & 0 deletions R/seriation_coerce.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#' @include AllClasses.R AllGenerics.R
NULL

#' @export
#' @rdname as_seriation
#' @aliases as_seriation,CA-method
setMethod(
f = "as_seriation",
signature = c(object = "CA"),
definition = function(object, margin = c(1, 2), axes = 1) {
## Validation
arkhe::assert_length(axes, 1)

margin <- as.integer(margin)
axes <- as.integer(axes)

## Original sequences
data <- dimensio::get_data(object)
i <- seq_len(nrow(data))
j <- seq_len(ncol(data))

## Correspondence analysis
rows <- dimensio::get_coordinates(object, margin = 1)
cols <- dimensio::get_coordinates(object, margin = 2)

## Reorder in case if supplementary observations
rows <- rows[order(object@rows@order), ]
cols <- cols[order(object@columns@order), ]

## Seriation order
row_coords <- if (any(margin == 1)) order(rows[, axes]) else i
col_coords <- if (any(margin == 2)) order(cols[, axes]) else j

## New PermutationOrder object
.AveragePermutationOrder(
object,
rows_order = as.integer(row_coords),
columns_order = as.integer(col_coords)
)
}
)
45 changes: 45 additions & 0 deletions man/as_seriation.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/assess.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/order.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/permute.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/refine.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/seriate_average.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/seriate_rank.Rd

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

0 comments on commit d03ab65

Please sign in to comment.