-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path13-cluster_analysis.R
More file actions
132 lines (99 loc) · 4.83 KB
/
Copy path13-cluster_analysis.R
File metadata and controls
132 lines (99 loc) · 4.83 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
## ----echo=FALSE, eval=TRUE, message=FALSE, warning=FALSE----------------------------------------
library(knitr)
options(scipen = 999)
#This code automatically tidies code so that it does not reach over the page
opts_chunk$set(tidy.opts=list(width.cutoff=50),tidy=TRUE, rownames.print = FALSE, rows.print = 10)
opts_chunk$set(cache=T)
#knitr::clean_cache(clean = FALSE, path = opts_chunk$get("cache.path"))
set.seed(1)
## ----message=FALSE, warning=FALSE, echo=F, eval=TRUE,paged.print = FALSE------------------------
options(digits = 8)
## ----echo=FALSE,out.width = '25%',fig.align='center',fig.cap = ""-------------------------------
knitr::include_graphics("./images/cluster.PNG")
## -----------------------------------------------------------------------------------------------
load(url("https://github.com/WU-RDS/MRDA2021/raw/main/trackfeatures.RData"))
# remove duplicates
tracks <- na.omit(tracks[!duplicated(tracks$isrc), ])
## -----------------------------------------------------------------------------------------------
library(ggplot2)
library(stringr)
robin_schulz <- tracks[str_detect(tracks$artistName, "Robin Schulz"), ]
robin_schulz$artist <- "Robin Schulz"
adele <- tracks[str_detect(tracks$artistName, "Adele"), ]
adele$artist <- "Adele"
example_tracks <- rbind(robin_schulz, adele)
ggplot(example_tracks, aes(x = energy, y = acousticness, color = artist)) +
geom_point() +
theme_bw()
## -----------------------------------------------------------------------------------------------
tracks_scale <- data.frame(artist = example_tracks$artist, energy = scale(example_tracks$energy), acousticness = scale(example_tracks$acousticness))
tracks_scale <- na.omit(tracks_scale)
kmeans_clusters <- kmeans(tracks_scale[-1], 2)
kmeans_clusters$centers
## -----------------------------------------------------------------------------------------------
tracks_scale$cluster <- as.factor(kmeans_clusters$cluster)
ggplot(tracks_scale, aes(x = energy, y = acousticness, color = cluster, shape = artist)) +
geom_point(size = 3) +
theme_bw()
table(tracks_scale$artist, tracks_scale$cluster)
## ----echo=TRUE, results='hide', cache=TRUE------------------------------------------------------
library(NbClust)
famous_artists <- c(
'Ed Sheeran',
'Eminem',
'Rihanna',
'Taylor Swift',
'Queen'
)
famous_tracks <- tracks[tracks$artistName %in% famous_artists, ]
famous_tracks_scale <- scale(famous_tracks[4:ncol(famous_tracks)])
set.seed(123)
opt_K <- NbClust(famous_tracks_scale, method = "kmeans", max.nc = 10)
## -----------------------------------------------------------------------------------------------
table(opt_K$Best.nc["Number_clusters",])
## -----------------------------------------------------------------------------------------------
kmeans_tracks <- kmeans(famous_tracks_scale, 3)
kmeans_tracks$centers
## -----------------------------------------------------------------------------------------------
library(ggiraph)
library(ggiraphExtra)
centers <- data.frame(kmeans_tracks$centers)
centers$cluster <- 1:3
ggRadar(centers, aes(color = cluster), rescale = FALSE) +
ggtitle("Centers") +
theme_bw()
## -----------------------------------------------------------------------------------------------
famous_tracks$cluster <- as.factor(kmeans_tracks$cluster)
ggplot(famous_tracks, aes(y = cluster, fill = artistName)) +
geom_bar() +
theme_bw()
table(famous_tracks$artistName, famous_tracks$cluster)
## -----------------------------------------------------------------------------------------------
recommendation <- famous_tracks[str_detect(famous_tracks$trackName, "Lose Yourself|I Forgot That You Existed|The Archer"),]
recommendation[c("trackName", "artistName", "cluster")]
ggplot(recommendation, aes(instrumentalness, speechiness, color = cluster)) +
geom_point() +
geom_label(aes(label=trackName), hjust = "inward") +
theme_bw()
## ---- message=FALSE,warning=FALSE---------------------------------------------------------------
library(factoextra)
fviz_cluster(kmeans_tracks, data = famous_tracks_scale,
palette = hcl.colors(3, palette = "Dynamic"),
geom = "point",
ellipse.type = "convex",
ggtheme = theme_bw()
)
## ---- fig.width=15, fig.height=10---------------------------------------------------------------
pf_ri <- tracks[tracks$artistName %in% c("Pink Floyd", "Rihanna"),]
pf_ri_scale <- scale(pf_ri[,4:ncol(pf_ri)])
rownames(pf_ri_scale) <- pf_ri$trackName
hclust_tracks <- hclust(dist(pf_ri_scale))
plot(hclust_tracks)
## -----------------------------------------------------------------------------------------------
hclusters <- cutree(hclust_tracks,4)
pf_ri_hier <- data.frame(pf_ri_scale)
pf_ri_hier$cluster <- as.factor(hclusters)
hier_centers <- aggregate(. ~ cluster, pf_ri_hier, mean)
ggRadar(hier_centers, aes(color = cluster), rescale = T) +
ggtitle("Centers") +
theme_bw()