# How to use principal components as inputs in hierarchical clustering analysis in R

For my statistical analysis I want to follow the steps of a paper I read.

I have a dataset in which each row corresponds to a dive carried out by a whale ('id' in table below) and the columns to the variables calculated for each dive (maximum depth, duration, speed, etc.).

id   max_depths duration pd_times    d_rate    a_rate  bottom_dur bottom_prop
1          57      166       41  0.5288462  0.9152542          2    1.204819
2          26      165       43  0.2688172  0.3333333          2    1.212121
3          18      140       90  0.1911765  0.3500000         31   22.142857
4          23       88      141  0.3437500  0.5625000         23   26.136364
5          51      177       47  0.5384615  0.6849315         77   43.502825
6          19      170      394  0.2631579  0.2400000         62   36.470588


My goal is to carry out an hierarchical cluster analysis to see if I can find different dive types.

I want to start by:

1. Performing a PCA using the 'stats' package in R (function prcomp() or princomp()) to reduce multicollinearity and the dimensionality of the data.
2. After this, using the combined principal components that explain at least 80-85% of the variance, I want to calculate the dissimilarity structure using vegdist() of the 'vegan' package and then
3. Use hclust() to perform the actual clustering analysis.

However, I am unsure on how to use the principal components as input in step 2.

Using prcomp() to compute the PCA I get the following output:

List of 5
$sdev : num [1:11] 2.055 1.679 1.126 1.009 0.946 ...$ rotation: num [1:11, 1:11] 0.3101 0.3492 0.0284 0.0371 0.1052 ...
..- attr(*, "dimnames")=List of 2
.. ..$: chr [1:11] "max_depths" "duration" "pd_times" "d_rate" ... .. ..$ : chr [1:11] "PC1" "PC2" "PC3" "PC4" ...
$center : Named num [1:11] 66.633 244.131 213.088 0.906 0.811 ... ..- attr(*, "names")= chr [1:11] "max_depths" "duration" "pd_times" "d_rate" ...$ scale   : Named num [1:11] 47.291 140.131 1089.682 0.488 0.494 ...
..- attr(*, "names")= chr [1:11] "max_depths" "duration" "pd_times" "d_rate" ...
$x : num [1:2654, 1:11] -1.909 -2.45 -2.182 -1.858 0.145 ... ..- attr(*, "dimnames")=List of 2 .. ..$ : chr [1:2654] "1" "2" "3" "4" ...
.. ..$: chr [1:11] "PC1" "PC2" "PC3" "PC4" ... - attr(*, "class")= chr "prcomp"  What should I use as input in step 2 (dissimilarity structure) and why? $rotation (variable loadings)? $x (principal components of interest)? Thanks in advance!! • I like the output of singular value decomposition because it provides a rotation matrix that, with zeroed eigenvalues, allows you to map to a denser (if truncated) space. You can compute the R-squared value for the reconstruction (it is a ratio of variances). Two terms I have seen are "whitening" and "inverse whitening". I like transforming to the reduced coordinate space after I know how many eigenvalues to zero. Mar 17, 2021 at 13:15 • @EngrStudent I am a bit confused with your answer, I want to use the output of prcomp() so could you tell me how what you said can be applied given that? Mar 18, 2021 at 10:55 • hclust wants a dissimilarity/distance matrix as input. This can be computed by function dist from the x component of the PCA output. In fact, this can as well be computed from the raw data matrix without doing PCA first, and actually I wonder whether this would be better, because the PCA does information reduction, and it isn't clear to me why you want to reduce information. Furthermore the quality of whatever you do will crucially depend on which of the clustering methods available in hclust you use, which distance, whether variables are standardised etc. Mar 19, 2021 at 18:22 • @Lewian I am using the PCA output because I have a large dataset and correlated variables so my goal by performing this step is to reduce the dimensionality and multicollinearity of the data. The paper I am following had a similar dataset and followed this approach so I think I should also apply it in my case Mar 22, 2021 at 11:01 • Large dimensionality is not necessarily a problem with distance computations, neither is multicollinearity. Exceptions do exist (in which case chances are PCA will be of little help for clustering anyway), but it depends a lot on the specific problem and the data. Without further information I'd always prefer the raw data to the PCA output for clustering for the simple reason that PCA reduces information and there is no guarantee whatsoever that the information cut away be PCA is useless for clustering, regardless of the dimensionality or multicollinearity issues. Mar 22, 2021 at 13:11 ## 1 Answer The answer is to use$X You find directions which explain the most variation in the data. You choose a few of them (eg. enough to explain 85% of the variance) and you project your data onto those directions. What you end up with is a lower dimensional dataset which captures much of the variance in the original dataset. This is what you want to use. From Introduction to Statistical Learning.

For instance, the first two principal components of a data set span the plane that is closest to the n observations, in terms of average squared Euclidean distance [...] The first three principal components of a data set span the three-dimensional hyperplane that is closest to the n observations, and so forth.

Your data, projected onto the principal component directions, is given by \$x of the result from the prcomp function. From ?prcomp, the description for the value of x is "if retx is true the value of the rotated data (the centred (and scaled if requested) data multiplied by the rotation matrix) is returned." You can just select the first n columns of x in order to get the first n principal components.

Also see Introduction to Statistical Learning page 403

Also, see this answer to another question about PCA using prcomp results.