use eigenvectors and values to plot a cluster plot in r

To plot a cluster plot using eigenvectors and eigenvalues in R, you can follow these steps:

  1. Compute the eigenvectors and eigenvalues of your data matrix. Let's assume you have a data matrix called data_matrix.
# Compute covariance matrix
cov_matrix <- cov(data_matrix)

# Compute eigenvectors and eigenvalues
eigen_results <- eigen(cov_matrix)
eigenvectors <- eigen_results$vectors
eigenvalues <- eigen_results$values
208 chars
8 lines
  1. Use the eigenvectors corresponding to the largest eigenvalues to project your data onto a lower-dimensional space. Let's say you want to project onto a 2-dimensional space.
projection_matrix <- eigenvectors[, 1:2]
projected_data <- as.matrix(data_matrix) %*% projection_matrix
104 chars
3 lines
  1. Plot the projected data points on a 2D scatter plot to visualize the clusters.
plot(projected_data, col = 'blue', pch = 19, xlab = 'PC1', ylab = 'PC2')
73 chars
2 lines

This will plot the data points onto a 2D scatter plot using the first two principal components as the axes. You can further customize the plot by coloring the points based on actual clusters or adding labels, etc.

related categories

gistlibby LogSnag