-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLecture3_MachineLearning_Unsupervised.Rmd
More file actions
199 lines (161 loc) · 5.85 KB
/
Copy pathLecture3_MachineLearning_Unsupervised.Rmd
File metadata and controls
199 lines (161 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
title: "Lecture 3 - Unsupervised_Learning"
output: html_document
date: "2023-09-27"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
#Part 2 - Unsupervised analysis
##Install packages as needed
```{r}
packages_to_install=c("ggplot2","ggrepel","dendextend")
install.packages(setdiff(packages_to_install, rownames(installed.packages())))
rm(packages_to_install)
```
##Load packages
```{r}
require(ggplot2)
require(ggrepel)
require(dendextend)
```
##Load animal attribute data set
```{r}
animaldata=read.csv("Lecture3_animaltable.csv",as.is=T,header=T,row.names=1)
```
##Explore the data set using "head" or "summary" or in the workspace
```{r}
##Write your code here
head(animaldata)
table(animaldata$type)
```
##Step 1: separate categories from data matrix, assign colors to each category
```{r}
animalmat=animaldata[,-17]
animalcategories=animaldata[,17]
names(animalcategories)=rownames(animalmat)
colvec=c("red","orange","darkgreen","blue","steelblue","purple","black")
plotcolors=colvec[as.numeric(as.factor(animalcategories))]
```
##Step 2: Dimensionality reduction using PCA
```{r}
pr1=prcomp(animalmat)
```
##Explore the PCA output using head/summary/environment window
```{r}
##Write your code here
```
##Step 3: Examine the proportion of variance
```{r}
pcsummary=summary(pr1)
barplot(pcsummary$importance[2,])
```
##Step 4: Examine feature loadings for PC1
```{r}
barplot(pr1$rotation[,1],las=2,ylab="PC1 loading")
```
##Exercise: Examine feature loadings for PC2, PC3, and PC4
```{r}
##Write your code here
```
##Step 4: Plot the animals in PC space
```{r}
###This is complicated plotting code using ggplot - it allows for much more flexibility, but takes some practice to get used to
pcs_to_plot=c(1,2)
dat = data.frame(pr1$x[,pcs_to_plot])
dat$animal=rownames(dat)
p <- ggplot(dat, aes(PC1, PC2, label = animal)) +
geom_point(color = plotcolors)
p2 <- p + geom_text_repel(max.overlaps = 80) + theme_bw()
p2
```
##Exercise: try plotting PC2 and PC3, and then PC3 and PC4
```{r}
###Modify the code below
pcs_to_plot=c(1,2)
dat = data.frame(pr1$x[,pcs_to_plot])
dat$animal=rownames(dat)
p <- ggplot(dat, aes(PC1, PC2, label = animal)) + ##this line also needs to be modified
geom_point(color = plotcolors)
p2 <- p + geom_text_repel(max.overlaps = 80) + theme_bw()
p2
```
##Step 5: Testing out clustering of animals using hierarchical clustering
```{r}
##Hierarchical clustering without dimensionality reduction
###Specify a distance metric and a grouping method - here, use Euclidean distance and complete linkage
#By default, the hcluster function clusters the rows of a data.frame
distance_values = dist(animalmat,method = "euclidean")
clustering = hclust(distance_values,method = "complete")
dendrogram1 = as.dendrogram(clustering)
labels_colors(dendrogram1) = plotcolors[order.dendrogram(dendrogram1)]
plot(dendrogram1)
```
###Try playing around with various grouping methods
```{r}
grouping_method = "single" ###possible choices: "single", "average", "complete", "ward.D"
distance_values = dist(animalmat,method = "euclidean")
clustering = hclust(distance_values,method = grouping_method)
dendrogram1 = as.dendrogram(clustering)
labels_colors(dendrogram1) = plotcolors[order.dendrogram(dendrogram1)]
plot(dendrogram1)
```
##Cutting the tree to generate a specific number of clusters
```{r}
###Generate hierarchical tree
distance_values = dist(animalmat,method = "euclidean")
clustering <- hclust(distance_values,method = "complete")
dendrogram1 = as.dendrogram(clustering)
labels_colors(dendrogram1) = plotcolors[order.dendrogram(dendrogram1)]
plot(dendrogram1)
###Cut the tree to get 7 clusters
clusters <- cutree(clustering,k = 7)
hclust_correlation_complete_7=data.frame(cluster=clusters[clustering$order])
hclust_correlation_complete_7
```
##Cutting the tree at a specific height (distance metric)
```{r}
###Generate hierarchical tree
distance_values = dist(animalmat,method = "euclidean")
clustering <- hclust(distance_values,method = "complete")
dendrogram1 = as.dendrogram(clustering)
labels_colors(dendrogram1) = plotcolors[order.dendrogram(dendrogram1)]
plot(dendrogram1)
###Cut the tree at a specific height (3)
abline(h=3,lty=2)
clusters <- cutree(clustering, h=3)
hclust_correlation_complete_3=data.frame(cluster=clusters[clustering$order])
hclust_correlation_complete_3
```
##Check how well these clusters recapitulate the known classes
```{r}
table(hclust_correlation_complete_7[,1],animalcategories[rownames(hclust_correlation_complete_7)])
```
##K-means clustering
###The default distance metric is the Euclidean distance, whereas the algorithm can be selected
###Note that k-means clustering has randomness, since the initial cluster centers are selected randomly - to ensure reproducibility, need to set a random seed
```{r}
set.seed(0) ###setting the random seed
###k-means clustering, specifying 7 clusters
clustering_kmeans=kmeans(animalmat,center=7)
kmeans_7=data.frame(cluster=clustering_kmeans$cluster[order(clustering_kmeans$cluster)])
kmeans_7
```
##Check how well these clusters recapitulate the known classes
```{r}
table(kmeans_7[,1],animalcategories[rownames(kmeans_7)])
```
##Visualize k means clusters on the reduced dimension PCA plot
```{r}
pcs_to_plot=c(1,2)
dat = data.frame(pr1$x[,pcs_to_plot])
dat$animal=rownames(dat)
p <- ggplot(dat, aes(PC1, PC2, label = animal)) +
geom_point(color = plotcolors)
p2 <- p + geom_text_repel(max.overlaps = 80) + theme_bw() + ggtitle("Original classes")
p2
kmeans_colors=colvec[kmeans_7[rownames(dat),1]]
p <- ggplot(dat, aes(PC1, PC2, label = animal)) + ##this line also needs to be modified
geom_point(color = kmeans_colors)
p2 <- p + geom_text_repel(max.overlaps = 80) + theme_bw() + ggtitle("k-means clusters")
p2
```