DATA EXPLORATION

##   ï..INDEX TARGET FixedAcidity VolatileAcidity CitricAcid ResidualSugar
## 1        1      3          3.2           1.160      -0.98          54.2
## 2        2      3          4.5           0.160      -0.81          26.1
## 3        4      5          7.1           2.640      -0.88          14.8
## 4        5      3          5.7           0.385       0.04          18.8
## 5        6      4          8.0           0.330      -1.26           9.4
## 6        7      0         11.3           0.320       0.59           2.2
##   Chlorides FreeSulfurDioxide TotalSulfurDioxide Density   pH Sulphates
## 1    -0.567                NA                268 0.99280 3.33     -0.59
## 2    -0.425                15               -327 1.02792 3.38      0.70
## 3     0.037               214                142 0.99518 3.12      0.48
## 4    -0.425                22                115 0.99640 2.24      1.83
## 5        NA              -167                108 0.99457 3.12      1.77
## 6     0.556               -37                 15 0.99940 3.20      1.29
##   Alcohol LabelAppeal AcidIndex STARS
## 1     9.9           0         8     2
## 2      NA          -1         7     3
## 3    22.0          -1         8     3
## 4     6.2          -1         6     1
## 5    13.7           0         9     2
## 6    15.4           0        11    NA

The data set consists of 12,795 records of different wines. The variables are mostly related to the chemical properties of the wines. The goal is to use the variables to create a model that predicts the number of cases of that wine that would be purchased by distribution companies.

The following is a list of variables that will be used to predict the number of cases:

The following are sumamry statistics for each of the variables described above:

##      TARGET       FixedAcidity     VolatileAcidity     CitricAcid     
##  Min.   :0.000   Min.   :-18.100   Min.   :-2.7900   Min.   :-3.2400  
##  1st Qu.:2.000   1st Qu.:  5.200   1st Qu.: 0.1300   1st Qu.: 0.0300  
##  Median :3.000   Median :  6.900   Median : 0.2800   Median : 0.3100  
##  Mean   :3.029   Mean   :  7.076   Mean   : 0.3241   Mean   : 0.3084  
##  3rd Qu.:4.000   3rd Qu.:  9.500   3rd Qu.: 0.6400   3rd Qu.: 0.5800  
##  Max.   :8.000   Max.   : 34.400   Max.   : 3.6800   Max.   : 3.8600  
##                                                                       
##  ResidualSugar        Chlorides       FreeSulfurDioxide TotalSulfurDioxide
##  Min.   :-127.800   Min.   :-1.1710   Min.   :-555.00   Min.   :-823.0    
##  1st Qu.:  -2.000   1st Qu.:-0.0310   1st Qu.:   0.00   1st Qu.:  27.0    
##  Median :   3.900   Median : 0.0460   Median :  30.00   Median : 123.0    
##  Mean   :   5.419   Mean   : 0.0548   Mean   :  30.85   Mean   : 120.7    
##  3rd Qu.:  15.900   3rd Qu.: 0.1530   3rd Qu.:  70.00   3rd Qu.: 208.0    
##  Max.   : 141.150   Max.   : 1.3510   Max.   : 623.00   Max.   :1057.0    
##  NA's   :616        NA's   :638       NA's   :647       NA's   :682       
##     Density             pH          Sulphates          Alcohol     
##  Min.   :0.8881   Min.   :0.480   Min.   :-3.1300   Min.   :-4.70  
##  1st Qu.:0.9877   1st Qu.:2.960   1st Qu.: 0.2800   1st Qu.: 9.00  
##  Median :0.9945   Median :3.200   Median : 0.5000   Median :10.40  
##  Mean   :0.9942   Mean   :3.208   Mean   : 0.5271   Mean   :10.49  
##  3rd Qu.:1.0005   3rd Qu.:3.470   3rd Qu.: 0.8600   3rd Qu.:12.40  
##  Max.   :1.0992   Max.   :6.130   Max.   : 4.2400   Max.   :26.50  
##                   NA's   :395     NA's   :1210      NA's   :653    
##   LabelAppeal          AcidIndex          STARS      
##  Min.   :-2.000000   Min.   : 4.000   Min.   :1.000  
##  1st Qu.:-1.000000   1st Qu.: 7.000   1st Qu.:1.000  
##  Median : 0.000000   Median : 8.000   Median :2.000  
##  Mean   :-0.009066   Mean   : 7.773   Mean   :2.042  
##  3rd Qu.: 1.000000   3rd Qu.: 8.000   3rd Qu.:3.000  
##  Max.   : 2.000000   Max.   :17.000   Max.   :4.000  
##                                       NA's   :3359

Correlation of Variables

The following are the correlation values between each of the variables. The closer the correlation is to 1 or -1, the more highly correlated the variables.

The target variable is positively correlated with LabelAppeal and STARS. The target variable is negavitely correlated with VolatileAcidity and Acid Index.

In terms of the independent variables, AcidIndex and FixedAcidity are positively correlated. This makes sense as they are both measuring acidity.

STARS and LabelAppeal are positively correlated. Perhaps the appearance affects the number of stars the wine is given. Or perhaps better wines tend to make more effort on their label.

Investigating the Relationship Between AcidIndex and FixedAcidity

Acid index is an integer value, which ranges between 4 and 17. Its mean is 7.8 and its median is 8. It is skewed to the right. Wines with acid indeces 5 and below as well as those with indeces 10 and above are outliers.

Fixed Acidity is a decimal value, which ranges between -18.1 and 34.4. By far most of the values lie between 5 and 10. The mean and median are both about 7, and the other values are equally distributed above and below. Fixed Acidities above 15 and below -1 are outliers.

The graph of acid index vs. fixed acidity displays a slight positive trend, but it is not as clear of a relationship as I was expecting.

Investigating the Relationship Between STARS and LabelAppeal

Wines are rated with stars ranging as integers between 1 and 4. Most wines are given ratings of 1 or 2. There are no outliers.

Label appeal is an integer rating between -2 and 2. The distribution looks normal and is centered at 0. There are no outliers.

There is a positive trend between label appeal and the number of stars.

Missing Values

There are missing values for Residual Sugar, Chlorides, FreeSulfurDioxide, TotalSulfurDioxide, pH, Sulphates, Alcohol and STARS.

The distributions for Residual Sugar, Chlorides, FreeSulfurDioxide, TotalSulfurDioxide, pH, Sulphates, Alcohol have most of the values clustered around the mean.

DATA PREPARATION

Because the variables with missing values have most of teh data near the mean, I will impute the mean for the missing values.

To address the correlation between the number of stars and label appeal, I will combine those variables by adding the values together.

Build Models

Creating a Test Set and Training Set

Backward Elimination - Poisson Regression Model 1

## 
## Call:
## glm(formula = TARGET ~ ., family = "poisson", data = train1)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.4955  -0.5200   0.2114   0.6480   2.4736  
## 
## Coefficients:
##                      Estimate Std. Error z value Pr(>|z|)    
## (Intercept)         2.132e+00  2.488e-01   8.571  < 2e-16 ***
## FixedAcidity       -1.955e-03  1.075e-03  -1.819 0.068853 .  
## VolatileAcidity    -5.262e-02  8.252e-03  -6.377 1.81e-10 ***
## CitricAcid          5.298e-03  7.614e-03   0.696 0.486522    
## ResidualSugar      -1.114e-04  1.999e-04  -0.557 0.577282    
## Chlorides          -6.821e-02  2.126e-02  -3.209 0.001333 ** 
## FreeSulfurDioxide   1.347e-04  4.498e-05   2.995 0.002741 ** 
## TotalSulfurDioxide  8.381e-05  2.931e-05   2.860 0.004240 ** 
## Density            -5.065e-01  2.441e-01  -2.075 0.037958 *  
## pH                 -3.471e-02  9.927e-03  -3.496 0.000472 ***
## Sulphates          -1.902e-02  7.318e-03  -2.599 0.009347 ** 
## Alcohol             4.842e-03  1.801e-03   2.688 0.007196 ** 
## AcidIndex          -1.167e-01  5.775e-03 -20.207  < 2e-16 ***
## stars_label         2.065e-01  4.761e-03  43.382  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 13784  on 7676  degrees of freedom
## Residual deviance: 11261  on 7663  degrees of freedom
## AIC: 30440
## 
## Number of Fisher Scoring iterations: 5

Residual Sugar has the (highest p value) lowest affect on the target and will be removed next.

## 
## Call:
## glm(formula = TARGET ~ FixedAcidity + VolatileAcidity + CitricAcid + 
##     Chlorides + FreeSulfurDioxide + TotalSulfurDioxide + Density + 
##     pH + Sulphates + Alcohol + AcidIndex + stars_label, family = "poisson", 
##     data = train1)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.4938  -0.5168   0.2118   0.6484   2.4718  
## 
## Coefficients:
##                      Estimate Std. Error z value Pr(>|z|)    
## (Intercept)         2.132e+00  2.488e-01   8.569  < 2e-16 ***
## FixedAcidity       -1.943e-03  1.074e-03  -1.808 0.070540 .  
## VolatileAcidity    -5.259e-02  8.252e-03  -6.373 1.85e-10 ***
## CitricAcid          5.304e-03  7.614e-03   0.697 0.486026    
## Chlorides          -6.818e-02  2.126e-02  -3.207 0.001340 ** 
## FreeSulfurDioxide   1.342e-04  4.497e-05   2.984 0.002849 ** 
## TotalSulfurDioxide  8.348e-05  2.930e-05   2.849 0.004384 ** 
## Density            -5.065e-01  2.441e-01  -2.075 0.037961 *  
## pH                 -3.478e-02  9.926e-03  -3.503 0.000459 ***
## Sulphates          -1.897e-02  7.317e-03  -2.592 0.009541 ** 
## Alcohol             4.852e-03  1.801e-03   2.694 0.007066 ** 
## AcidIndex          -1.167e-01  5.775e-03 -20.205  < 2e-16 ***
## stars_label         2.064e-01  4.758e-03  43.391  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 13784  on 7676  degrees of freedom
## Residual deviance: 11261  on 7664  degrees of freedom
## AIC: 30438
## 
## Number of Fisher Scoring iterations: 5

Citric Acid has the (highest p value) lowest affect on the target and will be removed next.

## 
## Call:
## glm(formula = TARGET ~ FixedAcidity + VolatileAcidity + Chlorides + 
##     FreeSulfurDioxide + TotalSulfurDioxide + Density + pH + Sulphates + 
##     Alcohol + AcidIndex + stars_label, family = "poisson", data = train1)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.4969  -0.5183   0.2126   0.6493   2.4732  
## 
## Coefficients:
##                      Estimate Std. Error z value Pr(>|z|)    
## (Intercept)         2.133e+00  2.488e-01   8.572  < 2e-16 ***
## FixedAcidity       -1.937e-03  1.074e-03  -1.803 0.071361 .  
## VolatileAcidity    -5.277e-02  8.248e-03  -6.397 1.58e-10 ***
## Chlorides          -6.827e-02  2.126e-02  -3.211 0.001321 ** 
## FreeSulfurDioxide   1.345e-04  4.497e-05   2.991 0.002784 ** 
## TotalSulfurDioxide  8.329e-05  2.930e-05   2.843 0.004469 ** 
## Density            -5.077e-01  2.441e-01  -2.080 0.037482 *  
## pH                 -3.473e-02  9.926e-03  -3.499 0.000467 ***
## Sulphates          -1.906e-02  7.316e-03  -2.605 0.009181 ** 
## Alcohol             4.886e-03  1.801e-03   2.714 0.006655 ** 
## AcidIndex          -1.165e-01  5.766e-03 -20.196  < 2e-16 ***
## stars_label         2.065e-01  4.758e-03  43.400  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 13784  on 7676  degrees of freedom
## Residual deviance: 11262  on 7665  degrees of freedom
## AIC: 30437
## 
## Number of Fisher Scoring iterations: 5

Fixed Acidity has the (highest p value) lowest affect on the target and will be removed next.

