Cecilia's Site

Logo

My class work for bimm143 at UC San Diego

View the Project on GitHub cewang-bot/NEW_BIMM143

Lab 9: Candy Mini-Project

Cecilia Wang (A18625854)

Background

In this mini-project, we will explore FiveThirtyEight’s Halloween Candy dataset.

Data Import

Our dataset is a CSV file so we used read.csv() function.

candy_file <- read.csv("candy-data.csv")

candy = data.frame(candy_file,row.names = 1)
head(candy)
             chocolate fruity caramel peanutyalmondy nougat crispedricewafer
100 Grand            1      0       1              0      0                1
3 Musketeers         1      0       0              0      1                0
One dime             0      0       0              0      0                0
One quarter          0      0       0              0      0                0
Air Heads            0      1       0              0      0                0
Almond Joy           1      0       0              1      0                0
             hard bar pluribus sugarpercent pricepercent winpercent
100 Grand       0   1        0        0.732        0.860   66.97173
3 Musketeers    0   1        0        0.604        0.511   67.60294
One dime        0   0        0        0.011        0.116   32.26109
One quarter     0   0        0        0.011        0.511   46.11650
Air Heads       0   0        0        0.906        0.511   52.34146
Almond Joy      0   1        0        0.465        0.767   50.34755

Q1. How many different candy types are in this dataset?

sum(nrow(candy))
[1] 85

Q2. How many fruity candy types are in the dataset?

sum(candy$fruity==1)
[1] 38

Q3. What is your favorite candy (other than Twix) in the dataset and what is it’s winpercent value?

library(dplyr)
Attaching package: 'dplyr'

The following objects are masked from 'package:stats':

    filter, lag

The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
candy |> 
  filter(row.names(candy)=="Nerds") |> 
  select(winpercent)
      winpercent
Nerds   55.35405

Q4. What is the winpercent value for “Kit Kat”?

candy |> 
  filter(row.names(candy)=="Kit Kat") |> 
  select(winpercent)
        winpercent
Kit Kat    76.7686

Q5. What is the winpercent value for “Tootsie Roll Snack Bars”?

candy |> 
  filter(row.names(candy)=="Tootsie Roll Snack Bars") |> 
  select(winpercent)
                        winpercent
Tootsie Roll Snack Bars    49.6535

Q6. Is there any variable/column that looks to be on a different scale to the majority of the other columns in the dataset?

library("skimr")
skim(candy)
   
Name candy
Number of rows 85
Number of columns 12
_______________________  
Column type frequency:  
numeric 12
________________________  
Group variables None

Data summary

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
chocolate 0 1 0.44 0.50 0.00 0.00 0.00 1.00 1.00 ▇▁▁▁▆
fruity 0 1 0.45 0.50 0.00 0.00 0.00 1.00 1.00 ▇▁▁▁▆
caramel 0 1 0.16 0.37 0.00 0.00 0.00 0.00 1.00 ▇▁▁▁▂
peanutyalmondy 0 1 0.16 0.37 0.00 0.00 0.00 0.00 1.00 ▇▁▁▁▂
nougat 0 1 0.08 0.28 0.00 0.00 0.00 0.00 1.00 ▇▁▁▁▁
crispedricewafer 0 1 0.08 0.28 0.00 0.00 0.00 0.00 1.00 ▇▁▁▁▁
hard 0 1 0.18 0.38 0.00 0.00 0.00 0.00 1.00 ▇▁▁▁▂
bar 0 1 0.25 0.43 0.00 0.00 0.00 0.00 1.00 ▇▁▁▁▂
pluribus 0 1 0.52 0.50 0.00 0.00 1.00 1.00 1.00 ▇▁▁▁▇
sugarpercent 0 1 0.48 0.28 0.01 0.22 0.47 0.73 0.99 ▇▇▇▇▆
pricepercent 0 1 0.47 0.29 0.01 0.26 0.47 0.65 0.98 ▇▇▇▇▆
winpercent 0 1 50.32 14.71 22.45 39.14 47.83 59.86 84.18 ▃▇▆▅▂

Winpercent

Q7. What do you think a zero and one represent for the candy$chocolate column?

skim(candy$chocolate)
   
Name candy$chocolate
Number of rows 85
Number of columns 1
_______________________  
Column type frequency:  
numeric 1
________________________  
Group variables None

Data summary

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
data 0 1 0.44 0.5 0 0 0 1 1 ▇▁▁▁▆

nothing below this percentile.

Exploratory analysis

Q8. Plot a histogram of winpercent values

hist(candy$winpercent)

library(ggplot2)

