Spotify is one of the leading companies among the music industry. On Spotify, one can search for music by a specific artist or by the title of the song. However, one can also discover new music based on the genre of music and playlists. In this analysis, I will aim to answer several questions based on the Spotify API data set. For example, if you ever wondered what the popularity of a song would be depending on different variables such as danceability, tempo, loudness, and etc. Another question to be answered is how the popularity of genres change based on the year the albums came out. Lastly, I will look at the different genres and look for potential attributes among those genres. For example, are there any key differences among the genres to identify latin music?
I plan on answering these questions by looking at most of the variables in the dataset of 23 variables. I plan on cleaning the data initially to remove missing values and low values that could be potential outliers. From here, I will run a regression analysis to predict the popularity of a song which answers the main question of this analysis. Next, I will factor the year the songs’ albums came out and see if there are any years that have the highest popularity based on genre as well. Lastly, I will look at probability density graphs of the quantitative variables grouped by genre to see if there are any significant identifying factors for each genre.
library(stats) #To perform our regression analysis.
library(Rspotify) #Access to Spotify API, contains our data
## Warning: package 'Rspotify' was built under R version 4.2.3
library(DT) #to create a nice datatable for a preview of our data
## Warning: package 'DT' was built under R version 4.2.3
library(date) #to use the as.Date() to convert characters to dates for the 2nd and third part of the analysis
library(plotly) #to allow user interaction on plots and to turn ggplots into plotly plots
## Warning: package 'plotly' was built under R version 4.2.3
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.2.3
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(ggplot2) #to make probability density plots
This data comes from the spotifyr package which was produced by Charlie Thompson, Josiah Parry, Donal Phipps, and Tom Wolff. This data was used by Kaylin Pavlik to come up with conclusions about how to classify genres based on the Spotify API which keeps track of audio features for each song. This data contains 32834 songs and 23 variables based on each song. The 23 variables contain multiple data types such as numeric, characteristic, and dates. It seems that this data was generated in 2020 as that it is the most recent year the songs in our data came out. More information about the dataset and how the variables are measured can be found here.
To import the data, we will use the following code:
spotify_songs=as.data.frame(read.csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-01-21/spotify_songs.csv'))
dim(spotify_songs)
## [1] 32833 23
Data cleaning was already done by Kaylin Pavlik but I will include some additional data cleaning. For the variable danceability, there is a track that corresponds with a danceability value of 0 which seems very unlikely so I will remove it from our data. For the variable valence, its a measure from 0 to 1 describing the musical positiveness, so I will remove the low values below .01 because they seem very low and unlikely. These values include .00000 and .00001 which I would consider outliers. For the variable energy, it measures a perceptual measure of intensity and activity. I will remove the values below .01 because they also seem unlikely. For the variable liveness, it measures the presence of an audience in the recording, so I will also remove the values below .01 because they seem unlikely. Lastly, for the variable duration_ms, it measures the duration of the song in milliseconds. I will remove the values below 25000 ms since this corresponds to 25 seconds which seems like a reasonable length to be considered a song.
which(spotify_songs$danceability<.01)
## [1] 11364
which(spotify_songs$valence<.01)
## [1] 11364 17003 17005 17010 17013 17015
which(spotify_songs$energy<.01)
## [1] 1517 17019
which(spotify_songs$liveness<.01)
## [1] 10989 11364 20184
which(spotify_songs$duration_ms<25000)
## [1] 11364
From this R code, we see that we need to remove rows 1517, 10989, 11364, 17003, 17005, 17010, 17013, 17015, 17019, and 20184.
spotify_songs=spotify_songs[-c(1517, 10989, 11364, 17003, 17005, 17010, 17013, 17015, 17019, 20184),]
dim(spotify_songs)
## [1] 32823 23
From this, we see that we have removed 10 rows corresponding to the conditions used for data cleaning. Now, we will check to see if any rows are missing data.
which(complete.cases(spotify_songs)==F)
## [1] 8151 9282 9283 19560 19803
We see that there are missing values for rows 8151, 9282, 9283, 19560, and 19803. Hence, we will remove these since there are only 5 of them.
spotify_songs=spotify_songs[-c(8151,9282,9283,19560,19803),]
dim(spotify_songs)
## [1] 32818 23
Now that we have removed missing values and did data cleaning by conditions, we will look at the most condensed form of our clean data.
datatable(head(spotify_songs,30))
Now that the data is cleaned, we will begin our analysis by looking at our first question of predicting the popularity of a song. To start, I will run a linear regression model with popularity as our response variable and danceability, energy, key, loudness, mode, speechiness, acousticness, instrumentalness, liveness, valence, tempo, duration_ms, and playlist_genre. In order to run regression with the categorical variable genre, we have to create a dummy variable for it which is the code below.
spotify_dummy=spotify_songs
spotify_dummy$playlist_genre[spotify_dummy$playlist_genre=="pop"]=1
spotify_dummy$playlist_genre[spotify_dummy$playlist_genre=="latin"]=2
spotify_dummy$playlist_genre[spotify_dummy$playlist_genre=="edm"]=3
spotify_dummy$playlist_genre[spotify_dummy$playlist_genre=="r&b"]=4
spotify_dummy$playlist_genre[spotify_dummy$playlist_genre=="rap"]=5
spotify_dummy$playlist_genre[spotify_dummy$playlist_genre=="rock"]=6
spotify_dummy$playlist_genre=as.numeric(spotify_dummy$playlist_genre)
#factor(spotify_songs$playlist_genre)
Now that the dummy variable has been created, we can get into the regression analysis. We will start by doing the full model and then performing stepwise regression to find the best model.
fullmodel=lm(spotify_dummy$track_popularity~spotify_dummy$playlist_genre+spotify_dummy$danceability+spotify_dummy$energy+spotify_dummy$key+spotify_dummy$loudness+spotify_dummy$mode
+spotify_dummy$mode+spotify_dummy$speechiness+spotify_dummy$acousticness+spotify_dummy$instrumentalness+spotify_dummy$liveness+spotify_dummy$valence+spotify_dummy$tempo+spotify_dummy$duration_ms)
reduced=step(fullmodel,direction="backward")
## Start: AIC=208738.1
## spotify_dummy$track_popularity ~ spotify_dummy$playlist_genre +
## spotify_dummy$danceability + spotify_dummy$energy + spotify_dummy$key +
## spotify_dummy$loudness + spotify_dummy$mode + spotify_dummy$mode +
## spotify_dummy$speechiness + spotify_dummy$acousticness +
## spotify_dummy$instrumentalness + spotify_dummy$liveness +
## spotify_dummy$valence + spotify_dummy$tempo + spotify_dummy$duration_ms
##
## Df Sum of Sq RSS AIC
## - spotify_dummy$key 1 763 18970471 208737
## <none> 18969709 208738
## - spotify_dummy$mode 1 4668 18974377 208744
## - spotify_dummy$danceability 1 8306 18978014 208750
## - spotify_dummy$acousticness 1 8882 18978591 208751
## - spotify_dummy$tempo 1 9606 18979315 208753
## - spotify_dummy$speechiness 1 10463 18980172 208754
## - spotify_dummy$valence 1 13182 18982891 208759
## - spotify_dummy$liveness 1 14421 18984129 208761
## - spotify_dummy$playlist_genre 1 17790 18987499 208767
## - spotify_dummy$instrumentalness 1 215900 19185608 209107
## - spotify_dummy$duration_ms 1 219234 19188943 209113
## - spotify_dummy$loudness 1 304062 19273771 209258
## - spotify_dummy$energy 1 356681 19326390 209347
##
## Step: AIC=208737.4
## spotify_dummy$track_popularity ~ spotify_dummy$playlist_genre +
## spotify_dummy$danceability + spotify_dummy$energy + spotify_dummy$loudness +
## spotify_dummy$mode + spotify_dummy$speechiness + spotify_dummy$acousticness +
## spotify_dummy$instrumentalness + spotify_dummy$liveness +
## spotify_dummy$valence + spotify_dummy$tempo + spotify_dummy$duration_ms
##
## Df Sum of Sq RSS AIC
## <none> 18970471 208737
## - spotify_dummy$mode 1 4163 18974634 208743
## - spotify_dummy$danceability 1 8274 18978746 208750
## - spotify_dummy$acousticness 1 8944 18979415 208751
## - spotify_dummy$tempo 1 9538 18980009 208752
## - spotify_dummy$speechiness 1 10381 18980853 208753
## - spotify_dummy$valence 1 13306 18983777 208758
## - spotify_dummy$liveness 1 14424 18984895 208760
## - spotify_dummy$playlist_genre 1 17824 18988295 208766
## - spotify_dummy$instrumentalness 1 215736 19186207 209106
## - spotify_dummy$duration_ms 1 218827 19189298 209112
## - spotify_dummy$loudness 1 303887 19274359 209257
## - spotify_dummy$energy 1 356317 19326789 209346
summary(reduced)
##
## Call:
## lm(formula = spotify_dummy$track_popularity ~ spotify_dummy$playlist_genre +
## spotify_dummy$danceability + spotify_dummy$energy + spotify_dummy$loudness +
## spotify_dummy$mode + spotify_dummy$speechiness + spotify_dummy$acousticness +
## spotify_dummy$instrumentalness + spotify_dummy$liveness +
## spotify_dummy$valence + spotify_dummy$tempo + spotify_dummy$duration_ms)
##
## Residuals:
## Min 1Q Median 3Q Max
## -59.409 -17.553 2.875 18.845 65.206
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 7.982e+01 1.682e+00 47.465 < 2e-16 ***
## spotify_dummy$playlist_genre -4.639e-01 8.356e-02 -5.552 2.85e-08 ***
## spotify_dummy$danceability 4.035e+00 1.067e+00 3.783 0.000155 ***
## spotify_dummy$energy -2.977e+01 1.199e+00 -24.823 < 2e-16 ***
## spotify_dummy$loudness 1.493e+00 6.511e-02 22.924 < 2e-16 ***
## spotify_dummy$mode 7.217e-01 2.690e-01 2.683 0.007300 **
## spotify_dummy$speechiness -5.835e+00 1.377e+00 -4.237 2.27e-05 ***
## spotify_dummy$acousticness 2.883e+00 7.332e-01 3.933 8.41e-05 ***
## spotify_dummy$instrumentalness -1.217e+01 6.303e-01 -19.315 < 2e-16 ***
## spotify_dummy$liveness -4.405e+00 8.820e-01 -4.994 5.94e-07 ***
## spotify_dummy$valence 3.079e+00 6.420e-01 4.797 1.62e-06 ***
## spotify_dummy$tempo 2.073e-02 5.105e-03 4.061 4.89e-05 ***
## spotify_dummy$duration_ms -4.448e-05 2.287e-06 -19.453 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 24.05 on 32805 degrees of freedom
## Multiple R-squared: 0.07368, Adjusted R-squared: 0.07334
## F-statistic: 217.4 on 12 and 32805 DF, p-value: < 2.2e-16
From our reduced model summary, we can take a brief look at the R-squared value which is .07368 which is not great. Our model is fairly weak. We will look at a few residual plots next.
par(mfrow=(c(2,2)))
plot(reduced)
From our residual plots, we see that the residuals don’t look good. We have a funnel pattern in our residual vs fitted plot. Our normal Q-Q plot looks okay. Our standardized residuals versus the fitted values looks okay. Our residuals vs leverage also doesn’t look good. There are quite a few points that should be considered leverage points and could be removed from the model. Lastly, we will look at the regression equation to answer an example question of how to use this equation.
coef(reduced)
## (Intercept) spotify_dummy$playlist_genre
## 7.981962e+01 -4.638896e-01
## spotify_dummy$danceability spotify_dummy$energy
## 4.035294e+00 -2.977201e+01
## spotify_dummy$loudness spotify_dummy$mode
## 1.492501e+00 7.216794e-01
## spotify_dummy$speechiness spotify_dummy$acousticness
## -5.835417e+00 2.883465e+00
## spotify_dummy$instrumentalness spotify_dummy$liveness
## -1.217358e+01 -4.404796e+00
## spotify_dummy$valence spotify_dummy$tempo
## 3.079414e+00 2.073104e-02
## spotify_dummy$duration_ms
## -4.448059e-05
Our regression equation is popularity= 79.82-.464*playlist_genre+4.035*danceability-29.77*energy+1.493*loudness+.7217*mode-5.835*speechiness+2.883*accousticness-12.174*instrumentalness -4.405*liveness+3.079*valence+.02073*tempo-.00004448*duration_ms.
What is the popularity of a song when it’s playlist genre is pop, the danceability is .7, the energy is .7, the loudness is -20, the mode is 1, the speechiness is .5, the accousticness is .7, the instrumentalness is .7, the liveness is .3, the valence is .7, the tempo is 150, and the duration in milliseconds is 180000?
79.82-0.464*1+4.035*0.7-29.77*0.7+1.493*(-20)+0.7217*1-5.835*(0.5)+2.883*(0.7)-12.174*0.7 -4.405*0.3+3.079*0.7+0.02073*150-0.00004448*180000
## [1] 18.7189
From our R code, we see that the popularity is 18.7189. So our equation works with those numbers, but since our r-square is so low and our residuals are not adequate, I would not rely on it. Especially when we get loudness values approaching -40db as this can cause for our popularity to be a negative number even though it is measured from 0 to 100 only.
Next, we will look at the second question which deals with the year the songs album came out, the playlist genre, and the songs popularity. In order to do this, we need to convert our track_album_release_date to dates. However, some of the dates just list the year and not the exact date of the year, so we have to fix this by inserting 01-01 as the month and day of the year. The following R code will do so:
new_track_album_release_date=ifelse(nchar(spotify_songs$track_album_release_date)==4,paste0(spotify_songs$track_album_release_date,"-01-01"),spotify_songs$track_album_release_date)
#new_track_album_release_date
spotify_songs$track_album_release_date=as.Date(new_track_album_release_date)
class(spotify_songs$track_album_release_date)
## [1] "Date"
Now that we have converted the track_album_release_date to dates, we can start graphing.
plot_ly(spotify_songs,x=~track_album_release_date,y=~track_popularity,color=~playlist_genre,type="scatter",
mode="markers",symbol=~playlist_genre) %>%
layout(xaxis=list(title="Track Album Release Date",type="date"),
yaxis=list(title="Track Popularity"))
## Warning: Ignoring 31 observations
The above code generates a very useful scatter plot that has the Track Album Release Date on the x-axis and the Track popularity on the y-axis. Plotly allows for us to move the y-axis up and down or to click and drag next to the y-axis to look at a range, such as 40 to 80 track popularity. We can also do the same with the x-axis. We can also look at each playlist genre on its own by clicking on the legend to remove the groups that are not of concern to us. I will attach the graphs for each playlist genre below with the default ranges.
Above, we are looking at the rock genre, we see that over the range of our data, it has remained fairly stable in terms of popularity over the decades. A majority of the data points lie within 0 and 80 popularity from 1960 to 2020. There is also a minority of points that are above the 80 track popularity line. It is safe to say that over the decades, rock music has had its mix of being both fairly popular and not so popular depending on the track, with some exceptions that have been extremely popular.
Above, we are looking at the rap genre. We can see from the graph that while there is a minority of tracks before 1990, the majority of rap in our dataset comes post 1990. The track popularity has a majority of points from 0 to 80 popularity between 1990 and 2020. An interesting trend is that post 2015 we see a lot of rap songs go above 80 popularity suggesting that the popularity is increasing in the past couple of years.
Above is the graph for r&b. We see that there is a good amount of songs before 1985, but post 1985 is where most of our sample comes from. We see that the songs popularity remains fairly the same overall ranging from 0 to 80 until 2015 where there is a surge in popularity above 80.
Above is the graph for pop music. We see that our sample includes pop music from 1970 to 2020 with a majority of them being post 1980. Again, we see that the popularity ranges from 0 to 80 until about 2010 where there is a surge in pop songs above 80 popularity.
Above we see the graph for latin music. Our sample ranges from about 1965 to 2020 with a majority of latin songs being post 1980. Again we see a consistent range from 0 to 80 until 2015 where we see a surge above 80 popularity.
Above we see the graph for edm music. Our data ranges from 1975 to 2020 with a majority of edm songs being post 2000. Again we see about the same trend where edm ranges from 0 to 80 popularity until about 2010 where there is a good amount of points above 80 popularity.
Now that we have looked at all 6 graphs, I think it is fair to say that the general trend for each type of music besides rock is that they start to have an increasing amount of popularities above 80 post 2010 or 2015 depending on the type. They all also maintain the popularity scaling from 0 to 80 over the decades. For rock music, they have a number of songs above 80 popularity over the decades but no sudden surge in songs above 80 popularity like the other genres.
Now, I will show a bar graph which is useful at showing how the total popularity changes during each year for each genre. First, I have convert the dates so it will become yearly data which is what the code below does. It turns all the 2019 songs into having the date 01-01-2019 and does this for each year.
spotify_songs$track_album_release_date=as.Date(cut(spotify_songs$track_album_release_date,breaks="year"))
plot_ly(spotify_songs,x=~track_album_release_date,y=~track_popularity,color=~playlist_genre,type="bar") %>%
layout(xaxis=list(title="Track Album Release Date",type="date"),
yaxis=list(title="Track Popularity"))
## Warning: Ignoring 31 observations
We can use this graph to look at how the overall popularity goes up and down per genre over each year. Below are the graphs of the 6 genres and we will see if they match the trends identified from the scatter plot. It is important to note that 2020 wasn’t a full year for this dataset so it won’t have as tall of heights.
Above is the overall popularity per year for rock music. We can see that the overall popularity for each year remains very steady with some years going up and other years going down. It’s noticeable here that in the year 2019 we see a big increase in the popularity of rock music. This is the same trend as the scatter plot besides the spike in rock music popularity for 2019.
Above is the overall popularity per year for rap music. We see that it is fairly the same up until 2010 where the bars start to increase in height which means an increase in popularity with a peak in 2019. This is the same trend that we identified in the scatter plot.
Above is the overall popularity per year for r&b music. We see that it is fairly the same height up until 2010 where the bars start to increase until it peaks in 2019. This suggests that after 2010 there was an increase in popularity for r&b music which is what we identified in the scatter plot as well.
Above is the overall popularity per year for pop music. We see that the bars are about the same height up until 2000 where there starts to be an increase in height until it reaches its peak in 2019. After 2010, the heights start rapidly increasing which means that there was an increase in popularity after 2010. This is the same trend as identified in the scatter plot.
Above is the overall popularity per year for latin music. We see that the heights start to increase at 2000 and they start rapidly increasing after 2014 and reaches its peak in 2019. This suggests that there was an increase in popularity post 2014 which is what we saw in the scatter plot.
Lastly, we have the overall popularity for edm music per year. It starts to have noticeable heights in 2008 which leads to it increasing until it reaches its peak in 2019. This implies that the popularity increases rapidly post 2008 and especially after 2013 which is what we saw in the scatter plot.
Through the interactive scatter plot, I have shown how the track popularity changes per genre with the track album release date specifically when tracks start to have popularities above 80 and allowed for us to identify some trends in our data. The bar chart confirmed these trends by allowing us to see how the total popularity changed per year for each genre. It is safe to say from all the bar charts that all genres had an increase in popularity leading up to the year 2019 where each genre had its peak popularity which answers the question we wanted to answer. Also, looking at the peaks for each genre in 2019, we see that latin had the most total track popularity, followed by edm, rap, pop, r&b and then rock.
The last thing we want to answer is if there are any identifying factors for each genre. To answer this, I will graph the probability densities for each variable per genre.
dance=ggplot(spotify_songs,aes(x=danceability,fill=playlist_genre))+geom_density(alpha=.4)+ggtitle("Danceability probability densities per genre")+
labs(x="Danceability",y="Kernel estimate")
ggplotly(dance)
The above graph was generated using ggplot to plot the data and then was converted to a plotly graph so we can interact and look at individual genres on their own if we wish. This is the probability density graph for danceability. Danceability is how suitable a track is for dancing and is measured from 0 to 1. Looking at this graph, we see that latin music has the highest probability for the highest danceability. We see that rock has the highest probability for the lowest danceability.
energy=ggplot(spotify_songs,aes(x=energy,fill=playlist_genre))+geom_density(alpha=.4)+ggtitle("Energy probability densities per genre")+
labs(x="Energy",y="Kernel estimate")
ggplotly(energy)
The above graph shows the energy probability densities per genre. Energy is a perceptual measure of intensity and activity which is measured from 0 to 1. We see that edm has the highest probability for a high energy measure. We also see that r&b has the highest probability for an energy between .25 and .5.
key=ggplot(spotify_songs,aes(x=key,fill=playlist_genre))+geom_density(alpha=.4)+ggtitle("Key probability densities per genre")+
labs(x="key",y="Kernel estimate")
ggplotly(key)
The above graph shows the key probability densities per genre. Key is the overall key of the track. Each key gets it’s own number starting with 0 being C. We see that for a low key, the highest probability belongs to rap. If the overall key of the song is C/ C#/Dflat/D then it has the highest probability to be rap. If a key is around 10, it also has the highest probability to be rap. If a key is around 9, it has the highest probability to be rock.
loudness=ggplot(spotify_songs,aes(x=loudness,fill=playlist_genre))+geom_density(alpha=.4)+ggtitle("Loudness probability densities per genre")+
labs(x="Loudness",y="Kernel estimate")
ggplotly(loudness)
The above graph is the probability densities per genre for loudness. Loudness is how loud a track is in decibels. We see that if a song has a -5 dB level, it has the highest probability to be edm.
mode=ggplot(spotify_songs,aes(x=mode,fill=playlist_genre))+geom_density(alpha=.4)+ggtitle("Mode probability densities per genre")+
labs(x="Mode",y="Kernel estimate")
ggplotly(mode)
The above graph is the probability densities per genre for mode. Mode is either major (1) or minor (0). We see that if a song’s mode is major, then it has the highest probability to be rock.
speech=ggplot(spotify_songs,aes(x=speechiness,fill=playlist_genre))+geom_density(alpha=.4)+ggtitle("Speechiness probability densities per genre")+
labs(x="Speechiness",y="Kernel estimate")
ggplotly(speech)
Above are the probability densities per genre for speechiness. Speechiness is the presence of spoken words in the track measured from 0 to 1. If a song has a low speechiness level, it has the highest probability to be rock. If a song has a speechiness level between .25 and .5, it has the highest probability to be rap.
acoustic=ggplot(spotify_songs,aes(x=acousticness,fill=playlist_genre))+geom_density(alpha=.4)+ggtitle("Acousticness probability densities per genre")+
labs(x="Acousticness",y="Kernel estimate")
ggplotly(acoustic)
Above is the probability densities per genre for acousticness. Acousticness is a confidence measure if the track is acoustic measuring from 0 to 1. If a song has a low acousticness, then it has the highest probability to belong to edm.
instrumental=ggplot(spotify_songs,aes(x=instrumentalness,fill=playlist_genre))+geom_density(alpha=.4)+ggtitle("Instrumentalness probability densities per genre")+
labs(x="Instrumentalness",y="Kernel estimate")
ggplotly(instrumental)
Above is the probability densities per genre for instrumentalness. Instrumentalness is a measure that predicts whether a track contains no vocals, measured from 0 to 1. We cannot really identify any significant differences among the genres.
live=ggplot(spotify_songs,aes(x=liveness,fill=playlist_genre))+geom_density(alpha=.4)+ggtitle("Liveness probability densities per genre")+
labs(x="Liveness",y="Kernel estimate")
ggplotly(live)
Above are the probability densities per genre for liveness. Liveness is a measure to track the presence of an audience in the recording. We cannot identify any significant differences among the genres.
valence=ggplot(spotify_songs,aes(x=valence,fill=playlist_genre))+geom_density(alpha=.4)+ggtitle("Valence probability densities per genre")+
labs(x="Valence",y="Kernel estimate")
ggplotly(valence)
Above are the probability densities per genre for valence. Valence is the musical positiveness conveyed by a track measured from 0 to 1. If a song has low valence, it has the highest probability to be edm. If a song has a high valence, it has the highest probability to be latin.
tempo=ggplot(spotify_songs,aes(x=tempo,fill=playlist_genre))+geom_density(alpha=.4)+ggtitle("Tempo probability densities per genre")+
labs(x="Tempo",y="Kernel estimate")
ggplotly(tempo)
Above are the probability densities per genre for tempo. Tempo is the beats per minute. If a song has a tempo of around 125, it has the highest probability to be edm.
duration=ggplot(spotify_songs,aes(x=duration_ms,fill=playlist_genre))+geom_density(alpha=.4)+ggtitle("Duration(ms) probability densities per genre")+
labs(x="Duration(ms)",y="Kernel estimate")
ggplotly(duration)
Above are the probability densities per genre for duration. Duration is the length of the song in milliseconds. If a song has a duration of 200000 ms, it has the highest probability to be a pop song.
pop=ggplot(spotify_songs,aes(x=track_popularity,fill=playlist_genre))+geom_density(alpha=.4)+ggtitle("Track popularity probability densities per genre")+
labs(x="Track popularity",y="Kernel estimate")
ggplotly(pop)
Above are the probability densities per genre for track popularity. Popularity is the song popularity measured from 0 to 100. If a track has a popularity between 0 and 4, it has the highest probability to be edm. If a track has a popularity between 12 and 40, it has the highest probability to be edm. If a track has a popularity between 43 and 60, it has the highest probability to be rap.
Now that we have analyzed all the probability density graphs, we will look at identifying factors of each genre. For pop, they have the highest probability to be around 200000 ms. For latin, they have the highest probability to have a danceability around .75 and have the highest probability to have high valence. For edm, they have the highest probability for low valence, a tempo around 125, lower track popularity, low acousticness, and high energy. For r&b, they have the highest probability for an energy between .25 and .5. For rap, they have the highest probability for a track popularity of around 50, for a low duration, for a speechiness between .2 and .5, for a low key, and for a high key around 10. For rock, they have the highest probability for a danceability between 0 and .5, for the mode to be major, and for a low speechiness. We have now answered the last problem statement of which genres have the highest probability for certain traits.
In this analysis, we have looked at 3 different problem statements. We have looked at predicting the track popularity based off other variables in the dataset, how the popularity of genres change based on the year the albums came out, and if there any distinct features for each genre. To begin we cleaned the data where there was missing values and potential outliers. For the first problem statement, we ran a full linear regression model using the quantitative variables and by creating a dummy variable for playlist genre. We utilized the stats package to perform our regression analysis. From here, we performed stepwise regression to find the best model. This gave us a very weak model with an r-squared of .07. We then looked at the residuals of the model and saw that there were some problems with equal variances among the residuals and that there were some leverage points based on cook’s distance. Then we looked at the regression equation and predicted the popularity based on a number of conditions. Overall, despite our regression not being ideal, it allowed for us to see the significant factors that would be in our final model which were playlist genre, danceability, energy, loudness, mode, speechiness, acousticness, instrumentalness, liveness, valence, tempo, and duration.
For the second problem statement, we used the variables track popularity and track album release date grouped by playlist genre to create an interactive scatter plot and bar graph which allows for us to either see the individual tracks popularity based on the year they came out or the total track popularity per year for each genre. We utilized the date and plotly packages to convert to dates and create interactive graphs. From our analysis, we saw that each genre had an increase in total track popularity from 2010 to 2019 where it peaked. We also saw that after 2010, we started to see an increase in track popularity over 80 for each genre besides rock which has always been consistent with having some tracks above 80 popularity.
For the third problem statement, we looked at the probability densities of each quantitative variable grouped by genre to see if there were any identifying conditions for each genre. We utilized the ggplot2 package to create the initial plot which was then converted to an interactive plot by using the plotly package. From these graphs, we saw that latin music has high probability to have a danceability around .75 and to have high valence. We saw that pop has the highest probability to be around 200000 ms. We saw that edm has the highest probability to have low valence, a tempo around 125, lower track popularity, low acousticness, and high energy. We saw that r&b has the highest probability for an energy between .25 and .5. We saw that rap music has the highest probability for a track popularity around 50, a low duration, a speechiness between .2 and .5, and for a either a low key or a high key around 10. We saw rock has the highest probability for a danceability between 0 and .5, the mode to be major, and for a low speechiness.
The implications of my analysis are that each genre is seeing increasing popularity among the recent years as compared to decades before. I think it would be safe to say that even though the data is from 2020, if we had an updated version of the dataset to include the years up until 2023, that we would see the popularity still going up. We also saw that it is very tough to get an accurate prediction of how popular a track will be depending on other factors in our dataset.
The limitations of my analysis is that there were only 6 genres so if someone wanted to know about a different genre than the 6 used in the analysis, this analysis wouldn’t be good for them. Another limitation is that this is just a small subset of about 5000+ songs for each genre compared to all the songs that are in each genre that exist. Someone could improve or build on my analysis by gathering a larger sample size for each genre or adding in additional genres that they care about. Another improvement is that if someone wanted to make the regression analysis better, they could look at transforming variables to fix the funnel shape pattern in the residuals vs fitted plot which would then fix the non constant variance among the residuals problem. This could allow for a more accurate prediction of track popularity when the transformation is applied. Alternatively, the leverage points identified in the residuals vs leverage plot could be removed from the model to see if it leads to a more accurate prediction.