DS4 Song Simulation

4-02-2024

What is the likelihood of a song of each genre being played?

First we need to install tidyverse packages.
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

6 rows

df<-read_csv("Class Playlist Clean DS4 - Sheet1 (2).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.
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)
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
df%>% count(Genre)
## # 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
Genre_counts<-df%>%count(Genre)
we then have to add a new column in my genre counts table to find the theoretical probability of each genre.
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
The long decimals don’t look very satisfying so I’m going to round them to 2 decimal places.
Genre_counts$prob<-round(Genre_counts$prob,2)
Genre_counts
## # A tibble: 6 × 3
##   Genre                        n  prob
##   <fct>                    <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

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.
sample<-sample_n(df,10)
sample
## # A tibble: 10 × 3
##    Genre                    Artist        Song                
##    <fct>                    <chr>         <chr>               
##  1 Genre 6 - country/rock   Life on mars  david bowie         
##  2 Genre 5 - alt/indie/folk Grateful Dead Cassidy             
##  3 Genre 2 - pop/kpop/Latin Yonaguni      bad bunny           
##  4 Genre 2 - pop/kpop/Latin Bruno Mars    Just the Way You Are
##  5 Genre 4 -  rnb/soul      Drake         Marvins Room        
##  6 Genre 2 - pop/kpop/Latin Peso Pluma    BELLAKEO            
##  7 Genre 5 - alt/indie/folk 2 8 1 4       Arcadia             
##  8 Genre 5 - alt/indie/folk Big Thief     Simulation Swarm    
##  9 Genre 4 -  rnb/soul      Chet Baker    Almost blue         
## 10 Genre 1 - hiphop/rap     dj khaled     wild thoughts
Now I will adjust this table to show me just the genres and their probabilities.
sample_counts<-sample%>%count(Genre)%>%mutate(probability=n/sum(n))
sample_counts
## # A tibble: 5 × 3
##   Genre                        n probability
##   <fct>                    <int>       <dbl>
## 1 Genre 1 - hiphop/rap         1         0.1
## 2 Genre 2 - pop/kpop/Latin     3         0.3
## 3 Genre 4 -  rnb/soul          2         0.2
## 4 Genre 5 - alt/indie/folk     3         0.3
## 5 Genre 6 - country/rock       1         0.1
In the new table with the decimals in the second place it shows us that the most likely genre is Hip hop/Rap with a 0.6 probability. The least common genres are 3 and 4 with a 0.1 probability.

Larger Sampling Simulation

Sampling 20

sample_n(df, 20) %>% count(Genre) %>% mutate(probability = round(n/sum(n), 2))
## # A tibble: 6 × 3
##   Genre                        n probability
##   <fct>                    <int>       <dbl>
## 1 Genre 1 - hiphop/rap         9        0.45
## 2 Genre 2 - pop/kpop/Latin     1        0.05
## 3 Genre 3 -  house/ska         2        0.1 
## 4 Genre 4 -  rnb/soul          2        0.1 
## 5 Genre 5 - alt/indie/folk     5        0.25
## 6 Genre 6 - country/rock       1        0.05

Sampling 30

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        12        0.4 
## 2 Genre 2 - pop/kpop/Latin     4        0.13
## 3 Genre 4 -  rnb/soul          3        0.1 
## 4 Genre 5 - alt/indie/folk     6        0.2 
## 5 Genre 6 - country/rock       5        0.17

Sampling 40

sample_n(df, 40) %>% count(Genre) %>% mutate(probability = round(n/sum(n), 2))
## # A tibble: 6 × 3
##   Genre                        n probability
##   <fct>                    <int>       <dbl>
## 1 Genre 1 - hiphop/rap        13        0.32
## 2 Genre 2 - pop/kpop/Latin     8        0.2 
## 3 Genre 3 -  house/ska         2        0.05
## 4 Genre 4 -  rnb/soul          3        0.07
## 5 Genre 5 - alt/indie/folk     8        0.2 
## 6 Genre 6 - country/rock       6        0.15

Sampling 70

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        22        0.31
## 2 Genre 2 - pop/kpop/Latin    11        0.16
## 3 Genre 3 -  house/ska         1        0.01
## 4 Genre 4 -  rnb/soul          8        0.11
## 5 Genre 5 - alt/indie/folk    14        0.2 
## 6 Genre 6 - country/rock      14        0.2

Sampling 175

sample_n(df, 175) %>% count(Genre) %>% mutate(probability = round(n/sum(n), 2))
## # A tibble: 6 × 3
##   Genre                        n probability
##   <fct>                    <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

Analyze and Synthesize

Do genres you hear played on shuffle represent the genres of the songs on our class playlist?

I feel as if that it does represent a pretty good amount of our class playlist, the largest genre is Hiphop and Rap. When coding the random songs it would have at least one or 2 songs from the Hip Hop and Rap genre, compared to the other genres I feel it is fair compared to the other genres based on the randomizing of the songs.

What did you learn about experimental and theoretical probability and how do they apply in this situation?

I learned that theoretical probability plays a large part in coding because its more based on mathematical analysis while experimental is based around trial.

Reflection

In what ways did coding a simulation support you for answering this unit question? What else might you use simulations for?

I learned a whole lot of what it takes to code genres or randomizing songs so I think he helped me understand how the whole thing works, I think I would have to do more coding things like this to get a full understanding about the whole world of coding. I feel as if you would use this kind of simulation for business reports like finding data for a company funds or other things relating to songs or music in general.

The piece of coding that I found most difficult was sample_n(df, ) %>% count(Genre) %>% mutate(probability = round(n/sum(), )) I kept having trouble with it not being able to run in the simulation

I feel most proud of being able to figure out this coding in the first place, I was struggling a lot in the beginning trying to understand it but once I actually got the hang of writing code I found it to be quite fun and interesting.

I think I would just like to learn more about how this simulation works, I find this super interesting so honestly anything to do with coding I think will be fun and interesting to learn about.

Something I would do differently is actually trying to understand how to write the code before I ask questions, once I was able to grasp it I felt like it was super easy to me, so I think maybe trying something before asking how to do it will be good if I do this a second time around.