Week 8

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
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
library(dplyr)
library(tidyverse)
library(ggthemes)
library(ggrepel)
library(AmesHousing)
library(boot)
library(broom)
library(lindia)

# remove scientific notation
options(scipen = 6)

# default theme, unless otherwise noted
theme_set(theme_minimal())

For this data dive I first began by importing my data set, and following this I will complete each task of the assignment.

Continuous “Valuable” Column of Data (Response Variable)

Given that my data is a chocolate csv, the chocolate’s rating is likely of the most value to people when it comes to data regarding chocolate.

Categorical Explanatory Variable

The column of data that might influence my response variable of chocolate rating that is a categorical column of data could be the “country_of_bean_origin” column. The null hypothesis for (H0) using these two categories is “There is no significant difference in ratings amount different countries of bean origin”.

ANOVA test

result <- aov(rating ~ country_of_bean_origin, data = chocolate)
summary(result)
##                          Df Sum Sq Mean Sq F value Pr(>F)  
## country_of_bean_origin   61   17.2  0.2814   1.434 0.0162 *
## Residuals              2468  484.4  0.1963                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

The ANOVA test run provided us with the following results and their significance:

  • Df (Degrees of Freedom): This showed that there ate 61 levels or categories in the ‘country_of_bean_origin’ column.

  • Sum Sq (Sum of Squares): The sum of squares is attributed to the ‘country_of_bean_origin’ is 17.2 and the residuals sum of squares is 484.4.

  • Mean Sq(Mean Square): The mean square is the average sum of squares that is calculated by dividing the sum of squares by the degrees of freedom.

  • F value: The F value is the ratio of the variance among group means to the variance within the groups.

  • Pr(>F): This is the p-value associated with the F-statistic which is used to determine whether the observed result is statistically significant. In this result we got a p-value of 0.0162 which is less than 0.05 which is the usual significance level.

The ANOVA test provided us with a p-value(0.0162) is less than the significance level of 0.05 which leads me to reject the null hypothesis that states that “There is no significant difference in ratings amount different countries of bean origin”. This suggests that there is a statistically significant difference in ratings among chocolates from different countries of cocoa bean origin.

Single Continuous Data (might influence the response variable)

A continuous variable that may affect the response variable (rating) could be “cocoa_percent”. A linear regression model using these variables is the following:

library(ggplot2)
chocolate|>
  ggplot(mapping=aes(x=rating, y=cocoa_percent))+
  geom_point(size=2, color='darkblue')

I will use the linear model function to draw a line of best fit:

chocolate|>
  ggplot(mapping=aes(x=rating, y=cocoa_percent))+
  geom_point(size=2) +
  geom_smooth(method='lm', se=FALSE, color='darkblue')
## `geom_smooth()` using formula = 'y ~ x'

regression_model <- lm(rating ~ cocoa_percent, data = chocolate)
summary(regression_model)
## 
## Call:
## lm(formula = rating ~ cocoa_percent, data = chocolate)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.21541 -0.23867  0.03459  0.28459  0.99393 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     4.0295     0.1121  35.949  < 2e-16 ***
## cocoa_percent  -1.1630     0.1560  -7.456 1.22e-13 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4406 on 2528 degrees of freedom
## Multiple R-squared:  0.02152,    Adjusted R-squared:  0.02113 
## F-statistic: 55.59 on 1 and 2528 DF,  p-value: 1.218e-13

In order to make sure that the relationship between cocoa_percent and rating is roughly linear we did the above and we received a coefficient -1.163 that estimates the change in average rating for a one-unit increase in cocoa percentage. Upon running the linear regression model on the ‘cocoa_percent’ variable we received the following insight:

  • Coefficients:

    • Intercept (4.0295): This is the estimated average rating when the cocoa percentage is 0. This is difficult to interpret as cocoa is what makes chocolate a chocolate so if there is no cocoa then I take it as the rating is based off the pure sugar.

    • Cocoa Percent (-1.1630): This coefficient represents the estimated change in the average rating for a one-unit increase in cocoa percentage. In this case, as cocoa percentage increases, the average rating decreases by approximately 1.1630. This makes sense as more cocoa could result in a more bitter tasting chocolate.

  • Residuals:

    • These are the differences between the actual and predicted ratings. The distribution of residuals (-2.21541, 0.03459, 0.99393) indicates how well the model fits the data. The indicated residuals have some variability suggesting that the model might not capture all factors influencing ratings.
  • R-squared (0.02152):

    • The R-squared value is low indicating that only about 2.152% of the variability in ratings can be explained by the cocoa percentage in this model.
  • P-value (1.218e-13):

    • The p-value associated with the F-statistic is extremely low suggesting that the overall model is statistically significant so the cocoa percentage is significant enough to predict the rating of the chocolates.

From these tests and results I suggest that in order for chocolate bar companies to optimize reading through this data, they may want to consider producing chocolates with cocoa percentages that fall within range where the chocolate rating is higher.