ggplot(data.frame(winpercent = candy$winpercent),
       aes(x = winpercent)) +
  geom_histogram(bins = 20)

Q9. Is the distribution of winpercent values symmetrical?

No

Q10. Is the center of the distribution above or below 50%?

mean(candy$winpercent)
[1] 50.31676
summary((candy$winpercent))
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  22.45   39.14   47.83   50.32   59.86   84.18 

The center is below 50%.

Q11. On average is chocolate candy higher or lower ranked than fruit candy?

mean(candy$winpercent[candy$chocolate == 1]) >
mean(candy$winpercent[candy$fruity == 1])
[1] TRUE

Chocolate candy ranked higher than fruit candy

Q12. Is this difference statistically significant?

t.test(candy$winpercent[candy$chocolate == 1],
candy$winpercent[candy$fruity == 1])
    Welch Two Sample t-test

data:  candy$winpercent[candy$chocolate == 1] and candy$winpercent[candy$fruity == 1]
t = 6.2582, df = 68.882, p-value = 2.871e-08
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 11.44563 22.15795
sample estimates:
mean of x mean of y 
 60.92153  44.11974 

p-value is less than 0.05, the difference statistically significant.

Overall Candy Rankings

Q13. What are the five least liked candy types in this set?

candy |> arrange(winpercent) |> head(5)
                   chocolate fruity caramel peanutyalmondy nougat
Nik L Nip                  0      1       0              0      0
Boston Baked Beans         0      0       0              1      0
Chiclets                   0      1       0              0      0
Super Bubble               0      1       0              0      0
Jawbusters                 0      1       0              0      0
                   crispedricewafer hard bar pluribus sugarpercent pricepercent
Nik L Nip                         0    0   0        1        0.197        0.976
Boston Baked Beans                0    0   0        1        0.313        0.511
Chiclets                          0    0   0        1        0.046        0.325
Super Bubble                      0    0   0        0        0.162        0.116
Jawbusters                        0    1   0        1        0.093        0.511
                   winpercent
Nik L Nip            22.44534
Boston Baked Beans   23.41782
Chiclets             24.52499
Super Bubble         27.30386
Jawbusters           28.12744

Q14. What are the top 5 all time favorite candy types out of this set?

candy |> arrange(winpercent) |> tail(5)
                          chocolate fruity caramel peanutyalmondy nougat
Snickers                          1      0       1              1      1
Kit Kat                           1      0       0              0      0
Twix                              1      0       1              0      0
Reese's Miniatures                1      0       0              1      0
Reese's Peanut Butter cup         1      0       0              1      0
                          crispedricewafer hard bar pluribus sugarpercent
Snickers                                 0    0   1        0        0.546
Kit Kat                                  1    0   1        0        0.313
Twix                                     1    0   1        0        0.546
Reese's Miniatures                       0    0   0        0        0.034
Reese's Peanut Butter cup                0    0   0        0        0.720
                          pricepercent winpercent
Snickers                         0.651   76.67378
Kit Kat                          0.511   76.76860
Twix                             0.906   81.64291
Reese's Miniatures               0.279   81.86626
Reese's Peanut Butter cup        0.651   84.18029

Q15. Make a first barplot of candy ranking based on winpercent values.

ggplot(candy) + 
  aes(x = winpercent, y = rownames(candy)) +
  geom_bar(stat = "identity") +
  theme_minimal() + theme(axis.text.y = element_text(size = 4))

Q16. This is quite ugly, use the reorder() function to get the bars sorted by winpercent?

my_cols=rep("black", nrow(candy))
my_cols[as.logical(candy$chocolate)] = "chocolate"
my_cols[as.logical(candy$bar)] = "brown"
my_cols[as.logical(candy$fruity)] = "pink"


ggplot(candy) + 
  aes(x = winpercent, y =reorder(rownames(candy),winpercent)) +
  geom_bar(stat = "identity") +
  theme_minimal() + theme(axis.text.y = element_text(size = 4))+ geom_col(fill=my_cols)

Q17. What is the worst ranked chocolate candy?

Sixlets

Q18. What is the best ranked fruity candy?

Starburst

Taking a look at pricepercent

library(ggrepel)

# How about a plot of win vs price
ggplot(candy) +
  aes(winpercent, pricepercent, label=rownames(candy)) +
  geom_point(col=my_cols) + 
  geom_text_repel(col=my_cols, size=2, max.overlaps = 15)

Q19. Which candy type is the highest ranked in terms of winpercent for the least money - i.e. offers the most bang for your buck?

Reese’s Miniatures

Q20. What are the top 5 most expensive candy types in the dataset and of these which is the least popular?

