Calculating Experimental Probabilities

Elliot Spaulding

3/19/2024

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

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

Looking at the first 6 rows, I think the column titles work and all 3 columns are an accurate type (character). I think I would like my genre column to be a factor type column so that I can categorize by the different Genres. Then I will look at the counts of each genre.

df$Genre <- as.factor(df$Genre)
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

Now I’m going to save this data frame so I can easily access it again later.

Genre_counts <- df %>% count(Genre)

Modeling

Calculate Theoretical Probabilities

Next I need to find the theoretical probabilities of getting each genre by creating a new column in my genre counts table.

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

I don’t like all those long decimal numbers, 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

This new table tells me that there is an 33% chance of getting a hiphop/rap song, a 14% chance of getting a pop/kpop/Latin song, a 3% chance of getting a house/ska song, a 8% chance of getting an rnb/soul song, a 26% chance of getting an alt/indie/folk song, and a 16% chance of getting a country/rock song.

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   Lesbian vampyres from outer space scary b*ches      
##  2 Genre 5 - alt/indie/folk Pheobe Bridgers                   The Gold          
##  3 Genre 5 - alt/indie/folk maneskin                          supermodel        
##  4 Genre 1 - hiphop/rap     Nicki Minaj                       Starships         
##  5 Genre 5 - alt/indie/folk Grateful Dead                     Cassidy           
##  6 Genre 5 - alt/indie/folk Cafune                            Talk              
##  7 Genre 2 - pop/kpop/Latin Bruno Mars                        Just the Way You …
##  8 Genre 6 - country/rock   Coyote Kisses                     Six Shooter       
##  9 Genre 6 - country/rock   aratic monkeys                    she looks like fun
## 10 Genre 1 - hiphop/rap     $uicideboy$                       Not Even Ghosts A…

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: 4 × 3
##   Genre                        n probability
##   <fct>                    <int>       <dbl>
## 1 Genre 1 - hiphop/rap         2         0.2
## 2 Genre 2 - pop/kpop/Latin     1         0.1
## 3 Genre 5 - alt/indie/folk     4         0.4
## 4 Genre 6 - country/rock       3         0.3

These probabilities are all nice so I don’t think I need to round them.

I’d say that only 2 of my favorite genres are on there out of the 6. My favorites are rock, and alt. It’s pretty close to eachother, theoretical probabilty has hiphop at 33%, meanwhile in expirimental pobability its 30%.

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         5        0.25
## 2 Genre 2 - pop/kpop/Latin     1        0.05
## 3 Genre 3 -  house/ska         1        0.05
## 4 Genre 4 -  rnb/soul          2        0.1 
## 5 Genre 5 - alt/indie/folk     6        0.3 
## 6 Genre 6 - country/rock       5        0.25
sample_n(df, 30) %>% count(Genre) %>% mutate(probability = round(n/sum(n), 2))
## # A tibble: 6 × 3
##   Genre                        n probability
##   <fct>                    <int>       <dbl>
## 1 Genre 1 - hiphop/rap        11        0.37
## 2 Genre 2 - pop/kpop/Latin     3        0.1 
## 3 Genre 3 -  house/ska         1        0.03
## 4 Genre 4 -  rnb/soul          3        0.1 
## 5 Genre 5 - alt/indie/folk     8        0.27
## 6 Genre 6 - country/rock       4        0.13
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        14        0.35
## 2 Genre 2 - pop/kpop/Latin     7        0.17
## 3 Genre 3 -  house/ska         1        0.03
## 4 Genre 4 -  rnb/soul          4        0.1 
## 5 Genre 5 - alt/indie/folk     9        0.22
## 6 Genre 6 - country/rock       5        0.12
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     9        0.13
## 3 Genre 3 -  house/ska         2        0.03
## 4 Genre 4 -  rnb/soul          7        0.1 
## 5 Genre 5 - alt/indie/folk    15        0.21
## 6 Genre 6 - country/rock      15        0.21
sample_n(df, 80) %>% count(Genre) %>% mutate(probability = round(n/sum(n), 2))
## # A tibble: 6 × 3
##   Genre                        n probability
##   <fct>                    <int>       <dbl>
## 1 Genre 1 - hiphop/rap        30        0.38
## 2 Genre 2 - pop/kpop/Latin     8        0.1 
## 3 Genre 3 -  house/ska         2        0.03
## 4 Genre 4 -  rnb/soul          5        0.06
## 5 Genre 5 - alt/indie/folk    20        0.25
## 6 Genre 6 - country/rock      15        0.19
sample_n(df, 100) %>% 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.29
## 2 Genre 2 - pop/kpop/Latin    15        0.15
## 3 Genre 3 -  house/ska         3        0.03
## 4 Genre 4 -  rnb/soul          9        0.09
## 5 Genre 5 - alt/indie/folk    27        0.27
## 6 Genre 6 - country/rock      17        0.17

Analyze & Synthesize

Do the genres you hear played on shuffle represent the genres of the songs in our class playlist? Yes, because both on shuffle and the amount of songs from a specific genre on the play list were really close to each other.

The data I have as proof would be that HipHop stayed close to 30% most of the time, and that country stayed close to 15% most of the time.

I learned that theoretically probability is kinda like a hypothesis before the experimental probability because it’s an educated guess.

Reflection

The coding helped me understand what the actual percentages of song preferences were. I would probably use these code simulations for population votes and preferences.

I think the hardest code to write was the %>% because they were usually really long.

I feel the most proud of the graph codes that help me make a graph because I can actually see the results.

I would probably like to know how to program a bar graph or something.