I chose the red wine data set because I LOVE red wine! To begin exploring this data set, first I listed and summarized the variables, and then reviewed some of the values:
## [1] "X" "fixed.acidity" "volatile.acidity"
## [4] "citric.acid" "residual.sugar" "chlorides"
## [7] "free.sulfur.dioxide" "total.sulfur.dioxide" "density"
## [10] "pH" "sulphates" "alcohol"
## [13] "quality"
## X fixed.acidity volatile.acidity citric.acid
## Min. : 1.0 Min. : 4.60 Min. :0.1200 Min. :0.000
## 1st Qu.: 400.5 1st Qu.: 7.10 1st Qu.:0.3900 1st Qu.:0.090
## Median : 800.0 Median : 7.90 Median :0.5200 Median :0.260
## Mean : 800.0 Mean : 8.32 Mean :0.5278 Mean :0.271
## 3rd Qu.:1199.5 3rd Qu.: 9.20 3rd Qu.:0.6400 3rd Qu.:0.420
## Max. :1599.0 Max. :15.90 Max. :1.5800 Max. :1.000
## residual.sugar chlorides free.sulfur.dioxide
## Min. : 0.900 Min. :0.01200 Min. : 1.00
## 1st Qu.: 1.900 1st Qu.:0.07000 1st Qu.: 7.00
## Median : 2.200 Median :0.07900 Median :14.00
## Mean : 2.539 Mean :0.08747 Mean :15.87
## 3rd Qu.: 2.600 3rd Qu.:0.09000 3rd Qu.:21.00
## Max. :15.500 Max. :0.61100 Max. :72.00
## total.sulfur.dioxide density pH sulphates
## Min. : 6.00 Min. :0.9901 Min. :2.740 Min. :0.3300
## 1st Qu.: 22.00 1st Qu.:0.9956 1st Qu.:3.210 1st Qu.:0.5500
## Median : 38.00 Median :0.9968 Median :3.310 Median :0.6200
## Mean : 46.47 Mean :0.9967 Mean :3.311 Mean :0.6581
## 3rd Qu.: 62.00 3rd Qu.:0.9978 3rd Qu.:3.400 3rd Qu.:0.7300
## Max. :289.00 Max. :1.0037 Max. :4.010 Max. :2.0000
## alcohol quality
## Min. : 8.40 Min. :3.000
## 1st Qu.: 9.50 1st Qu.:5.000
## Median :10.20 Median :6.000
## Mean :10.42 Mean :5.636
## 3rd Qu.:11.10 3rd Qu.:6.000
## Max. :14.90 Max. :8.000
## 'data.frame': 1599 obs. of 13 variables:
## $ X : int 1 2 3 4 5 6 7 8 9 10 ...
## $ fixed.acidity : num 7.4 7.8 7.8 11.2 7.4 7.4 7.9 7.3 7.8 7.5 ...
## $ volatile.acidity : num 0.7 0.88 0.76 0.28 0.7 0.66 0.6 0.65 0.58 0.5 ...
## $ citric.acid : num 0 0 0.04 0.56 0 0 0.06 0 0.02 0.36 ...
## $ residual.sugar : num 1.9 2.6 2.3 1.9 1.9 1.8 1.6 1.2 2 6.1 ...
## $ chlorides : num 0.076 0.098 0.092 0.075 0.076 0.075 0.069 0.065 0.073 0.071 ...
## $ free.sulfur.dioxide : num 11 25 15 17 11 13 15 15 9 17 ...
## $ total.sulfur.dioxide: num 34 67 54 60 34 40 59 21 18 102 ...
## $ density : num 0.998 0.997 0.997 0.998 0.998 ...
## $ pH : num 3.51 3.2 3.26 3.16 3.51 3.51 3.3 3.39 3.36 3.35 ...
## $ sulphates : num 0.56 0.68 0.65 0.58 0.56 0.56 0.46 0.47 0.57 0.8 ...
## $ alcohol : num 9.4 9.8 9.8 9.8 9.4 9.4 9.4 10 9.5 10.5 ...
## $ quality : int 5 5 5 6 5 5 5 7 7 5 ...
I wanted to focus on how the variables can explain quality ratings. To get basic information on the quality, I made a histogram to get the counts. The majority of the observations were towards the middle of the quality scale:
I plotted a histogram to show how the observations fell on the alcohol content range. Fewer observations are in the higher alcohol range:
I tried to play around with the log10 of the alcohol content, since alcohol is left-skewed, but this was a dead end – didn’t really shed any new light on this variable:
I plotted each of the variables to learn more about their ranges and distributions:
There are 1599 obervations of 13 variables. The variables are: fixed.acidity, volatile.acidity, citric.acid, residual.sugar, chlorides, free.sulfur.dioxide, total.sulfur.dioxide, density, pH, sulphates, alcohol and quality.
Most red wines had a data quality rating between 5 and 6, which is right in the middle of the observed range of 3 to 8.
The main feature is quality, and whether and how the presence of other features impacts red wine quality for better or worse. I am interested in determining which features are correlated with improved or degraded quality ratings.
Alchohol appears to have a positive correlation with quality, whereas volatile.acidity has a negative correlation (i.e., a higher volatile.acidity correlates with lower wine quality rating).
Yes, I created buckets for Hi, Med and Low alcohol content, using tertiles as the break points.
The alcohol counts had a left-skewed distribution, which surprised me. I tried using the log10 of the alcohol content, but that transformation did not shed any additional light on this variable.
I made an initial comparison plot, to take a quick look at how the variables relate to one another. There are too many variables in this to get much out of this plot visually, however:
I wanted to plot a variable that might be correlated with quality, to get a feel for how the relationship looks visually, so chose alcohol and added a smoothing function to get a feel for the relationship:
To find the strength of the relationship, I also ran a correlation test between alcohol and quality, which yielded a significant R coefficient (0.48):
##
## Pearson's product-moment correlation
##
## data: redwine$alcohol and redwine$quality
## t = 21.639, df = 1597, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.4373540 0.5132081
## sample estimates:
## cor
## 0.4761663
Next, I wanted to see how each variable impacts quality, by showing each variable’s relationship to quality on a grid with one another. This grid of univariate plots did not visually look like much of a strong relationship with quality:
A different way of showing correlations is via the ggcorr() function, which makes the relationships with quality a little more clear than with ggpairs() function.
I wanted to measure the strength of the relationship of some of the variable pairs that stood out on the ggcorr() chart above, by calculating the exact correlations:
##
## Pearson's product-moment correlation
##
## data: redwine$total.sulfur.dioxide and redwine$free.sulfur.dioxide
## t = 35.84, df = 1597, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.6395786 0.6939740
## sample estimates:
## cor
## 0.6676665
##
## Pearson's product-moment correlation
##
## data: redwine$fixed.acidity and redwine$citric.acid
## t = 36.234, df = 1597, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.6438839 0.6977493
## sample estimates:
## cor
## 0.6717034
##
## Pearson's product-moment correlation
##
## data: redwine$fixed.acidity and redwine$density
## t = 35.877, df = 1597, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.6399847 0.6943302
## sample estimates:
## cor
## 0.6680473
I wanted to take a few variables whose plots above look like had a relationship with quality, and add a smoothing line to the plots to highlight that relationship. I looked at alchohol (positive relationship with quality) and volatile.acidity (negative relationship with quality):
Also I wanted to see how the alcohol and volatile.acidity variables look on a boxplot. The quality ratings are in whole numbers so the range of quality ratings for each variable look similar. Volatile.acidity has a few more outliers.
Alchohol appears to have a positive correlation with quality, whereas volatile.acidity has a negative correlation (i.e., a higher volatile.acidity correlates with lower wine quality rating).
Not surprisingly, total.sulfur.dioxide and free.sulfur.dioxide were very closely correlated, at an R-coefficient of 0.668, as were fixed.acidity and citric.acid, at an R-coefficient of 0.671, but also fixed.acidity and density had a correlation factor of 0.668, which was not an obvious relationship.
Alcohol had the strongest relationship with quality, with a correlation value of 0.476, but the strongest relationship with the original variables is the 2 sulfur dioxide variables and the fixed.acidity and density variables mentioned above, each with a correlation of 0.668.
I put the alcohol variable into some buckets under a new variable alcohol_level, in order to group the observations into Low-Med-Hi alcohol content:
## Low Med Hi NA's
## 550 531 516 2
And I plotted the quality with alcohol and colored by alcohol “bucket”. You can see the color bands that show that higher alcohol content tends to be associated with higher quality:
Also I wanted to see how the buckets visually compared in a boxplot. You can really notice that the Hi alcohol_level bucket has higher observed quality ratings:
I wanted to model the relationship of alcohol_level (a categorical variable) with quality, so used the the lm() function. The ‘Hi’ alcohol_level has 0.84405 higher quality rating than the ‘Low’ alcohol_level, and about 0.502 higher quality rating (0.84405 -0.34231) than the ‘Med’ alcohol_level. However, only about 18.47% of the variance in quality can be explained by the model:
##
## Call:
## lm(formula = quality ~ alcohol_level, data = redwine)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.09496 -0.25091 -0.09496 0.40678 2.40678
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.25091 0.03102 169.249 < 2e-16 ***
## alcohol_levelMed 0.34231 0.04427 7.733 1.85e-14 ***
## alcohol_levelHi 0.84405 0.04459 18.928 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.7276 on 1594 degrees of freedom
## (2 observations deleted due to missingness)
## Multiple R-squared: 0.1847, Adjusted R-squared: 0.1837
## F-statistic: 180.6 on 2 and 1594 DF, p-value: < 2.2e-16
I also wanted to see how much well the model fitted values correlated with actual values for quality based on alcohol_level, so ran a correlation on fitted versus actual quality:
##
## Pearson's product-moment correlation
##
## data: quality and model.lm$fitted.values
## t = 19.012, df = 1595, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.3889661 0.4689813
## sample estimates:
## cor
## 0.4298172
And finally, to look at the correlation between alcohol_level and quality variables, I converted the alcohol_level ordinal variable to numeric, so that I could run a Spearman correlation test for it. Spearman’s coefficient rho is 0.447, showing that the two variables have a significant, positive correlation:
##
## Spearman's rank correlation rho
##
## data: redwine$alcohol_level.num and redwine$quality
## S = 375540000, p-value < 2.2e-16
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## 0.4467834
Alcohol was a strong indicator of quality, particularly when you put the observations into the separate groups. The ‘Hi’ alcohol content group shows a very different, stronger relationship with quality, with a Spearman’s coefficient rho of 0.447
Yes, fixed.acidity and density had a strong correlation (0.668), which I cannot explain with the data available. Perhaps denser wine means greater fruit particles that lends more acidity to the wine – that is definitely a question related to the chemistry of wine!
Yes, I modeled quality with alcohol_level, but was only able to explain about 18.47% of the variance in quality observations, so clearly alcohol_level alone is not all that factors into red wine quality. There are likely other items that were not measured in this red wine data set that may be able to explain quality ratings, such as grape type, brand or price.
Of all the original variables that could explain the quality ratings, alcohol definitely had the strongest correlation to quality, although it is only a moderately significant correlation of 0.476. The line through the middle shows the mean of the quality rating, and slanted line shows the regression line for alcohol level and quality, indicating the positive relationship: as alcohol content increases, the quality rating increases.
I wanted to show that how ‘Hi’ alcohol levels (determined by the upper tertile of alcohol content) tended to be associated with higher quality ratings, so I colored this plot based on alcohol level grouping/bucket. More ‘Hi’ alcohol level observations are related to higher quality ratings, as compared to ‘Med’ and ‘Low’ alcohol levels. The upper and lower bands represent 95% and 5% of observations, and the center solid line represents the median value of alcohol content at each quality rating.
You can really see with this boxplot that the ‘Hi’ alcohol_level observations have very different (higher) observed quality ratings than the ‘Med’ or ‘Low’ buckets. Higher alcohol content tends to have observations of higher quality ratings, as evidenced by the values in the boxes which represent the mean quality value, as well as by the Spearman’s rho coefficient of 0.447 calculated earlier in this report. For the mean calculations, I also excluded the outliers with quality rating values less than 4 or greater than 8, to get a clearer picture of the difference in the observations of quality with each of the three alcohol level groups:
The most frequent challenge in this project was determining the correct syntax for the various plotting methods selected. To address this, I often had to research via the forums, Google-search or the course lessons to find examples of how best to generate the visualizations I had in mind and work through coding errors.
Probably a bigger challenge was deciding what to do with the variables, including what to look at, which ones to compare, and how to expand on the comparisons and view the variables and their relationships in several different ways. I hope this will come more easily the more I am able to play around with ooking at datasets and their characteristics.
I really liked creating a new variable, using the examples from the course lessons and adapting for the red wine dataset. Differences in values of the explaining variable (alcohol level) really popped out when grouping into Hi-Med-Low. High alcohol levels did have a very different range and median for quality ratings when compared to medium and low alcohol levels, and the boxplot based on these groupings showed this well.
Since the red wine study on quality was only for Portuguese “Vinho Verde” wines, it would be interesting to have a wider sample of types of wine, and also to have the additional information mentioned in the text file as not being possible due to privacy issues, such as data about grape types, wine brand, wine selling price. Those variables could be better indicators of quality than the chemical components alone.