gistlib
main.r# Create a community matrix community_matrix <- matrix( c(1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1), nrow = 5, byrow = TRUE ) # Create Laplacian Matrix laplacian_matrix <- diag(rowSums(community_matrix)) - community_matrix # Compute eigenvectors and eigenvalues laplacian_eigen <- eigen(laplacian_matrix) sorted_eigenvectors <- laplacian_eigen$vectors[, order(laplacian_eigen$values)] # K-means clustering library(stats) kmeans_result <- kmeans(sorted_eigenvectors[, 1:2], centers = 2) # Plot cluster graph library(ggplot2) library(reshape2) df <- data.frame( x = sorted_eigenvectors[, 1], y = sorted_eigenvectors[, 2], cluster = factor(kmeans_result$cluster) ) ggplot(data = df, aes(x = x, y = y, color = cluster)) + geom_point() + labs(title = "Cluster Graph") 836 chars35 lines
# Create a community matrix community_matrix <- matrix( c(1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1), nrow = 5, byrow = TRUE ) # Create Laplacian Matrix laplacian_matrix <- diag(rowSums(community_matrix)) - community_matrix # Compute eigenvectors and eigenvalues laplacian_eigen <- eigen(laplacian_matrix) sorted_eigenvectors <- laplacian_eigen$vectors[, order(laplacian_eigen$values)] # K-means clustering library(stats) kmeans_result <- kmeans(sorted_eigenvectors[, 1:2], centers = 2) # Plot cluster graph library(ggplot2) library(reshape2) df <- data.frame( x = sorted_eigenvectors[, 1], y = sorted_eigenvectors[, 2], cluster = factor(kmeans_result$cluster) ) ggplot(data = df, aes(x = x, y = y, color = cluster)) + geom_point() + labs(title = "Cluster Graph")
gistlibby LogSnag