Week 4 Data Dive (Yuritzi Avila-Robledo)

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ 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
library(dplyr)
chocolate <- read.csv("~/Desktop/chocolate.csv")
library(readr)
summary(chocolate)
##       ref       company_manufacturer company_location    review_date  
##  Min.   :   5   Length:2530          Length:2530        Min.   :2006  
##  1st Qu.: 802   Class :character     Class :character   1st Qu.:2012  
##  Median :1454   Mode  :character     Mode  :character   Median :2015  
##  Mean   :1430                                           Mean   :2014  
##  3rd Qu.:2079                                           3rd Qu.:2018  
##  Max.   :2712                                           Max.   :2021  
##  country_of_bean_origin specific_bean_origin_or_bar_name cocoa_percent   
##  Length:2530            Length:2530                      Min.   :0.4200  
##  Class :character       Class :character                 1st Qu.:0.7000  
##  Mode  :character       Mode  :character                 Median :0.7000  
##                                                          Mean   :0.7164  
##                                                          3rd Qu.:0.7400  
##                                                          Max.   :1.0000  
##  ingredients        most_memorable_characteristics     rating     
##  Length:2530        Length:2530                    Min.   :1.000  
##  Class :character   Class :character               1st Qu.:3.000  
##  Mode  :character   Mode  :character               Median :3.250  
##                                                    Mean   :3.196  
##                                                    3rd Qu.:3.500  
##                                                    Max.   :4.000

Collection of 5 Random Samples (With Replacement)

num_rows=1:nrow(chocolate)
num_half_samples=nrow(chocolate)/2

set.seed(123)
sample_1<-chocolate[sample(num_rows,num_half_samples,replace=TRUE),]
sample_2<-chocolate[sample(num_rows,num_half_samples,replace=TRUE),]
sample_3<-chocolate[sample(num_rows,num_half_samples,replace=TRUE),]
sample_4<-chocolate[sample(num_rows,num_half_samples,replace=TRUE),]
sample_5<-chocolate[sample(num_rows,num_half_samples,replace=TRUE),]

I think it would be better to compare the chocolate based off the country of the cocoa bean origin. In order to do this I will group by ‘country_of_bean_origin’ and it will be interesting to see the summary statistics of each chocolate.

sample_1_summary<-sample_1|>
  group_by(country_of_bean_origin)|>
  summarise(mean_rating=mean(rating))


sample_2_summary<-sample_2|>
  group_by(country_of_bean_origin)|>
  summarise(mean_rating=mean(rating))


sample_3_summary<-sample_3|>
  group_by(country_of_bean_origin)|>
  summarise(mean_rating=mean(rating))


sample_4_summary<-sample_4|>
  group_by(country_of_bean_origin)|>
  summarise(mean_rating=mean(rating))


sample_5_summary<-sample_5|>
  group_by(country_of_bean_origin)|>
  summarise(mean_rating=mean(rating))

Above are the 5 summaries of the 5 samples based on the chocolate’s cocoa bean country of origin and its average rating. I would like to display a plot of the average rating for each chocolates country of origin in each sample.

library(ggplot2)
ggplot()+
  geom_point(data=sample_1_summary, mapping=aes(x=country_of_bean_origin, y=mean_rating), color="red")+
  geom_point(data=sample_2_summary, mapping=aes(x=country_of_bean_origin, y=mean_rating), color="blue")+
  geom_point(data=sample_3_summary, mapping=aes(x=country_of_bean_origin, y=mean_rating), color="pink")+
  geom_point(data=sample_4_summary, mapping=aes(x=country_of_bean_origin, y=mean_rating), color="purple")+
  geom_point(data=sample_5_summary, mapping=aes(x=country_of_bean_origin, y=mean_rating), color="yellow")+
  labs(title="Sampling Averages of Rating Per Country of Bean Origin",x='Country of Bean Origin',y='Avg Rating')+
  theme(axis.text.x = element_text(angle = 90, vjust = 1, hjust=1))