## 
## Call:
## glm(formula = TARGET ~ VolatileAcidity + Chlorides + FreeSulfurDioxide + 
##     TotalSulfurDioxide + Density + pH + Sulphates + Alcohol + 
##     AcidIndex + stars_label, family = "poisson", data = train1)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.4951  -0.5167   0.2090   0.6489   2.4738  
## 
## Coefficients:
##                      Estimate Std. Error z value Pr(>|z|)    
## (Intercept)         2.132e+00  2.488e-01   8.569  < 2e-16 ***
## VolatileAcidity    -5.286e-02  8.247e-03  -6.409 1.46e-10 ***
## Chlorides          -6.828e-02  2.126e-02  -3.212 0.001320 ** 
## FreeSulfurDioxide   1.328e-04  4.496e-05   2.954 0.003141 ** 
## TotalSulfurDioxide  8.349e-05  2.929e-05   2.851 0.004363 ** 
## Density            -5.091e-01  2.441e-01  -2.086 0.037011 *  
## pH                 -3.473e-02  9.925e-03  -3.499 0.000467 ***
## Sulphates          -1.913e-02  7.314e-03  -2.615 0.008929 ** 
## Alcohol             4.853e-03  1.801e-03   2.695 0.007034 ** 
## AcidIndex          -1.180e-01  5.706e-03 -20.673  < 2e-16 ***
## stars_label         2.065e-01  4.758e-03  43.411  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 13784  on 7676  degrees of freedom
## Residual deviance: 11265  on 7666  degrees of freedom
## AIC: 30438
## 
## Number of Fisher Scoring iterations: 5

The following variables have a positive effect on the number of cases chosen by distributors to buy: Free Sulfur Dioxide, Total Sulfur Dioxide, Alcohol and the Number of Stars and Label Appeal.

The following variables have a negative effect on the number of cases chosen by distributors to buy: Volatile Acidity, Chlorides, Density, pH, Sulphates, and Acid Index.

Prediction from Poisson Model 1

##     true
## pred   0   1   2   3   4   5   6   7   8
##    1  73  13  19  14   4   2   0   0   0
##    2 460  75 340 511 263  53   4   1   0
##    3 452   5  99 449 645 266  46   4   0
##    4  89   0   3  55 287 296  96   8   0
##    5  11   0   0   5  60 138  91  19   1
##    6   1   0   0   0  13  41  56  14   3
##    7   0   0   0   0   2   8  10   6   1
##    8   0   0   0   0   0   2   2   0   0
##    9   0   0   0   0   0   0   1   1   0
## [1] 0.2518562
## [1] 0.64068

Model 1 predicts the number of cases of wine bought correctly 25% of the time. Model 1 predicts the number of cases bought within 1, 64% of the time.

## [1] 1.63389

On average, the predicion for the number of cases of wine purchased, is off by 1.6 cases.

Model 2 - Negative Binomial Regression

To build a second model, I will start with only the variables that displayed correlation with the target variable.

Creating a Test Set and Training Set

## Warning in theta.ml(Y, mu, sum(w), w, limit = control$maxit, trace =
## control$trace > : iteration limit reached

## Warning in theta.ml(Y, mu, sum(w), w, limit = control$maxit, trace =
## control$trace > : iteration limit reached
## 
## Call:
## glm.nb(formula = TARGET ~ ., data = train2, init.theta = 142084.9598, 
##     link = log)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.2310  -0.2733   0.0630   0.3750   1.6680  
## 
## Coefficients:
##                  Estimate Std. Error z value Pr(>|z|)    
## (Intercept)      1.462269   0.285029   5.130 2.89e-07 ***
## VolatileAcidity -0.021241   0.009657  -2.199   0.0278 *  
## Chlorides       -0.031617   0.024176  -1.308   0.1910    
## Density         -0.276642   0.283245  -0.977   0.3287    
## Sulphates       -0.001884   0.008263  -0.228   0.8197    
## Alcohol          0.004990   0.002055   2.428   0.0152 *  
## AcidIndex       -0.046191   0.006990  -6.608 3.89e-11 ***
## STARS            0.186056   0.008850  21.024  < 2e-16 ***
## LabelAppeal      0.181743   0.009369  19.399  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for Negative Binomial(142085) family taken to be 1)
## 
##     Null deviance: 4181.3  on 4626  degrees of freedom
## Residual deviance: 2869.4  on 4618  degrees of freedom
##   (3050 observations deleted due to missingness)
## AIC: 16683
## 
## Number of Fisher Scoring iterations: 1
## 
## 
##               Theta:  142085 
##           Std. Err.:  281224 
## Warning while fitting theta: iteration limit reached 
## 
##  2 x log-likelihood:  -16663.29

Residual Sugar has the (highest p value) lowest affect on the target and will be removed next.

## Warning in theta.ml(Y, mu, sum(w), w, limit = control$maxit, trace =
## control$trace > : iteration limit reached

## Warning in theta.ml(Y, mu, sum(w), w, limit = control$maxit, trace =
## control$trace > : iteration limit reached
## 
## Call:
## glm.nb(formula = TARGET ~ VolatileAcidity + Chlorides + Density + 
##     Alcohol + AcidIndex + STARS + LabelAppeal, data = train2, 
##     init.theta = 141078.4712, link = log)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.2350  -0.2728   0.0609   0.3761   1.6704  
## 
## Coefficients:
##                  Estimate Std. Error z value Pr(>|z|)    
## (Intercept)      1.433137   0.271656   5.276 1.32e-07 ***
## VolatileAcidity -0.022060   0.009228  -2.391   0.0168 *  
## Chlorides       -0.026236   0.023094  -1.136   0.2559    
## Density         -0.240579   0.269214  -0.894   0.3715    
## Alcohol          0.004442   0.001951   2.276   0.0228 *  
## AcidIndex       -0.046743   0.006612  -7.070 1.55e-12 ***
## STARS            0.186738   0.008448  22.104  < 2e-16 ***
## LabelAppeal      0.182654   0.008962  20.381  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for Negative Binomial(141078.5) family taken to be 1)
## 
##     Null deviance: 4627.9  on 5083  degrees of freedom
## Residual deviance: 3170.0  on 5076  degrees of freedom
##   (2593 observations deleted due to missingness)
## AIC: 18327
## 
## Number of Fisher Scoring iterations: 1
## 
## 
##               Theta:  141078 
##           Std. Err.:  265634 
## Warning while fitting theta: iteration limit reached 
## 
##  2 x log-likelihood:  -18308.96

Density has the (highest p value) lowest affect on the target and will be removed next.

## Warning in theta.ml(Y, mu, sum(w), w, limit = control$maxit, trace =
## control$trace > : iteration limit reached

## Warning in theta.ml(Y, mu, sum(w), w, limit = control$maxit, trace =
## control$trace > : iteration limit reached
## 
## Call:
## glm.nb(formula = TARGET ~ VolatileAcidity + Chlorides + Alcohol + 
##     AcidIndex + STARS + LabelAppeal, data = train2, init.theta = 140924.3924, 
##     link = log)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.2362  -0.2718   0.0626   0.3728   1.6512  
## 
## Coefficients:
##                  Estimate Std. Error z value Pr(>|z|)    
## (Intercept)      1.196218   0.059345  20.157  < 2e-16 ***
## VolatileAcidity -0.022253   0.009227  -2.412   0.0159 *  
## Chlorides       -0.026948   0.023080  -1.168   0.2430    
## Alcohol          0.004452   0.001952   2.281   0.0225 *  
## AcidIndex       -0.047051   0.006603  -7.126 1.03e-12 ***
## STARS            0.186809   0.008449  22.111  < 2e-16 ***
## LabelAppeal      0.182649   0.008962  20.381  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for Negative Binomial(140924.4) family taken to be 1)
## 
##     Null deviance: 4627.9  on 5083  degrees of freedom
## Residual deviance: 3170.8  on 5077  degrees of freedom
##   (2593 observations deleted due to missingness)
## AIC: 18326
## 
## Number of Fisher Scoring iterations: 1
## 
## 
##               Theta:  140924 
##           Std. Err.:  265211 
## Warning while fitting theta: iteration limit reached 
## 
##  2 x log-likelihood:  -18309.76

Fixed Acidity has the (highest p value) lowest affect on the target and will be removed next.

## Warning in theta.ml(Y, mu, sum(w), w, limit = control$maxit, trace =
## control$trace > : iteration limit reached

## Warning in theta.ml(Y, mu, sum(w), w, limit = control$maxit, trace =
## control$trace > : iteration limit reached
## 
## Call:
## glm.nb(formula = TARGET ~ VolatileAcidity + Alcohol + AcidIndex + 
##     STARS + LabelAppeal, data = train2, init.theta = 141292.9793, 
##     link = log)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.2300  -0.2743   0.0622   0.3770   1.6575  
## 
## Coefficients:
##                  Estimate Std. Error z value Pr(>|z|)    
## (Intercept)      1.209627   0.057641  20.985  < 2e-16 ***
## VolatileAcidity -0.021717   0.009016  -2.409   0.0160 *  
## Alcohol          0.004747   0.001896   2.504   0.0123 *  
## AcidIndex       -0.049207   0.006434  -7.649 2.03e-14 ***
## STARS            0.185965   0.008229  22.599  < 2e-16 ***
## LabelAppeal      0.183262   0.008705  21.052  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for Negative Binomial(141293) family taken to be 1)
## 
##     Null deviance: 4890.7  on 5376  degrees of freedom
## Residual deviance: 3339.1  on 5371  degrees of freedom
##   (2300 observations deleted due to missingness)
## AIC: 19371
## 
## Number of Fisher Scoring iterations: 1
## 
## 
##               Theta:  141293 
##           Std. Err.:  258602 
## Warning while fitting theta: iteration limit reached 
## 
##  2 x log-likelihood:  -19357.13

The following variables have a positive impact on the number of cases of wine bought: alcohol, stars and label appeal. The following variables have a negative impact on the number of cases of wine bought: volatile acidity and acid index.

Prediction from Negative Binomial Regression- Model 2

Any values that are not predicted by the model, will be imputed to be the mean of the number of cases bought.

##     true
## pred   0   1   2   3   4   5   6   7   8
##    2  79  36 155 138  25   3   0   0   0
##    3 965  57 296 728 587 196  39   7   1
##    4  40   0  10 162 528 318  66   3   0
##    5   2   0   0   6 115 204 105  12   0
##    6   0   0   0   0  16  74  74  19   0
##    7   0   0   0   0   3   9  18   8   4
##    8   0   0   0   0   0   2   4   4   0
## [1] 0.3315748
## [1] 0.6981243

This model correctly predicts the number of cases purchased 33% of the time. This model predicts the number of cases bought within 1, 70% of the time.

## [1] 1.639561

On average, the predicion for the number of cases of wine purchased, is off by 1.6 cases.

Multiple Linear Regression Model - Model 3

I will build model 3 using the same data set as model 2, but I will combine the variables stars and label appeal by adding them.

Creating a Test Set and Training Set

Backward Elimination

## 
## Call:
## lm(formula = train3$TARGET ~ ., data = train3)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.1608 -0.5005  0.1272  0.7363  3.2490 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      4.330532   0.629733   6.877 6.94e-12 ***
## VolatileAcidity -0.078479   0.021471  -3.655  0.00026 ***
## Chlorides       -0.123343   0.053625  -2.300  0.02149 *  
## Density         -1.063305   0.627156  -1.695  0.09006 .  
## Sulphates       -0.008018   0.018404  -0.436  0.66309    
## Alcohol          0.021061   0.004527   4.652 3.38e-06 ***
## AcidIndex       -0.162061   0.014715 -11.014  < 2e-16 ***
## stars_label      0.699526   0.011887  58.849  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.153 on 4619 degrees of freedom
##   (3050 observations deleted due to missingness)
## Multiple R-squared:  0.4454, Adjusted R-squared:  0.4446 
## F-statistic: 529.9 on 7 and 4619 DF,  p-value: < 2.2e-16

