Halloween Candy Ranking

Read the fivethirtyeight article and watch the short movie

https://fivethirtyeight.com/videos/the-ultimate-halloween-candy-power-ranking/

Here’s some documentation on these data:

https://github.com/fivethirtyeight/data/tree/master/candy-power-ranking

Step 0: Load the Data

candy<-read.csv("https://raw.githubusercontent.com/fivethirtyeight/data/master/candy-power-ranking/candy-data.csv",
 header=TRUE)

library(tidyverse)

Step 1: Structure

str(candy)
## 'data.frame':    85 obs. of  13 variables:
##  $ competitorname  : chr  "100 Grand" "3 Musketeers" "One dime" "One quarter" ...
##  $ chocolate       : int  1 1 0 0 0 1 1 0 0 0 ...
##  $ fruity          : int  0 0 0 0 1 0 0 0 0 1 ...
##  $ caramel         : int  1 0 0 0 0 0 1 0 0 1 ...
##  $ peanutyalmondy  : int  0 0 0 0 0 1 1 1 0 0 ...
##  $ nougat          : int  0 1 0 0 0 0 1 0 0 0 ...
##  $ crispedricewafer: int  1 0 0 0 0 0 0 0 0 0 ...
##  $ hard            : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ bar             : int  1 1 0 0 0 1 1 0 0 0 ...
##  $ pluribus        : int  0 0 0 0 0 0 0 1 1 0 ...
##  $ sugarpercent    : num  0.732 0.604 0.011 0.011 0.906 ...
##  $ pricepercent    : num  0.86 0.511 0.116 0.511 0.511 ...
##  $ winpercent      : num  67 67.6 32.3 46.1 52.3 ...

Step 2: One-way Table

How many candies have chocolate?

# 1 = Yes Chocolate
# 0 = No Chocolate
tabChoc<-table(candy$chocolate)
tabChoc
## 
##  0  1 
## 48 37

What is the probability if you randomly choose a candy it will have chocolate?

prop.table(tabChoc)
## 
##         0         1 
## 0.5647059 0.4352941

Step 3: Simple Bar Graph (1-Var)

candy$chocolate<-as.factor(candy$chocolate)

ggplot(candy, aes(x=chocolate, fill=chocolate))+
  geom_bar()

Step 4: Two-way table

How many candies have chocolate and caramel?

# Row = Chocolate
# Col = Caramel
tabChocCarm<-table(candy$chocolate, candy$caramel)
tabChocCarm
##    
##      0  1
##   0 44  4
##   1 27 10

If I randomly selected a candy, what is the probability that its chocolate and caramel?

# Joint
prop.table(tabChocCarm)
##    
##              0          1
##   0 0.51764706 0.04705882
##   1 0.31764706 0.11764706

If I randomly selected a candy and it was chocolate, what is the probability that its chocolate and caramel?

# Conditional
prop.table(tabChocCarm, 1)
##    
##              0          1
##   0 0.91666667 0.08333333
##   1 0.72972973 0.27027027

Step 5: Bar Graphs with 2-Vars

candy$caramel<-as.factor(candy$caramel)

# stacked
ggplot(candy, aes(x=chocolate, fill=caramel))+
  geom_bar()

# dodge
ggplot(candy, aes(x=chocolate, fill=caramel))+
  geom_bar(position="dodge")

# fill
ggplot(candy, aes(x=chocolate, fill=caramel))+
  geom_bar(position="fill")