Skip to content

Commit 759ada2

Browse files
author
OVVO-Financial
committed
NNS 11.1 Beta
1 parent b9bb5ed commit 759ada2

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Package: NNS
22
Type: Package
33
Title: Nonlinear Nonparametric Statistics
44
Version: 11.1
5-
Date: 2025-02-13
5+
Date: 2025-02-15
66
Authors@R: c(
77
person("Fred", "Viole", role=c("aut","cre"), email="[email protected]"),
88
person("Roberto", "Spadim", role=c("ctb"))

NNS_11.1.tar.gz

127 Bytes
Binary file not shown.

NNS_11.1.zip

858 Bytes
Binary file not shown.

R/SD_Cluster.R

+20-6
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,15 @@
5151
NNS.SD.cluster <- function(data, degree = 1, type = "discrete", min_cluster = 1, dendrogram = FALSE) {
5252
clusters <- list()
5353
iteration <- 1
54+
n <- ncol(data)
55+
56+
if(is.null(colnames(data))) colnames(data) <- paste0("X_",1:ncol(data))
57+
original_names <- colnames(data)
5458

5559
# Ensure the input data is a matrix
5660
remaining_data <- as.matrix(data)
5761

62+
5863
# Continue clustering until the number of remaining columns is less than or equal to min_cluster
5964
while (ncol(remaining_data) > min_cluster) {
6065
# Use the original NNS.SD.efficient.set call as provided
@@ -97,21 +102,29 @@ NNS.SD.cluster <- function(data, degree = 1, type = "discrete", min_cluster = 1,
97102

98103
# Flatten the clusters into a single vector and generate cluster labels
99104
all_vars <- unlist(clusters)
105+
106+
107+
100108
cluster_labels <- unlist(lapply(seq_along(clusters), function(i) rep(i, length(clusters[[i]]))))
101109

102110

103111
if(dendrogram){
104112
# Ensure there are at least two variables for hierarchical clustering
105113
if (length(all_vars) < 2) {
106114
warning("Not enough variables for hierarchical clustering. Returning clusters only.")
107-
return(list("Clusters" = clusters, "Dendrogram" = NULL))
115+
return(list("Clusters" = clusters, "Order" = NULL))
108116
}
109117

110-
# Create a distance matrix based on cluster labels
111-
dist_matrix <- as.dist(outer(cluster_labels, cluster_labels, function(a, b) abs(a - b)))
112-
# For a "dist" object, assign labels using the Labels attribute instead of rownames.
118+
# Use the extraction order inherent in all_vars as a tie-breaker.
119+
extraction_order <- seq_along(all_vars)
120+
121+
epsilon <- 1e-3 # small tie-breaker weight
122+
dist_matrix <- as.dist(
123+
outer(cluster_labels, cluster_labels, function(a, b) n * abs(a - b)) +
124+
epsilon * outer(extraction_order, extraction_order, function(i, j) abs(i - j))
125+
)
113126
attr(dist_matrix, "Labels") <- all_vars
114-
127+
115128
# Perform hierarchical clustering
116129
hc <- hclust(dist_matrix, method = "complete")
117130

@@ -121,7 +134,8 @@ NNS.SD.cluster <- function(data, degree = 1, type = "discrete", min_cluster = 1,
121134
ylab = "SD Distance",
122135
sub = ""
123136
)
124-
137+
138+
hc$order <- match(hc$labels, original_names)
125139

126140
return(list("Clusters" = clusters, "Dendrogram" = hc))
127141
} else return(list("Clusters" = clusters))

src/NNS.dll

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)