Assignment 7

Author

Nick Stroh

Question

In the modern day music, the top priority for many record labels is to gain widespread appeal for their clients. Recently, songs that have been mainstays on the music charts have climbed up higher throughout their extended run towards their high peak position. This lead to the question that will be answered by the work in this document:

Do songs that stay on the chart longer reach higher peak positions, and does that relationship differ by country?

Now, onto the set up

Set Up

── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.0     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.1     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.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

Data Download from web scraping

This data comes from kworb.net, which is an aggregate of music charts, and in this case, we will be scraping from their Spotify music data from 20 different countries across the world.

music_url <- "https://myxavier-my.sharepoint.com/:x:/g/personal/strohn_xavier_edu/IQAqzweaQyeFTZBv4Qrn0q9hAWc6vyrrbQBZ1oDb9paOykY?download=1"
charts_raw <- read_csv(music_url) 
Rows: 4000 Columns: 12
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (6): P+, Artist and Title, (x?), Streams+, 7Day+, country
dbl (3): Pos, Days, Pk
num (3): Streams, 7Day, Total

ℹ 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.

Here is where we will wrangle all of the data so it is ready for usage in the visualizations

Analysis and Results

Output 1

This table shows how many songs, and the typical days on chart and peak position, for each country. Each country has 200 songs, so they are similar in size and can be fairly compared.

charts %>% 
  group_by(country) %>% 
  summarise(n = n(),
            median_days = median(days),
            median_peak = median(pk),
            median_streams = median(streams)) %>% 
  knitr::kable(caption = "Chart summary by country")
Chart summary by country
country n median_days median_peak median_streams
ar 200 389.0 12.0 54602.5
au 200 435.5 13.0 52481.5
br 200 192.5 22.0 225460.0
ca 200 324.0 12.0 49509.5
de 200 235.5 11.0 78468.0
es 200 166.5 13.0 67921.0
fr 200 177.0 14.0 75947.0
gb 200 319.5 13.0 86577.0
in 200 313.0 21.0 0.0
it 200 243.0 6.0 51713.5
jp 200 719.5 8.0 82171.0
kr 200 241.0 21.0 16270.5
mx 200 359.5 11.0 259849.0
ng 200 100.0 7.0 52175.5
nl 200 136.0 12.0 33469.5
ph 200 415.5 14.0 168711.5
se 200 231.0 15.0 20342.0
tr 200 172.0 15.0 105763.0
us 200 326.0 11.5 334696.5
za 200 137.0 19.5 18147.0

Output 2

Most songs have been on the chart for a short time, while a few have been on there for years. Because the data is so skewed, I used a log scale for days on chart in the later analysis.

ggplot(charts, aes(days)) +
  geom_histogram(bins = 40) +
  scale_x_log10() +
  labs(x = "Days on chart (log scale)", y = "Number of songs")

Output 3

In the following visualization, each of the dots is a song. The line slopes upward, which means songs that stay on the chart longer tend to reach better peak positions. The dots are spread widely around the line, so the relationship is moderate, even as many songs do not follow the trend.

ggplot(charts, aes(log_days, peak_score)) +
  geom_point(alpha = 0.15) +
  geom_smooth(method = "lm") +
  labs(x = "Log2 of days on chart", y = "Peak score (higher = better peak)")
`geom_smooth()` using formula = 'y ~ x'

Output 4

Each panel shows the same relationship for one country. Most countries show upward trend in how songs aggregate around the lines. However, the lines differ in steepness, and a few countries look a little more flat or vary in length. This suggests the relationship is not identical everywhere.

ggplot(charts, aes(log_days, peak_score)) +
  geom_point(alpha = 0.15, size = 0.7) +
  geom_smooth(method = "lm", se = FALSE) +
  facet_wrap(~ country, ncol = 4)
`geom_smooth()` using formula = 'y ~ x'

Output 5

Finally, I tested whether the relationship between days on chart and peak position is the same in every country. The result was significant (F = 4.18, p < 0.001), so the relationship differs by country. However, the improvement in fit was small, so the differences are real but modest.

m_main <- lm(peak_score ~ log_days + country, data = charts)
m_int  <- lm(peak_score ~ log_days * country, data = charts)
anova(m_main, m_int)
Analysis of Variance Table

Model 1: peak_score ~ log_days + country
Model 2: peak_score ~ log_days * country
  Res.Df     RSS Df Sum of Sq     F    Pr(>F)    
1   3979 3657477                                 
2   3960 3585600 19     71877 4.178 3.032e-09 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Conclusion

Overall, looking at the data that was scraped and the information we were able to extract. I can determine that songs that stay on the chart longer more often tend to reach high peak positions, though the relationship is moderate. The relationship shows differences when applied to different countries, although the differences are minor compared to the whole trend. These results describe an association among songs currently on the chart and they do not show that staying on the chart causes a higher peak.

Sources

Kworb: https://kworb.net/spotify/