White Wines Data Set
Text File
This project explores a data set of the chemical properties and quality rating for white wines of the Portuguese “Vinho Verde” variety. While I enjoy drinking white wines on occasion, I know basically nothing about the chemical properties they might exhibit, nor particularly anything about what makes one good or poor quality. I will be documenting the journey with brief insights into my thought process along the way.
First, I just want to get a sense of the overall structure and some general characteristics of the data set.
## 'data.frame': 4898 obs. of 13 variables:
## $ X : int 1 2 3 4 5 6 7 8 9 10 ...
## $ fixed.acidity : num 7 6.3 8.1 7.2 7.2 8.1 6.2 7 6.3 8.1 ...
## $ volatile.acidity : num 0.27 0.3 0.28 0.23 0.23 0.28 0.32 0.27 0.3 0.22 ...
## $ citric.acid : num 0.36 0.34 0.4 0.32 0.32 0.4 0.16 0.36 0.34 0.43 ...
## $ residual.sugar : num 20.7 1.6 6.9 8.5 8.5 6.9 7 20.7 1.6 1.5 ...
## $ chlorides : num 0.045 0.049 0.05 0.058 0.058 0.05 0.045 0.045 0.049 0.044 ...
## $ free.sulfur.dioxide : num 45 14 30 47 47 30 30 45 14 28 ...
## $ total.sulfur.dioxide: num 170 132 97 186 186 97 136 170 132 129 ...
## $ density : num 1.001 0.994 0.995 0.996 0.996 ...
## $ pH : num 3 3.3 3.26 3.19 3.19 3.26 3.18 3 3.3 3.22 ...
## $ sulphates : num 0.45 0.49 0.44 0.4 0.4 0.44 0.47 0.45 0.49 0.45 ...
## $ alcohol : num 8.8 9.5 10.1 9.9 9.9 10.1 9.6 8.8 9.5 11 ...
## $ quality : int 6 6 6 6 6 6 6 6 6 6 ...
The structure of the data set reveals the names and precision levels of the 11 chemical properties tested, all of which are ‘num’ data types. The quality is an ‘int’ data type. The X column is simply used for indexing, so I decided to drop it.
## [1] 4898 12
After dropping the X column, the dimensions are 4898 rows (one for each wine) and 12 columns - 11 for chemical properties and 1 for quality.
## [1] 937
DISCLAIMER: The data set was provided by Udacity as “clean,” but I happened to discover over 900 duplicate rows (about 19%). After asking a Udacity Mentor, I was advised to continue with the analysis without removing the duplicates.
## fixed.acidity volatile.acidity citric.acid residual.sugar
## Min. : 3.800 Min. :0.0800 Min. :0.0000 Min. : 0.600
## 1st Qu.: 6.300 1st Qu.:0.2100 1st Qu.:0.2700 1st Qu.: 1.700
## Median : 6.800 Median :0.2600 Median :0.3200 Median : 5.200
## Mean : 6.855 Mean :0.2782 Mean :0.3342 Mean : 6.391
## 3rd Qu.: 7.300 3rd Qu.:0.3200 3rd Qu.:0.3900 3rd Qu.: 9.900
## Max. :14.200 Max. :1.1000 Max. :1.6600 Max. :65.800
## chlorides free.sulfur.dioxide total.sulfur.dioxide density
## Min. :0.00900 Min. : 2.00 Min. : 9.0 Min. :0.9871
## 1st Qu.:0.03600 1st Qu.: 23.00 1st Qu.:108.0 1st Qu.:0.9917
## Median :0.04300 Median : 34.00 Median :134.0 Median :0.9937
## Mean :0.04577 Mean : 35.31 Mean :138.4 Mean :0.9940
## 3rd Qu.:0.05000 3rd Qu.: 46.00 3rd Qu.:167.0 3rd Qu.:0.9961
## Max. :0.34600 Max. :289.00 Max. :440.0 Max. :1.0390
## pH sulphates alcohol quality
## Min. :2.720 Min. :0.2200 Min. : 8.00 Min. :3.000
## 1st Qu.:3.090 1st Qu.:0.4100 1st Qu.: 9.50 1st Qu.:5.000
## Median :3.180 Median :0.4700 Median :10.40 Median :6.000
## Mean :3.188 Mean :0.4898 Mean :10.51 Mean :5.878
## 3rd Qu.:3.280 3rd Qu.:0.5500 3rd Qu.:11.40 3rd Qu.:6.000
## Max. :3.820 Max. :1.0800 Max. :14.20 Max. :9.000
I ran some summary statistics for each column.
Quality has a range from 3 to 9, so no perfect (10) or really terrible (0, 1, or 2) scores. Alcohol ranges from 8 to 14.2, which is what I’ve typically noticed in wines I’ve seen before. pH ranges from about 2.7 to 3.8, which means all the wines are quite acidic. I’m curious which properties contribute the most to the acidity measured by pH, fixed acidity perhaps? I also want to explore which properties influence the quality of a wine. I’ll hazard a guess that alcohol, residual sugar, and chlorides (salt) affect the quality rating the most since I associate those three variables with taste.
Next, I want to create some rough plots to better understand the distribution of each variable.
##
## 3 4 5 6 7 8 9
## 20 163 1457 2198 880 175 5
I decided to make a histogram and table for quality first. The distribution is fairly normal, with almost half of all wines rated as a 6. Only 5 elite wines received a 9, less than 1 in a 1,000.
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 8.00 9.50 10.40 10.51 11.40 14.20
I will next check out the variables I predicted will influence quality the most, starting with alcohol. Interestingly, the peak is at 9.5%, which is only 1.5% above the minimum of 8%, and then it gradually falls until the maximum of 14.2%.
I personally prefer wines with a lower alcohol percentage, so it will be interesting to see how alcohol percentage is related to quality through bivariate analysis.
The first histogram for residual sugar revealed a very skewed distribution with some extremely high outliers, so I transformed the data by log10. The transformation revealed a somewhat bimodal distribution with one peak slightly less than 2 and another slightly less than 10. I wonder what would cause the two peaks. It could have to do with some variable not included in the data set, such as the kind of grapes used or some aspect of the farming or production process.
##
## 0.6 0.7 0.8 0.9 0.95
## 2 7 25 39 4
## [1] 77
## [1] 1.6%
## [1] 65.8
The text file mentions that it’s rare wines with less than 1g/l of residual sugar, and wines with greater than 45g/l are considered “sweet.” There were some wines with less than 1g/l (rounded to 1.6% of the data set), and only one wine could be considered “sweet” with 65.8g/l.
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.00900 0.03600 0.04300 0.04577 0.05000 0.34600
Chlorides are simply the amount of salt in a wine. While the first histogram has a long tail, about 98% could be shown in a second histogram at less than 0.1. Furthermore, 75% has 0.05 or less and the mean was 0.043.
I just wanted to peek at the chlorides in wines rated 8 or 9, almost all of which were well less than 0.1.
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 2.720 3.090 3.180 3.188 3.280 3.820
Next, I’ll look at pH. The distribution looks almost perfectly normal. Remember, the lower the pH, the more acidic the wine is. The median and mean are almost identical, with an interquartile range from about 3 to 3.3.
Next is a histogram of fixed acidity, which has a fairly normal distribution peaking around 7, and just a few outliers over 10.
The histogram for volatile acidity peaks at about 0.3 and is skewed to the right a bit with outliers above 0.7.
The histograms for fixed and volatile acidity both have a similar shape to pH. It will be interesting to explore the relationship between them further in the bivariate section of the project.
##
## 0.45 0.46 0.47 0.48 0.49 0.5 0.51 0.52 0.53 0.54 0.55 0.56 0.57 0.58 0.59 0.6
## 46 51 38 39 215 35 25 23 16 19 11 22 13 21 6 6
## 0.61 0.62 0.63 0.64 0.65 0.66 0.67 0.68 0.69 0.7 0.71 0.72 0.73 0.74 0.78 0.79
## 9 14 4 6 8 7 7 7 5 3 9 5 5 41 2 2
## 0.8
## 2
For histograms of citric acid, narrowing the binwidth to 0.01 revealed deviant spikes from the relatively normal distribution. However, the spikes are not at 0.5 and 0.75 as they first appear, but actually at 0.49 and 0.74.
The first histogram for free sulfur dioxide had one outlier out around 300, so I set the limits to below 150 in the second plot, as well as narrow the binwidth. There’s still a bit of a tail past 75 or so but otherwise looks pretty normal.
Total sulfur dioxide also looked to be pretty normal other than a few outliers over about 250.
Sulphates unsurprisingly showed a similar trend in shape and distribution as the previous two related attributes of free and total sulfur dioxide, but without such a long tail.
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.9871 0.9917 0.9937 0.9940 0.9961 1.0390
Finally, density is the last variable for univariate analysis. The text file describes the density of wine being close to water depending on the percent of alcohol and sugar content. The box plot shows a vast majority of wines less dense than water (i.e. less than 1), with a few very special outliers over 1.01. I was curious to look at the attributes of those outliers in more detail.
Indeed, the three wines with the highest density are also the ones with the highest residual sugar (two are actually duplicates noted by the above disclaimer). I expect this relationship will become more clear with a full bivariate analysis.
The data set is comprised of 4,898 white wines, each measured for 11 chemical properties and assessed for quality on a scale from 0 to 10. I kept the data set intact except for dropping one column that was being used for indexing.
I’m primarily interested in the quality rating of wine and what attributes might influence it. I think alcohol, residual sugar, and chlorides (salt) might have the most influence on taste, and by extension the quality of a wine. Over half of the wines are rated 6 for quality, with no 1s, 2s, or 10s. 20 wines received a 3 and only 5 wines received a 9.
I’m also interested in what attributes affect a wine’s pH level. All of the wines are quite acidic with a median of 3.18 and the inner interquartile range falling between about 3 to 3.3. I suspect fixed acidity will influence pH the most, because the measurements are all over 3g/l, whereas measurements of volatile acidity are almost all under 1g/l.
Here are a few other interesting observations:
I’m already planning to create scatter plots for quality and pH, but I’ll start with a quick correlation matrix to see if there are any other relationships worth investigating.
Quality has a positive correlation of 0.44 with alcohol percentage, meaning the more alcohol in a wine the higher it tended to be rated for quality. The two other variables I thought would have strong relationships with quality were actually quite weak, residual sugar at -0.10 and chlorides at -0.21. It’s also interesting that they are both negative, so wines with less sugar and salt tend to have slightly higher quality. And while sugar doesn’t correlate much with quality, it does moderately correlate with alcohol percentage at -0.45, meaning the more residual sugar in a wine, the less alcohol percentage there tends to be.
As I suspected, pH has the strongest correlation with fixed acidity of -0.43, meaning the more fixed acidity present, the lower the pH and thus more acidic a wine is.
The strongest correlations of the entire data set are for density and residual sugar at 0.84, as well as density and alcohol percentage at -0.78. So residual sugar tends to make a wine more dense than water, while alcohol tends to make a wine less dense than water. It will be interesting to create a multivariate plot of those three variables later in the project.
I started with a scatter plot of quality and alcohol percentage, but there was a lot of overplotting, so I lowered the alpha value for some transparency, jittered the points, and added a trend line in the second plot. There’s a lot of 5 rated wines with less than 10% alcohol while wines rated 7 or above usually have more than 10% alcohol.
Maybe the trend will be clearer with a box plot.
So the median values for alcohol percentage clearly trend upward for quality ratings from 5 to 9. Curiously, quality ratings for 3 and 4 break that trend, but are still lower than 6 and above.
When plotting the relationship between quality ad chlorides, it was clear most wines were below 0.1g/l for chlorides, so I zoomed in for the second plot. Aiming for less than 0.05g/l seems like a best practice for winemakers. While the correlation was somewhat weak at -0.21, it’s still interesting to think about how such a small amount of salt, and even smaller variations in the amount of salt, can go a long way in changing the flavor of a wine.
Remembering the bimodal distribution I observed earlier in the log10 transformed histogram, I transformed the y-axis for residual sugar by log10 in the second plot. It looks like the wines with less than 5g/l trend slightly upward over quality, but wines with more than 5g/l trend slightly downward…seemingly canceling each other out and producing the quite insignificant correlation of -0.10.
I kept the same log10 scale for residual sugar on the y-axis but replaced quality with alcohol on the x-axis. While the trend line of -0.45 shows that wines with more alcohol percentage tend to have less sugar, there’s also a slight pattern of the amount of residual sugar converging towards a centralized area between 1 and 10g/l as alcohol percentage increases. especially above 12% alcohol.
The relationship between pH and fixed acidity at -0.43 is quite clear in this scatter plot, as well as the density of data points for the interquartile range of pH from 3 to 3.3.
The relationship between density and residual sugar was the strongest of the data set at 0.84. This plot clearly shows that the more sugar in a wine, the more dense it becomes. The second plot zooms in for a closer look at the main grouping.
The relationship between density and alcohol percentage was also quite strong at -0.78, so density tends to decrease as alcohol increases. The second plot zooms in for a closer look at the main grouping.
After creating a correlation matrix, I chose to make scatter plots of how quality, pH, and density interacted with various other variables.
Quality had the strongest relationship with alcohol percentage at 0.44, meaning the more alcohol in a wine, the higher it tended to be rated. I was a little surprised to discover the weak relationship between quality and chlorides at -0.21. However, the quite insignificant relationship between residual sugar and quality at -0.10 will perhaps be the biggest surprise for me of the project. Residual sugar did moderately correlate with alcohol at -0.45 though, so perhaps there will still be an interesting multivariate plot to look at between quality, alcohol, and residual sugar.
pH had the strongest correlation with fixed acidity at -0.43, so the more fixed acidity in a wine, the lower its pH (and thus more acidic). The scatter plot also clearly showed a high density of points between 3 and 3.3, roughly the interquartile range of pH.
Though I initially didn’t think density would be particularly interesting, it ended up be part of the two strongest correlations in the data set. First with residual sugar at 0.84, and second with alcohol percentage at -0.78. Because one correlation is positive and the other negative, it means residual sugar pushes the density higher while alcohol percentage brings density down. I think the relationship between these three variables will make for an interesting multivariate plot in the next section.
I plan to start creating multivariate plots simply by adding color to represent a third variable to some of the bivariate plots from the previous section, then see where it takes me.
I was most excited to plot the relationship between density, residual sugar, and alcohol since I assumed the strong correlations would make the trends easy to see. The darker the shade of purple, the more alcohol a wine has. It’s clear that wines with a high alcohol percentage and low residual sugar have the lowest density, while wines with lower alcohol percentage and higher residual sugar have the highest density. Since most wines have a density less than 1 (the density of water), it appears alcohol is generally winning the war against residual sugar.
Not quite sure of what it would look like, I simply took the bivariate plot of quality and residual sugar (with the y-axis transformed by log10) and added alcohol percentage with a diverging color scheme. Blue represents a lower alcohol percentage, red represents a higher alcohol percentage, and yellow is in the middle.
Clearly, the higher-rated wines tend to have a higher alcohol percentage. However, I think the most interesting pattern that emerged was the blue triangle of wines with low alcohol percentage at the top of the plot. It seems to indicates that a high-quality wine with a low alcohol percentage can be made, but it should have slightly less than 15 g/l of residual sugar…almost precisely. Deviate higher or lower than that, and the quality of the wine tends to suffer. I want to take a closer look.
Here I simply zoomed in on the y-axis of the previous plot and reduced the level of transparency. The converging triangle is more apparent, and it looks like 14g/l of residual sugar is the sweet spot for wines with low alcohol percentage Hmm, what about chlorides?
Following similar steps as residual sugar, I simply took the bivariate plot of quality and chlorides (zoomed in to less than 0.1g/l of chlorides) and again added alcohol percentage with a diverging color scheme.
Wow, I had to double-check my variables to make sure it was the correct plot. There’s that converging blue triangle again, indicating that a high-quality wine with a low alcohol percentage should have roughly what looks like 0.06g/l of chlorides. Deviate higher or lower than that, and the quality of the wine tends to suffer. Of course, I again want to take a closer look.
Again, I simply zoomed in on the y-axis of the previous plot and reduced the level of transparency. The converging triangle is more apparent, and it looks like around 0.0565g/l of chlorides is the sweet (or should I say salty?) spot for wines with low alcohol percentage.
I want to see how all these plots compare to each other side by side.
I decided to zoom into the residual sugar y-axis at less than 30, transform the chlorides y-axis to be log10 like residual sugar, and remove the legends. Otherwise, the plots are the same as before. The top two plots are residual sugar, the bottom two are chlorides, the left side is zoomed out, and the right side is zoomed in. I find the similar shapes of wines with low alcohol percentage (blue points) to be quite remarkable.
There are 18 wines with less than 9% alcohol and an 8 for quality, most of which have amounts of residual sugar and chlorides around those target amounts of 14g/l of residual sugar 0.0565g/l of chlorides. (Admittedly, most of these wines appear to be duplicates noted in the disclaimer.)
First, I looked at the relationship between density, residual sugar, and alcohol percentage. It was interesting to visualize how the density of a wine is strongly pushed up by residual sugar, but almost equally strongly pulled down by alcohol percentage.
While chlorides and especially residual sugar kind of let me down with their weak correlations with quality in the bivariate section, I still decided to plot them along with alcohol percentage to see what would happen…and I’m glad I did. Among wines with a low alcohol percentage, roughly less than 9%, there emerged a “sweet and salty” spot for the amounts of residual sugar at 14g/l and chlorides 0.565g/l respectively. Wines with a quality rating of 8 were tightly clustered around these target amounts, while lesser quality wines saw greater and greater variance above or below the target amounts.
I’ve selected three plots that I think convey the most important findings of my exploratory data analysis of white wines. Each plot is accompanied by a description and the reasons why I chose it.
The first final plot shows how a wine’s density is affected by residual sugar and alcohol percentage. It includes the two strongest bivariate correlations in the data set: density and residual sugar at 0.84 and density and alcohol at -0.78. As residual sugar increases, so does density, but alcohol brings the density of a wine down. A large majority of wines have a density less than water.
I chose this plot because I initially thought density would be one of the least interesting variables in the data set, but it ended up being surprisingly insightful. In particular, I think it’s interesting how residual sugar and alcohol push the density of a wine in opposite directions. This plot kind of encapsulates a mini-chemistry lesson.
The second final plot shows how wines with a higher alcohol percentage tend to receive higher quality ratings. Alcohol percentage was the most correlated variable with quality at 0.44. No wines received a quality rating of 0, 1, 2, or 10.
I chose this plot because it answers one of the first and most important questions I asked about the data set, which was what properties influence quality the most. I incorrectly assumed that residual sugar and chlorides would have a stronger relationship with a wine’s quality. I’m also a bit disappointed that higher-quality wines tend to have more alcohol percentage, because I personally don’t usually like wines with a lot of alcohol.
The third final plot shows the similar distributions of residual sugar and chlorides (salt) in wines with low alcohol percentage (the blue spots are all roughly less than 9%). The right side zooms in on each plot to show the convergence of values as quality increases - towards 14g/l of residual sugar and 0.0565g/l of chlorides for wines that received an 8 for quality.
I chose this plot because the results of the bivariate plots weren’t very impressive, so I was surprised to see how interesting they suddenly became simply by adding a third variable. Furthermore, I prefer to drink wines with a lower alcohol percentage and was glad to see that a few wines were able to break the general trend of higher alcohol percentage leading to higher ratings of quality.
The white wine data set contained 11 measurements of chemical properties and a quality rating for 4,898 white wines of the Portuguese “Vinho Verde” variety. I started my exploration by familiarizing myself with the overall structure of the data set and plotting each individual variable. Then I used a correlation matrix to guide bivariate and multivariate exploration, which was primarily focused around how quality, pH, and density were related to other variables.
I correctly predicted that fixed acidity had the strongest relationship with pH. I actually wasn’t very interested in density at the beginning of the project. However, after discovering that the strongest correlations in the data set were related to density, residual sugar, and alcohol percentage, I decided to investigate those relationships further with multivariate plots and ended up learning a bit of chemistry along the way.
I was initially a bit disappointed that the amount of chlorides (salt) or residual sugar in a wine had such weak relationships with a wine’s quality. However, after adding alcohol percentage as a third variable, a very interesting pattern emerged. While, generally speaking, wines with higher alcohol percentage tended to receive higher ratings for quality, there were a few wines with a very low alcohol percentage that scored an 8 for quality. Furthermore, most of these wines had about 14g/l of residual sugar and 0.0565g/l of chlorides, with divergence above or below generally resulting in a lower quality rating. If I were to go into the business of making wine, I would look to these unique wines for inspiration on how to make a high-quality wine with a low alcohol percentage.
I think there’s a lot of potential for future work on this data set. First, I"d like to remove the duplicates noted in the disclaimer and see how the results of the analysis change. I’d also like to analyze a subset of the data that includes only wines with less than 9 or 10 percent alcohol. Specifically, I’d like to further explore how deviating from 14g/l of residual sugar and 0.0565g/l of chlorides relates to the quality of a wine.