ord <- order(candy$pricepercent, decreasing = TRUE)
head( candy[ord,c(11,12)], n=5 )
                         pricepercent winpercent
Nik L Nip                       0.976   22.44534
Nestle Smarties                 0.976   37.88719
Ring pop                        0.965   35.29076
Hershey's Krackel               0.918   62.28448
Hershey's Milk Chocolate        0.918   56.49050

Nik L Nip

Exploring the correlation structure

library(corrplot)
corrplot 0.95 loaded
cij <- cor(candy)
corrplot(cij)

Q22. Examining this plot what two variables are anti-correlated (i.e. have minus values)?

Chocolate with fruity are anti-correlated. This is reasonable because fruity candy should not be overlapping with chocolate, it make the candy taste very bad.

Q23. Similarly, what two variables are most positively correlated?

Chocolate with winpercent.

Principal Component Analysis

pca <- prcomp(candy,scale=T )
summary(pca)
Importance of components:
                          PC1    PC2    PC3     PC4    PC5     PC6     PC7
Standard deviation     2.0788 1.1378 1.1092 1.07533 0.9518 0.81923 0.81530
Proportion of Variance 0.3601 0.1079 0.1025 0.09636 0.0755 0.05593 0.05539
Cumulative Proportion  0.3601 0.4680 0.5705 0.66688 0.7424 0.79830 0.85369
                           PC8     PC9    PC10    PC11    PC12
Standard deviation     0.74530 0.67824 0.62349 0.43974 0.39760
Proportion of Variance 0.04629 0.03833 0.03239 0.01611 0.01317
Cumulative Proportion  0.89998 0.93832 0.97071 0.98683 1.00000
plot(pca$x[,1:2], col=my_cols, pch=16)

# Make a new data-frame with our PCA results and candy data
my_data <- cbind(candy, pca$x[,1:3])

p <- ggplot(my_data) + 
        aes(x=PC1, y=PC2, 
            size=winpercent/100,  
            text=rownames(my_data),
            label=rownames(my_data)) +
        geom_point(col=my_cols,alpha=0.8)
p

library(ggrepel)

p + geom_text_repel(size=1.5, col=my_cols, max.overlaps = 8)  + 
  theme(legend.position = "none") +
  labs(title="Halloween Candy PCA Space",
       subtitle="Colored by type: chocolate bar (dark brown), chocolate other (light brown), fruity (red), other (black)",
       caption="Data from 538")

ggplot(pca$rotation) +
  aes(y = reorder(rownames(pca$rotation), PC1), x = PC1) +
  geom_bar(stat = "identity") +
  theme_grey()

Q24. Complete the code to generate the loadings plot above. What original variables are picked up strongly by PC1 in the positive direction? Do these make sense to you? Where did you see this relationship highlighted previously?

Fruity, pluribus, and hard are in the positive direction. Chocolate,bar,winpercent,and pricepercent are in the negative direction. We see the negative correlation between chocolate and fruity and positive correlation between chocolate and bar, chocolate and winpercent.

Summary

Q25. Based on your exploratory analysis, correlation findings, and PCA results, what combination of characteristics appears to make a “winning” candy? How do these different analyses (visualization, correlation, PCA) support or complement each other in reaching this conclusion?

Visualization, correlation, and PCA support that the combination of chocolate, peanuty/alomndy, and bar make a winning candy.

Q26. Are popular candies more expensive? In other words: is price significantly different between “winners” and “losers”? List both average values and a P-value along with your answer.

losers = candy[which(candy$winpercent < 50),]
winners = candy[which(candy$winpercent >= 50),]

mean(winners$pricepercent)
[1] 0.580359
mean(losers$pricepercent)
[1] 0.3743696
t.test(winners,candy$pricepercent)
    Welch Two Sample t-test

data:  winners and candy$pricepercent
t = 6.2917, df = 468.34, p-value = 7.216e-10
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 3.542237 6.759764
sample estimates:
mean of x mean of y 
5.6198831 0.4688824 

Yes, popular candies are more expansive. And the difference is statistically significant since the p-value is less than 0.05.

Q27. Are candies with more sugar more likely to be popular? What is your interpretation of the means and P-value in this case?

mean(winners$sugarpercent)
[1] 0.5343077
mean(losers$sugarpercent)
[1] 0.4314565
t.test(winners,candy$sugarpercent)
    Welch Two Sample t-test

data:  winners and candy$sugarpercent
t = 6.2799, df = 468.31, p-value = 7.741e-10
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 3.532496 6.749976
sample estimates:
mean of x mean of y 
5.6198831 0.4786471 

Yes, candies with more sugar more likely to be popular. And the difference is statistically significant since the p-value is less than 0.05.