Introduction

This project stimulates the shuffle effect on a playlist, such as those used by Spotify or Apple Music. The playlist was made using the favorite songs of students from Ms. Hood’s Period 7 Data Science class, and their respective genres. This data was comprised into a spreadsheet, which will serve as our “playlist.” We will use this data to explore calculating theoretical probability.

Essential Question

The guiding question of this project is: What is the likelihood of a song of each genre being played?

Setting Up

Load Necessary Libraries

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.0     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── 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

Import Data

df <- read_csv("Playlist - Clean.csv")
## Rows: 175 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): Genre, Artist, Song
## 
## ℹ 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.

Look at the first few lines of data

head(df)
## # A tibble: 6 × 3
##   Genre                    Artist         Song                          
##   <chr>                    <chr>          <chr>                         
## 1 Genre 1 - hiphop/rap     8k             When you call                 
## 2 Genre 2 - pop/kpop/Latin Yonaguni       bad bunny                     
## 3 Genre 1 - hiphop/rap     $uicideboy$    Diemonds                      
## 4 Genre 1 - hiphop/rap     $uicideboy$    Not Even Ghosts Are This Empty
## 5 Genre 1 - hiphop/rap     03 Greedo      Trap House                    
## 6 Genre 5 - alt/indie/folk 10,000 Maniacs Trouble Me

Calculating Probability

Now we’ll count the number of times each genre appears in the playlist.

df%>%count(df$Genre)
## # A tibble: 6 × 2
##   `df$Genre`                   n
##   <chr>                    <int>
## 1 Genre 1 - hiphop/rap        58
## 2 Genre 2 - pop/kpop/Latin    25
## 3 Genre 3 -  house/ska         5
## 4 Genre 4 -  rnb/soul         14
## 5 Genre 5 - alt/indie/folk    45
## 6 Genre 6 - country/rock      28

This data frame will be saved as “Genre_Counts” so it can be easily accessed later on.

Genre_Counts <- df%>%count(Genre)

Now we will find the theoretical probability of each genre appearing.

To do this, we must create a new column in the “Genre_Count” table.

Genre_Counts <- Genre_Counts %>% mutate(prob = n/sum(n))
Genre_Counts
## # A tibble: 6 × 3
##   Genre                        n   prob
##   <chr>                    <int>  <dbl>
## 1 Genre 1 - hiphop/rap        58 0.331 
## 2 Genre 2 - pop/kpop/Latin    25 0.143 
## 3 Genre 3 -  house/ska         5 0.0286
## 4 Genre 4 -  rnb/soul         14 0.08  
## 5 Genre 5 - alt/indie/folk    45 0.257 
## 6 Genre 6 - country/rock      28 0.16

This data is good, but to make it easier to read we’ll use the round function to get rid of the long decimal numbers.

Genre_Counts$prob <- round(Genre_Counts$prob, 2)
Genre_Counts
## # A tibble: 6 × 3
##   Genre                        n  prob
##   <chr>                    <int> <dbl>
## 1 Genre 1 - hiphop/rap        58  0.33
## 2 Genre 2 - pop/kpop/Latin    25  0.14
## 3 Genre 3 -  house/ska         5  0.03
## 4 Genre 4 -  rnb/soul         14  0.08
## 5 Genre 5 - alt/indie/folk    45  0.26
## 6 Genre 6 - country/rock      28  0.16

Results

This table tells us that the probability of a song playing from each genre is .33% chance for hiphop/rap, .14% chance for pop/kpop/Latin, .03% chance for house/ska, .08% chance for rnb/soul, .26% for alt/indie/folk, and .16% for country/rock.

—–

Further Exploration: Calculating Experimental Probabilities

Now we will simulate how shuffle works by pulling a random sample of songs and saving it as a new table.

sample <- sample_n(df, 10)
sample
## # A tibble: 10 × 3
##    Genre                    Artist                  Song                        
##    <chr>                    <chr>                   <chr>                       
##  1 Genre 1 - hiphop/rap     $uicideboy$             Not Even Ghosts Are This Em…
##  2 Genre 1 - hiphop/rap     Kendrick Lamar          Alright                     
##  3 Genre 6 - country/rock   Billy Joel              Summer, Highland Falls      
##  4 Genre 6 - country/rock   Ferry                   STRIKE 3                    
##  5 Genre 2 - pop/kpop/Latin bad bunny               kpop                        
##  6 Genre 1 - hiphop/rap     Drake                   teenage fever               
##  7 Genre 1 - hiphop/rap     Polo G                  Martin and Gina             
##  8 Genre 6 - country/rock   Queens of the Stone Age No One Knows                
##  9 Genre 4 -  rnb/soul      running on e            Brent F                     
## 10 Genre 6 - country/rock   Red Hot Chili Peppers   Can't Stop

Now we will adjust this table to show just the genres and their probabilities.

sample_counts <- sample %>% count(Genre) %>% mutate(probability = n/sum(n))
sample_counts
## # A tibble: 4 × 3
##   Genre                        n probability
##   <chr>                    <int>       <dbl>
## 1 Genre 1 - hiphop/rap         4         0.4
## 2 Genre 2 - pop/kpop/Latin     1         0.1
## 3 Genre 4 -  rnb/soul          1         0.1
## 4 Genre 6 - country/rock       4         0.4

Results

