DS4: Song Shuffle Simulation 2024-02-26

Introduction

Before getting started, the tidyverse package needs to be installed

install.packages("tidyverse")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.3'
## (as 'lib' is unspecified)
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

Now I’m going to import the data and save it as a data frame

df <- read_csv("Class Playlist Clean DS4 - Sheet1.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.

Next I need to look at the first 6 rows of the data frame

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
df$Genre <- as.factor(df$Genre)
df$Genre <- as.factor(df$Genre)
head(df)
## # A tibble: 6 × 3
##   Genre                    Artist         Song                          
##   <fct>                    <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

Now I’m going to look at the counts for each genre using the piping operator

df %>% count(df$Genre)
## # A tibble: 6 × 2
##   `df$Genre`                   n
##   <fct>                    <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
df$Genre <- as.factor(df$Genre)

Now I’m going to save this as a new data frame

genre_counts <- df %>% count(Genre)
genre_counts
## # A tibble: 6 × 2
##   Genre                        n
##   <fct>                    <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

Next a new column is going to be created for the probability

genre_counts <- genre_counts %>% mutate(prob = n/sum(n))
genre_counts
## # A tibble: 6 × 3
##   Genre                        n   prob
##   <fct>                    <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

There is a 33% chance of hiphop/rap, a .14% chance of pop/kpop/latin, a .03% chance of house/ska, .08% chance of rnb/soul, .26% alt/indie/folk, and finally .16% chance of country/rock.

Calculating Experimental Probabilities

Our next task is to simulate how shuffle works by pulling a random sample of songs and saving it as a new table.

df$Song <- as.factor(df$Song)
playlist <-sample_n(df, 10)
playlist
## # A tibble: 10 × 3
##    Genre                    Artist                  Song                       
##    <fct>                    <chr>                   <fct>                      
##  1 Genre 6 - country/rock   Pink Floyd              Time                       
##  2 Genre 1 - hiphop/rap     Bryson Tiller           Whatever She Wants         
##  3 Genre 1 - hiphop/rap     Travis Scott            Maria I'm Drunk            
##  4 Genre 6 - country/rock   Queens of the Stone Age No One Knows               
##  5 Genre 2 - pop/kpop/Latin Phantogram              Fall In Love               
##  6 Genre 2 - pop/kpop/Latin Ginger root             Loretta                    
##  7 Genre 6 - country/rock   Billy Joel              Summer, Highland Falls     
##  8 Genre 4 -  rnb/soul      Rihanna                 Nobody's Business          
##  9 Genre 1 - hiphop/rap     8k                      When you call              
## 10 Genre 5 - alt/indie/folk Japanese Breakfast      Everybody Wants to Love You

Now I will adjust this table to show me just the genres and their probabilities

sample_counts <- df %>% count(df$Genre) %>% mutate(prob = n/sum(n))
sample_counts
## # A tibble: 6 × 3
##   `df$Genre`                   n   prob
##   <fct>                    <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

Experimental Probability: .1% hiphop/rap,.1% pop/kpop/latin, 0% house/ska, .1% rnb/soul,.3% alt/indie/folk, .4% country/rock

The song genres are decently representative of the ones in our playlist, the sample showed alt/indie/folk the most which is the second highest genre on our playlist.

Theoretical vs Experimental

The theoretical probability shows that the bigger the amount of a certain genre, the more likely it is to show up resulting in a higher percent of theoretical probability. The experimental probability varied a bit from the theoretical probability. Alt/indie/folk genre and house/ska stayed within the same percentage range. The reason the two probabilities are different is because the experimental probability for each genre is based off an actual random sample size.

Larger Sample Sizes

sample_n(df,20)%>%count(Genre)%>%mutate(probability= round(n/sum(n),2))
## # A tibble: 5 × 3
##   Genre                        n probability
##   <fct>                    <int>       <dbl>
## 1 Genre 1 - hiphop/rap         4        0.2 
## 2 Genre 2 - pop/kpop/Latin     4        0.2 
## 3 Genre 4 -  rnb/soul          2        0.1 
## 4 Genre 5 - alt/indie/folk     7        0.35
## 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
##   <fct>                    <int>       <dbl>
## 1 Genre 1 - hiphop/rap         9        0.3 
## 2 Genre 2 - pop/kpop/Latin     4        0.13
## 3 Genre 4 -  rnb/soul          3        0.1 
## 4 Genre 5 - alt/indie/folk     7        0.23
## 5 Genre 6 - country/rock       7        0.23
sample_n(df,40)%>%count(Genre)%>%mutate(probability=round(n/sum(n),2))
## # A tibble: 4 × 3
##   Genre                        n probability
##   <fct>                    <int>       <dbl>
## 1 Genre 1 - hiphop/rap        17        0.42
## 2 Genre 2 - pop/kpop/Latin     9        0.22
## 3 Genre 5 - alt/indie/folk     7        0.17
## 4 Genre 6 - country/rock       7        0.17
sample_n(df,50)%>%count(Genre)%>%mutate(probability=round(n/sum(n),2))
## # A tibble: 6 × 3
##   Genre                        n probability
##   <fct>                    <int>       <dbl>
## 1 Genre 1 - hiphop/rap        18        0.36
## 2 Genre 2 - pop/kpop/Latin     5        0.1 
## 3 Genre 3 -  house/ska         3        0.06
## 4 Genre 4 -  rnb/soul          6        0.12
## 5 Genre 5 - alt/indie/folk    11        0.22
## 6 Genre 6 - country/rock       7        0.14
sample_n(df,60)%>%count(Genre)%>%mutate(probability=round(n/sum(n),2))
## # A tibble: 6 × 3
##   Genre                        n probability
##   <fct>                    <int>       <dbl>
## 1 Genre 1 - hiphop/rap        21        0.35
## 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    15        0.25
## 6 Genre 6 - country/rock      11        0.18
sample_n(df,70)%>%count(Genre)%>%mutate(probability=round(n/sum(n),2))
## # A tibble: 6 × 3
##   Genre                        n probability
##   <fct>                    <int>       <dbl>
## 1 Genre 1 - hiphop/rap        29        0.41
## 2 Genre 2 - pop/kpop/Latin     8        0.11
## 3 Genre 3 -  house/ska         3        0.04
## 4 Genre 4 -  rnb/soul          3        0.04
## 5 Genre 5 - alt/indie/folk    20        0.29
## 6 Genre 6 - country/rock       7        0.1

Analyze & Synthesize

Unit Question: Do the genres played on shuffle represent the genres of the songs on the class playlist?

Yes, a shuffled playlist (or sample size) represents the genres of the class playlist. This can be seen in any of the sample sizes pulled previously by the number of songs from each genre and how they correlate to the counts for each genre in the original playlist.

I learned that experimental probability is a lot more helpful for this type of situation because it gives real results that can then be calculated to find the probability percentage.

Reflection

Coding the simulation was an easier way to find results to help answer the question instead of making an actual playlist and trying to randomize it and record results.

The beginning pieces of code were the most challenging for me as well as the mutate function and piping operator. I think the more complicated and longer the code got, the more room there was for error.

I feel most proud of the code I wrote for creating a new column for genre counts because I was initially stuck on how to go about it.

I would like to learn about ways to effectively memorize code. I found myself having to frequently go back and check how to write the specific code I needed.

If I were to do this again, I would have asked for help earlier on when I was confused instead of getting more and more stuck and making it more complicated than it needed to be.