I have a matrix object in R of 1500 rows and 20 columns. Based on my own algorithm of analysis, I know there are roughly 7 clusters. In my matrix object, each row is one data point, and is labeled to which cluster it belongs by its row name. Thus, there are duplicated row names. I wanted to see if PCA would give me the approximate same clustering of my data.
Segment1 Segment2 Segment3 Segment4 Segment5 Segment6 Segment7 Segment8 Segment9 Segment10 Segment11 Segment12 Segment13 Segment14 Segment15 Segment16 Segment17 Segment18 Segment19 Segment20
B 38.40000 41.75200 44.38400 44.18400 45.37600 37.49600 41.36800 33.93600 38.00800 42.51200 46.49600 40.48000 45.40800 46.32800 43.78400 39.88800 38.84000 40.56800 42.03200 38.89185
C 45.53846 50.08462 39.91538 36.95385 34.96154 39.74615 38.01538 35.75385 35.54615 36.69231 35.20769 38.05385 39.29231 37.96923 37.30000 36.86923 39.19231 38.81538 43.69231 38.06400
C 46.05176 41.69412 38.80000 37.75529 39.67529 39.07765 39.17647 38.24941 39.58588 38.63529 38.30588 41.87765 38.97412 40.13647 42.27294 38.24471 35.41647 40.80000 38.07059 42.11294
B 44.20000 43.42857 44.80000 35.20000 35.91429 37.82857 51.45714 44.68571 46.68571 48.74286 41.25091 39.45455 38.17091 40.70182 40.39273 41.28727 40.63636 41.50909 41.68364 41.29455
E1 45.06909 41.09818 40.02909 42.50182 42.34909 39.84727 41.42909 40.47273 40.28000 40.51636 41.25091 39.45455 38.17091 40.70182 40.39273 41.28727 40.63636 41.50909 41.68364 41.29455
E3 40.87407 39.27704 44.13630 43.25037 35.86667 37.30667 38.76148 40.74667 38.93333 43.16148 37.47259 37.73630 38.34370 39.00148 36.96889 37.76593 39.14667 37.92593 37.62963 38.89185
(The first column are the row names, either being A,B,C,D,E1,E2 or E3 according to my clustering)
I used various packages and functions like pcomp(), princomp() from the stats package, and PCA() from the FactoMineR package. If I wanted to do hierarchical clustering with the HCPC() function from the same package, it was not possible because of the duplicated row names.
Using:
fviz_pca_ind(pca1, geom.ind = "point", axes = c(1,3) , col.ind = new$Class, palette = c("#66C2A5" ,"#FC8D62" ,"#8DA0CB", "#E78AC3" ,"#A6D854", "#FFD92F", "#E5C494"), addEllipses = T, legend.title="Classes")
I got the individual datapoints on my two PC axes with ellipses around them.
But for clustering:
PCA_FR <- PCA(mat_data, ncp = 4, graph = FALSE)
Hier_PCA<- HCPC(PCA_FR, graph = F)
I couldn't compute Hier_PCA because of duplicated row names.
> Hier_PCA<- HCPC(PCA_FR, graph = F)
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
In addition: Warning messages:
1: In data.row.names(row.names, rowsi, i) :
some row.names duplicated: 3,4,7,8,10,11,12,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,
How can I best cluster my data using the first three PCs, and visualize the 1500 datapoints, but labeled only in 7 ways?
text
to plot the labels at that positions as previously commented. (Please do not remove the comments so quickly moderator :) $\endgroup$