Sulphates has the (highest p value) lowest affect on the target and will be removed next.

## 
## Call:
## lm(formula = train3$TARGET ~ VolatileAcidity + Chlorides + Density + 
##     Alcohol + AcidIndex + stars_label, data = train3)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.1702 -0.5062  0.1326  0.7393  3.2499 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      4.179161   0.599403   6.972 3.52e-12 ***
## VolatileAcidity -0.078858   0.020484  -3.850  0.00012 ***
## Chlorides       -0.106191   0.051127  -2.077  0.03785 *  
## Density         -0.892421   0.595302  -1.499  0.13391    
## Alcohol          0.019009   0.004296   4.425 9.85e-06 ***
## AcidIndex       -0.162992   0.013868 -11.753  < 2e-16 ***
## stars_label      0.701503   0.011325  61.944  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.154 on 5077 degrees of freedom
##   (2593 observations deleted due to missingness)
## Multiple R-squared:  0.4476, Adjusted R-squared:  0.4469 
## F-statistic: 685.5 on 6 and 5077 DF,  p-value: < 2.2e-16

Density has the (highest p value) lowest affect on the target and will be removed next.

## 
## Call:
## lm(formula = train3$TARGET ~ VolatileAcidity + Chlorides + Alcohol + 
##     AcidIndex + stars_label, data = train3)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.1731 -0.5065  0.1316  0.7405  3.2131 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      3.299114   0.121084  27.246  < 2e-16 ***
## VolatileAcidity -0.079687   0.020479  -3.891 0.000101 ***
## Chlorides       -0.108478   0.051110  -2.122 0.033850 *  
## Alcohol          0.019039   0.004296   4.432 9.55e-06 ***
## AcidIndex       -0.163947   0.013856 -11.833  < 2e-16 ***
## stars_label      0.701657   0.011326  61.952  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.154 on 5078 degrees of freedom
##   (2593 observations deleted due to missingness)
## Multiple R-squared:  0.4473, Adjusted R-squared:  0.4468 
## F-statistic:   822 on 5 and 5078 DF,  p-value: < 2.2e-16

45% of the variation in the number of cases purchased is accounted for by this linear regression model.

The residuals at the lower end are not nearly normal. However much of the residuals do follow a normal distribution.

Prediction from Model 3

If the model does not predict a value for the TARGET, I will impute the mean value for the target. The root mean square error from model 3 is

## 6484 2715   39 7621 3372 2717 
##    2    6    3    3    4    1
## [1] 1.64218

On average, the predicion for the number of cases of wine purchased, is off by 1.6 cases.

##     true
## pred   0   1   2   3   4   5   6   7   8
##    1  16   4  18   8   2   0   0   0   0
##    2  75  33 164 165  30   7   0   0   0
##    3 943  56 265 647 507 185  46   8   2
##    4  49   0  14 206 569 312  60   3   0
##    5   3   0   0   8 152 242 122  14   0
##    6   0   0   0   0  14  58  75  23   3
##    7   0   0   0   0   0   2   3   5   0
## [1] 0.3333333
## [1] 0.7006643

The model predicts the correct number of cases bought 33% of the time. The model is correct within 1 case 70% of the time.

Model 4 - Poisson Model

This model will be built by creating variables that describe the presence or absence of a missing value. I will create new columns that contain a 0 if the value is present and 1 if the value is missing. I will do this for the following variables: residual sugar, chlorides, free sulfur dioxide, total sulfur dioxide, pH, sulphates, alcohol and stars. The missing values will be imputed with the mean. I will then create a correlation plot to see if the absence of values is correlated to the target.

The target is negatively correlated with the absence of stars. This makes sense because if a wine is not rated, people will be less likely to purchase it and distributors will be less likely to buy a case. I will remove the other variables that contain values for missing values and leave in the values for wines that are missing stars for buiding a model.

Creating a Test Set and Training Set

Backward Elimination - Poisson Regression Model 4

## 
## Call:
## glm(formula = TARGET ~ ., family = "poisson", data = train4)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.1391  -0.6692   0.0020   0.4487   3.7892  
## 
## Coefficients:
##                      Estimate Std. Error z value Pr(>|z|)    
## (Intercept)         1.722e+00  2.490e-01   6.917 4.63e-12 ***
## FixedAcidity       -9.664e-04  1.078e-03  -0.897 0.369777    
## VolatileAcidity    -3.152e-02  8.311e-03  -3.793 0.000149 ***
## CitricAcid         -3.928e-03  7.600e-03  -0.517 0.605293    
## ResidualSugar       4.351e-05  2.008e-04   0.217 0.828425    
## Chlorides          -4.077e-02  2.123e-02  -1.920 0.054824 .  
## FreeSulfurDioxide   9.111e-05  4.482e-05   2.033 0.042051 *  
## TotalSulfurDioxide  5.387e-05  2.942e-05   1.831 0.067083 .  
## Density            -2.428e-01  2.437e-01  -0.996 0.319178    
## pH                 -1.878e-02  9.917e-03  -1.893 0.058299 .  
## Sulphates          -1.275e-02  7.369e-03  -1.730 0.083612 .  
## Alcohol             3.473e-03  1.799e-03   1.930 0.053560 .  
## LabelAppeal         1.562e-01  7.961e-03  19.628  < 2e-16 ***
## AcidIndex          -7.417e-02  5.930e-03 -12.507  < 2e-16 ***
## STARS               1.891e-01  7.934e-03  23.836  < 2e-16 ***
## missing_stars      -1.058e+00  2.205e-02 -47.973  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 13784.4  on 7676  degrees of freedom
## Residual deviance:  8277.5  on 7661  degrees of freedom
## AIC: 27460
## 
## Number of Fisher Scoring iterations: 6

Residual Sugar has the (highest p value) lowest affect on the target and will be removed next.

## 
## Call:
## glm(formula = TARGET ~ FixedAcidity + VolatileAcidity + CitricAcid + 
##     Chlorides + FreeSulfurDioxide + TotalSulfurDioxide + Density + 
##     pH + Sulphates + Alcohol + LabelAppeal + AcidIndex + STARS + 
##     missing_stars, family = "poisson", data = train4)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.1394  -0.6686   0.0022   0.4469   3.7893  
## 
## Coefficients:
##                      Estimate Std. Error z value Pr(>|z|)    
## (Intercept)         1.722e+00  2.490e-01   6.918 4.59e-12 ***
## FixedAcidity       -9.704e-04  1.077e-03  -0.901 0.367710    
## VolatileAcidity    -3.153e-02  8.311e-03  -3.793 0.000149 ***
## CitricAcid         -3.933e-03  7.600e-03  -0.517 0.604846    
## Chlorides          -4.078e-02  2.123e-02  -1.921 0.054780 .  
## FreeSulfurDioxide   9.134e-05  4.480e-05   2.039 0.041487 *  
## TotalSulfurDioxide  5.399e-05  2.941e-05   1.836 0.066410 .  
## Density            -2.430e-01  2.437e-01  -0.997 0.318734    
## pH                 -1.874e-02  9.915e-03  -1.890 0.058733 .  
## Sulphates          -1.277e-02  7.369e-03  -1.733 0.083121 .  
## Alcohol             3.471e-03  1.799e-03   1.929 0.053748 .  
## LabelAppeal         1.563e-01  7.960e-03  19.633  < 2e-16 ***
## AcidIndex          -7.417e-02  5.930e-03 -12.507  < 2e-16 ***
## STARS               1.892e-01  7.930e-03  23.855  < 2e-16 ***
## missing_stars      -1.058e+00  2.205e-02 -47.974  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 13784.4  on 7676  degrees of freedom
## Residual deviance:  8277.6  on 7662  degrees of freedom
## AIC: 27459
## 
## Number of Fisher Scoring iterations: 6

Citric Acid has the (highest p value) lowest affect on the target and will be removed next.

## 
## Call:
## glm(formula = TARGET ~ FixedAcidity + VolatileAcidity + Chlorides + 
##     FreeSulfurDioxide + TotalSulfurDioxide + Density + pH + Sulphates + 
##     Alcohol + LabelAppeal + AcidIndex + STARS + missing_stars, 
##     family = "poisson", data = train4)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.1373  -0.6702   0.0011   0.4481   3.7901  
## 
## Coefficients:
##                      Estimate Std. Error z value Pr(>|z|)    
## (Intercept)         1.722e+00  2.490e-01   6.916 4.64e-12 ***
## FixedAcidity       -9.752e-04  1.077e-03  -0.905 0.365315    
## VolatileAcidity    -3.139e-02  8.306e-03  -3.779 0.000158 ***
## Chlorides          -4.070e-02  2.123e-02  -1.917 0.055222 .  
## FreeSulfurDioxide   9.115e-05  4.480e-05   2.034 0.041910 *  
## TotalSulfurDioxide  5.427e-05  2.941e-05   1.845 0.065004 .  
## Density            -2.423e-01  2.437e-01  -0.994 0.320126    
## pH                 -1.877e-02  9.915e-03  -1.893 0.058301 .  
## Sulphates          -1.270e-02  7.367e-03  -1.724 0.084673 .  
## Alcohol             3.446e-03  1.799e-03   1.916 0.055357 .  
## LabelAppeal         1.562e-01  7.959e-03  19.627  < 2e-16 ***
## AcidIndex          -7.435e-02  5.920e-03 -12.559  < 2e-16 ***
## STARS               1.892e-01  7.930e-03  23.858  < 2e-16 ***
## missing_stars      -1.058e+00  2.205e-02 -47.972  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 13784.4  on 7676  degrees of freedom
## Residual deviance:  8277.9  on 7663  degrees of freedom
## AIC: 27457
## 
## Number of Fisher Scoring iterations: 6

Fixed Acidity has the (highest p value) lowest affect on the target and will be removed next.

## 
## Call:
## glm(formula = TARGET ~ VolatileAcidity + Chlorides + FreeSulfurDioxide + 
##     TotalSulfurDioxide + Density + pH + Sulphates + Alcohol + 
##     LabelAppeal + AcidIndex + STARS + missing_stars, family = "poisson", 
##     data = train4)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.1535  -0.6664   0.0024   0.4492   3.8044  
## 
## Coefficients:
##                      Estimate Std. Error z value Pr(>|z|)    
## (Intercept)         1.723e+00  2.490e-01   6.919 4.56e-12 ***
## VolatileAcidity    -3.143e-02  8.306e-03  -3.784 0.000154 ***
## Chlorides          -4.078e-02  2.123e-02  -1.921 0.054756 .  
## FreeSulfurDioxide   9.022e-05  4.479e-05   2.014 0.043967 *  
## TotalSulfurDioxide  5.438e-05  2.941e-05   1.849 0.064441 .  
## Density            -2.439e-01  2.437e-01  -1.001 0.316976    
## pH                 -1.877e-02  9.914e-03  -1.893 0.058304 .  
## Sulphates          -1.276e-02  7.366e-03  -1.733 0.083122 .  
## Alcohol             3.431e-03  1.799e-03   1.908 0.056449 .  
## LabelAppeal         1.563e-01  7.958e-03  19.646  < 2e-16 ***
## AcidIndex          -7.507e-02  5.866e-03 -12.798  < 2e-16 ***
## STARS               1.891e-01  7.929e-03  23.847  < 2e-16 ***
## missing_stars      -1.058e+00  2.205e-02 -47.988  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 13784.4  on 7676  degrees of freedom
## Residual deviance:  8278.7  on 7664  degrees of freedom
## AIC: 27456
## 
## Number of Fisher Scoring iterations: 6

Density has the (highest p value) lowest affect on the target and will be removed next.