This table tells us the experimental probability of a song playing from each genre. One thing that stands out about this data is the lack of representation from genre 3 & 4. Both of these have an experimental probability of 0%, which is unsurprising as their theoretical probability were both under 10%. Pop/kpop/latin, alt/indie/folk, and country/rock each have a 20% chance of being played, and hiphop/rap has a 40% chance. The biggest suprise probably comes from alt/indie/rock’s data, which had a theoretical probability of 26% (only 4% less than hiphop/rap), but an expirimental probability of just 20%. This data could be improved, and made closer to it’s theoretical data, by using a larger sample size of shuffled songs– think 20 or 30 songs instead of 10.

Further Simulation

sample_n(df, 20) %>% count(Genre) %>% mutate(probability = round(n/sum(n), 2))
## # A tibble: 5 × 3
##   Genre                        n probability
##   <chr>                    <int>       <dbl>
## 1 Genre 1 - hiphop/rap         8        0.4 
## 2 Genre 2 - pop/kpop/Latin     3        0.15
## 3 Genre 4 -  rnb/soul          1        0.05
## 4 Genre 5 - alt/indie/folk     5        0.25
## 5 Genre 6 - country/rock       3        0.15
sample_n(df, 30) %>% count(Genre) %>% mutate(probability = round(n/sum(n), 2))
## # A tibble: 5 × 3
##   Genre                        n probability
##   <chr>                    <int>       <dbl>
## 1 Genre 1 - hiphop/rap         9        0.3 
## 2 Genre 2 - pop/kpop/Latin     7        0.23
## 3 Genre 4 -  rnb/soul          2        0.07
## 4 Genre 5 - alt/indie/folk     7        0.23
## 5 Genre 6 - country/rock       5        0.17
sample_n(df, 40) %>% count(Genre) %>% mutate(probability = round(n/sum(n), 2))
## # A tibble: 5 × 3
##   Genre                        n probability
##   <chr>                    <int>       <dbl>
## 1 Genre 1 - hiphop/rap        13        0.32
## 2 Genre 2 - pop/kpop/Latin     8        0.2 
## 3 Genre 4 -  rnb/soul          4        0.1 
## 4 Genre 5 - alt/indie/folk     6        0.15
## 5 Genre 6 - country/rock       9        0.22
sample_n(df, 60) %>% count(Genre) %>% mutate(probability = round(n/sum(n), 2))
## # A tibble: 6 × 3
##   Genre                        n probability
##   <chr>                    <int>       <dbl>
## 1 Genre 1 - hiphop/rap        24        0.4 
## 2 Genre 2 - pop/kpop/Latin     8        0.13
## 3 Genre 3 -  house/ska         1        0.02
## 4 Genre 4 -  rnb/soul          4        0.07
## 5 Genre 5 - alt/indie/folk    14        0.23
## 6 Genre 6 - country/rock       9        0.15
sample_n(df, 80) %>% count(Genre) %>% mutate(probability = round(n/sum(n), 2))
## # A tibble: 6 × 3
##   Genre                        n probability
##   <chr>                    <int>       <dbl>
## 1 Genre 1 - hiphop/rap        31        0.39
## 2 Genre 2 - pop/kpop/Latin    13        0.16
## 3 Genre 3 -  house/ska         1        0.01
## 4 Genre 4 -  rnb/soul          5        0.06
## 5 Genre 5 - alt/indie/folk    20        0.25
## 6 Genre 6 - country/rock      10        0.12
sample_n(df, 100) %>% count(Genre) %>% mutate(probability = round(n/sum(n), 2))
## # A tibble: 6 × 3
##   Genre                        n probability
##   <chr>                    <int>       <dbl>
## 1 Genre 1 - hiphop/rap        36        0.36
## 2 Genre 2 - pop/kpop/Latin    14        0.14
## 3 Genre 3 -  house/ska         4        0.04
## 4 Genre 4 -  rnb/soul          5        0.05
## 5 Genre 5 - alt/indie/folk    26        0.26
## 6 Genre 6 - country/rock      15        0.15

Analyze and Synthesize

As each shuffle simulation grows larger, they become more representative of the theoretical probability. In the largest simulation– containing 100 songs– the experimental probability of each genre was no more than 3% off from the theoretical probability.

The earlier simulations were less accurate, each having discrepancies of between 8-10% for at least one of the genres. However this is still a fairly small number, and only occurred in one or two genres per simulation. This pattern indicates that these tests are accurate, and the shuffles are representative of the overall playlist.

In this study I have learned about the differences between theoretical and experimental probability. I find it fascinating that mathematically something might be supposed to happen, but because of the many ever changing factors of life it doesn’t. I also learned that if you have a larger sample size, your experimental probability is more likely to be near the theoretical probability.

Reflection

Coding this simulation helped me to quickly understand data without complex math equations. One way to use simulations (or a simulation-like product) is to create a seating arrangement for a wedding dinner or the Hunger Games simulation games I played as a kid.

The hardest part of coding for this assignment was at the beginning when I had absolutely no idea what I was doing! I didn’t even know how to ask for help I was so lost. That meant that one I finally finished the first coding project in this unit I was so so proud!

In the future, I want to learn more about how and why code works. I felt like I was just memorizing phrases of a language, rather than taking the time to understand the meaning and the nuance. I think if I were to do this project again I would have procrastinated less. I ended up really enjoying the material, and wish I could have set aside more time to fully understand it.

—–