-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify_taste_analysis.R
192 lines (168 loc) · 7.94 KB
/
spotify_taste_analysis.R
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
library(ggplot2)
setwd("C:/Users/Charl/Programming/SpotifyPlaylistSplitter")
# Function to compute mean, median, and standard deviation excluding the diagonal
row_stats <- function(row) {
mean_val <- mean(row)
median_val <- median(row)
return(c(mean_val, median_val))
}
# Function to compute percentile
compute_percentile_ratio <- function(value) {
percentile <- sum(user_song_popularity$Ratio <= value['Ratio']) / length(user_song_popularity$Ratio) * 100
return(percentile)
}
# Function to compute percentile
compute_percentile_interesting <- function(value) {
percentile <- sum(user_song_popularity$interest_cons <= value['interest_cons']) / length(user_song_popularity$interest_cons) * 100
return(percentile)
}
# Function to compute percentile
compute_percentile_combined <- function(value) {
percentile <- sum(user_song_popularity$'Combined Score' <= value['Combined Score']) / length(user_song_popularity$'Combined Score') * 100
return(percentile)
}
song_matrix <- readRDS("user_song_adj_minsong2.rds")
#adjacency matrix of how songs link users
adjacency_matrix <- t(song_matrix) %*% song_matrix #user-user sparse adjacency matrix
#adjacency_matrix <- as.matrix(adjacency_matrix)
total_user_songs <- diag(adjacency_matrix)
diag(adjacency_matrix) <- 0
######## Calculates some statistics about user-user adjacency
common_songs <- rowSums(adjacency_matrix)
song_choice_stats <- t(apply(adjacency_matrix, 1, row_stats))
song_choice_ratio <- common_songs / total_user_songs
user_song_popularity <- cbind(total_user_songs, common_songs, song_choice_ratio, song_choice_stats)
colnames(user_song_popularity) <- c("Total songs", "Common songs", "Ratio", "Mean", "Median")
rownames(user_song_popularity) <- rownames(adjacency_matrix)
user_song_popularity <- as.data.frame(user_song_popularity)
###### Create matrix for users with unusual taste
interesting_users <- rownames(user_song_popularity[user_song_popularity$Ratio <= 14.5 & user_song_popularity$`Total songs`>100,])
interesting_users <- as.character(interesting_users) # Convert to character vector for subsetting
adjacency_matrix_subset <- adjacency_matrix[, interesting_users]
interest_cons <- rowSums(adjacency_matrix_subset)/user_song_popularity$'Total songs'
user_song_popularity$interest_cons <- interest_cons
####### Plot mean songs in common
# Compute density estimate
my_position <- user_song_popularity["Charlie",]$Mean
percentile <- sum(user_song_popularity$Mean <= my_position) / length(user_song_popularity$Mean) * 100
percentile <- sprintf("%.1f", percentile)
ggplot(user_song_popularity, aes(x = Mean)) +
geom_histogram(binwidth = 1, fill = "lightgreen", color = "black", alpha = 0.7) +
labs(title = "",#Histogram of Mean Number of Common Songs",
x ="",# "Mean Number of Common Songs",
y ="")+# "Frequency") +
theme_minimal() +
coord_cartesian(xlim = c(0, 30)) +
theme(
plot.background = element_rect(fill = "black"),
panel.background = element_rect(fill = "black"),
plot.title = element_text(hjust = 0.5),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(color = "white"),
text = element_text(color = "white"),
axis.text = element_text(color = 'white')
)
####### Plot Ratio songs in common
my_position <- user_song_popularity["Charlie",]$Ratio
percentile <- sum(user_song_popularity$Ratio <= my_position) / length(user_song_popularity$Ratio) * 100
percentile <- sprintf("%.1f", percentile)
percentiles <- apply(user_song_popularity, 1, compute_percentile_ratio)
user_song_popularity$'Ratio Percentile' <- percentiles
ggplot(user_song_popularity, aes(x = Ratio)) +
geom_histogram(binwidth = 10, fill = "lightgreen", color = "black", alpha = 0.7) +
labs(title = "",#Histogram of Mean Number of Common Songs",
x ="",# "Mean Number of Common Songs",
y ="")+# "Frequency") +
theme_minimal() +
coord_cartesian(xlim = c(0, 800)) +
theme(
plot.background = element_rect(fill = "black"),
panel.background = element_rect(fill = "black"),
plot.title = element_text(hjust = 0.5),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(color = "white"),
text = element_text(color = "white"),
axis.text = element_text(color = 'white')
)
####### Plot interesting user connections
# Compute density estimate
my_position <- user_song_popularity["Charlie",]$interest_cons
percentile <- sum(user_song_popularity$interest_cons <= my_position) /
length(user_song_popularity$interest_cons) * 100
percentile <- sprintf("%.1f", percentile)
user_song_popularity$'Interesting Percentile' <-
apply(user_song_popularity, 1, compute_percentile_interesting)
ggplot(user_song_popularity, aes(x = interest_cons)) +
geom_histogram(binwidth = 0.01, fill = "lightgreen", color = "black", alpha = 0.7) +
labs(title = "",#Histogram of Mean Number of Common Songs",
x ="",# "Mean Number of Common Songs",
y ="")+# "Frequency") +
theme_minimal() +
coord_cartesian(xlim = c(0, 1.05)) +
theme(
plot.background = element_rect(fill = "black"),
panel.background = element_rect(fill = "black"),
plot.title = element_text(hjust = 0.5),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(color = "white"),
text = element_text(color = "white"),
axis.text = element_text(color = 'white')
)
user_song_popularity$'Combined Score' <-
(user_song_popularity$'Interesting Percentile'/100)*
(user_song_popularity$'Ratio Percentile'/100)
user_song_popularity$'Comb score Percentile' <-
apply(user_song_popularity, 1, compute_percentile_combined)
user_song_popularity['Charlie',]
####### Plot interesting user connections
# Compute density estimate
my_position <- user_song_popularity["Charlie",]$'Combined Score'
percentile <- sum(user_song_popularity$'Combined Score' <= my_position) /
length(user_song_popularity$'Combined Score') * 100
percentile <- sprintf("%.1f", percentile)
ggplot(user_song_popularity, aes(x = `Combined Score`)) +
geom_histogram(binwidth = 0.02, fill = "lightgreen", color = "black", alpha = 0.7) +
labs(title = "", # Histogram of Mean Number of Common Songs",
x = "", # "Mean Number of Common Songs",
y = "") + # "Frequency") +
theme_minimal() +
coord_cartesian(xlim = c(0, 1)) +
theme(
plot.background = element_rect(fill = "black"),
panel.background = element_rect(fill = "black"),
plot.title = element_text(hjust = 0.5),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(color = "white"),
text = element_text(color = "white"),
axis.text = element_text(color = 'white')
)
cov(user_song_popularity[,c('Interesting Percentile','Ratio Percentile')])
index_X <- which(rownames(user_song_popularity) == "Charlie")
colors <- rep("white", nrow(user_song_popularity))
colors[index_X] <- "green"
sizes <- rep(0.1, nrow(user_song_popularity))
sizes[index_X] <- 3
plot(user_song_popularity$`Ratio Percentile`,
user_song_popularity$`Interesting Percentile`,
pch = 16, # Use dots for points
col = colors, # White points
cex = sizes,
bg = "black", # Background color (only affects the plotting area)
xlab = "", # Remove x-axis label
ylab = "", # Remove y-axis label
main = "", # Remove title
axes = FALSE, # Remove axis labels
#panel.first = par(bg = "black") # Set background color for the entire plot
)
filtered_users = user_song_popularity[user_song_popularity$'Total songs' > 20, ]
# Identify the user with the maximum Comb Score Percentile
best_taste_user <-rownames(filtered_users[which.max(filtered_users$'Comb score Percentile'),])
songs = rownames(song_matrix[song_matrix[,best_taste_user]>0,])
unique(sapply(strsplit(songs, " - "), `[`, 1))
worst_taste_user <-rownames(filtered_users[which.min(filtered_users$'Comb score Percentile'),])
songs = rownames(song_matrix[song_matrix[,worst_taste_user]>0,])
unique(sapply(strsplit(songs, " - "), `[`, 1))