--- title: 'Lab #7: Unsupervised learning' output: html_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) set.seed(24) ``` ## Principal Components Analysis 1. Read in the breast cancer imaging data "ispy1doctored2.csv" into a data frame called `dat` ```{r} ``` 2. Perform principal components on just the variables `"pixelVolT0", "pixelVolT1", "pixelVolT2", "pixelVolTfinal", "pixelVolPctChgT0_T1", "ftvPeT0", "ftvPeT1", "ftvPeT2", "ftvPeTfinal", "ftvPePctChgT0_T1", "age", "MRI_LD_T0", "MRI_LD_T1", "MRI_LD_T2", "MRI_LD_Tfinal"`. Use the command `pr.out <- prcomp(~ pixelVolT0 + pixelVolT1 + pixelVolT2 + pixelVolTfinal + pixelVolPctChgT0_T1 + ftvPeT0 + ftvPeT1 + ftvPeT2 + ftvPeTfinal + ftvPePctChgT0_T1 + age + MRI_LD_T0 + MRI_LD_T1 + MRI_LD_T2 + MRI_LD_Tfinal, data = dat, scale=TRUE)`. The argument `scale=TRUE` makes sure the data will be normalized before performing PCA. ```{r} ``` 3. Look at the `center` and `scale` components of `pr.out`. These correspond to the means and standard deviations of the variables that were used for scaling prior to implementing PCA. E.g. `pr.out$center` ```{r} ``` 4. Look at the `x` component of `pr.out`. This contains the PC scores for each observation. What are the values for PC1 and PC2 for the first observation? ```{r} ``` 5. Now look at the `rotation` matrix component of `pr.out` which gives the principal component loading vector. How many PCs are there? Does that agree with what you would expect? ```{r} ``` 5. Plot the first two principal components using the command `biplot`. Use `?biplot.prcomp` to learn how to use this function. Use the `scale=0` argument so that the arrows are scaled to represent the loadings. ```{r} ``` 6. The red arrows represent the loadings and there contributions to each PC. Why do you think there are many arrows that are almost overlapping? ```{r} ``` 7. Look at the `sdev` component of `pr.out`. This gives the standard deviation of each PC. ```{r} ``` 8. Generate a vector `pr.var` that is the variance explained by each PC by squaring the standard deviations. ```{r} ``` 9. Generate a new vector `pve` that is the proportion of variance explained by each vector. Generate the `sum` of `pve` as a confirmation that you are on the right lines. ```{r} ``` 10. Generate plots of proportion of variance explained and cummulative variance explained against PC number. You will need the function `cumsum` which compute the cumulative sum of elements in a numeric vector. ```{r} ``` 11. How many PCs do you you need to explain at least 80% of the variance? ```{r} ``` ## K-means clustering 12. Perform $K$-means clustering using the first 5 PCs with $K = 3$. Use 30 random starting configurations. ```{r} ``` 13. Output the cluster assignments of the 3 clusters ```{r} ``` 14. Generate a table of agreement between the cluster labels and the outcome pathologic complete response `pCR`. Do you think the cluster allocations are illuminating with respect to pCR? ```{r} ``` ## Hierarchical clustering 15. The `hclust()` function implements hierarchical clustering in `R`. We will plot the hierarchical clustering dendogram using complete, single, and average linkage clustering, with Euclidean distance as the similarity measure. Start by creating the distance matrix between all pairs of observations using the `dist` function. Use the original data and only use the predictors "pixelVolTfinal", "ftvPeTfinal", "age", "MRI_LD_Tfinal". ```{r} ``` 16. Generate hierarchical clustering with `method="complete"`. Use the command `hc.complete <- hclust(distx, method="complete")`. Plot the tree. ```{r} ``` 17. Based on the tree above, cluster the observations into 4 clusters. Use the `cutree` method to get the cluster assignments. How many observations are in each cluster? ```{r} ``` 18. Now repeat questions 16 and 17 but using `average` linkage. ```{r} ``` 19. Now repeat questions 16 and 17 but using `single` linkage. ```{r} ``` 20. How do the cluster assignments vary with respect to the type of linkage used? ```{r} ```