From this plot, the point that really caught my attention was Sample 5’s Tobago having an average rating of 4 for their chocolates produced. The country that harvests these cocoa beans with the lowest average rating of 2 coming from Sample 1 was Puerto Rico which caught my attention as I’d expect a warmer, more tropical country to have richer cocoa beans. This could be due to where those cocoa beans produced from Puerto Rico are manufactured, it may be that the manufacturer doesn’t keep the cocoa beans in great conditions for use in producing the chocolate.

Investigating the Differences Between the Samples

I will create a histogram for each sample set.

Sample 1 Histogram

ggplot()+
  geom_histogram(data=sample_1_summary,aes(mean_rating), binwidth = 0.1)+
  labs(title="Sample 1: Histogram of Rating per Country of Bean Origin",
       x="Avg Rating",
       y="# of countries")+
  theme_minimal()

In this sample population, the graph has a left skew where there were more countries that grow these cocoa beans that have a chocolate rating of 3.2 stars out of 5 stars.

Sample 2 Histogram

ggplot()+
  geom_histogram(data=sample_2_summary,aes(mean_rating), binwidth = 0.1)+
  labs(title="Sample 2: Histogram of Rating per Country of Bean Origin",
       x="Avg Rating",
       y="# of countries")+
  theme_minimal()

This histogram seems to be almost normal distributed with more countries having a chocolate rating around 3.1-3.2 stars out of 5.

Sample 3 Histogram

ggplot()+
  geom_histogram(data=sample_3_summary,aes(mean_rating), binwidth = 0.1)+
  labs(title="Sample 3: Histogram of Rating per Country of Bean Origin",
       x="Avg Rating",
       y="# of countries")+
  theme_minimal()

This histogram seems to be almost left-skewed, so in this sample population the average rating per country of origin closer to an average rating of 3.2.

Sample 4 Histogram

ggplot()+
  geom_histogram(data=sample_4_summary,aes(mean_rating), binwidth = 0.1)+
  labs(title="Sample 4: Histogram of Rating per Country of Bean Origin",
       x="Avg Rating",
       y="# of countries")+
  theme_minimal()

This histogram seems to be left skewed, so in this sample population there were more countries that grow these cocoa beans with chocolate ratings of 3.2-3.3.

Sample 5 Histogram

ggplot()+
  geom_histogram(data=sample_5_summary,aes(mean_rating), binwidth = 0.1)+
  labs(title="Sample 5: Histogram of Rating per Country of Bean Origin",
       x="Avg Rating",
       y="# of countries")+
  theme_minimal()

This histogram seems to be normally distributed with there being a larger amount of countries of cocoa bean origin having a chocolate bar rating of 3.1-3.2 stars.

What would you have called an anomaly in one sub-sample that you wouldn’t in another? Are there aspects of the data that are consistent among all sub-samples? Try to incorporate Monte Carlo simulations here (see Lab 3) if you can?

mean(chocolate$rating)
## [1] 3.196344
monte_carlo_sim <- function(n, m) {

  sim <- function (x) mean(sample_n(chocolate, n, replace = TRUE)$rating)
  
  x <- map_dbl(1:m, sim)
  
  df <- data.frame(
    iter = 1:m,
    sample_mean = x
  )
  return(df)
}
mc_sim_rating_mean <- mean(chocolate$rating)

n <- 10    
m <- 1000  


monte_carlo_sim(n, m) |>
  ggplot() +
  geom_histogram(mapping = aes(x = sample_mean), binwidth = 0.05, fill = "red") +
  geom_vline(xintercept = mc_sim_rating_mean, color = "orange") + 
  scale_x_continuous(breaks = seq(0, 5, by = 0.1)) +
  labs(title = "Monte Carlo Simulation",
       x = "Avg Rating",
       y = "Number of Samples") 

In this plot we see an almost normal distribution shape, and we may see that the largest sample has the average rating of 3.15, and of all the samples the average ratings are between 2.7-3.6.