#load in libraries and data
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(plotly)
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
wines <- read.csv("/Users/jamesflaumenhaft/STAT218/Homeworks/Wine_Training.csv")
#lets get an idea of the data by looking at the distribution of qualities
wines |>
ggplot(aes(x = quality))+
geom_bar()+
geom_text(stat='count', aes(label=..count..), vjust= 0)
## Warning: The dot-dot notation (`..count..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(count)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
Interesting, so most wines fall between a quality 5 and 6. So, we want
to be careful when selecting a variable to be sure it is predictive of
these rarer qualities. That is to say, a simple regression maybe
misleading because it could be really good at differentiating a 4 from 6
but not a 6 from an 8.
With this in mind, lets graph the variables that have the strongest regressions
First, volatile acidity a predictor of poor quality according to the regression
#volatile acidity and quality
wines|>
ggplot()+
geom_boxplot(aes(y = volatile.acidity, x = factor(quality)))+
geom_point(aes(y = volatile.acidity, x = factor(quality)), fill=NA, shape=21)
This looks great!
#Next, lets look at chlorides
wines|>
ggplot()+
geom_boxplot(aes(y = chlorides, x = factor(quality)))+
geom_point(aes(y = chlorides, x = factor(quality)), fill=NA, shape=21)
Does not look very promising. While these is a notable trend the
differences between the means seem to subtle.
#Next, lets look at total sulfur dioxide
wines|>
ggplot()+
geom_boxplot(aes(y = total.sulfur.dioxide, x = factor(quality)))+
geom_point(aes(y = total.sulfur.dioxide, x = factor(quality)), fill=NA, shape=21)
The mean differences betweeen the two extremes seem to similar.
#Next, sulphates (a positive predictor)
wines|>
ggplot()+
geom_boxplot(aes(y = sulphates, x = factor(quality)))+
geom_point(aes(y = sulphates, x = factor(quality)), fill=NA, shape=21)
The jump from 6 to 7 looks pretty good and so does the over all trend,
but the high outliers in the middle groups worry me.
#Next, alcohol (a positive predictor)
wines|>
ggplot()+
geom_boxplot(aes(y = alcohol, x = factor(quality)))+
geom_point(aes(y = alcohol, x = factor(quality)), fill=NA, shape=21)
I worry about the differences between the two extremes, but there is
definitely a strong trend.
Lets look at are seemingly best three variable graphed together.
#3d graph of volatile acidity, sulphates, and alcohol
wines|>
plot_ly(x = ~volatile.acidity, y = ~sulphates, z = ~alcohol, color = ~factor(quality)) |>
add_markers() |>
layout(scene = list(xaxis = list(title = 'volatile.acidity'),
yaxis = list(title = 'sulphates'),
zaxis = list(title = 'alcohol')))
Does not look great but doesnt look bad. My worry is that we need to make a model that distinguish particularly accurately at the extremes and I think this could have trouble distinguishing between a 6 and a 7 for example.