I am trying to perform multiple hierarchical clusters on various distance matrices and using different linkages for comparison.
In order to simplify the task I attempted in making a function that would iterate over a group of linkage methods to generate the agnes object but to no avail.
This is an example of what the function should be doing:
library(cluster)
Links <- c("single","average","ward") # vector of linkage methods
ClustH <- list() # list to hold HClusts
set.seed(123)
A <- rnorm(3, mean = 5, sd = 1)
B <- rnorm(3, mean = 2, sd = 1)
C <- rnorm(3, mean = 0, sd = 1)
DMat <- as.dist(cbind(A,B,C)) # distance matrix
for(i in 1:length(Links)){
ClustH[i] <- agnes(DMat, method = Links[i])
}
ClustH[1]
#[[1]]
#[1] 1 2 3
Instead of saving the entire agnes object in each element of the list, it instead saves just the first element of the object (i.e. order) which is obtained through <agnes object>$order
What am I doing wrong? Can you not save listed objects within lists? Any recommendations?