#Author: Fanshi Zhang

mydata <- read.table("./WineQT.csv",
                     header = TRUE,
                     sep = ",",
                     dec = ".")
head(mydata)
##   Id fixed.acidity volatile.acidity citric.acid residual.sugar chlorides
## 1  1           7.4             0.70        0.00            1.9     0.076
## 2  2           7.8             0.88        0.00            2.6     0.098
## 3  3           7.8             0.76        0.04            2.3     0.092
## 4  4          11.2             0.28        0.56            1.9     0.075
## 5  5           7.4             0.70        0.00            1.9     0.076
## 6  6           7.4             0.66        0.00            1.8     0.075
##   free.sulfur.dioxide total.sulfur.dioxide density   pH sulphates alcohol
## 1                  11                   34  0.9978 3.51      0.56     9.4
## 2                  25                   67  0.9968 3.20      0.68     9.8
## 3                  15                   54  0.9970 3.26      0.65     9.8
## 4                  17                   60  0.9980 3.16      0.58     9.8
## 5                  11                   34  0.9978 3.51      0.56     9.4
## 6                  13                   40  0.9978 3.51      0.56     9.4
##   quality
## 1       5
## 2       5
## 3       5
## 4       6
## 5       5
## 6       5

#Description

Source: https://www.kaggle.com/datasets/yasserh/wine-quality-dataset/data

ID: ID of wines

Fixed.acidity: fixed acidity of wines based on physicochemical tests

Citric.acid: citric acid of wines

Residual.sugar: residual sugar in g/L

Chlorides: cholorides in mg/L

Free.sulfur.dioxide: free sulfur dioxide in mg/L

Total.sulfur.dioxide: total sulfur dioxide in mg/L

Density: density in g/mL

pH: pH of wines

Sulphates: sulphates in mg/L

Alcohol: alcohol in V/V

Quality: quality of wine (score between 0 to 9)

library(psych)
describe(mydata[,-1])
##                      vars    n  mean    sd median trimmed   mad  min    max
## fixed.acidity           1 1143  8.31  1.75   7.90    8.13  1.33 4.60  15.90
## volatile.acidity        2 1143  0.53  0.18   0.52    0.52  0.18 0.12   1.58
## citric.acid             3 1143  0.27  0.20   0.25    0.26  0.25 0.00   1.00
## residual.sugar          4 1143  2.53  1.36   2.20    2.26  0.44 0.90  15.50
## chlorides               5 1143  0.09  0.05   0.08    0.08  0.01 0.01   0.61
## free.sulfur.dioxide     6 1143 15.62 10.25  13.00   14.34  8.90 1.00  68.00
## total.sulfur.dioxide    7 1143 45.91 32.78  37.00   41.20 26.69 6.00 289.00
## density                 8 1143  1.00  0.00   1.00    1.00  0.00 0.99   1.00
## pH                      9 1143  3.31  0.16   3.31    3.31  0.15 2.74   4.01
## sulphates              10 1143  0.66  0.17   0.62    0.64  0.12 0.33   2.00
## alcohol                11 1143 10.44  1.08  10.20   10.33  1.04 8.40  14.90
## quality                12 1143  5.66  0.81   6.00    5.60  1.48 3.00   8.00
##                       range skew kurtosis   se
## fixed.acidity         11.30 1.04     1.37 0.05
## volatile.acidity       1.46 0.68     1.36 0.01
## citric.acid            1.00 0.37    -0.72 0.01
## residual.sugar        14.60 4.35    27.50 0.04
## chlorides              0.60 6.01    46.78 0.00
## free.sulfur.dioxide   67.00 1.23     1.91 0.30
## total.sulfur.dioxide 283.00 1.66     5.06 0.97
## density                0.01 0.10     0.87 0.00
## pH                     1.27 0.22     0.91 0.00
## sulphates              1.67 2.49    11.93 0.01
## alcohol                6.50 0.86     0.21 0.03
## quality                5.00 0.29     0.30 0.02

#The average fixed acidity is 8.31, the median is 7.90 and the range is 11.30.

#Among the variables, the sd of total sulfur dioxide is the biggest.

mydata$qualityF <- factor(mydata$quality,
                          levels = c("1","2","3","4","5","6","7","8","9"),
                          labels = c("Faulty or unbalanced","borderline of faulty or unbalanced","Deadly dull","Average","Distinguished","Superior","A cut above superior","A humdinger","Truly exceptional"))
describeBy(mydata$alcohol,group = mydata$qualityF)
## 
##  Descriptive statistics by group 
## group: Faulty or unbalanced
## NULL
## ------------------------------------------------------------ 
## group: borderline of faulty or unbalanced
## NULL
## ------------------------------------------------------------ 
## group: Deadly dull
##    vars n mean   sd median trimmed  mad min  max range  skew kurtosis   se
## X1    1 6 9.69 0.89   9.82    9.69 0.89 8.4 10.9   2.5 -0.14    -1.59 0.36
## ------------------------------------------------------------ 
## group: Average
##    vars  n  mean   sd median trimmed  mad min  max range skew kurtosis   se
## X1    1 33 10.26 1.03     10   10.17 1.19   9 13.1   4.1 0.75    -0.27 0.18
## ------------------------------------------------------------ 
## group: Distinguished
##    vars   n mean   sd median trimmed  mad min  max range skew kurtosis   se
## X1    1 483  9.9 0.75    9.7    9.79 0.44 8.5 14.9   6.4 1.88     5.71 0.03
## ------------------------------------------------------------ 
## group: Superior
##    vars   n  mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 462 10.66 1.05   10.5   10.58 1.19 8.4  14   5.6 0.59    -0.07 0.05
## ------------------------------------------------------------ 
## group: A cut above superior
##    vars   n  mean   sd median trimmed  mad min max range skew kurtosis   se
## X1    1 143 11.48 0.98   11.6   11.48 1.04 9.2  14   4.8    0    -0.35 0.08
## ------------------------------------------------------------ 
## group: A humdinger
##    vars  n  mean   sd median trimmed  mad min max range  skew kurtosis  se
## X1    1 16 11.94 1.19  11.75   11.94 1.19 9.8  14   4.2 -0.14    -1.01 0.3
## ------------------------------------------------------------ 
## group: Truly exceptional
## NULL
hist(mydata$quality,
     main = "Distribution of Quality",
     xlab = "Quality",
     ylab = "Levels",
     breaks = seq(from = 1, to = 9, by = 1))

#The distribution of quality is right skewed.

library(car)
## 载入需要的程辑包:carData
## 
## 载入程辑包:'car'
## The following object is masked from 'package:psych':
## 
##     logit
scatterplot(x = mydata$volatile.acidity,
            y = mydata$quality,
            xlab = "Volatile Acidity",
            ylab = "Quality",
            smooth = F)

#The quality and the volatile acidity are negatively correlated. The higher the volatile acidity, the lower the quality.

library(ggplot2)
## 
## 载入程辑包:'ggplot2'
## The following objects are masked from 'package:psych':
## 
##     %+%, alpha
ggplot(mydata,aes(x = qualityF,y = pH)) + 
  geom_boxplot() +
  xlab("Quality") +
  theme_bw()

mydata2 <- mydata[,c(9,12,13)]

scatterplotMatrix(mydata2, smooth = F)

#The density and alcohol is negatively corelated. The alcohol and the quality is positively corelated.