## 
## Call:
## glm(formula = TARGET ~ VolatileAcidity + Chlorides + FreeSulfurDioxide + 
##     TotalSulfurDioxide + pH + Sulphates + Alcohol + LabelAppeal + 
##     AcidIndex + STARS + missing_stars, family = "poisson", data = train4)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.1543  -0.6667   0.0025   0.4512   3.7975  
## 
## Coefficients:
##                      Estimate Std. Error z value Pr(>|z|)    
## (Intercept)         1.482e+00  6.340e-02  23.371  < 2e-16 ***
## VolatileAcidity    -3.157e-02  8.306e-03  -3.801 0.000144 ***
## Chlorides          -4.148e-02  2.122e-02  -1.955 0.050609 .  
## FreeSulfurDioxide   8.978e-05  4.478e-05   2.005 0.044986 *  
## TotalSulfurDioxide  5.403e-05  2.940e-05   1.837 0.066139 .  
## pH                 -1.879e-02  9.915e-03  -1.895 0.058060 .  
## Sulphates          -1.278e-02  7.365e-03  -1.736 0.082603 .  
## Alcohol             3.456e-03  1.799e-03   1.921 0.054705 .  
## LabelAppeal         1.563e-01  7.957e-03  19.642  < 2e-16 ***
## AcidIndex          -7.528e-02  5.862e-03 -12.842  < 2e-16 ***
## STARS               1.891e-01  7.929e-03  23.853  < 2e-16 ***
## missing_stars      -1.058e+00  2.204e-02 -48.008  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 13784.4  on 7676  degrees of freedom
## Residual deviance:  8279.7  on 7665  degrees of freedom
## AIC: 27455
## 
## Number of Fisher Scoring iterations: 6

Sulphates has the (highest p value) lowest affect on the target and will be removed next.

## 
## Call:
## glm(formula = TARGET ~ VolatileAcidity + Chlorides + FreeSulfurDioxide + 
##     TotalSulfurDioxide + pH + Alcohol + LabelAppeal + AcidIndex + 
##     STARS + missing_stars, family = "poisson", data = train4)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.1811  -0.6700   0.0018   0.4474   3.8272  
## 
## Coefficients:
##                      Estimate Std. Error z value Pr(>|z|)    
## (Intercept)         1.477e+00  6.335e-02  23.319  < 2e-16 ***
## VolatileAcidity    -3.186e-02  8.305e-03  -3.837 0.000125 ***
## Chlorides          -4.112e-02  2.122e-02  -1.938 0.052631 .  
## FreeSulfurDioxide   8.791e-05  4.477e-05   1.964 0.049569 *  
## TotalSulfurDioxide  5.400e-05  2.941e-05   1.836 0.066316 .  
## pH                 -1.902e-02  9.914e-03  -1.918 0.055095 .  
## Alcohol             3.440e-03  1.799e-03   1.913 0.055812 .  
## LabelAppeal         1.562e-01  7.957e-03  19.630  < 2e-16 ***
## AcidIndex          -7.544e-02  5.861e-03 -12.873  < 2e-16 ***
## STARS               1.892e-01  7.929e-03  23.867  < 2e-16 ***
## missing_stars      -1.059e+00  2.204e-02 -48.035  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 13784.4  on 7676  degrees of freedom
## Residual deviance:  8282.7  on 7666  degrees of freedom
## AIC: 27456
## 
## Number of Fisher Scoring iterations: 6

Total Sulfur Dioxide has the (highest p value) lowest affect on the target and will be removed next.

## 
## Call:
## glm(formula = TARGET ~ VolatileAcidity + Chlorides + FreeSulfurDioxide + 
##     pH + Alcohol + LabelAppeal + AcidIndex + STARS + missing_stars, 
##     family = "poisson", data = train4)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.1785  -0.6722  -0.0011   0.4472   3.8299  
## 
## Coefficients:
##                     Estimate Std. Error z value Pr(>|z|)    
## (Intercept)        1.487e+00  6.312e-02  23.552  < 2e-16 ***
## VolatileAcidity   -3.190e-02  8.303e-03  -3.842 0.000122 ***
## Chlorides         -4.102e-02  2.121e-02  -1.934 0.053111 .  
## FreeSulfurDioxide  8.907e-05  4.477e-05   1.990 0.046645 *  
## pH                -1.916e-02  9.912e-03  -1.933 0.053194 .  
## Alcohol            3.365e-03  1.799e-03   1.871 0.061349 .  
## LabelAppeal        1.563e-01  7.955e-03  19.648  < 2e-16 ***
## AcidIndex         -7.571e-02  5.857e-03 -12.927  < 2e-16 ***
## STARS              1.896e-01  7.927e-03  23.917  < 2e-16 ***
## missing_stars     -1.059e+00  2.204e-02 -48.067  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 13784.4  on 7676  degrees of freedom
## Residual deviance:  8286.1  on 7667  degrees of freedom
## AIC: 27457
## 
## Number of Fisher Scoring iterations: 6

Alcohol has the (highest p value) lowest affect on the target and will be removed next.

## 
## Call:
## glm(formula = TARGET ~ VolatileAcidity + Chlorides + FreeSulfurDioxide + 
##     pH + LabelAppeal + AcidIndex + STARS + missing_stars, family = "poisson", 
##     data = train4)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.1753  -0.6722   0.0007   0.4507   3.8426  
## 
## Coefficients:
##                     Estimate Std. Error z value Pr(>|z|)    
## (Intercept)        1.525e+00  5.967e-02  25.557  < 2e-16 ***
## VolatileAcidity   -3.189e-02  8.306e-03  -3.840 0.000123 ***
## Chlorides         -4.183e-02  2.120e-02  -1.973 0.048514 *  
## FreeSulfurDioxide  8.710e-05  4.476e-05   1.946 0.051665 .  
## pH                -1.948e-02  9.909e-03  -1.966 0.049324 *  
## LabelAppeal        1.561e-01  7.955e-03  19.621  < 2e-16 ***
## AcidIndex         -7.621e-02  5.853e-03 -13.020  < 2e-16 ***
## STARS              1.905e-01  7.911e-03  24.086  < 2e-16 ***
## missing_stars     -1.060e+00  2.204e-02 -48.089  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 13784.4  on 7676  degrees of freedom
## Residual deviance:  8289.6  on 7668  degrees of freedom
## AIC: 27459
## 
## Number of Fisher Scoring iterations: 6

Free Sulfur Dioxide has the (highest p value) lowest affect on the target and will be removed next.

## 
## Call:
## glm(formula = TARGET ~ VolatileAcidity + Chlorides + pH + LabelAppeal + 
##     AcidIndex + STARS + missing_stars, family = "poisson", data = train4)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.1632  -0.6708  -0.0025   0.4525   3.8421  
## 
## Coefficients:
##                  Estimate Std. Error z value Pr(>|z|)    
## (Intercept)      1.530056   0.059614  25.666  < 2e-16 ***
## VolatileAcidity -0.031872   0.008307  -3.837 0.000125 ***
## Chlorides       -0.041949   0.021201  -1.979 0.047861 *  
## pH              -0.019619   0.009907  -1.980 0.047666 *  
## LabelAppeal      0.156317   0.007955  19.650  < 2e-16 ***
## AcidIndex       -0.076398   0.005852 -13.054  < 2e-16 ***
## STARS            0.190514   0.007910  24.086  < 2e-16 ***
## missing_stars   -1.060360   0.022036 -48.119  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for poisson family taken to be 1)
## 
##     Null deviance: 13784.4  on 7676  degrees of freedom
## Residual deviance:  8293.3  on 7669  degrees of freedom
## AIC: 27460
## 
## Number of Fisher Scoring iterations: 6

The following variables have a positive correlation with the number of cases of wine bought: label appeal and stars The following variables have a negative correlation with the number of cases of wine bought: volatile acidity, chlorides, pH, acid index and missing stars.

Prediction from Model 4

If the model does not predict a value for the TARGET, I will impute the mean value for the target. The root mean square error from model 4 is

## [1] 1.350322

On average, the predicion for the number of cases of wine purchased, is off by 1.4 cases.

##     true
## pred   0   1   2   3   4   5   6   7   8
##    1 748  48 143 178  97  31   6   2   0
##    2 140  34 115 116  37  15  10   2   0
##    3 158  11 191 548 453 129  12   1   0
##    4  39   0  12 177 519 310  67   3   0
##    5   1   0   0  15 140 227 106  13   0
##    6   0   0   0   0  24  76  83  22   1
##    7   0   0   0   0   4  17  19   8   4
##    8   0   0   0   0   0   1   3   2   0
## [1] 0.3024619
## [1] 0.7989449

The model predicts the correct number of cases bought 30% of the time. The model is correct within 1 case 80% of the time.

Model 5 - Negative Binomial Regression

To build this, I will start with only the variables that displayed correlation with the target variable. I will combine the variables stars and label appeal by adding them.

Creating a Test Set and Training Set

## 
## Call:
## glm.nb(formula = TARGET ~ ., data = train5, init.theta = 39437.65619, 
##     link = log)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.2193  -0.6451  -0.0023   0.4445   3.7743  
## 
## Coefficients:
##                  Estimate Std. Error z value Pr(>|z|)    
## (Intercept)      1.704928   0.246029   6.930 4.21e-12 ***
## VolatileAcidity -0.031795   0.008305  -3.828 0.000129 ***
## Chlorides       -0.041636   0.021224  -1.962 0.049792 *  
## Density         -0.238409   0.243647  -0.979 0.327825    
## Sulphates       -0.012789   0.007364  -1.737 0.082452 .  
## Alcohol          0.003550   0.001795   1.977 0.047992 *  
## AcidIndex       -0.075925   0.005834 -13.015  < 2e-16 ***
## missing_stars   -1.058453   0.022020 -48.068  < 2e-16 ***
## stars_label      0.173049   0.004603  37.595  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for Negative Binomial(39437.66) family taken to be 1)
## 
##     Null deviance: 13783.7  on 7676  degrees of freedom
## Residual deviance:  8296.1  on 7668  degrees of freedom
## AIC: 27468
## 
## Number of Fisher Scoring iterations: 1
## 
## 
##               Theta:  39438 
##           Std. Err.:  42547 
## Warning while fitting theta: iteration limit reached 
## 
##  2 x log-likelihood:  -27447.6

Density has the (highest p value) lowest affect on the target and will be removed next.

## 
## Call:
## glm.nb(formula = TARGET ~ VolatileAcidity + Chlorides + Sulphates + 
##     Alcohol + AcidIndex + missing_stars + stars_label, data = train5, 
##     init.theta = 39428.24142, link = log)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.2203  -0.6427  -0.0018   0.4468   3.7675  
## 
## Coefficients:
##                  Estimate Std. Error z value Pr(>|z|)    
## (Intercept)      1.469307   0.050568  29.056  < 2e-16 ***
## VolatileAcidity -0.031932   0.008305  -3.845 0.000121 ***
## Chlorides       -0.042307   0.021213  -1.994 0.046111 *  
## Sulphates       -0.012810   0.007363  -1.740 0.081910 .  
## Alcohol          0.003577   0.001795   1.992 0.046324 *  
## AcidIndex       -0.076130   0.005830 -13.059  < 2e-16 ***
## missing_stars   -1.058763   0.022017 -48.088  < 2e-16 ***
## stars_label      0.173056   0.004603  37.598  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for Negative Binomial(39428.24) family taken to be 1)
## 
##     Null deviance: 13784  on 7676  degrees of freedom
## Residual deviance:  8297  on 7669  degrees of freedom
## AIC: 27467
## 
## Number of Fisher Scoring iterations: 1
## 
## 
##               Theta:  39428 
##           Std. Err.:  42533 
## Warning while fitting theta: iteration limit reached 
## 
##  2 x log-likelihood:  -27448.56

Sulphates has the (highest p value) lowest affect on the target and will be removed next.

