Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better integration of package seriation #171

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions R/cor_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,19 @@ shave.cor_df <- function(x, upper = TRUE) {
}

#' @export
rearrange.cor_df <- function(x, method = "PCA", absolute = TRUE) {
rearrange.cor_df <- function(x, method = "PCA", absolute = FALSE) {

# Convert to original matrix
m <- as_matrix(x, diagonal = 1)

if (absolute) abs(m)
if (absolute)
m <- abs(m)

if (method %in% c("BEA", "BEA_TSP", "PCA", "PCA_angle")) {
if (method %in% seriation::list_seriation_methods("matrix")) {
ord <- seriation::seriate(m, method = method)
} else {
ord <- seriation::seriate(dist(m), method = method)
#ord <- seriation::seriate(dist(m), method = method)
ord <- seriation::seriate(sqrt(stats::as.dist(1 - m)), method = method)
}

ord <- seriation::get_order(ord)
Expand Down
23 changes: 17 additions & 6 deletions R/internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,30 @@ shave <- function(x, upper = TRUE) {
#' together.
#'
#' @param x cor_df. See \code{\link{correlate}}.
#' @param method String specifying the arrangement (clustering) method.
#' Clustering is achieved via \code{\link[seriation]{seriate}}, which can be
#' @param method String specifying the arrangement method.
#' Reordering is achieved via \code{\link[seriation]{seriate}}, which can be
#' consulted for a complete list of clustering methods. Default = "PCA".
#' @param absolute Boolean whether absolute values for the correlations should
#' be used for clustering.
#' be used for reordering.
#' @return cor_df. See \code{\link{correlate}}.
#' @export
#' @references
#' Hahsler M, Hornik K, Buchta C (2008). "Getting things in order:
#' An introduction to the R package seriation."
#' Journal of Statistical Software, 25(3), 1-34. ISSN 1548-7660.
#' \doi{10.18637/jss.v025.i03}
#' @examples
#' x <- correlate(mtcars)
#'
#' rearrange(x) # Default settings
#' rearrange(x, method = "HC") # Different seriation method
#' rearrange(x, absolute = FALSE) # Not using absolute values for arranging
#' rearrange(x) # Default settings (PCA)
#' rearrange(x, method = "PCA_angle") # Angle in 2D PCA projection
#' rearrange(x, method = "OLO") # Optimal leaf ordering
#' rearrange(x, method = "R2E") # Rank 2 ellipse seriation
#'
#' rearrange(x, absolute = TRUE) # Using absolute values for arranging
#'
#' # list all available seriation methods
#' seriation::list_seriation_methods()
rearrange <- function(x, method = "PC", absolute = TRUE) {
UseMethod("rearrange")
}