Introduction

library(tidyverse) #Used for working with data frames and visualizations
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tidytext) #Used for sentiment analysis
## Warning: package 'tidytext' was built under R version 4.3.2
library(textdata) #Used for sentiment analysis
## Warning: package 'textdata' was built under R version 4.3.2
library(lubridate) #Used for working with date variables

#I am a big fan of TheWeeknd's music. He is my favorite artist and I think it 
#will be very interesting to preform analysis on his discography.
#The data set I am using in this document includes data from spotify for each one
#of TheWeeknd's songs that are apart of albums. This means that songs not on an 
#album or songs TheWeeknd is featured in are not included. Below is a data 
#dictionary for each one of the variables in the data set

#Data dictionary:
#uniqueID = a ID for each song
#danceability = how danceable a song is. 
#energy = how energetic a song is. 
#key = what key the song is in.
#loudness = how loud the song is
#mode = indicator variable for the mode of the song
#speechiness = how speechy or wordy a song is
#acousticness = how acoustic a song is
#instrumentalness = how much instruments are used in the song
#liveness = how much the song sounds like it was preformed live
#valence = low valence meaning sad feeling and higher means happy
#tempo = tempo for each song is beats per minute
#duration_ms = length of a song is milliseconds
#track_name = name of the song
#album_name = name of the album song is apart of
#release_date = when the song was released
theweeknd <- read_csv("https://myxavier-my.sharepoint.com/:x:/g/personal/leamera_xavier_edu/EQizaDtgJVBAjt2gh3N4ho0BJq_eLsy3JsMZcB7xrTw01A?download=1",)
## Rows: 137 Columns: 23
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (9): type, id, uri, track_href, analysis_url, track_name, album_name, r...
## dbl (14): uniqueID, danceability, energy, key, loudness, mode, speechiness, ...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
theweeknd %>% 
  select(danceability,energy,loudness,speechiness,acousticness,instrumentalness,liveness,tempo) %>% 
  colMeans()
##     danceability           energy         loudness      speechiness 
##       0.56082482       0.60264234      -7.51660584       0.07253942 
##     acousticness instrumentalness         liveness            tempo 
##       0.26929432       0.01304721       0.20015693     123.90652555
#I selected a few variables from the data that were collected from the spotify
#api I found these interesting because I had no idea that songs on spotify were
#being scored for these categories

Descriptive Analysis

#Before I do any analysis I want to make some changes to the data. For Theweeknd's
#First 3 albums they were combined into one album called trilogy which added 3 
#new songs. The original versions are essentially the same as the songs in 
#trilogy so I am going to filter the originals out. Secondly, I am going to change
#the duration variable to be in seconds instead of milliseconds. Finally, I needed
#to treat the release date variable as a date variable. I also needed to remove
#some intro/interlude songs that are not actually songs

theweeknd <- theweeknd %>% 
  filter(album_name != "Echoes Of Silence (Original)") %>% 
  filter(album_name != "Thursday (Original)") %>% 
  filter(album_name != "House Of Balloons (Original)") %>% 
  mutate(duration_seconds = duration_ms / 1000,
         release_date = dmy(release_date))

theweeknd <- theweeknd %>% 
  slice(-1,-6,-12,-16)
#For my first visualization I wanted to confirm my hypothesis that TheWeeknd's 
#songs have had a similar average length throughout his career.

theweeknd %>% 
  group_by(album_name, release_date) %>% 
  summarise(mean = mean(duration_seconds)) %>% 
  arrange(release_date)
## `summarise()` has grouped output by 'album_name'. You can override using the
## `.groups` argument.
## # A tibble: 7 × 3
## # Groups:   album_name [7]
##   album_name                release_date  mean
##   <chr>                     <date>       <dbl>
## 1 Trilogy                   2012-11-13    325.
## 2 Kiss Land (Deluxe)        2013-09-10    325.
## 3 Beauty Behind The Madness 2016-03-16    275.
## 4 Starboy                   2016-11-25    229.
## 5 My Dear Melancholy,       2018-03-30    219.
## 6 After Hours (Deluxe)      2020-04-03    231.
## 7 Dawn FM (Alternate World) 2022-01-07    227.
#My hypothesis was definitely wrong. The table shows that throughout his career
#the average length of his songs has gotten shorter but it has remained somewhat
#the same since the release of Starboy. This is probably because of some very 
#long songs on Trilogy and Kiss Land.

#I wanted to see if there was a correlation between 
#the instrumentalness of a song and the acousticness of a song. Since a lot of
#his songs do not use instruments and have a score of 0 i will filter those out