## 
## Call:
## glm.nb(formula = TARGET ~ VolatileAcidity + Chlorides + Alcohol + 
##     AcidIndex + missing_stars + stars_label, data = train5, init.theta = 39388.61022, 
##     link = log)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.2487  -0.6424   0.0002   0.4465   3.7971  
## 
## Coefficients:
##                  Estimate Std. Error z value Pr(>|z|)    
## (Intercept)      1.464132   0.050482  29.003  < 2e-16 ***
## VolatileAcidity -0.032226   0.008304  -3.881 0.000104 ***
## Chlorides       -0.041927   0.021209  -1.977 0.048056 *  
## Alcohol          0.003567   0.001795   1.987 0.046961 *  
## AcidIndex       -0.076288   0.005829 -13.089  < 2e-16 ***
## missing_stars   -1.059256   0.022015 -48.114  < 2e-16 ***
## stars_label      0.173052   0.004603  37.595  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for Negative Binomial(39388.61) family taken to be 1)
## 
##     Null deviance: 13783.7  on 7676  degrees of freedom
## Residual deviance:  8300.1  on 7670  degrees of freedom
## AIC: 27468
## 
## Number of Fisher Scoring iterations: 1
## 
## 
##               Theta:  39389 
##           Std. Err.:  42466 
## Warning while fitting theta: iteration limit reached 
## 
##  2 x log-likelihood:  -27451.59

The variables that have a positive influence on the number of cases bought are: alcohol and stars_label The variables that have a negative infuence on the number of cases bought are: volatile acidity, chlorides, acid index, and not being rated in stars.

Prediction from Model 5

If the model does not predict a value for the TARGET, I will impute the mean value for the target. The root mean square error from model 5 is

## [1] 1.342922

On average, the predicion for the number of cases of wine purchased, is off by 1.3 cases.

##     true
## pred   0   1   2   3   4   5   6   7   8
##    1 738  48 143 176  93  31   5   2   0
##    2 148  36 119 120  38  15  11   2   0
##    3 156   9 188 555 439 122  11   1   0
##    4  41   0  11 172 555 322  64   3   0
##    5   3   0   0  11 128 223 108   9   0
##    6   0   0   0   0  18  78  86  26   1
##    7   0   0   0   0   3  13  17   8   4
##    8   0   0   0   0   0   2   4   2   0
## [1] 0.3114498
## [1] 0.804025

The model predicts the correct number of cases bought 31% of the time. The model is correct within 1 case, 80% of the time.

Model 6 - Muliple Linear Regression

I will use the same data frame as in model 5 to build model 6.

Backward Elimination

## 
## Call:
## lm(formula = train5$TARGET ~ ., data = train5)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.9726 -0.7865  0.0448  0.8308  6.0221 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      4.675404   0.566745   8.250  < 2e-16 ***
## VolatileAcidity -0.098863   0.019091  -5.179 2.29e-07 ***
## Chlorides       -0.135324   0.048867  -2.769 0.005632 ** 
## Density         -0.828552   0.562244  -1.474 0.140617    
## Sulphates       -0.036272   0.016864  -2.151 0.031521 *  
## Alcohol          0.013696   0.004141   3.307 0.000947 ***
## AcidIndex       -0.198034   0.011801 -16.781  < 2e-16 ***
## missing_stars   -2.286967   0.034978 -65.382  < 2e-16 ***
## stars_label      0.604700   0.011468  52.727  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.326 on 7668 degrees of freedom
## Multiple R-squared:  0.5293, Adjusted R-squared:  0.5288 
## F-statistic:  1078 on 8 and 7668 DF,  p-value: < 2.2e-16

Density has the (highest p value) lowest affect on the target and will be removed next.

## 
## Call:
## lm(formula = train5$TARGET ~ VolatileAcidity + Chlorides + Sulphates + 
##     Alcohol + AcidIndex + missing_stars + stars_label, data = train5)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.9746 -0.7887  0.0412  0.8323  6.0083 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)      3.854853   0.105631  36.493  < 2e-16 ***
## VolatileAcidity -0.099311   0.019090  -5.202 2.02e-07 ***
## Chlorides       -0.137389   0.048850  -2.812 0.004929 ** 
## Sulphates       -0.036372   0.016865  -2.157 0.031067 *  
## Alcohol          0.013801   0.004141   3.333 0.000864 ***
## AcidIndex       -0.198554   0.011797 -16.831  < 2e-16 ***
## missing_stars   -2.287974   0.034974 -65.419  < 2e-16 ***
## stars_label      0.604727   0.011469  52.726  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.326 on 7669 degrees of freedom
## Multiple R-squared:  0.5292, Adjusted R-squared:  0.5288 
## F-statistic:  1231 on 7 and 7669 DF,  p-value: < 2.2e-16

The variables that have a positive effect on the number of cases of wine bought are alcohol and stars_label.
The variables that have a negative effect on the number of cases of wine bought are volatile acidity, chlorides, sulphates, acid index and not having been given stars.

53% of the variation in the number of cases purchased is accounted for by this linear regression model.

The residuals almost follow a normal distribution.

Prediction from Model 6

If the model does not predict a value for the TARGET, I will impute the mean value for the target. The root mean square error from model 6 is

## [1] 1.338404

On average, the predicion for the number of cases of wine purchased, is off by 1.3 cases.

##     true
## pred   0   1   2   3   4   5   6   7   8
##    0 119  30  37  12   1   0   0   0   0
##    1 443  18 101 144  47   9   0   0   0
##    2 310  35 144 140  79  37  13   3   0
##    3 157  10 163 493 357  74   8   1   0
##    4  54   0  16 237 650 378  69   4   0
##    5   3   0   0   8 130 266 156  25   1
##    6   0   0   0   0  10  42  59  19   4
##    7   0   0   0   0   0   0   1   1   0
## [1] 0.3419304
## [1] 0.7780383

The model predicts the correct number of cases bought 34% of the time. The model is correct within 1 case 78% of the time.

SELECTING A MODEL

I will choose to use the negative binomial model 5 to make predictions. Model 5 gave the lowest root mean square error, of 1.3.

The variables that have a positive influence on the number of cases bought are: alcohol and stars_label The variables that have a negative infuence on the number of cases bought are: volatile acidity, chlorides, acid index, and not being rated in stars.

I don’t have an intuitive understanding about the alcohol content and likelihood of a distributor choosing to purchase cases of wine. It makes sense that wines with higher ratings and those packaged nicer would be purchased more frequenly by consumers. This would encourage distributors to choose those wines for purchase.

Volatile acidity determines the likelihood that the wine will spoil. It therefore makes sense that distributors would buy more cases with lower values of volatile acidity. Consumers are more likely to be wine that is rated. Therefore distributors are less likely to purchase cases of wine if the wines have not been rated.

I don’t have intuition about the connection between chlorides and acid index with the likelihood of distributors choosing to purchase cases.

The predictions for the evaluation set are below:

