Skip to content

Commit

Permalink
Merge pull request #31 from stscl/dev
Browse files Browse the repository at this point in the history
update `hclustgeo_disc()`
  • Loading branch information
SpatLyu authored Jan 6, 2025
2 parents a269f99 + f6b9788 commit 2c594c0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
17 changes: 12 additions & 5 deletions R/stratification.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ discretize_vector = \(x, n, method = 'natural',
#' please see `stats::hclust()`.
#' @param scale (optional) Whether to scaled the dissimilarities matrix, default is `TRUE`.
#' @param wt (optional) Vector with the weights of the observations. By default, `wt` is `NULL`.
#' @param cut (optional) Whether to cut the `hclust` tree, default is `TRUE`.
#' @param ... (optional) Other arguments passed to `stats::dist()`.
#'
#' @return A `vector` with grouped memberships if `n` are `scalar`, otherwise a `matrix` with grouped
#' memberships is returned where each column corresponds to the elements of `n`, respectively.
#' @return When `cut` is `TRUE`, returns a grouped membership: a `vector` if `n` is a scalar,
#' a `matrix` (columns correspond to elements of `n`) if not; otherwise, returns a `vector`
#' of the permuted original observations.
#' @export
#'
#' @examples
Expand All @@ -72,7 +74,7 @@ discretize_vector = \(x, n, method = 'natural',
#'
hclustgeo_disc = \(data, n, alpha = 0.5, D1 = NULL,
hclustm = "ward.D2", scale = TRUE,
wt = NULL, ...){
wt = NULL, cut = TRUE, ...){
if (inherits(data,"sf")) {
if (alpha != 0 & is.null(D1)) {
D1 = sdsfun::sf_distance_matrix(data)
Expand All @@ -85,6 +87,11 @@ hclustgeo_disc = \(data, n, alpha = 0.5, D1 = NULL,
}
D0 = as.matrix(stats::dist(data,...))
deltadist = stats::as.dist(RcppHClustGeoMat(D0,D1,alpha,scale,wt))
resh = stats::hclust(deltadist,method=hclustm,members=wt)
return(stats::cutree(resh,k = n))
resh = stats::hclust(deltadist,method = hclustm,members = wt)

if (cut) {
return(stats::cutree(resh,k = n))
} else {
return(resh$order)
}
}
8 changes: 6 additions & 2 deletions man/hclustgeo_disc.Rd

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

0 comments on commit 2c594c0

Please # to comment.