theweeknd %>% 
  filter(instrumentalness > 0)  %>% 
  ggplot(aes(instrumentalness, acousticness))+
  geom_point()+
  labs(title = "Instrumentalness By Acousticness",
       x = "Instrumentalness",
       y = "Acousticness")

#Based off of this visualization there does not seem to be a correlation between
#these two variables. I think that the reason for this is because there are a lot
#of songs that have a instrumentalness score of close to 0

#The next thing I wanted to investigate was if longer songs were less speechy 
#than shorter songs. I think that this might be the case because I think TheWeeknd's
#longer songs have a lower proportion of him actually singing. I will seperate 
#his songs into two groups: songs that are shorter than 4 and a half minutes 
#and songs that are longer and then compare the groups.

theweeknd %>% 
  mutate(length = ifelse(duration_seconds >= 270, "Long", "Short")) %>% 
  group_by(length) %>% 
  summarise(mean = mean(speechiness)) %>% 
  ggplot(aes(length,mean))+
  geom_col()+
  labs(title = "Speechiness of Long VS Short Songs",
       x = "Length of song",
       y = "Speechiness")

#It looks like my suspicion was correct that longer songs will be less speechy
#The difference between the two groups is around .1 speechiness.

#Another thing I wanted to investigate was if there was a correlation between
#the tempo of a song and the dancability of that song. My thought behind this
#was that songs with a higher tempo would be more upbeat and thus dancable

theweeknd %>% 
  ggplot(aes(tempo, danceability))+
  geom_point()+
  geom_smooth(method = lm)+
  labs(title = "Tempo By Dancability",
       y = "Dancability",
       x = "Tempo")
## `geom_smooth()` using formula = 'y ~ x'

#The visual does not suggest my hypothesis was true. In fact, it seems like the 
#opposite was true. The line in the graph suggests that there is in fact a small
#negative correlation between tempo and dancability.

#The final thing I wanted to investigate was what albums were the least upbeat 
#in TheWeeknd's discography. I wanted to investigate this because a lot of his
#albums have a reputation for being pretty sad especially Trilogy and After
#Hours. I wanted to see if these two albums were in fact the least lively. To do
#this, I will use the valence variable since it is supposed to measure this.

theweeknd %>% 
  group_by(album_name) %>% 
  summarise(mean = mean(valence)) %>% 
  ggplot(aes(album_name, mean))+
  geom_col()+
  theme(axis.text.x = element_text(angle = 45, hjust = 1))+
  labs(title = "Average Valence Of Each Album",
       y = "Mean Valence",
       x = "Album Name")

#This did confirm my hypothesis because as you can see After Hours and Trilogy
#had the lowest average valence score. I also expected Starboy to have a  
#high valence. It is surprising to me that Dawn FM is that high. I think it is
#very interesting that spotify is able to measure these things about each album

Sentiment Analysis

#Earlier in the document it was noted that the valence score for After Hours was
#quite low meaning the songs had a more sad theme. I want to explore if reviews
#on metacritic about After Hours shared a similar sad sentiment. To do this I 
#will use the Bing lexicon which scores certain words based positively or negatively
#I will also be filtering out some words that appear a lot because they are
#song names.

theweekndreviews <- read_csv("https://myxavier-my.sharepoint.com/:u:/g/personal/leamera_xavier_edu/EYHsj5zR84JAsf7tHGMoTN8BxDh6XBUYVPKqwFwM6ldnNw?download=1")
## Rows: 1042 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): review, album
## dbl (1): reviewid
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
afterhoursreviews <- theweekndreviews %>% 
  filter(album == "After Hours")

reviewwords <- afterhoursreviews %>% 
  unnest_tokens(word,review) %>% 
  anti_join(stop_words) 
## Joining with `by = join_by(word)`
bing <- get_sentiments("bing")

reviewwords %>% 
  filter(word != "faith", word != "heartless", word != "blinding") %>% 
  group_by(word) %>% 
  summarize(n=n()) %>% 
  inner_join(bing) %>%
  filter(n > 10) %>% 
  mutate(n = ifelse(sentiment == "negative",-n,n)) %>% 
  mutate(word = reorder(word,n)) %>% 
  ggplot(aes(word, n, fill = sentiment)) +
  geom_col() +
  coord_flip() +
  labs(title = "Sentiment of After Hours Reviews",
       x = "Words",
       y = "Number of times used")
## Joining with `by = join_by(word)`

#It turns out that the words the appear most often in the reviews do not appear
#to be negative or sad for the most part. Most of the words seem to be describing
#how much they enjoyed listening the the album. However, the words madness, 
#scared, and dark could have been used in a way that described the album as sad.