##    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15 
##    3    4    3    2    3    6    3    3    3    3    3    3    3    3    3 
##   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30 
##    2    2    3    4    6    2    3    3    2    4    5    3    7    5    3 
##   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45 
##    4    3    4    3    3    4    4    4    3    3    3    3    3    3    3 
##   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60 
##    4    3    5    4    3    4    3    4    3    2    2    5    4    3    3 
##   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75 
##    5    3    3    3    3    2    4    4    3    4    5    4    3    5    2 
##   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90 
##    3    3    4    5    4    2    3    2    2    3    3    3    5    4    5 
##   91   92   93   94   95   96   97   98   99  100  101  102  103  104  105 
##    3    3    3    3    3    3    4    3    3    3    3    3    4    4    3 
##  106  107  108  109  110  111  112  113  114  115  116  117  118  119  120 
##    3    3    6    6    3    3    3    4    3    3    5    4    4    2    3 
##  121  122  123  124  125  126  127  128  129  130  131  132  133  134  135 
##    3    3    3    5    3    3    2    3    4    3    3    4    3    3    3 
##  136  137  138  139  140  141  142  143  144  145  146  147  148  149  150 
##    4    3    3    6    3    3    4    3    3    3    2    4    4    6    3 
##  151  152  153  154  155  156  157  158  159  160  161  162  163  164  165 
##    3    3    3    3    3    3    3    6    3    3    3    4    3    4    4 
##  166  167  168  169  170  171  172  173  174  175  176  177  178  179  180 
##    3    4    6    3    3    6    5    3    3    3    6    4    3    3    4 
##  181  182  183  184  185  186  187  188  189  190  191  192  193  194  195 
##    3    3    3    3    4    3    3    4    3    3    5    4    4    3    5 
##  196  197  198  199  200  201  202  203  204  205  206  207  208  209  210 
##    3    3    3    2    4    3    5    3    4    2    6    3    3    3    3 
##  211  212  213  214  215  216  217  218  219  220  221  222  223  224  225 
##    3    2    5    3    5    3    3    4    3    3    3    2    5    3    4 
##  226  227  228  229  230  231  232  233  234  235  236  237  238  239  240 
##    4    3    3    3    3    4    3    3    3    3    2    2    3    4    4 
##  241  242  243  244  245  246  247  248  249  250  251  252  253  254  255 
##    3    3    3    3    3    3    2    3    3    3    3    3    3    3    4 
##  256  257  258  259  260  261  262  263  264  265  266  267  268  269  270 
##    6    3    5    3    3    4    6    5    3    4    3    4    4    3    4 
##  271  272  273  274  275  276  277  278  279  280  281  282  283  284  285 
##    3    3    3    4    3    4    3    3    3    3    3    3    3    3    4 
##  286  287  288  289  290  291  292  293  294  295  296  297  298  299  300 
##    4    5    6    3    3    3    3    3    3    3    3    3    3    3    3 
##  301  302  303  304  305  306  307  308  309  310  311  312  313  314  315 
##    4    3    2    4    5    4    3    3    3    3    3    4    3    4    2 
##  316  317  318  319  320  321  322  323  324  325  326  327  328  329  330 
##    3    3    3    3    3    4    2    4    4    3    3    7    6    3    3 
##  331  332  333  334  335  336  337  338  339  340  341  342  343  344  345 
##    6    4    4    3    6    4    4    3    3    3    4    6    4    4    4 
##  346  347  348  349  350  351  352  353  354  355  356  357  358  359  360 
##    5    4    4    4    4    3    3    3    3    4    4    4    4    3    3 
##  361  362  363  364  365  366  367  368  369  370  371  372  373  374  375 
##    6    3    5    2    3    3    3    3    4    3    4    3    3    3    3 
##  376  377  378  379  380  381  382  383  384  385  386  387  388  389  390 
##    4    2    6    4    5    3    4    4    3    3    3    5    4    2    3 
##  391  392  393  394  395  396  397  398  399  400  401  402  403  404  405 
##    3    2    3    2    4    3    4    6    3    4    3    3    2    4    3 
##  406  407  408  409  410  411  412  413  414  415  416  417  418  419  420 
##    4    5    3    4    4    3    3    3    4    5    4    7    3    4    3 
##  421  422  423  424  425  426  427  428  429  430  431  432  433  434  435 
##    3    3    2    3    3    3    4    3    5    4    5    5    3    3    2 
##  436  437  438  439  440  441  442  443  444  445  446  447  448  449  450 
##    3    3    5    3    3    3    5    3    2    5    3    3    3    3    5 
##  451  452  453  454  455  456  457  458  459  460  461  462  463  464  465 
##    2    4    3    3    3    4    3    3    5    3    5    4    3    3    3 
##  466  467  468  469  470  471  472  473  474  475  476  477  478  479  480 
##    3    2    4    3    4    3    2    3    3    3    3    4    4    4    3 
##  481  482  483  484  485  486  487  488  489  490  491  492  493  494  495 
##    2    3    5    3    2    3    3    3    3    3    3    3    5    3    3 
##  496  497  498  499  500  501  502  503  504  505  506  507  508  509  510 
##    4    3    3    4    4    3    3    3    4    4    3    3    3    5    4 
##  511  512  513  514  515  516  517  518  519  520  521  522  523  524  525 
##    4    3    3    5    6    3    4    4    4    3    3    3    3    3    5 
##  526  527  528  529  530  531  532  533  534  535  536  537  538  539  540 
##    4    3    5    3    3    5    4    3    3    5    3    4    5    3    3 
##  541  542  543  544  545  546  547  548  549  550  551  552  553  554  555 
##    4    3    3    3    4    3    5    3    3    3    3    5    3    4    2 
##  556  557  558  559  560  561  562  563  564  565  566  567  568  569  570 
##    3    3    3    4    3    4    3    3    2    3    3    3    3    3    5 
##  571  572  573  574  575  576  577  578  579  580  581  582  583  584  585 
##    4    3    4    3    7    6    3    3    5    4    2    3    4    3    5 
##  586  587  588  589  590  591  592  593  594  595  596  597  598  599  600 
##    4    3    6    4    3    5    3    5    3    3    3    4    3    3    3 
##  601  602  603  604  605  606  607  608  609  610  611  612  613  614  615 
##    3    3    3    2    2    2    3    4    3    5    3    4    3    3    3 
##  616  617  618  619  620  621  622  623  624  625  626  627  628  629  630 
##    5    6    6    3    3    3    3    5    5    3    4    4    4    3    3 
##  631  632  633  634  635  636  637  638  639  640  641  642  643  644  645 
##    2    5    3    3    3    4    4    7    3    3    3    4    3    3    3 
##  646  647  648  649  650  651  652  653  654  655  656  657  658  659  660 
##    3    6    6    3    6    5    5    4    4    3    5    3    3    3    3 
##  661  662  663  664  665  666  667  668  669  670  671  672  673  674  675 
##    4    3    4    5    4    3    3    4    4    3    3    3    3    3    3 
##  676  677  678  679  680  681  682  683  684  685  686  687  688  689  690 
##    5    4    3    3    3    3    3    4    5    5    4    3    4    5    4 
##  691  692  693  694  695  696  697  698  699  700  701  702  703  704  705 
##    4    5    2    3    3    3    5    3    3    3    3    3    2    4    3 
##  706  707  708  709  710  711  712  713  714  715  716  717  718  719  720 
##    3    3    3    3    4    3    4    3    3    4    5    3    4    4    3 
##  721  722  723  724  725  726  727  728  729  730  731  732  733  734  735 
##    3    3    3    3    3    3    3    3    3    6    3    3    4    3    4 
##  736  737  738  739  740  741  742  743  744  745  746  747  748  749  750 
##    3    4    3    4    3    3    4    4    3    3    3    3    3    3    3 
##  751  752  753  754  755  756  757  758  759  760  761  762  763  764  765 
##    4    3    3    4    3    3    4    3    3    3    3    3    3    2    4 
##  766  767  768  769  770  771  772  773  774  775  776  777  778  779  780 
##    5    3    4    3    3    4    5    5    3    4    5    3    3    2    3 
##  781  782  783  784  785  786  787  788  789  790  791  792  793  794  795 
##    6    6    3    4    5    3    3    4    3    3    4    4    5    3    4 
##  796  797  798  799  800  801  802  803  804  805  806  807  808  809  810 
##    5    3    5    3    3    4    5    3    3    3    5    3    3    3    4 
##  811  812  813  814  815  816  817  818  819  820  821  822  823  824  825 
##    3    3    4    3    3    6    3    3    3    5    3    3    4    3    6 
##  826  827  828  829  830  831  832  833  834  835  836  837  838  839  840 
##    4    3    3    4    3    5    6    3    6    3    4    3    3    2    4 
##  841  842  843  844  845  846  847  848  849  850  851  852  853  854  855 
##    6    5    3    3    2    5    4    3    3    2    4    3    3    4    3 
##  856  857  858  859  860  861  862  863  864  865  866  867  868  869  870 
##    2    2    4    3    3    3    5    2    3    5    4    3    3    3    2 
##  871  872  873  874  875  876  877  878  879  880  881  882  883  884  885 
##    3    3    3    3    4    3    3    2    3    3    4    4    5    3    3 
##  886  887  888  889  890  891  892  893  894  895  896  897  898  899  900 
##    3    3    3    4    4    2    4    3    4    3    3    3    2    3    3 
##  901  902  903  904  905  906  907  908  909  910  911  912  913  914  915 
##    2    2    4    3    6    2    3    5    5    3    3    4    2    4    3 
##  916  917  918  919  920  921  922  923  924  925  926  927  928  929  930 
##    4    7    3    3    6    3    3    5    2    3    3    4    4    3    3 
##  931  932  933  934  935  936  937  938  939  940  941  942  943  944  945 
##    4    2    3    3    2    3    3    3    4    3    3    4    2    3    2 
##  946  947  948  949  950  951  952  953  954  955  956  957  958  959  960 
##    6    3    6    3    6    3    4    4    3    3    2    3    4    3    3 
##  961  962  963  964  965  966  967  968  969  970  971  972  973  974  975 
##    7    4    3    3    3    4    3    3    4    3    5    3    4    4    6 
##  976  977  978  979  980  981  982  983  984  985  986  987  988  989  990 
##    3    3    4    3    4    4    5    3    4    3    4    3    4    3    3 
##  991  992  993  994  995  996  997  998  999 1000 1001 1002 1003 1004 1005 
##    5    3    3    3    3    4    3    5    3    4    3    3    4    6    4 
## 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 
##    3    4    5    3    2    3    5    3    3    4    3    4    5    2    4 
## 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 
##    4    3    3    3    2    4    3    3    4    3    6    3    3    3    4 
## 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 
##    4    4    3    3    3    3    3    4    4    4    3    5    3    3    3 
## 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 
##    2    3    3    3    5    4    2    3    3    3    3    3    5    2    3 
## 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 
##    3    3    3    3    3    3    3    4    5    3    2    5    4    3    4 
## 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 
##    2    4    3    6    4    3    3    3    3    3    3    4    3    4    2 
## 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 
##    3    3    4    3    3    3    4    3    4    3    5    4    4    3    5 
## 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 
##    4    3    3    5    3    3    4    5    3    4    4    3    2    3    4 
## 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 
##    4    3    3    3    3    3    6    3    3    3    3    4    3    2    3 
## 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 
##    4    6    3    3    3    4    3    7    4    6    4    2    4    3    4 
## 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 
##    4    3    5    4    4    5    3    5    3    3    4    3    6    5    4 
## 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 
##    5    3    3    4    3    2    2    6    4    5    3    5    3    3    2 
## 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 
##    7    3    4    3    3    3    4    3    4    3    2    4    7    3    3 
## 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 
##    5    3    3    4    3    4    4    3    6    4    2    4    3    3    4 
## 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 
##    3    3    3    3    2    3    3    3    3    3    3    6    3    5    3 
## 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 
##    3    3    4    3    4    3    3    3    2    4    6    3    5    3    3 
## 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 
##    5    3    3    3    2    4    3    3    4    3    5    5    3    3    5 
## 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 
##    4    3    3    4    3    3    3    4    3    4    3    3    3    3    3 
## 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 
##    4    3    3    4    3    3    3    3    2    3    3    3    3    3    3 
## 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 
##    3    3    3    4    3    4    4    3    3    3    5    2    5    4    4 
## 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 
##    3    4    3    3    4    5    3    5    4    4    3    3    3    3    4 
## 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 
##    3    3    5    2    5    3    3    5    3    3    4    5    4    4    7 
## 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 
##    3    4    3    4    3    3    4    4    4    4    4    4    4    6    3 
## 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 
##    3    3    5    4    3    3    3    5    4    3    4    3    5    6    5 
## 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 
##    3    3    3    3    6    4    3    3    3    3    3    5    3    3    3 
## 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 
##    2    3    6    2    5    3    2    5    3    5    3    2    3    5    4 
## 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 
##    3    3    3    3    3    4    3    8    4    4    3    3    3    4    3 
## 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 
##    3    3    3    4    3    3    4    3    3    4    4    3    3    3    3 
## 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 
##    3    5    4    3    3    2    3    3    5    4    5    4    3    5    5 
## 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 
##    4    4    4    4    3    2    5    3    2    3    4    4    3    3    3 
## 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 
##    3    3    3    3    3    5    3    3    3    3    4    3    5    3    2 
## 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 
##    3    3    4    3    4    3    4    3    3    3    4    2    3    2    3 
## 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 
##    4    3    3    3    3    3    3    3    3    3    3    5    4    5    3 
## 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 
##    3    4    3    2    3    3    2    3    2    3    4    3    3    6    5 
## 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 
##    4    3    6    4    3    3    3    3    5    4    3    3    4    3    4 
## 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 
##    3    3    5    3    3    6    3    4    3    3    4    3    3    4    3 
## 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 
##    4    4    3    3    7    4    4    3    2    4    4    3    3    5    3 
## 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 
##    3    4    2    2    4    3    3    6    2    5    3    3    3    6    3 
## 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 
##    3    3    3    3    3    4    3    3    2    3    5    3    3    4    4 
## 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 
##    3    2    4    3    3    4    3    3    3    3    3    4    4    5    3 
## 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 
##    4    3    3    4    4    3    3    5    3    3    3    3    4    2    3 
## 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 
##    3    3    3    5    2    3    3    4    5    3    5    3    3    2    5 
## 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 
##    5    3    3    3    4    3    4    4    3    3    3    3    6    4    5 
## 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 
##    4    3    6    5    4    3    3    4    5    3    2    2    2    4    2 
## 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 
##    4    4    3    3    2    3    4    7    3    3    3    6    3    4    3 
## 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 
##    3    6    3    3    4    4    2    3    3    3    4    5    3    3    3 
## 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 
##    4    4    3    3    6    6    3    3    4    4    3    3    3    3    5 
## 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 
##    5    5    5    4    3    3    4    3    6    3    3    3    3    3    4 
## 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 
##    4    4    6    8    4    3    3    3    4    3    4    3    3    3    3 
## 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 
##    5    4    3    4    3    4    2    5    3    2    3    3    3    4    6 
## 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 
##    4    3    3    2    3    3    4    3    4    3    5    2    4    3    3 
## 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 
##    5    5    2    3    4    3    3    5    4    4    3    3    2    3    3 
## 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 
##    5    4    3    3    4    4    2    3    3    2    3    3    4    3    4 
## 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 
##    5    4    4    3    6    6    6    4    3    6    3    3    2    3    3 
## 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 
##    3    4    4    3    4    5    3    4    5    4    3    3    3    3    4 
## 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 
##    6    5    3    3    3    3    3    3    4    3    3    3    5    3    3 
## 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 
##    2    5    3    3    4    2    3    3    5    4    4    3    3    5    5 
## 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 
##    3    3    3    4    3    6    6    4    2    4    3    2    4    2    3 
## 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 
##    4    4    3    3    4    3    3    2    3    3    3    2    3    5    6 
## 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 
##    3    3    3    3    3    3    3    4    4    3    3    3    3    4    3 
## 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 
##    3    3    3    3    3    4    4    3    3    3    3    5    4    3    3 
## 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 
##    3    3    3    5    3    4    5    6    3    4    2    3    7    4    3 
## 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 
##    3    3    6    4    3    3    3    3    4    3    4    3    5    3    3 
## 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 
##    3    4    2    4    3    4    3    5    3    4    3    3    3    3    3 
## 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 
##    3    4    4    3    5    5    4    4    3    3    3    6    3    7    6 
## 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 
##    3    3    3    6    4    4    5    7    5    4    3    3    5    3    4 
## 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 
##    6    3    5    7    4    4    3    3    4    3    4    3    4    4    2 
## 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 
##    3    3    3    3    4    3    3    3    3    3    3    4    3    3    4 
## 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 
##    3    3    5    3    6    3    2    4    4    3    3    3    3    4    4 
## 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 
##    5    2    3    3    4    3    3    4    3    4    3    3    3    3    4 
## 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 
##    2    5    3    4    4    5    3    3    3    4    6    4    4    3    3 
## 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 
##    4    4    5    3    3    3    4    5    3    2    3    4    3    3    4 
## 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 
##    5    3    4    6    3    2    3    3    3    3    5    4    3    3    3 
## 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 
##    3    4    3    6    4    3    3    3    2    3    2    3    3    2    3 
## 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 
##    3    4    3    3    8    2    3    3    3    5    5    5    3    3    2 
## 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 
##    3    3    3    5    3    2    2    2    3    3    3    3    3    6    3 
## 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 
##    3    3    3    3    3    6    3    3    3    3    2    5    3    3    4 
## 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 
##    3    3    3    4    3    3    3    3    3    7    3    4    2    2    5 
## 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 
##    4    5    3    3    3    5    4    3    5    3    3    4    3    2    3 
## 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 
##    3    2    3    3    3    3    4    3    3    3    3    4    3    3    2 
## 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 
##    5    5    3    3    5    4    5    4    4    3    3    5    5    3    5 
## 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 
##    3    2    2    2    3    3    3    3    5    3    3    3    3    3    3 
## 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 
##    4    3    3    7    5    3    3    3    5    3    3    4    3    3    3 
## 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 
##    3    3    3    2    3    3    3    6    3    3    4    3    5    4    3 
## 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 
##    3    3    3    5    3    3    4    5    5    4    3    5    3    3    3 
## 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 
##    4    4    3    3    3    3    3    3    6    3    3    3    3    3    3 
## 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 
##    6    4    3    5    4    3    3    4    4    5    4    4    7    3    3 
## 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 
##    5    3    4    3    3    3    3    3    3    4    2    3    3    3    3 
## 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 
##    3    3    2    3    4    3    3    5    5    3    4    3    3    3    7 
## 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 
##    4    4    3    4    3    3    3    2    3    4    3    5    6    2    4 
## 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 
##    3    3    3    3    3    3    4    3    3    3    3    3    3    3    2 
## 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 
##    3    3    5    3    3    2    3    3    3    3    3    3    3    4    3 
## 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 
##    3    3    4    3    3    3    3    3    5    4    3    3    3    4    3 
## 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 
##    3    4    4    6    3    3    3    3    6    3    4    4    3    3    3 
## 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 
##    3    3    3    3    4    3    3    4    3    3    4    3    3    4    3 
## 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 
##    4    5    9    3    4    4    3    5    3    3    4    3    4    3    4 
## 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 
##    3    3    3    4    5    5    3    3    4    4    2    5    3    5    4 
## 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 
##    4    6    3    5    3    4    4    4    3    7    3    3    3    5    4 
## 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 
##    3    2    3    4    5    3    3    3    3    3    3    3    3    5    3 
## 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 
##    4    4    3    3    3    4    3    4    3    4    3    6    3    3    6 
## 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 
##    5    4    4    4    3    3    6    4    7    3    3    4    3    5    5 
## 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 
##    4    4    3    3    5    3    3    4    3    3    3    3    2    3    7 
## 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 
##    3    3    4    3    6    3    3    4    3    2    3    3    3    5    3 
## 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 
##    3    3    6    5    5    3    4    2    3    3    5    3    3    5    5 
## 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 
##    4    6    5    4    3    4    3    3    4    4    4    3    7    6    4 
## 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 
##    3    3    4    6    3    4    4    4    4    3    4    3    4    3    3 
## 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 
##    3    3    3    3    6    4    4    3    3    3    6    4    3    2    3 
## 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 
##    3    3    3    3    3    3    3    3    4    4    3    3    4    5    3 
## 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 
##    3    4    3    5    2    3    3    3    2    5    3    3    3    4    3 
## 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 
##    3    4    3    4    3    4    3    3    3    3    7    3    4    5    3 
## 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 
##    3    4    4    2    3    6    4    3    3    7    3    3    3    4    6 
## 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 
##    4    3    5    3    3    3    4    3    3    3    3    3    2    4    3 
## 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 
##    4    3    4    5    2    5    4    5    3    3    3    3    3    5    3 
## 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 
##    3    4    3    3    3    3    3    4    7    5    3    3    3    2    3 
## 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 
##    5    4    3    3    6    4    4    4    4    3    3    4    3    3    3 
## 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 
##    4    3    6    4    3    5    4    6    3    4    3    3    4    3    5 
## 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 
##    4    3    3    4    3    3    3    5    3    3    3    5    3    3    5 
## 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 
##    3    5    3    3    3    2    5    2    3    4    4    4    4    3    3 
## 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 
##    3    3    6    3    6    5    4    2    3    4    3    3    3    2    5 
## 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 
##    4    3    3    3    3    3    5    3    3    4    3    3    3    3    3 
## 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 
##    6    3    3    3    3    3    4    3    3    3    3    5    3    4    3 
## 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 
##    3    4    4    4    2    3    5    3    5    3    3    2    2    3    6 
## 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 
##    3    3    3    3    5    3    2    3    5    3    2    2    3    2    3 
## 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 
##    3    3    3    3    3    3    3    3    2    4    3    3    3    3    3 
## 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 
##    3    4    3    2    6    3    3    2    3    3    4    4    2    5    3 
## 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 
##    3    2    3    4    4    3    3    5    3    3    3    5    3    6    5 
## 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 
##    4    3    3    3    3    3    4    5    3    3    4    3    3    4    3 
## 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 
##    5    3    5    3    3    3    3    4    3    3    4    3    4    3    3 
## 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 
##    7    4    4    3    2    3    5    4    3    2    3    3    4    3    4 
## 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 
##    3    3    3    6    3    7    4    4    4    5    3    3    5    4    4 
## 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 
##    3    4    6    3    3    3    6    3    4    3    2    3    3    3    3 
## 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 
##    4    3    4    3    4    3    4    4    4    3    4    3    5    3    4 
## 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 
##    4    3    3    3    3    3    2    3    4    3    3    3    3    3    3 
## 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 
##    5    2    5    3    3    3    4    6    4    2    4    3    3    4    3 
## 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 
##    3    3    2    3    3    3    3    3    5    3    4    3    4    3    3 
## 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 
##    3    5    3    3    3    2    3    3    3    3    3    4    3    5    3 
## 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 
##    3    3    3    3    3    3    4    5    3    2    3    2    3    4    5 
## 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 
##    5    5    2    3    2    3    6    3    3    4    5    4    6    7    4 
## 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 
##    3    4    3    3    3    4    3    4    4    4    3    3    3    3    3 
## 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 
##    5    3    6    2    3    3    3    3    3    3    3    3    8    4    4 
## 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 
##    4    4    3    4    4    3    3    3    3    4    4    4    6    3    3 
## 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 
##    2    3    3    2    5    3    3    3    3    6    3    2    3    4    5 
## 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 
##    3    3    5    4    4    3    3    3    3    3    4    3    3    3    5 
## 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 
##    4    3    3    3    3    3    3    4    4    3    4    5    2    3    3 
## 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 
##    5    3    3    3    3    3    4    4    3    3    3    3    3    3    4 
## 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 
##    3    7    3    4    5    5    3    3    4    4    4    4    3    3    3 
## 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 
##    3    2    3    5    6    3    3    3    3    2    3    3    5    3    3 
## 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 
##    3    3    3    3    3    3    3    3    3    2    5    3    3    3    3 
## 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 
##    3    3    3    3    2    4    4    6    5    4    4    3    3    3    3 
## 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 
##    2    2    3    4    4    3    3    3    4    3    5    6    4    3    3 
## 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 
##    5    4    3    2    4    4    4    4    3    5    3    4    3    8    3 
## 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 
##    4    5    3    3    3    3    4    4    6    3    5    3    3    4    4 
## 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 
##    4    3    5    3    4    2    4    3    2    3    4    5    3    3    3 
## 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 
##    3    3    5    4    3    3    3    3    3    2    3    3    3    4    3 
## 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 
##    3    3    3    5    3    3    3    5    3    5    6    5    3    3    3 
## 3331 3332 3333 3334 3335 
##    4    6    4    3    4

