-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwinequalitypredictor.Rmd
1104 lines (585 loc) · 24.3 KB
/
winequalitypredictor.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Wine Quality Predictor"
author: "Shradhit Subudhi"
date: "December 3, 2018"
output:
'rmdformats:: readthedown': default
rmdformats::readthedown: default
---
# INTRODUCTION
**Team Member :** Shradhit Subudhi <br />
\newline
**Professor's Name :** Dr. Douglas Jones <br />
\newline
**Course Period :** Fall - 2018 (September - December) <br />
\newline
**Date :** "December 3, 2018" <br />
\newline
## Inspiration for Project
**"In wine, there's truth"** - Pliny the Elder.
## Question
**Can we predict the quality of wine using data analysis techniques?**
# Wine Data Set - Info
The two datasets are related to red and white variants of the Portuguese *"Vinho Verde"* wine. Due to privacy and logistic issues, only physicochemical (inputs) and sensory (the output) variables are available (e.g. there is no data about grape types, wine brand, wine selling price, etc.). <br />
These datasets can be viewed as classification or regression tasks. The classes are ordered and not balanced (e.g. there are much more normal wines than excellent or poor ones). Outlier detection algorithms could be used to detect the few excellent or poor wines. Also, we are not sure if all input variables are relevant. So it could be interesting to test feature selection methods. <br />
> We worked on predicting the **white wine quality** for this project. Worst wine is represented with 0 and the best wine is represented as 10.
# Packages Installed
```{r}
library("rmdformats")
library("corrgram")
library("MASS")
library("ggplot2")
library("naniar")
library("e1071")
library("lattice")
library("caret")
library("car")
library("caTools")
library("corrplot")
library("knitr")
```
# Inputting Data
Importing the data with the help of a url and assigning it to white.
```{r}
white_url <- "https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-white.csv"
whitewine <- read.csv(white_url, header = TRUE, sep = ";")
white <- whitewine
```
# Data - First View
Viewing the loaded data.
```{r}
str(white)
```
Looking at the output we get to know that we have 4898 samples and 12 variables. <br />
The response variable is *quality.* <br />
The *eleven predictor variables* are of the *numeric class* and the *response variable*, quality, is of the *integer class.* <br />
We don't see any categorical variable in the dataset. <br />
## Columns
We then check the names of the variables present in the data.
```{r}
colnames(white)
```
Look at the summary to better understand the dataset.
```{r}
summary(white)
```
## Removing the Duplicate Rows
```{r}
#white <- unique(white)
white <- white[!duplicated(white), ]
dim(white)
```
The rows got reduced to 3961 after removing the duplicates.
## Checking for NA
### Graphically
Checking if there are any missing (NA) values.
```{r}
vis_miss(white)
```
100% values of all columns are present.
### Technically
```{r}
sum(is.na(white))
```
There is no NA values in the data set.
## Response Count
```{r}
table(white$quality)
```
We can see that the class is skewed i.e. it has imbalance. <br />
There are 3961 samples but only 20 points are from 3rd class and only 5 points are from the 9th class.<br />
Wines with lowest and highest quality are rare. Generally, wines have quality 5 and 6 which is in the medium range(not low, not high).
```{r}
#rounds the values in its first argument to the specified number of decimal places (default 0).
round(cor(white, method = "pearson"), 2)
```
##Correlation Matrix
We now check the correlation matrix, confidence interval with the help of corrplot package.It also helps us to do matrix reordering.
```{r}
corrplot(cor(white))
```
```{r}
corrgram(white, type="data", lower.panel=panel.conf,
upper.panel=panel.shade, main= "Corrgram for wine quality dataset", order=T, cex.labels=1.2)
```
Sugar,pH and citric acid factors are not playing a really big role in the quality of wine.<br />
alcohol is kind of strongly correlated to the quality of wine.<br />
##Histogram
Plotting the histograms using hist() for the data.
```{r}
attach(white)
par(mfrow=c(2,2), oma = c(1,1,0,0) + 0.1, mar = c(3,3,1,1) + 0.1)
barplot((table(quality)), col=c("slateblue4", "slategray", "slategray1", "slategray2", "slategray3", "skyblue4"))
mtext("Quality", side=1, outer=F, line=2, cex=0.8)
truehist(fixed.acidity, h = 0.5, col="slategray3")
mtext("Fixed Acidity", side=1, outer=F, line=2, cex=0.8)
truehist(volatile.acidity, h = 0.05, col="slategray3")
mtext("Volatile Acidity", side=1, outer=F, line=2, cex=0.8)
truehist(citric.acid, h = 0.1, col="slategray3")
mtext("Citric Acid", side=1, outer=F, line=2, cex=0.8)
```
```{r}
par(mfrow=c(2,2), oma = c(1,1,0,0) + 0.1, mar = c(3,3,1,1) + 0.1)
truehist(residual.sugar, h = 5, col="slategray3")
mtext("Residual Sugar", side=1, outer=F, line=2, cex=0.8)
truehist(chlorides, h = 0.01, col="slategray3")
mtext("Chloride", side=1, outer=F, line=2, cex=0.8)
truehist(alcohol, h = 0.5, col="slategray3")
mtext("Alcohol", side=1, outer=F, line=2, cex=0.8)
truehist(density, h = 0.005, col="slategray3")
mtext("Density", side=1, outer=F, line=2, cex=0.8)
```
```{r}
par(mfrow=c(2,2), oma = c(1,1,0,0) + 0.1, mar = c(3,3,1,1) + 0.1)
truehist(free.sulfur.dioxide, h = 10, col="slategray3")
mtext("Free Sulfur Dioxide", side=1, outer=F, line=2, cex=0.8)
truehist(pH, h = 0.1, col="slategray3")
mtext("pH values", side=1, outer=F, line=2, cex=0.8)
truehist(sulphates, h = 0.1, col="slategray3")
mtext("sulphates", side=1, outer=F, line=2, cex=0.8)
truehist(total.sulfur.dioxide, h = 20, col="slategray3")
mtext("total.sulfur.dioxide", side=1, outer=F, line=2, cex=0.8)
```
##Boxplot
Y ^(lambda) where Y would be the values in the dataset and lambda would be the exponent (ranges from -5 to 5).<br />
Examples<br />
If lambda = 2, all the values would be squared. <br />
If lambda = 0.5, the square root of the values would be taken. <br/>
If lambda = 0, the log of the values would be taken. <br />
Plotting the boxplots using boxplot() for the data.
```{r}
par(mfrow=c(1,5), oma = c(1,1,0,0) + 0.1, mar = c(3,3,1,1) + 0.1)
boxplot(fixed.acidity, col="slategray2", pch=19)
mtext("Fixed Acidity", cex=0.8, side=1, line=2)
boxplot(volatile.acidity, col="slategray2", pch=19)
mtext("Volatile Acidity", cex=0.8, side=1, line=2)
boxplot(citric.acid, col="slategray2", pch=19)
mtext("Citric Acid", cex=0.8, side=1, line=2)
boxplot(residual.sugar, col="slategray2", pch=19)
mtext("Residual Sugar", cex=0.8, side=1, line=2)
boxplot(chlorides, col="slategray2", pch=19)
mtext("Chlorides", cex=0.8, side=1, line=2)
```
```{r}
par(mfrow=c(1,6), oma = c(1,1,0,0) + 0.1, mar = c(3,3,1,1) + 0.1)
boxplot(alcohol, col="slategray2", pch=19)
mtext("Alcohol", cex=0.8, side=1, line=2)
boxplot(density, col="slategray2", pch=19)
mtext("density", cex=0.8, side=1, line=2)
boxplot(free.sulfur.dioxide, col="slategray2", pch=19)
mtext("free.sulfur.dioxide", cex=0.8, side=1, line=2)
boxplot( pH, col="slategray2", pch=19)
mtext("pH", cex=0.8, side=1, line=2)
boxplot(sulphates, col="slategray2", pch=19)
mtext("sulphates", cex=0.8, side=1, line=2)
boxplot(total.sulfur.dioxide, col="slategray2", pch=19)
mtext("total.sulfur.dioxide", cex=0.8, side=1, line=2)
detach(white)
```
> Observations regarding variables: All variables have outliers.
- Quality has most values concentrated in the categories 5, 6 and 7. Only a small proportion is in the categories [3, 4] and [8, 9] and none in the categories [1, 2, 10]. <br />
- Fixed acidity, volatile acidity and citric acid have outliers. If those outliers are eliminated then distribution of the variables may be considered to be symmetric. <br />
- Residual sugar has a positively skewed distribution; even after eliminating the outliers, distribution will remain skewed. <br />
- Some of the variables, e.g . free sulphur dioxide and density, have a few outliers but these are very different from the rest.Mostly outliers are on the larger side. <br />
- Alcohol has an irregular shaped distribution but it does not have pronounced outliers. <br />
## Skewness
Checking the skewness of the individual variables of the data to check whether the data is normally distributed or not.
```{r}
attach(white)
skewness(quality)
```
```{r}
skewness(chlorides)
```
```{r}
skewness(free.sulfur.dioxide)
```
```{r}
skewness(residual.sugar)
```
```{r}
skewness(alcohol)
```
```{r}
skewness(citric.acid)
```
```{r}
skewness(density)
```
```{r}
skewness(fixed.acidity)
```
```{r}
skewness(volatile.acidity)
```
```{r}
skewness(total.sulfur.dioxide)
```
```{r}
skewness(sulphates)
```
```{r}
skewness(pH)
```
```{r}
colnames(white)
detach(white)
```
## Skewness Rule of Thumb
**If skewness is 0, the data are perfectly symmetrical.** <br />
**As a general rule of thumb: If skewness is less than -1 or greater than 1, the distribution is highly skewed.** <br />
**If skewness is between -0.5 and 0.5, the distribution is approximately symmetric.** <br />
# Data Transformation / Preparation
## Box Cox Transformation
We use the Boxcox transformation and transform the data and then again check the skewness.
```{r}
preprocess_white <- preProcess(white[,1:11], c("BoxCox", "center", "scale"))
new_white <- data.frame(trans = predict(preprocess_white, white))
colnames(new_white)
```
### Skewness - Transformed Data
```{r}
skewness(new_white$trans.quality)
```
```{r}
skewness(new_white$trans.chlorides)
```
```{r}
skewness(new_white$trans.free.sulfur.dioxide)
```
```{r}
skewness(new_white$trans.residual.sugar)
```
```{r}
skewness(new_white$trans.alcohol)
```
```{r}
skewness(new_white$trans.citric.acid)
```
```{r}
skewness(new_white$trans.density)
```
```{r}
skewness(new_white$trans.fixed.acidity)
```
```{r}
skewness(new_white$trans.volatile.acidity)
```
```{r}
skewness(new_white$trans.total.sulfur.dioxide)
```
```{r}
skewness(new_white$trans.sulphates)
```
```{r}
skewness(new_white$trans.pH)
```
## Removal of Outliers
Most parametric statistics, like means, standard deviations, and correlations, and every statistic based on these, are highly sensitive to outliers.The assumptions of common statistical procedures, like linear regression and ANOVA, are also based on these statistics, outliers can disrupt the analysis. Thus, we remove the outliers. <br />
Possibly, the most important step in data preparation is to identify outliers. Since this is a multivariate data, we consider only those points which do not have any predictor variable value to be outside of limits constructed by boxplots. The following rule is applied: <br />
A predictor value is considered to be an outlier only if it is greater than SD - 3.
The rationale behind this rule is that the extreme outliers are all on the higher end of the values and the distributions are all positively skewed.<br /><br />
```{r}
new_white <- new_white[!abs(new_white$trans.fixed.acidity) > 3,]
new_white <- new_white[!abs(new_white$trans.volatile.acidity) > 3,]
new_white <- new_white[!abs(new_white$trans.citric.acid) > 3,]
new_white <- new_white[!abs(new_white$trans.residual.sugar) > 3,]
new_white <- new_white[!abs(new_white$trans.chlorides) > 3,]
new_white <- new_white[!abs(new_white$trans.density) > 3,]
new_white <- new_white[!abs(new_white$trans.pH) > 3,]
new_white <- new_white[!abs(new_white$trans.sulphates) > 3,]
new_white <- new_white[!abs(new_white$trans.alcohol) > 3,]
```
We now check the correlation matrix, confidence interval with the help of corrplot package.It also helps us to do matrix reordering.
```{r}
corrplot(cor(new_white), type = "lower")
corrgram(new_white, type="data", lower.panel=panel.conf,
upper.panel=panel.shade, main= "Corrgram for wine quality dataset", order=T, cex.labels=1.1)
```
# Linear Model
## Assumption for linear model
Linear regression is an analysis that assesses whether one or more predictor variables explain the dependent (criterion) variable. The regression has five key assumptions: <br />
Linear relationship <br />
Multivariate normality <br />
No or little multicollinearity <br />
No auto-correlation <br />
Homoscedasticity <br />
## Train - Test Set
```{r}
set.seed(100)
trainingRowIndex <- sample(1:nrow(new_white), 0.8*nrow(new_white))
whitetrain <- new_white[trainingRowIndex, ]
whitetest <- new_white[-trainingRowIndex, ]
```
## Model Selection
Base Model - Trying to choose the best linear model..
```{r}
linear_0 <- lm(trans.quality ~ . , whitetrain) # taking all the points.
summary(linear_0)
```
## Checking Multicollinarilty
In multicollinearity,collinearity exists between three or more variables even if no pair of variables has a particularly high correlation. This means that there is redundancy between predictor variables. <br />
In the presence of multicollinearity, the solution of the regression model becomes unstable. <br />
Multicollinearity can assessed by computing a score called the variance inflation factor (or VIF), which measures how much the variance of a regression coefficient is inflated due to multicollinearity in the model. <br />
The VIF of a predictor is a measure for how easily it is predicted from a linear regression using the other predictors.
```{r}
# Variance inflation factor
vif(linear_0)
```
We can see multicollinarity over here. We remove trans.density cause it exibits multicollinarity.
Now fitting the new model.
```{r}
linear_1 <- lm(trans.quality ~ . -trans.density , whitetrain)
summary(linear_1)
```
```{r}
vif(linear_1)
```
We fit another model.
```{r}
linear_2 <- lm(trans.quality ~ . -trans.density - trans.fixed.acidity, whitetrain)
summary(linear_2)
```
We fit another model removing trans.citric.acid.
```{r}
linear_4 <- lm(trans.quality ~ . -trans.density - trans.fixed.acidity - trans.citric.acid, whitetrain)
summary(linear_4)
```
Trying to figure out the important predictor values
```{r}
corrplot(cor(new_white), type = "lower")
```
```{r}
vif(linear_4)
```
##ANOVA
Analysis of variance (ANOVA) is a collection of statistical models and their associated estimation procedures used to analyze the differences among group means in a sample.
ANOVA provides a statistical test of whether the population means of several groups are equal, and therefore generalizes the t-test to more than two groups.
It is useful for comparing (testing) three or more group means for statistical significance.
```{r}
par(mfrow=c(2,2), oma = c(1,1,0,0) + 0.1, mar = c(3,3,1,1) + 0.1)
plot(linear_4)
```
## F - Test
Null Hypothesis - Ho : B1 = B2 = B3 = B4 = 0
Alternate Hypothesis - Ho : B1 or B2 ....B11 != 0 (Bj where j ranges from 1 to 11)
F- Statistic - p value <= 2.2e -16
We can see that p value is less than 0.05. Thus we can reject the null hypothesis and accept the alternate hypothesis.
Thus,we can say that the model is significant.
## Stepwise
```{r}
steplinear <- step(linear_0)
```
```{r}
summary(steplinear)
```
###ANOVA
Analysis of variance (ANOVA) is a collection of statistical models and their associated estimation procedures used to analyze the differences among group means in a sample.
ANOVA provides a statistical test of whether the population means of several groups are equal, and therefore generalizes the t-test to more than two groups.
It is useful for comparing (testing) three or more group means for statistical significance.
```{r}
anova(linear_4,steplinear)
```
Used the stepwise model because it has lower RSS and ANOVA tells that it's significant.
```{r}
par(mfrow=c(2,2), oma = c(1,1,0,0) + 0.1, mar = c(3,3,1,1) + 0.1)
plot(steplinear)
```
**Residuals vs Fitted** plot shows if residuals have non-linear patterns.Residuals around a horizontal line without distinct patterns, that is a good indication we don't have non-linear relationships. <br />
**Normal QQ** plot shows residuals fitting the line. Hence, can call it norlly distibuted residuals. <br />
**Scale-Location** plot shows if residuals are spread equally along the ranges of predictors. This is how you can check the assumption of equal variance (homoscedasticity). It's good if you see a horizontal line with equally (randomly) spread points. <br />
**Residuals vs Leverage** plot has a typical look when there is some influential case. You can barely see Cook's distance lines (a red dashed line) because all cases are well inside of the Cook's distance.
## Predicting - Trained Set
Predict is a generic function for predictions from the results of various model fitting functions. The function invokes particular methods which depend on the class of the first argument.
```{r}
distPred <- predict(steplinear, whitetrain)
head(distPred)
```
## Converting the Number to a Whole Number
We use the ceiling operation for rounding the numeric values towards near integer values.
```{r}
distPred1 <- ceiling(distPred)
head(distPred1)
```
## Training Data Confusion Matrix
Table function performs categorical tabulation of data with the variable and its frequency. table() function is also helpful in creating Frequency tables with condition and cross tabulations.
```{r}
trn_tab <- table(predicted = distPred1, actual = whitetrain$trans.quality)
trn_tab
```
## Accuracy for the Linear Model
We check the accuracy of the linear model.
```{r}
sum(diag(trn_tab))/length(whitetest$trans.quality)
```
Accuracy Prediction over train set Linear Model is 26%.
## Testing or Validating the Model
```{r}
distPred <- predict(steplinear, whitetest)
head(distPred)
```
Round the numeric values to the nearest integer values.
```{r}
distPred1 <- ceiling(distPred)
head(distPred1)
```
```{r}
tst_tab <- table(predicted = distPred1, actual = whitetest$trans.quality)
tst_tab
```
##Accuracy - Test Data
Checking the accuracy of the test data.
```{r}
sum(diag(tst_tab))/length(whitetest$trans.quality)
```
Accuracy Prediction over test set Linear Model is 6.13%.
# Ordinal Logistic Regression Model
## Assumptions for Logistic Regression
First, binary logistic regression requires the dependent variable to be **binary and ordinal** logistic regression requires the dependent variable to be ordinal. <br />
Second, logistic regression requires the observations to be independent of each other. In other words, the observations should not come from repeated measurements or matched data. <br />
Third, logistic regression requires there to be **little or no multicollinearity** among the independent variables. This means that the independent variables should not be too highly correlated with each other. <br />
Fourth, logistic regression assumes **linearity of independent variables and log odds**. although this analysis does not require the dependent and independent variables to be related linearly, it requires that the independent variables are linearly related to the log odds. *(Did Not check for this! To improve model. We can try this in future.)* <br />
Finally, logistic regression typically requires a large sample size.
Ordinal logistic regression can be used to model a ordered factor response.
We use as.factor() to covert the variables into factors, that is, categorical variables.
```{r}
#using the orignal and making changes specifically required for Logistic Regession.
white$quality2 <- as.factor(white$quality)
```
## Train - Test Set
```{r}
set.seed(3000)
spl = sample.split(white$quality2, SplitRatio = 0.7)
whitetrain = subset(white, spl==TRUE)
whitetest = subset(white, spl==FALSE)
head(whitetrain)
```
## Fitting Model
polr() is the fuction used to fit the ordinal logistic regression model.
```{r}
require(MASS)
require(reshape2)
# Hess=TRUE to let the model output show the observed information matrix from optimization which is used to get standard errors.
o_lrm <- polr(quality2 ~ . - quality, data = whitetrain, Hess=TRUE)
```
```{r}
# should not use vif to check for multicollinarilty in case of categorical veriable.
vif(o_lrm)
```
```{r}
summary(o_lrm)
```
Smaller the AIC better is the model. So let's try step wise logistic regression.
## Stepwise
```{r}
o_lr = step(o_lrm)
```
We see some of the variables got eliminated. <br />
fixed.acidity, alcohol, density, sulphates, pH, free.sulfur.dioxide, residual.sugar, volatile.acidity are the variables being considered.
```{r}
head(fitted(o_lr))
```
## Training Set Accuracy
predicting -
```{r}
p <- predict(o_lr, type = "class")
head(p)
```
## Confusion Matrix Test
```{r}
cm1 = as.matrix(table(Actual = whitetrain$quality2, Predicted = p))
cm1
```
```{r}
sum(diag(cm1))/length(whitetrain$quality2)
```
Training Set Accuracy is 53.08%
## Test Set Accuracy
Accuray for the test set.
```{r}
tst_pred <- predict(o_lr, newdata = whitetest, type = "class")
```
## Confusion Matrix Test
```{r}
cm2 <- table(predicted = tst_pred, actual = whitetest$quality2)
cm2
```
## Test Set Accuracy
```{r}
sum(diag(cm2))/length(whitetrain$quality2)
```
Test Set Accuracy - 22.8 %
# Binomial Logistic Regression Model
The variable to be predicted is binary and hence we use binomial logistic regression.
```{r}
white$category[white$quality <= 5] <- 0
white$category[white$quality > 5] <- 1
white$category <- as.factor(white$category)
```
```{r}
head(white)
```
## Train Test Split
```{r}
set.seed(3000)
spl = sample.split(white$category, SplitRatio = 0.7)
whitetrain = subset(white, spl==TRUE)
whitetest = subset(white, spl==FALSE)