knitr::opts_chunk$set(warning = FALSE, echo = FALSE, messages = FALSE)
pacman::p_load(tidyverse, bit64, patchwork, GGally, regclass, broom)

library(dplyr)
options(dplyr.summarise.inform = FALSE)

music <- read.csv('spotify-2023.csv')

Introduction

We decided to use a free Kaggle data set that was collected from Spotify’s Most Streamed Songs, taken in September 2023. It contains 948 unique songs and contains information such as artist, bpm and streaming numbers. It also contains values gathered using machine learning, including a song’s energy level, happiness and danceability. As the data is taken directly from Spotify’s charts, we suspect little sampling bias, though some could possibly arise based on the demographic of people that use Spotify. We chose this data set in order to have a better understanding and hopefully insight into what criteria play the biggest role in creating a hit song or artist. We will also be able to analyze trends in the data across categories such as key or release date. In order to clean our data set, we checked each row (song) in the data set and dropped any with non ASCII values that would cause errors in the code. We also renamed the columns of the data set for brevity.

Loading and Cleaning the Data

Analysis

This graph shows a bar chart of the 10 artists that have the most streams across all of their tracks in our data set. For each artist, the height of the bar shows the number of streams they have achieved, and annotated above the bars is the average positivity factor across all of that artists’ tracks as well as the number of songs in the data set. When creating this graph, I wanted to create a graph that serves as an introduction into our data. It expresses roughly how many songs are contained in our data for some notable artists, shows that our data contains interesting statistics like the positivity of a track, as well as providing a scale on which we can see the number of streams for the most popular artists on Spotify. I was surprised by the results of the graph for a couple reasons. I did not expect the scale for the number of streams to have to be so high for a data set containing > 1000 songs; I originally tried to set the max at 5 Billion, and was confused when my bars were all the same height. Second, I was surprised to see little correlation for the average positivity of the artists’ tracks. There’s a fairly even split of artists >50 and <50, which tells me there is little correlation between a song’s popularity and the positivity of the track

Scatterplots of Each Subjective Category

Analysis

This graph shows a set of scatterplots that have points for each song metric value on the y axis and the total number of streams on the x axis. For each metric, there is a best fit line going through the scatterplot to show any apparent trend in the values as the number of streams incresse. The data is clipped from songs with 500 M to 2.5B streams so as to make the graphs more visible, as there were few songs below and above each of those thresholds. My goal for creating this graph was to observe the average value and any trends that may occur for each metric across our data. Although each one of the best fit lines are very flat and do not appear to be great models, we can still infer some interesting things from these graphs. I was unsurprised to see the metrics with the highest average values were danceability and energy, as most popular music is upbeat and rhythmic. Similarly, I was unsurprised to see the metrics with the lowest values were instrumentalness, spoken words, and acousticness, as most popular music uses electronic sounds and has little emphasis on lyrics. However, I was surprised to see that positivity had the least correlation and an average value of almost exactly 50. There’s a common notion that sad music doesn’t become as popular as happy music, but our data shows that there is actually very little correlation.

Average Number of Streams Given Release Date

Analysis

My graphs show the average number of streams a song would receive, based on the release date of each song in the dataset. Because there were significant trends only on certain time frames, I included 3 different graphs to illustrate the difference in number of popular songs from each year. The aim of this graph is to determine what time period most popular songs are from, and to determine how factors such as seasonality might have influenced the average number of streams. The orange points on the graph each represent the average streams by songs released on that day. The smaller yellow points are each individual song. This is to determine any individual songs that are outliers in the data. The results do show some definite trends. Because all the songs in the data set were chosen by current popularity and streaming numbers, there is a heavy weight in the data set towards recently released music. In particular, the past year and a half (2022-present) has significantly more songs released in the data set. Additionally, there are far fewer songs in the data set from earlier than 2010. This suggests that most music listeners prefer to listen to music that has been released within the last 2 years. Another interesting point is that the average number of streams increases to peak around 2017 before sharply declining the more recent the song is. This could be considered a sort of “culling point”, where a songs released that are older begin to fall off the charts, unless they are gneeratioanl hits. Because recent songs have had less days to accumulate streams, their overall stream count is much lower than older songs.

Analysis

The purpose of this graph was to display the streams of the tracks grouped by their respective Key that it is written in. What I was searching for was a potential pattern of certain Keyed music being more played than the other keys. However, when inspecting the graph, it does not appear that there is any real statistical significance of how many streams a song will get based on its key. The only thing I can deduce from the plot is that key F and B have the largest average streams, while G and A have the smallest average. Additionally, G and D keys have significant outliers in the 1.5 to 3 billion range. One thing that is interesting, but somewhat unintended is that the songs without a Key assigned to them have just as much, and similar data as the ones that have a key. This is somewhat dissapointing and I may need to shift my efforts elsewhere pending feedback.

Analysis

After a somewhat disappointing boxplot in my previous code chunk, I was left unsatisfied with my efforts, and wanted to try again. This time, taking some of the feedback I received, I decided to still do a boxplot, but on a little more granular level. This time, I broke it down by the metric and metric values rather than streams. This made the data a lot easier to work with in terms of interpreting results, as I am no longer looking at a scale in billions with such minor differences to pick apart. Rather, the scale is now 0-100 representing the metric value of each metric, and I have a multiple for each metric, splitting up the results even more. As for the results, there is a lot more to interpret compared to my last attempt. For instance, the first thing anyone would notice is that instrumentalness doesn’t even have an actual boxplot. The only thing visible is the many outliers for each key, and the solid line representing the median at about zero for every key. When opening up the data in another tab and scrolling through it is very easy to pick up on why this occurs: out of the hundreds and thousands of songs in the data set, most of them have an instrumentalness value of 0. The only other scores are the outliers shown by the graph, which is why the graph is severly right-skewed. On the other hand, the multiple for positivity is a very uniform plot with no outliers present. That means across all keys, they have very similar positivity values with an IQR of about 23-75 and no outliers. Another very interesting thing to note is that danceability and energy are very similar across the board, which makes sense in the context of upbeat music. Overall, I am much happier with these results, as there is much more variation, and these graphs tell us a lot more.

