Title: | Clustering for networks |
---|---|
Description: | Facilitates network clustering and evaluation of cluster configurations. |
Authors: | Mike Nowak <[email protected]>, Solomon Messing <[email protected]>, Sean J Westwood <[email protected]>, and Dan McFarland <[email protected]> |
Maintainer: | Sean J Westwood <[email protected]> |
License: | GPL-2 |
Version: | 0.2 |
Built: | 2024-11-07 06:16:34 UTC |
Source: | CRAN |
Evaluates clustering solutions for n = 1, n = 2, ..., n = n clusters, by comparing the clustered matrix to the observed correlation matrix. Returns a correlation vector and a plot. Designed for networks.
clustConfigurations(vertices, hclustresult, observedcorrelation)
clustConfigurations(vertices, hclustresult, observedcorrelation)
vertices |
scalar value indicating the number of vertices |
hclustresult |
hclust result matrix object (or similar object that works with the cutree() function) |
observedcorrelation |
the observed correlation matrix |
This function helps the user discern the number of clusters that best describe the underlying data. It loops through all of possible clusters (1 through n, where n is the number of actors in the network). For each solution corresponding to a given number of clusters, it uses the cutree() to assign the vertices (or columns) to their respective clusters corresponding to that solution.
From this, the function generates a matrix of within- and between- cluster correlations. When there is one cluster for each vertex in the network, the cell values will be identical to the observed correlation matrix. When there is one cluster for the whole network, the values will all be equal to the average correlation across the observed matrix.
From a visual inspection of the correlation matrix, the user can decide on the proper number of clusters in this network.
clustConfigurations$correlations |
a vector of length n showing correlation between clustered and observed correlation matrix |
Mike Nowak [email protected]
# Generate socmatrix socmatrix = matrix(c(1,1,0,0,0,1,1,0,0,0,0,0,1,0,0,0,0,0,1,1,0,0,0,1,0), nrow = 5, ncol = 5) socmatrix socmatrix_cors <- cor(socmatrix) socmatrix_cors # To use correlation values in hierarchical clustering, they must # first be coerced into a "dissimilarity structure" using dist(). # We subtract the values from 1 so that they are all greater than # or equal to 0; thus, highly dissimilar (i.e., negatively # correlated) actors have higher values. dissimilarity <- 1 - socmatrix_cors socmatrix_dist <- as.dist(dissimilarity) socmatrix_dist # hclust() performs a hierarchical agglomerative clustering # operation based on the values in the dissimilarity matrix # yielded by as.dist() above. The standard visualization is a # dendrogram. socmatrix_hclust <- hclust(socmatrix_dist) plot(socmatrix_hclust) # cutree() allows us to use the output of hclust() to set # different numbers of clusters and assign vertices to clusters # as appropriate. For example: cutree(socmatrix_hclust, k=2) # Now we'll try to figure out the number of clusters that best # describes the underlying data. To do this, we'll loop through # all of the possible numbers of clusters (1 through n, where n is # the number of actors in the network). For each solution # corresponding to a given number of clusters, we'll use cutree() # to assign the vertices to their respective clusters # corresponding to that solution. # # From this, we can generate a matrix of within- and between- # cluster correlations. Thus, when there is one cluster for each # vertex in the network, the cell values will be identical to the # observed correlation matrix, and when there is one cluster for # the whole network, the values will all be equal to the average # correlation across the observed matrix. # # We can then correlate each by-cluster matrix with the observed # correlation matrix to see how well the by-cluster matrix fits # the data. We'll store the correlation for each number of # clusters in a vector, which we can then plot. # First, find n: num_vertices = ncol(socmatrix) # Next, use the clustConfigurations function: clustered_observed_cors <-clustConfigurations(num_vertices,socmatrix_hclust,socmatrix_cors) # Choose n where the line starts to flatten beyond 45 degrees. # Three looks like a good number for this example. num_clusters = 3 clusters <- cutree(socmatrix_hclust, k = num_clusters) clusters ( cluster_cor_mat <- clusterCorr(socmatrix_cors, clusters) )
# Generate socmatrix socmatrix = matrix(c(1,1,0,0,0,1,1,0,0,0,0,0,1,0,0,0,0,0,1,1,0,0,0,1,0), nrow = 5, ncol = 5) socmatrix socmatrix_cors <- cor(socmatrix) socmatrix_cors # To use correlation values in hierarchical clustering, they must # first be coerced into a "dissimilarity structure" using dist(). # We subtract the values from 1 so that they are all greater than # or equal to 0; thus, highly dissimilar (i.e., negatively # correlated) actors have higher values. dissimilarity <- 1 - socmatrix_cors socmatrix_dist <- as.dist(dissimilarity) socmatrix_dist # hclust() performs a hierarchical agglomerative clustering # operation based on the values in the dissimilarity matrix # yielded by as.dist() above. The standard visualization is a # dendrogram. socmatrix_hclust <- hclust(socmatrix_dist) plot(socmatrix_hclust) # cutree() allows us to use the output of hclust() to set # different numbers of clusters and assign vertices to clusters # as appropriate. For example: cutree(socmatrix_hclust, k=2) # Now we'll try to figure out the number of clusters that best # describes the underlying data. To do this, we'll loop through # all of the possible numbers of clusters (1 through n, where n is # the number of actors in the network). For each solution # corresponding to a given number of clusters, we'll use cutree() # to assign the vertices to their respective clusters # corresponding to that solution. # # From this, we can generate a matrix of within- and between- # cluster correlations. Thus, when there is one cluster for each # vertex in the network, the cell values will be identical to the # observed correlation matrix, and when there is one cluster for # the whole network, the values will all be equal to the average # correlation across the observed matrix. # # We can then correlate each by-cluster matrix with the observed # correlation matrix to see how well the by-cluster matrix fits # the data. We'll store the correlation for each number of # clusters in a vector, which we can then plot. # First, find n: num_vertices = ncol(socmatrix) # Next, use the clustConfigurations function: clustered_observed_cors <-clustConfigurations(num_vertices,socmatrix_hclust,socmatrix_cors) # Choose n where the line starts to flatten beyond 45 degrees. # Three looks like a good number for this example. num_clusters = 3 clusters <- cutree(socmatrix_hclust, k = num_clusters) clusters ( cluster_cor_mat <- clusterCorr(socmatrix_cors, clusters) )
clusterCorr
by-cluster correlation matrix
clusterCorr(observed_cor_matrix, cluster_vector)
clusterCorr(observed_cor_matrix, cluster_vector)
observed_cor_matrix |
observed correlation matrix |
cluster_vector |
vector of cluster membership |
clusterCorr |
a by-cluster correlation matrix |
Mike Nowak [email protected]
# Generate socmatrix socmatrix = matrix(c(1,1,0,0,0,1,1,0,0,0,0,0,1,0,0,0,0,0,1,1,0,0,0,1,0), nrow = 5, ncol = 5) socmatrix socmatrix_cors <- cor(socmatrix) socmatrix_cors # To use correlation values in hierarchical clustering, they must # first be coerced into a "dissimilarity structure" using dist(). # We subtract the values from 1 so that they are all greater than # or equal to 0; thus, highly dissimilar (i.e., negatively # correlated) actors have higher values. dissimilarity <- 1 - socmatrix_cors socmatrix_dist <- as.dist(dissimilarity) socmatrix_dist # hclust() performs a hierarchical agglomerative clustering # operation based on the values in the dissimilarity matrix # yielded by as.dist() above. The standard visualization is a # dendrogram. socmatrix_hclust <- hclust(socmatrix_dist) plot(socmatrix_hclust) # cutree() allows us to use the output of hclust() to set # different numbers of clusters and assign vertices to clusters # as appropriate. For example: cutree(socmatrix_hclust, k=2) # Now we'll try to figure out the number of clusters that best # describes the underlying data. To do this, we'll loop through # all of the possible numbers of clusters (1 through n, where n is # the number of actors in the network). For each solution # corresponding to a given number of clusters, we'll use cutree() # to assign the vertices to their respective clusters # corresponding to that solution. # # From this, we can generate a matrix of within- and between- # cluster correlations. Thus, when there is one cluster for each # vertex in the network, the cell values will be identical to the # observed correlation matrix, and when there is one cluster for # the whole network, the values will all be equal to the average # correlation across the observed matrix. # # We can then correlate each by-cluster matrix with the observed # correlation matrix to see how well the by-cluster matrix fits # the data. We'll store the correlation for each number of # clusters in a vector, which we can then plot. # First, find n: num_vertices = ncol(socmatrix) # Next, use the clustConfigurations function: clustered_observed_cors <-clustConfigurations(num_vertices,socmatrix_hclust,socmatrix_cors) # Choose n where the line starts to flatten beyond 45 degrees. # Three looks like a good number for this example. num_clusters = 3 clusters <- cutree(socmatrix_hclust, k = num_clusters) clusters ( cluster_cor_mat <- clusterCorr(socmatrix_cors, clusters) )
# Generate socmatrix socmatrix = matrix(c(1,1,0,0,0,1,1,0,0,0,0,0,1,0,0,0,0,0,1,1,0,0,0,1,0), nrow = 5, ncol = 5) socmatrix socmatrix_cors <- cor(socmatrix) socmatrix_cors # To use correlation values in hierarchical clustering, they must # first be coerced into a "dissimilarity structure" using dist(). # We subtract the values from 1 so that they are all greater than # or equal to 0; thus, highly dissimilar (i.e., negatively # correlated) actors have higher values. dissimilarity <- 1 - socmatrix_cors socmatrix_dist <- as.dist(dissimilarity) socmatrix_dist # hclust() performs a hierarchical agglomerative clustering # operation based on the values in the dissimilarity matrix # yielded by as.dist() above. The standard visualization is a # dendrogram. socmatrix_hclust <- hclust(socmatrix_dist) plot(socmatrix_hclust) # cutree() allows us to use the output of hclust() to set # different numbers of clusters and assign vertices to clusters # as appropriate. For example: cutree(socmatrix_hclust, k=2) # Now we'll try to figure out the number of clusters that best # describes the underlying data. To do this, we'll loop through # all of the possible numbers of clusters (1 through n, where n is # the number of actors in the network). For each solution # corresponding to a given number of clusters, we'll use cutree() # to assign the vertices to their respective clusters # corresponding to that solution. # # From this, we can generate a matrix of within- and between- # cluster correlations. Thus, when there is one cluster for each # vertex in the network, the cell values will be identical to the # observed correlation matrix, and when there is one cluster for # the whole network, the values will all be equal to the average # correlation across the observed matrix. # # We can then correlate each by-cluster matrix with the observed # correlation matrix to see how well the by-cluster matrix fits # the data. We'll store the correlation for each number of # clusters in a vector, which we can then plot. # First, find n: num_vertices = ncol(socmatrix) # Next, use the clustConfigurations function: clustered_observed_cors <-clustConfigurations(num_vertices,socmatrix_hclust,socmatrix_cors) # Choose n where the line starts to flatten beyond 45 degrees. # Three looks like a good number for this example. num_clusters = 3 clusters <- cutree(socmatrix_hclust, k = num_clusters) clusters ( cluster_cor_mat <- clusterCorr(socmatrix_cors, clusters) )
generate_cluster_cor_mat
generates the cluster correlation matrix
to examine the within- and between-cluster correlations.
generate_cluster_cor_mat(observed_cor_matrix, cluster_vector)
generate_cluster_cor_mat(observed_cor_matrix, cluster_vector)
observed_cor_matrix |
observed correlation matrix |
cluster_vector |
vector of clusters |
cluster_cor_mat |
a cluster correlation matrix |
Mike Nowak [email protected]
##
##
permute_matrix
permute the network to examine the within- and between-cluster correlations.
permute_matrix(mem_vector, adj_matrix)
permute_matrix(mem_vector, adj_matrix)
mem_vector |
vector of cluster membership |
adj_matrix |
adjacency matrix |
permute_matrix |
a permuted matrix |
Mike Nowak [email protected]
##
##