APPENDIX

wine <- read.csv(“https://raw.githubusercontent.com/swigodsky/Data621/master/wine-training-data.csv”, stringsAsFactors = FALSE) head(wine) #nrow(wine) meantarget <- round(mean(wine$TARGET))

wine_index_removed <- wine[-1] summary(wine_index_removed)

library(corrplot) correlation <- cor(wine_index_removed, method = “pearson”,use=“complete.obs”) #correlation corrplot(correlation, type=“upper”, method=“color”)

plot(wine\(AcidIndex, ylab="Acid Index") hist(wine\)AcidIndex, xlab=“Acid Index”,main=“Histogram of Acid Index”) boxplot(wine$AcidIndex, main=“Acid Index”)

plot(wine\(FixedAcidity, ylab="Fixed Acidity") hist(wine\)FixedAcidity, xlab=“Fixed Acidity”,main=“Histogram of Fixed Acidity”) boxplot(wine$FixedAcidity, main=“Fixed Acidity”)

plot(wine\(FixedAcidity,wine\)AcidIndex, xlab=“Fixed Acidity”, ylab=“Acid Index”, main=“Relationship Between Acid Index and Fixed Acidity”) abline(lm(wine\(AcidIndex~wine\)FixedAcidity),col=“red”)

hist(wine\(STARS, xlab="Stars",main="Histogram of Stars") boxplot(wine\)STARS, main=“Stars”)

hist(wine\(LabelAppeal, xlab="Label Appeal",main="Histogram of Label Appeal") boxplot(wine\)LabelAppeal, main=“Label Appeal”)

plot(wine\(LabelAppeal,wine\)STARS, xlab=“Label Appeal”, ylab=“Stars”, main=“Relationship Between Label Appeal and Stars”) abline(lm(wine\(STARS~wine\)LabelAppeal),col=“red”)

plot(wine\(ResidualSugar, ylab="Residual Sugar") hist(wine\)ResidualSugar, xlab=“Residual Sugar”,main=“Histogram of Residual Sugar”) boxplot(wine$ResidualSugar, main=“Residual Sugar”)

plot(wine\(Chlorides, ylab="Chlorides") hist(wine\)Chlorides, xlab=“Chlorides”,main=“Histogram of Chlorides”) boxplot(wine$Chlorides, main=“Chlorides”)

plot(wine\(FreeSulfurDioxide, ylab="Free Sulfur Dioxide") hist(wine\)FreeSulfurDioxide, xlab=“Free Sulfur Dioxide”,main=“Histogram of Free Sulfur Dioxide”) boxplot(wine$FreeSulfurDioxide, main=“Free Sulfur Dioxide”)

plot(wine\(TotalSulfurDioxide, ylab="Total Sulfur Dioxide") hist(wine\)TotalSulfurDioxide, xlab=“Total Sulfur Dioxide”,main=“Histogram of Total Sulfur Dioxide”) boxplot(wine$TotalSulfurDioxide, main=“Total Sulfur Dioxide”)

plot(wine\(pH, ylab="pH") hist(wine\)pH, xlab=“pH”,main=“Histogram of pH”) boxplot(wine$pH, main=“pH”)

plot(wine\(Sulphates, ylab="Sulphates") hist(wine\)Sulphates, xlab=“Sulphates”,main=“Histogram of Sulphates”) boxplot(wine$Sulphates, main=“Sulphates”)

plot(wine\(Alcohol, ylab="Alcohol") hist(wine\)Alcohol, xlab=“Alcohol”,main=“Histogram of Alcohol”) boxplot(wine$Alcohol, main=“Alcohol”)

wine_imputed <- wine_index_removed

mean_res_sugar <- mean(wine_imputed\(ResidualSugar, na.rm=T) wine_imputed\)ResidualSugar[is.na(wine_imputed$ResidualSugar)] <- mean_res_sugar

mean_chlorides <- mean(wine_imputed\(Chlorides, na.rm=T) wine_imputed\)Chlorides[is.na(wine_imputed$Chlorides)] <- mean_chlorides

mean_freesulfdiox <- mean(wine_imputed\(FreeSulfurDioxide, na.rm=T) wine_imputed\)FreeSulfurDioxide[is.na(wine_imputed$FreeSulfurDioxide)] <- mean_freesulfdiox

mean_pH <- mean(wine_imputed\(pH, na.rm=T) wine_imputed\)pH[is.na(wine_imputed$pH)] <- mean_pH

mean_totsulfdiox <- mean(wine_imputed\(TotalSulfurDioxide, na.rm=T) wine_imputed\)TotalSulfurDioxide[is.na(wine_imputed$TotalSulfurDioxide)] <- mean_totsulfdiox

mean_sulphates <- mean(wine_imputed\(Sulphates, na.rm=T) wine_imputed\)Sulphates[is.na(wine_imputed$Sulphates)] <- mean_sulphates

mean_alcohol <- mean(wine_imputed\(Alcohol, na.rm=T) wine_imputed\)Alcohol[is.na(wine_imputed$Alcohol)] <- mean_alcohol

mean_stars <- mean(wine_imputed\(STARS, na.rm=T) wine_imputed\)STARS[is.na(wine_imputed$STARS)] <- mean_stars

stars_label <- wine_imputed\(STARS + wine_imputed\)LabelAppeal wine_imputed <- cbind(wine_imputed, stars_label) wine_imputed <-subset(wine_imputed, select=-c(STARS, LabelAppeal))

set.seed(15) n <- nrow(wine_imputed) shuffle_df1 <- wine_imputed[sample(n),] train_indeces <- 1:round(0.6n) train1 <- shuffle_df1[train_indeces,] test_indeces <- (round(.6n)+1):n test1 <- shuffle_df1[test_indeces,]

poisson1 <- glm(TARGET ~ ., data=train1, family=“poisson”) summary(poisson1)

poisson1 <- update(poisson1, .~. -ResidualSugar, data = train1, family=“poisson”) summary(poisson1)

poisson1 <- update(poisson1, .~. -CitricAcid, data = train1, family=“poisson”) summary(poisson1)

poisson1 <- update(poisson1, .~. -FixedAcidity, data = train1, family=“poisson”) summary(poisson1)

acc <- function(pred, test){ totalnum <- length(pred) numRight <- length(which(pred==test$TARGET)) accuracy <- numRight/totalnum return(accuracy) }

accplusminus1 <- function(pred, test){ totalnum <- length(pred) numRightish <- length(which(pred==test\(TARGET+1|pred==test\)TARGET-1|pred==test$TARGET)) accuracy1 <- numRightish/totalnum return(accuracy1) }

err <- function(pred, test){ totalnum <- length(pred) numWrong <- length(which(pred!=test$TARGET)) error <- numWrong/totalnum return(error) }

pred_p1 <- predict(poisson1, newdata=test1, type=“response”) pred_p1 <- round(pred_p1) table(pred=pred_p1, true=test1$TARGET) accuracy <- acc(pred_p1,test1) print(accuracy) print(accplusminus1(pred_p1,test1))

error1 <- pred_p1-test1$TARGET rmse1 <- sqrt(mean(error1^2)) rmse1

wine2 <- wine_index_removed wine2 <- subset(wine2, select=c(TARGET,VolatileAcidity, Chlorides,Density,Sulphates,Alcohol, AcidIndex,STARS, LabelAppeal))

set.seed(15) n <- nrow(wine2) shuffle_df2 <- wine2[sample(n),] train_indeces <- 1:round(0.6n) train2 <- shuffle_df2[train_indeces,] test_indeces <- (round(.6n)+1):n test2 <- shuffle_df2[test_indeces,]

library(MASS) nb2 <- glm.nb(TARGET ~ ., data=train2) summary(nb2)

nb2 <- update(nb2, .~. -Sulphates, data = train2) summary(nb2)

nb2 <- update(nb2, .~. -Density, data = train2) summary(nb2)

nb2 <- update(nb2, .~. -Chlorides, data = train2) summary(nb2)

pred_nb2 <- predict(nb2, newdata=test2, type=“response”) pred_nb2[is.na(pred_nb2)] <- meantarget pred_nb2 <- round(pred_nb2) table(pred=pred_nb2, true=test2$TARGET) accuracy <- acc(pred_nb2,test2) print(accuracy) print(accplusminus1(pred_nb2,test2))

error2 <- pred_nb2-test2$TARGET rmse2 <- sqrt(mean(error2^2)) rmse2

wine3 <- wine2 stars_label <- wine3\(STARS + wine3\)LabelAppeal wine3 <- cbind(wine3, stars_label) wine3 <-subset(wine3, select=-c(STARS, LabelAppeal))

set.seed(15) n <- nrow(wine3) shuffle_df3 <- wine3[sample(n),] train_indeces <- 1:round(0.6n) train3 <- shuffle_df3[train_indeces,] test_indeces <- (round(.6n)+1):n test3 <- shuffle_df3[test_indeces,]

lm3 <- lm(train3$TARGET ~., data=train3) summary(lm3)

lm3 <- update(lm3, .~. -Sulphates, data = train3) summary(lm3)

lm3 <- update(lm3, .~. -Density, data = train3) summary(lm3)

qqnorm(resid(lm3)) qqline(resid(lm3))

pred3 <- predict(lm3, newdata=test3, type=“response”) pred3[pred3<0]<-0 pred3[is.na(pred3)] <- meantarget pred3 <- round(pred3) error <- pred3-test3$TARGET head(pred3) rmse3 <- sqrt(mean(error^2)) rmse3

table(pred=pred3, true=test3$TARGET) accuracy <- acc(pred3,test3) print(accuracy) print(accplusminus1(pred3,test3))

wine4 <- wine_index_removed

missing_sugar <- ifelse(is.na(wine4\(ResidualSugar),1,0) missing_chlorides <- ifelse(is.na(wine4\)Chlorides),1,0) missing_freesulfdiox <- ifelse(is.na(wine4\(FreeSulfurDioxide),1,0) missing_total_sulf_diox <- ifelse(is.na(wine4\)TotalSulfurDioxide),1,0) missing_pH <- ifelse(is.na(wine4\(pH),1,0) missing_sulphates <- ifelse(is.na(wine4\)Sulphates),1,0) missing_alcohol <- ifelse(is.na(wine4\(Alcohol),1,0) missing_stars <- ifelse(is.na(wine4\)STARS),1,0)

wine4 <- cbind(wine4, missing_sugar,missing_chlorides,missing_alcohol,missing_freesulfdiox,missing_total_sulf_diox,missing_pH,missing_sulphates,missing_stars)

wine4\(ResidualSugar[is.na(wine4\)ResidualSugar)] <- mean_res_sugar wine4\(Chlorides[is.na(wine4\)Chlorides)] <- mean_chlorides wine4\(FreeSulfurDioxide[is.na(wine4\)FreeSulfurDioxide)] <- mean_freesulfdiox wine4\(pH[is.na(wine4\)pH)] <- mean_pH wine4\(TotalSulfurDioxide[is.na(wine4\)TotalSulfurDioxide)] <- mean_totsulfdiox wine4\(Sulphates[is.na(wine4\)Sulphates)] <- mean_sulphates wine4\(Alcohol[is.na(wine4\)Alcohol)] <- mean_alcohol wine4\(STARS[is.na(wine4\)STARS)] <- mean_stars

correlation4 <- cor(wine4, method = “pearson”) corrplot(correlation4, type=“upper”, method=“color”)

wine4 <-subset(wine4, select=-c(missing_alcohol,missing_chlorides,missing_freesulfdiox,missing_pH,missing_sugar,missing_sulphates,missing_total_sulf_diox)) set.seed(15) n <- nrow(wine4) shuffle_df4 <- wine4[sample(n),] train_indeces <- 1:round(0.6n) train4 <- shuffle_df4[train_indeces,] test_indeces <- (round(.6n)+1):n test4 <- shuffle_df4[test_indeces,]

poisson4 <- glm(TARGET ~ ., data=train4, family=“poisson”) summary(poisson4)

poisson4 <- update(poisson4, .~. -ResidualSugar, data = train4) summary(poisson4)

poisson4 <- update(poisson4, .~. -CitricAcid, data = train4) summary(poisson4)

poisson4 <- update(poisson4, .~. -FixedAcidity, data = train4) summary(poisson4)

poisson4 <- update(poisson4, .~. -Density, data = train4) summary(poisson4)

poisson4 <- update(poisson4, .~. -Sulphates, data = train4) summary(poisson4)

poisson4 <- update(poisson4, .~. -TotalSulfurDioxide, data = train4) summary(poisson4)

poisson4 <- update(poisson4, .~. -Alcohol, data = train4) summary(poisson4)

poisson4 <- update(poisson4, .~. -FreeSulfurDioxide, data = train4) summary(poisson4)

pred4 <- predict(poisson4, newdata=test4, type=“response”) pred4[pred4<0]<-0 pred4[is.na(pred4)] <- meantarget pred4 <- round(pred4) error <- pred4-test4$TARGET head(pred4) rmse4 <- sqrt(mean(error^2)) rmse4

table(pred=pred4, true=test3$TARGET) accuracy <- acc(pred4,test4) print(accuracy) print(accplusminus1(pred4,test4))

wine5 <- wine4 stars_label <- wine5\(STARS + wine5\)LabelAppeal wine5 <- cbind(wine5, stars_label) wine5 <- subset(wine5, select=c(TARGET,VolatileAcidity, Chlorides,Density,Sulphates,Alcohol, AcidIndex,missing_stars,stars_label))

set.seed(15) n <- nrow(wine5) shuffle_df5 <- wine5[sample(n),] train_indeces <- 1:round(0.6n) train5 <- shuffle_df5[train_indeces,] test_indeces <- (round(.6n)+1):n test5 <- shuffle_df5[test_indeces,]

nb5 <- glm.nb(TARGET ~ ., data=train5) summary(nb5)

nb5 <- update(nb5, .~. -Density, data = train5) summary(nb5)

nb5 <- update(nb5, .~. -Sulphates, data = train5) summary(nb5)

pred5 <- predict(nb5, newdata=test5, type=“response”) pred5[pred5<0]<-0 pred5[is.na(pred5)] <- meantarget pred5 <- round(pred5) error <- pred5-test5$TARGET rmse5 <- sqrt(mean(error^2)) rmse5

table(pred=pred5, true=test3$TARGET) accuracy <- acc(pred5,test5) print(accuracy) print(accplusminus1(pred5,test5))

lm6 <- lm(train5$TARGET ~., data=train5) summary(lm6)

lm6 <- update(lm6, .~. -Density, data = train5) summary(lm6)

qqnorm(resid(lm6)) qqline(resid(lm6))

pred6 <- predict(lm6, newdata=test5, type=“response”) pred6[pred6<0]<-0 pred6[is.na(pred6)] <- meantarget pred6 <- round(pred6) error <- pred6-test5$TARGET rmse6 <- sqrt(mean(error^2)) rmse6

table(pred=pred6, true=test5$TARGET) accuracy <- acc(pred6,test5) print(accuracy) print(accplusminus1(pred6,test5))

eval_data <- read.csv(‘https://raw.githubusercontent.com/swigodsky/Data621/master/wine-evaluation-data.csv’)

stars_label <- eval_data\(STARS + eval_data\)LabelAppeal

missing_stars <- ifelse(is.na(eval_data$STARS),1,0)

eval_data\(ResidualSugar[is.na(eval_data\)ResidualSugar)] <- mean_res_sugar eval_data\(Chlorides[is.na(eval_data\)Chlorides)] <- mean_chlorides eval_data\(FreeSulfurDioxide[is.na(eval_data\)FreeSulfurDioxide)] <- mean_freesulfdiox eval_data\(pH[is.na(eval_data\)pH)] <- mean_pH eval_data\(TotalSulfurDioxide[is.na(eval_data\)TotalSulfurDioxide)] <- mean_totsulfdiox eval_data\(Sulphates[is.na(eval_data\)Sulphates)] <- mean_sulphates eval_data\(Alcohol[is.na(eval_data\)Alcohol)] <- mean_alcohol eval_data\(STARS[is.na(eval_data\)STARS)] <- mean_stars

eval_data <- cbind(eval_data, stars_label, missing_stars)

pred_eval <- predict(nb5, newdata=eval_data, type=“response”) pred_eval[pred_eval<0]<-0 pred_eval[is.na(pred_eval)] <- meantarget pred_eval <- round(pred_eval) pred_eval