Machine learning - Linear Regression

## 
## Call:
## lm(formula = danceability ~ ., data = music_linreg)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -40.414  -8.506   1.017   9.458  28.140 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      62.93820    2.64272  23.816  < 2e-16 ***
## positivity        0.25516    0.02069  12.331  < 2e-16 ***
## energy           -0.09336    0.03605  -2.590  0.00977 ** 
## acousticness     -0.15940    0.02140  -7.448 2.38e-13 ***
## instrumentalness -0.02250    0.05071  -0.444  0.65736    
## liveness         -0.08568    0.03408  -2.514  0.01213 *  
## spoken_words      0.24696    0.04560   5.416 7.96e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 12.9 on 833 degrees of freedom
## Multiple R-squared:  0.2519, Adjusted R-squared:  0.2465 
## F-statistic: 46.74 on 6 and 833 DF,  p-value: < 2.2e-16
## # A tibble: 7 × 5
##   term             estimate std.error statistic    p.value
##   <chr>               <dbl>     <dbl>     <dbl>      <dbl>
## 1 (Intercept)       62.9       2.64      23.8   0         
## 2 positivity         0.255     0.0207    12.3   0         
## 3 energy            -0.0934    0.0360    -2.59  0.00977   
## 4 acousticness      -0.159     0.0214    -7.45  0         
## 5 instrumentalness  -0.0225    0.0507    -0.444 0.657     
## 6 liveness          -0.0857    0.0341    -2.51  0.0121    
## 7 spoken_words       0.247     0.0456     5.42  0.00000008

Machine Learning Analysis

After creating the scatter plots that correlated our song metrics to streams, I didn’t have high hopes for any machine learning algorithms. The data appeared to be highly scattered and uncorrelated, which is exactly what running linear regression on the data showed. The correlation plots I created did not show strong levels of correlation compared to the example from class, with the strongest correlation being energy ~ acousticness at .6. I was curious to see how linear regression did against data that wasn’t well correlated, and the model that I ended up getting suggests that it is not very accurate at all. The model has a residual standard error of 12.9; for a column whose values are from 0-100, this means that our model is only accurate to within 12% of the true value, which is not really helpful at all. After running linear regression for every metric column, danceability yielded the lowest residual standard error with some of the others being 20+. Although we know our model is not very accurate, we can still intrepret the estimates to gain a loose understanding of how the metrics affect danceability. Songs that are more more positive and have more spoken words tend to be more danceable, with an increase of 1% in those categories correlating to a .25% increase in danceability when all other variables stay the same. The energy, instrumentalness, and liveness have a negligible effect on the danceability indicated by the very low estimate values and non-zero p-values. And lastly, songs that have more acoustic elements are prone to be less danceable by about .15% when all other variables stay the same.

Conclusions

In general, I think we expected to find more conclusive evidence to what makes a track popular. Many of the things we did to display some sort of correlation, like the scatterplots for each song metric, did not result in enough information to draw strong conclusions. We may not have had enough data to show any strong evidence or have accurate machine learning analysis, but the results we have suggest that there are too many factors in a song becoming a hit to make any generalizations or strong statements as to why those songs become popular. The release date to average streams graph shows a pattern most likely caused by music consumer trends. Most listeners prefer new music, while the most popular songs ever are all at least a few years old. While this is a noteworthy observation, it fails to shed light on any specific factor that contribute to a song’s popularity. As for the box plots, this was a good lesson in trying different way to visualize the results you want. I was really curious to see the difference in the data if it were to be grouped by key. My first thought was to visualize the amount of streams by each key in something like a box plot. When that fell through due to the lack of variation in stream counts, I was disappointed but didn’t want to give up on the idea. With a little feedback on the draft, I had the idea to still group by key, but instead make multiple box plots by metric using facet_wrap. Finally, by going to a slightly more granular level, I was able to show the variation of metric values among keys. As for the results, they were somewhat as expected as energy, positivity and danceability metrics were similar across each key..

Limitations and Recommendations

Thankfully for us, our data did not come with many limitations. Our data set was very robust and had a wide variety of data types to conduct multiple types of visualizations and analysis. We had lots of categorical variables such as artist, metric and key, as well as tons of quantitative variables such as streams, metric values, and playlist adds. We even had a binary column, Major/minor. Some limitations we did run into were the lack of data from years before 2010’s which Joseph highlighted in his visualizations, and tons of missing data highlighted by Chris’s box plots. For example, there were just as many missing keys as there were actual keys, meaning there is thousands of data points missing just in the keys column. For next time, this taught us to really inspect the data set before choosing it for a project. When it comes down to crunch time, the last thing you want to deal with is missing data and finding enough paths of analysis for said data. Overall, we did not run into any major issues that prevented us from completing the project, and we were very happy with the results.

Data Set Available At: https://www.kaggle.com/code/rafaymemon/detailed-eda-spotify-songs/input