#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
#install.packages("klaR")
library(klaR)
## Loading required package: MASS
##
## Attaching package: 'MASS'
##
## The following object is masked from 'package:plotly':
##
## select
##
## The following object is masked from 'package:dplyr':
##
## select
#install.packages("psych")
library(psych)
##
## Attaching package: 'psych'
##
## The following objects are masked from 'package:ggplot2':
##
## %+%, alpha
library(MASS)
#install.packages("ggord")
# Enable the r-universe repo
library(rpart)
library(rpart.plot)
#options(repos = c(
#fawda123 = 'https://fawda123.r-universe.dev',
#CRAN = 'https://cloud.r-project.org'))
# Install ggord
#install.packages('ggord')
library(ggord)
library(randomForest)
## randomForest 4.7-1.1
## Type rfNews() to see new features/changes/bug fixes.
##
## Attaching package: 'randomForest'
##
## The following object is masked from 'package:psych':
##
## outlier
##
## The following object is masked from 'package:dplyr':
##
## combine
##
## The following object is masked from 'package:ggplot2':
##
## margin
wines <- read.csv("/Users/jamesflaumenhaft/STAT218/Homeworks/Wine_Training.csv")
Lets run a multivariable regression to help select three variables to best predict new wine qualities.
#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.
#Next, pH (not significant)
wines|>
ggplot()+
geom_boxplot(aes(y = pH, x = factor(quality)))+
geom_point(aes(y = pH, x = factor(quality)), fill=NA, shape=21)
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')))
This looks okay, but really we are most interested in distinguishing bad wines (quality of 3 or 4), decent wines (quality of 5 or 6), and good wines (quality of 7 or 8). So, lets visualize that.
wines|>
plot_ly(y = ~volatile.acidity, x = ~sulphates, z = ~alcohol, color = ~factor(quality), colors = c("red", "red", "darkgrey", "darkgrey", "steelblue", "steelblue")) |>
add_markers() |>
layout(scene = list(xaxis = list(title = 'volatile.acidity'),
yaxis = list(title = 'sulphates'),
zaxis = list(title = 'alcohol')))
Looks fairly well segregated.
Lets try to build some models and compare there accuracy.
We are specifically interested in the very good and the very bad wines. So in addition to using average error of the whole model, we look at average error for values of 3/4 and values of 7/8.
Lets make a random forest and see which variables really are most important
This confirms the results of the regression, So, based on this we chose the three most important variables:
Sulphates, Alcohol, and volatile.acidity
What if we combine the results of the LDA and the results of the tree
#LDA and predictions table
linear <- lda(quality ~ sulphates + volatile.acidity + alcohol, data = wines)
linear
## Call:
## lda(quality ~ sulphates + volatile.acidity + alcohol, data = wines)
##
## Prior probabilities of groups:
## 3 4 5 6 7 8
## 0.005459509 0.029117379 0.429481347 0.405823476 0.118289354 0.011828935
##
## Group means:
## sulphates volatile.acidity alcohol
## 3 0.5533333 0.9858333 10.416667
## 4 0.6046875 0.7303125 10.304688
## 5 0.6127754 0.5771504 9.934322
## 6 0.6687892 0.5018274 10.640845
## 7 0.7503077 0.3964231 11.498077
## 8 0.7815385 0.3569231 11.776923
##
## Coefficients of linear discriminants:
## LD1 LD2 LD3
## sulphates 1.9414083 1.2550412 -5.6798862
## volatile.acidity -3.1032774 5.2955892 -1.1925392
## alcohol 0.8128081 0.6138691 0.3557154
##
## Proportion of trace:
## LD1 LD2 LD3
## 0.9010 0.0987 0.0003
preds <- predict(linear, wines)
LDApreds <- as.tibble(preds$posterior)
## Warning: `as.tibble()` was deprecated in tibble 2.0.0.
## ℹ Please use `as_tibble()` instead.
## ℹ The signature and semantics have changed, see `?as_tibble`.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
#Now lets use those probabilities alongside our three variables in a random forest
winesLDA <- cbind(wines, LDApreds)
colnames(winesLDA)[13]<- c("three")
colnames(winesLDA)[14]<- c("four")
colnames(winesLDA)[15]<- c("five")
colnames(winesLDA)[16]<- c("six")
colnames(winesLDA)[17]<- c("seven")
colnames(winesLDA)[18]<- c("eight")
Now we have LDA predictions along with the actual data.
#combining the categories
#use string detect?
goodwinesLDA <- winesLDA |>
mutate(rating = case_when(str_detect(quality, "3")
|str_detect(quality, "4") ~ "bad",
str_detect(quality, "5")
|str_detect(quality, "6") ~ "decent",
str_detect(quality, "7")
|str_detect(quality, "8") ~ "good"))
rfgood <- randomForest(factor(rating) ~ volatile.acidity + sulphates + alcohol,
data = goodwinesLDA,
ntree = 1000,
mtry = 2)
rfgood
##
## Call:
## randomForest(formula = factor(rating) ~ volatile.acidity + sulphates + alcohol, data = goodwinesLDA, ntree = 1000, mtry = 2)
## Type of random forest: classification
## Number of trees: 1000
## No. of variables tried at each split: 2
##
## OOB estimate of error rate: 15.2%
## Confusion matrix:
## bad decent good class.error
## bad 4 34 0 0.89473684
## decent 4 858 56 0.06535948
## good 0 73 70 0.51048951
This is overall a pretty good model. It misses a lot of the bad wines, but maybe we can adjust that by changing the thresholds or by using the LDA predictions.
Lets use the LDA predictions to decide the top ten from the predictions of this tree.
#lets sort the random forest predictions
#using probability of being a certainty quality
goodwinesLDAtree <- goodwinesLDA |>
mutate(predicted = rfgood$predicted) |>
mutate(high = 10*eight + seven - six) |>
mutate(low = 10*three + four - five)
#sorting the best
#this generates the top ten wines
#that were predicted to be good wine
#in terms of most likely to be an 8
goodwinesLDAtree |>
arrange(-high) |>
filter(predicted == "good") |>
head(10) |>
mutate(success = if_else(quality == 7 | quality == 8, 1, 0)) |>
pull(success) |>
sum() / 10
## [1] 0.7
#sorting the worst
#this generates the bottom ten wines
#that were predicted to be bad/decent wines
#in terms of most likely to be an 3
goodwinesLDAtree |>
arrange(-low) |>
filter(predicted == "bad" | predicted == "decent") |>
head(10) |>
mutate(success = if_else(quality == 3 | quality == 4, 1, 0)) |>
pull(success) |>
sum() / 10
## [1] 0.6
Not bad. Not lets try to sort by votes
Lets start with the best:
#now lets try a similar method of sorting but using the posterior votes to sort
votes <- rfgood$votes
winesFINAL <- goodwinesLDAtree |>
cbind(votes)
winesFINAL |>
arrange(-winesFINAL$good) |>
head(10) |>
mutate(success = if_else(quality == 7 | quality == 8, 1, 0)) |>
pull(success) |>
sum() / 10
## [1] 0.9
Now lets do the worst:
goodwinesLDAtree |>
arrange(-winesFINAL$bad) |>
head(10) |>
mutate(success = if_else(quality == 3 | quality == 4, 1, 0)) |>
pull(success) |>
sum() / 10
## [1] 0.4
We now have a fairly effective model that predicts wine quality with a random forest and sorts those prediction with LDA information in order to decide the top ten.
Lets now apply it to the new data.
NEWwines <- read.csv("/Users/jamesflaumenhaft/STAT218/Homeworks/wine_data_new.csv")
#first lets predict using the random forest
predictions <- predict(rfgood, NEWwines, type = "prob")
dfprediction <- as.data.frame(predictions)
winetree <- NEWwines |>
cbind(dfprediction)
#LDA
newpreds <- predict(linear, NEWwines)
NEWLDApreds <- as.tibble(newpreds$posterior)
#Now lets use those probabilities alongside our three variables in a random forest
winesprediction <- cbind(winetree, NEWLDApreds)
colnames(winesprediction)[8]<- c("three")
colnames(winesprediction)[9]<- c("four")
colnames(winesprediction)[10]<- c("five")
colnames(winesprediction)[11]<- c("six")
colnames(winesprediction)[12]<- c("seven")
colnames(winesprediction)[13]<- c("eight")
Sorting using random forest votes
#10 best
winesprediction |>
arrange(-good) |>
head(10) |>
pull(wine_id)
## [1] 389 190 80 292 390 481 223 239 16 235
#10 worst
winesprediction |>
arrange(-bad) |>
head(10) |>
pull(wine_id)
## [1] 20 142 261 77 252 101 267 325 414 137
Random forest + LDA Method:
Now we sort based on LDA
winespredictionFINAL <- winesprediction |>
mutate(high = 10*eight + seven - six) |>
mutate(low = 10*three + four - five)
#10 best using LDA sorting
winespredictionFINAL |>
arrange(-high) |>
head(10) |>
pull(wine_id)
## [1] 76 207 436 235 110 80 157 115 312 223
#10 worst using LDA sorting
winespredictionFINAL |>
arrange(-low) |>
head(10) |>
pull(wine_id)
## [1] 20 142 51 423 252 77 78 406 293 258