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
I will be looking at the following 3 pairs:
Cocoa Percentage vs Rating
Cocoa Percentage vs Review Date
Review Date vs Rating
1) Cocoa Percentage vs Rating
chocolate$cocoa_percentage<-chocolate$cocoa_percent*100
rating_based_on_cocoa_percent <- chocolate |>
group_by(cocoa_percentage)|>
summarize(avg_rating_per_cocoa_percentage = mean(rating,na.rm = TRUE))
rating_based_on_cocoa_percent
## # A tibble: 46 × 2
## cocoa_percentage avg_rating_per_cocoa_percentage
## <dbl> <dbl>
## 1 42 2.75
## 2 46 2.75
## 3 50 3.75
## 4 53 2
## 5 55 2.86
## 6 56 3.25
## 7 57 2.75
## 8 58 3.12
## 9 60 3.01
## 10 60.5 2.75
## # ℹ 36 more rows
2) Cocoa Percentage vs Review Date
cocoa_percentage_vs_review_date <- chocolate |>
group_by(review_date)|>
summarize(avg_cocoa_percentage_per_review_date = mean(cocoa_percent,na.rm = TRUE))
cocoa_percentage_vs_review_date
## # A tibble: 16 × 2
## review_date avg_cocoa_percentage_per_review_date
## <int> <dbl>
## 1 2006 0.704
## 2 2007 0.721
## 3 2008 0.727
## 4 2009 0.704
## 5 2010 0.708
## 6 2011 0.710
## 7 2012 0.716
## 8 2013 0.723
## 9 2014 0.723
## 10 2015 0.720
## 11 2016 0.718
## 12 2017 0.715
## 13 2018 0.712
## 14 2019 0.720
## 15 2020 0.707
## 16 2021 0.718
3) Review Date vs Rating
average_rating_per_review_date <- chocolate |>
group_by(review_date) |>
summarize(avg_rating_review_year = mean(rating,na.rm = TRUE))
average_rating_per_review_date
## # A tibble: 16 × 2
## review_date avg_rating_review_year
## <int> <dbl>
## 1 2006 3.00
## 2 2007 3.10
## 3 2008 3
## 4 2009 3.07
## 5 2010 3.15
## 6 2011 3.26
## 7 2012 3.18
## 8 2013 3.20
## 9 2014 3.19
## 10 2015 3.24
## 11 2016 3.23
## 12 2017 3.36
## 13 2018 3.19
## 14 2019 3.13
## 15 2020 3.26
## 16 2021 3.32
Plot 1: Cocoa Percentage vs Rating
ggplot(rating_based_on_cocoa_percent, aes(x = cocoa_percentage, y = avg_rating_per_cocoa_percentage)) +
geom_point() +
labs(title = "Average Rating vs Cocoa Percentage",
x = "Cocoa Percentage",
y = "Average Rating")
From the plot above, which depicts the average rating based on the percentage of cocoa found in the chocolate bar, it seems that the most amount of cocoa results in lower ratings. This would make sense as too much cocoa could result in a more bitter tasting chocolate. It looks like the sweet spot (literally) for the most favorable tasting chocolate is a chocolate bar having a 60-70% cocoa percentage.
Plot 2: Cocoa Percentage vs Review Date
ggplot(cocoa_percentage_vs_review_date,aes(x=review_date,y=avg_cocoa_percentage_per_review_date))+geom_bar(stat="identity",fill="red")+labs(x="Review Date",y="Avg Cocoa Percentage", title="Average Cocoa Percentage per Review Date")+ theme(axis.text.x = element_text(angle = 90, vjust = 1, hjust=1))
From the plot above we can see that the average cocoa percentage was fairly similar throughout the review dates of the chocolate bars. From this I was potentially thinking that if there was a dip in average cocoa percentage there could be a confounding variable such as extremities in weather that could have caused the cocoa percentage found in the chocolate bars to decrease or increase.
Plot 3: Review Date vs Rating
ggplot(average_rating_per_review_date,aes(x=review_date, y=avg_rating_review_year))+geom_bar(stat="identity",fill="red")+labs(x="Review Date",y="Avg Rating for Review Year", title="Average Rating Per Review Date")
From the plot above we can see that the average rating was fairly similar throughout the review dates of the chocolate bars. If there were a drastic difference in any review date year with average rating I would have then seeked Plot 2: Cocoa Percentage vs Review Date in order to find check that may be there could have been a confounding variable such as weather extremities that could have caused cocoa percentage and its review to be affected by the years weather.
Combination 1: Cocoa Percentage vs Rating
chocolate$cocoa_percentage<-chocolate$cocoa_percent*100
rating_based_on_cocoa_percent <- chocolate |>
group_by(cocoa_percentage)|>
summarize(avg_rating_per_cocoa_percentage = mean(rating,na.rm = TRUE))
cor(rating_based_on_cocoa_percent$cocoa_percentage, rating_based_on_cocoa_percent$avg_rating_per_cocoa_percentage, method='pearson')
## [1] -0.1607946
From the data of cocoa percentage vs rating we received a -0.16 indicating there is a negative, weak correlation between cocoa percentage and avg rating, so as cocoa percentage increases the rating decreases. However, I believed there would be a stronger correlation, I believe the very little correlation could be linked to the differences in food palettes between people, some think bitter is better and some think sweeter is better!
Combination 2: Cocoa Percentage vs Review Date
cocoa_percentage_vs_review_date <- chocolate |>
group_by(review_date)|>
summarize(avg_cocoa_percentage_per_review_date = mean(cocoa_percent,na.rm = TRUE))
cor(cocoa_percentage_vs_review_date$review_date, cocoa_percentage_vs_review_date$avg_cocoa_percentage_per_review_date, method='spearman')
## [1] 0.04117647
Above we received a correlation value of 0.04 indicating there is a little-positive to no correlation between cocoa percentage vs review date. With this pair I was hoping to check and see if there were large dips in cocoa percentage where extreme weather conditions impacted cocoa percentages.
Combination 3: Review Date vs Rating
average_rating_per_review_date <- chocolate |>
group_by(review_date) |>
summarize(avg_rating_review_year = mean(rating,na.rm = TRUE))
cor(average_rating_per_review_date$review_date, average_rating_per_review_date$avg_rating_review_year, method='spearman')
## [1] 0.7029412
From the above correlation value we can assume that there exists a strong linear correlation between review date versus rating, so as the years continued on the rating on chocolates. I’d assume that as we’ve advanced in technology, chocolate is able to become a better craft for large manufacturers.
library(boot)
# we use a "_" so we don't overwrite the original function
boot_ci <- function (v, func = median, conf = 0.95, n_iter = 1000) {
# the `boot` library needs the function in this format
boot_func <- \(x, i) func(x[i])
b <- boot(v, boot_func, R = n_iter)
boot.ci(b, conf = conf, type = "perc")
}
#Confidence Interval for Rating
boot_ci(v = chocolate$rating , func = mean, conf = 0.95)
## BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
## Based on 1000 bootstrap replicates
##
## CALL :
## boot.ci(boot.out = b, conf = conf, type = "perc")
##
## Intervals :
## Level Percentile
## 95% ( 3.178, 3.213 )
## Calculations and Intervals on Original Scale
The 95% confidence interval for overall chocolate rating was 3.179-3.214 rating out of 5.
Combination 1: Cocoa Percentage vs Rating
#Confidence Interval for Response Variable
boot_ci(v = rating_based_on_cocoa_percent$avg_rating_per_cocoa_percentage, func = mean, conf = 0.95)
## BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
## Based on 1000 bootstrap replicates
##
## CALL :
## boot.ci(boot.out = b, conf = conf, type = "perc")
##
## Intervals :
## Level Percentile
## 95% ( 2.930, 3.125 )
## Calculations and Intervals on Original Scale
The 95% confidence interval for avg_rating_per_cocoa_percentage in response to cocoa percentages was 2.921–3.121 rating out of 5. This is a tighter 95% confidence interval in comparison to the overall ratings which indicates that we have a more stable estimate that cocoa percentage does after the response variable of rating.
Combination 2: Review Date vs Cocoa Percentage
#Confidence Interval for Cocoa Percentage
boot_ci(v = chocolate$cocoa_percentage, func = mean, conf = 0.95)
## BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
## Based on 1000 bootstrap replicates
##
## CALL :
## boot.ci(boot.out = b, conf = conf, type = "perc")
##
## Intervals :
## Level Percentile
## 95% (71.43, 71.85 )
## Calculations and Intervals on Original Scale
#Confidence Interval for Response Variable
boot_ci(v = cocoa_percentage_vs_review_date$avg_cocoa_percentage_per_review_date, func = mean, conf = 0.95)
## BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
## Based on 1000 bootstrap replicates
##
## CALL :
## boot.ci(boot.out = b, conf = conf, type = "perc")
##
## Intervals :
## Level Percentile
## 95% ( 0.7121, 0.7185 )
## Calculations and Intervals on Original Scale
The 95% confidence interval for the avg_cocoa_percentage_per_review_date is 71.121-71.84% of cocoa which is wider than the coca_percentage 95% confidence interval which could indicate a less stable relationship, which makes sense with the no correlation that exists between review date and avg cocoa.
Combination 3: Review Date vs Rating
average_rating_per_review_date <- chocolate |>
group_by(review_date) |>
summarize(avg_rating_review_year = mean(rating,na.rm = TRUE))
#Confidence Interval for Response Variable
boot_ci(v = average_rating_per_review_date $avg_rating_review_year, func = mean, conf = 0.95)
## BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
## Based on 1000 bootstrap replicates
##
## CALL :
## boot.ci(boot.out = b, conf = conf, type = "perc")
##
## Intervals :
## Level Percentile
## 95% ( 3.138, 3.230 )
## Calculations and Intervals on Original Scale
This 95% confidence interval for average_rating_per_review_date between 3.133-3.230 which is higher, and narrower than ratings 95% confidence interval. This plus the correlation coefficient could indicate that as years have progressed, chocolate ratings have increased which could be due to better technology!