# =======================
# Example 1: Crabs dataset with distance method (Euclidean distance)
# =======================
# Step 1: Compute proximity matrix
if (requireNamespace("MASS", quietly = TRUE)) {
df_crabs <- as.matrix(MASS::crabs[, -c(1:3)]) # Use continuous variables only
row_prox_crabs <- computeProximity(
data = df_crabs,
proxType = 0, # 0 = Euclidean distance
side = 0, # 0 = row-wise proximity
isContainMissingValue = 0
)
# Step 2: Obtain R2E ordering
r2e_order_crabs <- ellipse_sort(row_prox_crabs) # R2E ordering
# Step 3: Apply AVG-R2E ordering
hctree_result_crabs <- hctree_sort(
row_prox_crabs, # use distance matrix directly
externalOrder = r2e_order_crabs, # apply r2e order
orderType = 2, # 2 = Average-linkage
flipType = 1 # 1 = Flip based on externalOrder
)
avg_r2e_order_crabs <- hctree_result_crabs$order + 1
# Inspect results
avg_r2e_order_crabs
}
# =======================
# Example 2: Crabs dataset with distance method (Pearson correlation)
# =======================
if (requireNamespace("MASS", quietly = TRUE)) {
df_crabs <- as.matrix(MASS::crabs[, -c(1:3)]) # Use continuous variables only
row_prox_pearson <- computeProximity(
data = df_crabs,
proxType = 1, # 1 = Pearson correlation (internally 1 - cor)
side = 0, # 0 = row-wise proximity
isContainMissingValue = 0
)
# Step 2: Obtain R2E ordering
r2e_order_pearson <- ellipse_sort(row_prox_pearson) # R2E ordering
# Step 3: Inspect results
dist_pearson <- as.dist(1 - row_prox_pearson) # convert correlation matrix to distance matrix
dist_pearson_MT <- as.matrix(dist_pearson)
hctree_result_pearson <- hctree_sort(
dist_pearson_MT, # use distance matrix directly
externalOrder = r2e_order_pearson, # apply r2e order
orderType = 2, # 2 = Average-linkage
flipType = 1 # 1 = Flip based on externalOrder
)
avg_r2e_order_pearson <- hctree_result_pearson$order + 1
# Inspect results
avg_r2e_order_pearson
}
Run the code above in your browser using DataLab