Bu çalışmada beyaz şarabın kalitesinin ilişkili olduğu parametreleri anlamaya çalışıyoruz.

İlk olarak tidyverse paketimizi ve sonra verimizi yükleyelim.

library(tidyverse)
## -- Attaching packages ------------------------------------------------------------------------------------------ tidyverse 1.2.1 --
## <U+221A> ggplot2 2.2.1     <U+221A> purrr   0.2.4
## <U+221A> tibble  1.4.2     <U+221A> dplyr   0.7.4
## <U+221A> tidyr   0.7.2     <U+221A> stringr 1.2.0
## <U+221A> readr   1.1.1     <U+221A> forcats 0.2.0
## -- Conflicts --------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
white=read.csv("C:/Users/yenia/Desktop/winequality-white.csv",sep=";")

Burada verimizde ayraç “;” olduğundan seperator olarak tanıtmamız gerekti.

Şimdi Veri setindeki sütun başlıklarını anlayalım:

names(white)
##  [1] "fixed.acidity"        "volatile.acidity"     "citric.acid"         
##  [4] "residual.sugar"       "chlorides"            "free.sulfur.dioxide" 
##  [7] "total.sulfur.dioxide" "density"              "pH"                  
## [10] "sulphates"            "alcohol"              "quality"

Özet bilgileri alarak veri incelemesine devam ediyoruz:

glimpse(white)
## Observations: 4,898
## Variables: 12
## $ fixed.acidity        <dbl> 7.0, 6.3, 8.1, 7.2, 7.2, 8.1, 6.2, 7.0, 6...
## $ volatile.acidity     <dbl> 0.27, 0.30, 0.28, 0.23, 0.23, 0.28, 0.32,...
## $ citric.acid          <dbl> 0.36, 0.34, 0.40, 0.32, 0.32, 0.40, 0.16,...
## $ residual.sugar       <dbl> 20.70, 1.60, 6.90, 8.50, 8.50, 6.90, 7.00...
## $ chlorides            <dbl> 0.045, 0.049, 0.050, 0.058, 0.058, 0.050,...
## $ free.sulfur.dioxide  <dbl> 45, 14, 30, 47, 47, 30, 30, 45, 14, 28, 1...
## $ total.sulfur.dioxide <dbl> 170, 132, 97, 186, 186, 97, 136, 170, 132...
## $ density              <dbl> 1.0010, 0.9940, 0.9951, 0.9956, 0.9956, 0...
## $ pH                   <dbl> 3.00, 3.30, 3.26, 3.19, 3.19, 3.26, 3.18,...
## $ sulphates            <dbl> 0.45, 0.49, 0.44, 0.40, 0.40, 0.44, 0.47,...
## $ alcohol              <dbl> 8.8, 9.5, 10.1, 9.9, 9.9, 10.1, 9.6, 8.8,...
## $ quality              <int> 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 7,...

Verimizin büyüklüğü karşısında ortalma değerler üzerinden ilerlemeye karar veriyoruz. Ortalama değerleri kalite parametresine göre gruplayalım:

white %>%
  group_by(quality) %>%
  summarise(Ort_Sabit_asitlik=mean(fixed.acidity),Ort_Degisken_Asitlik=mean(volatile.acidity),Ort_Sitrik=mean(citric.acid),Ort_Seker=mean(residual.sugar),Ort_sSO2=mean(free.sulfur.dioxide),tSO2=mean(total.sulfur.dioxide),Ort_Yogunluk=mean(density),Ort_pH=mean(pH),Ort_Sulfat=mean(sulphates),Ort_Alkol=mean(alcohol))

Verilerimizi görselleştirerek ortalama değerlerin kalite için birleşim oranlarını görmeye çalışalım:

white2<-white %>%
  group_by(quality) %>%
  summarise(Ort_Sabit_asitlik=mean(fixed.acidity),Ort_Degisken_Asitlik=mean(volatile.acidity),Ort_Sitrik=mean(citric.acid),Ort_Seker=mean(residual.sugar),Ort_sSO2=mean(free.sulfur.dioxide),tSO2=mean(total.sulfur.dioxide),Ort_Yogunluk=mean(density),Ort_pH=mean(pH),Ort_Sulfat=mean(sulphates),Ort_Alkol=mean(alcohol))
white3<-white2 %>% gather(key=degisken, value=icerik,-quality)
ggplot(white3,aes(x=quality,y=icerik))+geom_bar(stat="identity",aes(fill=degisken))

Birbirlerinden farklı oranda gelen ölçülerin aradaki ölçek farkı nedeniyle üstüste bakıldığında yanıltıcı olduğunu keşfettik ve çalışmamızı belirli ölçekler üzerinde tek tek inceleme yaparak yürütmeye karar verdik ve bar plot ile çalışmamıza başladık.

white4<-white3 %>% filter(degisken=="Ort_Seker")
  ggplot(white4,aes(x=quality,y=icerik))+geom_bar(stat="identity",aes(fill=degisken))

Tek bir şekille çalışmanın algı dağınıklığı ve zaman kaybına yol açması nedeniyle daha hızlı çalışma yollarını araştırmaya başladık ve bu ihtiyacımızın cevabı olarak multiplot fonksiyonunu bulduk.

multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  library(grid)

  # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)

  numPlots = length(plots)

  # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
    # Make the panel
    # ncol: Number of columns of plots
    # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                    ncol = cols, nrow = ceiling(numPlots/cols))
  }

 if (numPlots==1) {
    print(plots[[1]])

  } else {
    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))

    # Make each plot, in the correct location
    for (i in 1:numPlots) {
      # Get the i,j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))

      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                      layout.pos.col = matchidx$col))
    }
  }
}

Multiplot fonksiyonu kullanarak en önemli gördüğümüz dört değişkeni çizdirdik.

#white4<-white3 %>% filter(degisken=="Ort_Seker")
  p1<-ggplot(white3 %>% filter(degisken=="Ort_Seker"),aes(x=quality,y=icerik))+geom_bar(stat="identity",aes(fill=degisken))
  p2<-ggplot(white3 %>% filter(degisken=="Ort_pH"),aes(x=quality,y=icerik))+geom_bar(stat="identity",aes(fill=degisken))
  p3<-ggplot(white3 %>% filter(degisken=="Ort_Alkol"),aes(x=quality,y=icerik))+geom_bar(stat="identity",aes(fill=degisken))
  p4<-ggplot(white3 %>% filter(degisken=="Ort_Yogunluk"),aes(x=quality,y=icerik))+geom_bar(stat="identity",aes(fill=degisken))
  multiplot(p1,p2,p3,p4, cols=2)

Elde edilen bar grafik sonucunda pH ve yoğunluk değişkenlerinin daha detaylı incelenebilmesi için daha hassas ölçeğe ihtiyaç olduğu anlaşıldı.Bunun için ilk olarak scale_y_continuous fonksiyonunu kullanarak çözüm bulmaya çalıştık. Ancak;bar grafiklerin her zaman sıfırdan başlaması sebebiyle ölçek değişikliği sonucunda verimizi görsel olarak kaybettik.

  p1<-ggplot(white3 %>% filter(degisken=="Ort_Seker"),aes(x=quality,y=icerik))+geom_bar(stat="identity",aes(fill=degisken))
  p2<-ggplot(white3 %>% filter(degisken=="Ort_pH"),aes(x=quality,y=icerik))+geom_bar(stat="identity",aes(color=degisken))+scale_y_continuous(limits = c(2.8,3.8))
  p3<-ggplot(white3 %>% filter(degisken=="Ort_Alkol"),aes(x=quality,y=icerik))+geom_bar(stat="identity",aes(fill=degisken))
  p4<-ggplot(white3 %>% filter(degisken=="Ort_Yogunluk"),aes(x=quality,y=icerik))+geom_bar(stat="identity",aes(color=degisken))+scale_y_continuous(limits = c(0.992,0.9955))
  multiplot(p1,p2,p3,p4, cols=2)
## Warning: Removed 1 rows containing missing values (position_stack).

Çizgi grafiğin otoscale özelliğini kullanarak çizim aşamasını bitirdik.

  p1<-ggplot(white3 %>% filter(degisken=="Ort_Seker"),aes(x=quality,y=icerik))+geom_line(aes(color=degisken))
  p2<-ggplot(white3 %>% filter(degisken=="Ort_pH"),aes(x=quality,y=icerik))+geom_line(aes(color=degisken))
  p3<-ggplot(white3 %>% filter(degisken=="Ort_Alkol"),aes(x=quality,y=icerik))+geom_line(aes(color=degisken))
  p4<-ggplot(white3 %>% filter(degisken=="Ort_Yogunluk"),aes(x=quality,y=icerik))+geom_line(aes(color=degisken))
  multiplot(p1,p2,p3,p4, cols=2)

Yapılan inceleme sonucunda değişkenler için şu bulgulara ulaştık: 1-Şeker, “kararında” kullanılmalı 2-Belirli kalitenin üstündeki şaraplarda alkol miktarı arttıkça şarabın kalitesi yükselmektedir. 3-pH’ın yükselmesi ve sıvının bazikleşmesi kaliteyi ciddi oranda artırır. 4-Şarabın yoğunluğunun artırılması kalitede negatif etkilidir.

Buraya kadar manuel gözlem yöntemleri ile çalışmıştık. Bu aşamada analitik yöntemler ile çalışmamızı devam ettirdik.

Öncelikle tanımlayıcı (descriptive) testler yapmalıyız.

Bileşenlerin etkisini incelemek istediğimizde; -şekerin yükseldiğinde kalitenin düştüğünü -yoğunluk arttığında kalitenin düştüğünü net olarak görebiliyoruz

plot(white,col=2,pch=".",cex=4)

Regresyon analizine tabi tuttuğumuzda sitrik asit, klorit ve toplam kükürt miktarının kalite için önemli parametreler olmadığını keşfediyoruz. Ancak; R2 değerimizin %28 oluşu, lineer regresyonun başarılı bir model olmadığını gösteriyor.

regr01=lm(quality~.,white)
summary(regr01)
## 
## Call:
## lm(formula = quality ~ ., data = white)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.8348 -0.4934 -0.0379  0.4637  3.1143 
## 
## Coefficients:
##                        Estimate Std. Error t value Pr(>|t|)    
## (Intercept)           1.502e+02  1.880e+01   7.987 1.71e-15 ***
## fixed.acidity         6.552e-02  2.087e-02   3.139  0.00171 ** 
## volatile.acidity     -1.863e+00  1.138e-01 -16.373  < 2e-16 ***
## citric.acid           2.209e-02  9.577e-02   0.231  0.81759    
## residual.sugar        8.148e-02  7.527e-03  10.825  < 2e-16 ***
## chlorides            -2.473e-01  5.465e-01  -0.452  0.65097    
## free.sulfur.dioxide   3.733e-03  8.441e-04   4.422 9.99e-06 ***
## total.sulfur.dioxide -2.857e-04  3.781e-04  -0.756  0.44979    
## density              -1.503e+02  1.907e+01  -7.879 4.04e-15 ***
## pH                    6.863e-01  1.054e-01   6.513 8.10e-11 ***
## sulphates             6.315e-01  1.004e-01   6.291 3.44e-10 ***
## alcohol               1.935e-01  2.422e-02   7.988 1.70e-15 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7514 on 4886 degrees of freedom
## Multiple R-squared:  0.2819, Adjusted R-squared:  0.2803 
## F-statistic: 174.3 on 11 and 4886 DF,  p-value: < 2.2e-16

Bir önceki lineer yaklaşımda a+bx yapısını kullanmıştık. Bu defa a gibi bir değişkenin olmadığını varsayalım ve “intersect” değişkenini (sabitini) kaldıralım ve testi tekrar edelim. Tekrar edilen test R2 parametresini %98 den fazla bir duruma getirdi. Başka bir ifade ile yapının %99a yakın bölümünü modelle açıklayabilir duruma geldik. Bu durumda iken hangi parametrenin ne derece önemli lduğunu yorumlayalım:

-sitrik asit miktarı, kalite için önemli bir parametre değildir. -klorit miktarı, kalite için önemli bir parametre değildir. -pH ve toplam kükürt miktarının değişimi, kaliteyi az etkilemektedir.

regr02=lm(quality~-1+.,white)
summary(regr02)
## 
## Call:
## lm(formula = quality ~ -1 + ., data = white)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.9144 -0.4958 -0.0333  0.4675  3.1762 
## 
## Coefficients:
##                        Estimate Std. Error t value Pr(>|t|)    
## fixed.acidity        -0.0505906  0.0150754  -3.356 0.000797 ***
## volatile.acidity     -1.9585102  0.1138903 -17.196  < 2e-16 ***
## citric.acid          -0.0293492  0.0961648  -0.305 0.760229    
## residual.sugar        0.0249884  0.0025917   9.642  < 2e-16 ***
## chlorides            -0.9425824  0.5430204  -1.736 0.082660 .  
## free.sulfur.dioxide   0.0047908  0.0008390   5.710 1.20e-08 ***
## total.sulfur.dioxide -0.0008776  0.0003731  -2.352 0.018699 *  
## density               2.0420461  0.3532997   5.780 7.94e-09 ***
## pH                    0.1683951  0.0835957   2.014 0.044022 *  
## sulphates             0.4164536  0.0973279   4.279 1.91e-05 ***
## alcohol               0.3656334  0.0111203  32.880  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7562 on 4887 degrees of freedom
## Multiple R-squared:  0.9839, Adjusted R-squared:  0.9838 
## F-statistic: 2.707e+04 on 11 and 4887 DF,  p-value: < 2.2e-16

Bu defa karar ağaçlarımızla tanımlayıcı çalışmamızı sürdürelim: Variance importance bölümünde alkol miktarının en önemli, yoğunluk, değişken asitlik oranlarının nispeten önemli ama pH değerinin görece en önemsiz kalite belirleyici faktör olduğunu görüyoruz.

library(rpart)
library(rpart.plot)

agacmodeli=rpart(quality~.,white)
agacmodeli
## n= 4898 
## 
## node), split, n, deviance, yval
##       * denotes terminal node
## 
##  1) root 4898 3840.99000 5.877909  
##    2) alcohol< 10.85 3085 1844.90600 5.605511  
##      4) volatile.acidity>=0.2525 1610  775.33480 5.360870 *
##      5) volatile.acidity< 0.2525 1475  868.03800 5.872542  
##       10) volatile.acidity>=0.2075 744  370.02020 5.713710 *
##       11) volatile.acidity< 0.2075 731  460.14500 6.034200  
##         22) density< 0.99788 614  337.98860 5.929967 *
##         23) density>=0.99788 117   80.47863 6.581197 *
##    3) alcohol>=10.85 1813 1377.65900 6.341423  
##      6) free.sulfur.dioxide< 11.5 114  123.62280 5.412281 *
##      7) free.sulfur.dioxide>=11.5 1699 1149.01600 6.403767  
##       14) alcohol< 11.74167 822  542.07300 6.197080 *
##       15) alcohol>=11.74167 877  538.91450 6.597491 *
summary(agacmodeli)
## Call:
## rpart(formula = quality ~ ., data = white)
##   n= 4898 
## 
##           CP nsplit rel error    xerror       xstd
## 1 0.16100651      0 1.0000000 1.0003233 0.02127454
## 2 0.05246918      1 0.8389935 0.8393692 0.02002615
## 3 0.02734202      2 0.7865243 0.7928140 0.01956267
## 4 0.01771117      3 0.7591823 0.7668382 0.01854087
## 5 0.01035548      4 0.7414711 0.7510928 0.01806168
## 6 0.01000000      6 0.7207602 0.7423509 0.01788770
## 
## Variable importance
##              alcohol              density     volatile.acidity 
##                   34                   23                   12 
##            chlorides total.sulfur.dioxide  free.sulfur.dioxide 
##                   11                    8                    5 
##       residual.sugar          citric.acid            sulphates 
##                    5                    1                    1 
##                   pH 
##                    1 
## 
## Node number 1: 4898 observations,    complexity param=0.1610065
##   mean=5.877909, MSE=0.7841955 
##   left son=2 (3085 obs) right son=3 (1813 obs)
##   Primary splits:
##       alcohol              < 10.85    to the left,  improve=0.16100650, (0 missing)
##       density              < 0.992025 to the right, improve=0.11010040, (0 missing)
##       chlorides            < 0.0395   to the right, improve=0.07812748, (0 missing)
##       total.sulfur.dioxide < 158.5    to the right, improve=0.04360317, (0 missing)
##       free.sulfur.dioxide  < 11.75    to the left,  improve=0.03625599, (0 missing)
##   Surrogate splits:
##       density              < 0.992005 to the right, agree=0.865, adj=0.637, (0 split)
##       chlorides            < 0.0375   to the right, agree=0.755, adj=0.339, (0 split)
##       total.sulfur.dioxide < 114.5    to the right, agree=0.692, adj=0.169, (0 split)
##       residual.sugar       < 5.05     to the right, agree=0.667, adj=0.099, (0 split)
##       sulphates            < 0.345    to the right, agree=0.643, adj=0.036, (0 split)
## 
## Node number 2: 3085 observations,    complexity param=0.05246918
##   mean=5.605511, MSE=0.5980247 
##   left son=4 (1610 obs) right son=5 (1475 obs)
##   Primary splits:
##       volatile.acidity    < 0.2525   to the right, improve=0.10923780, (0 missing)
##       free.sulfur.dioxide < 13.5     to the left,  improve=0.03824083, (0 missing)
##       citric.acid         < 0.235    to the left,  improve=0.03184711, (0 missing)
##       alcohol             < 10.11667 to the left,  improve=0.03071881, (0 missing)
##       pH                  < 3.315    to the left,  improve=0.01537558, (0 missing)
##   Surrogate splits:
##       total.sulfur.dioxide < 159.5    to the right, agree=0.596, adj=0.155, (0 split)
##       citric.acid          < 0.265    to the left,  agree=0.582, adj=0.127, (0 split)
##       alcohol              < 9.85     to the left,  agree=0.563, adj=0.086, (0 split)
##       pH                   < 3.295    to the left,  agree=0.561, adj=0.081, (0 split)
##       density              < 0.99423  to the right, agree=0.551, adj=0.060, (0 split)
## 
## Node number 3: 1813 observations,    complexity param=0.02734202
##   mean=6.341423, MSE=0.7598782 
##   left son=6 (114 obs) right son=7 (1699 obs)
##   Primary splits:
##       free.sulfur.dioxide  < 11.5     to the left,  improve=0.07623107, (0 missing)
##       alcohol              < 11.74167 to the left,  improve=0.06019267, (0 missing)
##       total.sulfur.dioxide < 67.5     to the left,  improve=0.03123416, (0 missing)
##       fixed.acidity        < 7.35     to the right, improve=0.02877365, (0 missing)
##       residual.sugar       < 1.275    to the left,  improve=0.02534408, (0 missing)
##   Surrogate splits:
##       total.sulfur.dioxide < 53.5     to the left,  agree=0.946, adj=0.149, (0 split)
##       volatile.acidity     < 0.875    to the right, agree=0.938, adj=0.018, (0 split)
## 
## Node number 4: 1610 observations
##   mean=5.36087, MSE=0.4815744 
## 
## Node number 5: 1475 observations,    complexity param=0.01035548
##   mean=5.872542, MSE=0.5885003 
##   left son=10 (744 obs) right son=11 (731 obs)
##   Primary splits:
##       volatile.acidity    < 0.2075   to the right, improve=0.04363035, (0 missing)
##       free.sulfur.dioxide < 13.5     to the left,  improve=0.02336004, (0 missing)
##       fixed.acidity       < 9.1      to the right, improve=0.01926279, (0 missing)
##       alcohol             < 10.11667 to the left,  improve=0.01690201, (0 missing)
##       residual.sugar      < 12.525   to the left,  improve=0.01643145, (0 missing)
##   Surrogate splits:
##       residual.sugar       < 6.75     to the right, agree=0.612, adj=0.218, (0 split)
##       density              < 0.99533  to the right, agree=0.589, adj=0.171, (0 split)
##       total.sulfur.dioxide < 131.5    to the right, agree=0.586, adj=0.166, (0 split)
##       free.sulfur.dioxide  < 34.5     to the right, agree=0.579, adj=0.150, (0 split)
##       alcohol              < 9.775    to the left,  agree=0.572, adj=0.135, (0 split)
## 
## Node number 6: 114 observations
##   mean=5.412281, MSE=1.084411 
## 
## Node number 7: 1699 observations,    complexity param=0.01771117
##   mean=6.403767, MSE=0.6762895 
##   left son=14 (822 obs) right son=15 (877 obs)
##   Primary splits:
##       alcohol       < 11.74167 to the left,  improve=0.05920581, (0 missing)
##       fixed.acidity < 7.35     to the right, improve=0.02821988, (0 missing)
##       pH            < 3.245    to the left,  improve=0.02366385, (0 missing)
##       chlorides     < 0.0395   to the right, improve=0.02329787, (0 missing)
##       density       < 0.98989  to the right, improve=0.02191549, (0 missing)
##   Surrogate splits:
##       density              < 0.990795 to the right, agree=0.712, adj=0.405, (0 split)
##       volatile.acidity     < 0.2675   to the left,  agree=0.640, adj=0.255, (0 split)
##       chlorides            < 0.0365   to the right, agree=0.633, adj=0.242, (0 split)
##       total.sulfur.dioxide < 142.5    to the right, agree=0.574, adj=0.119, (0 split)
##       residual.sugar       < 1.675    to the left,  agree=0.573, adj=0.118, (0 split)
## 
## Node number 10: 744 observations
##   mean=5.71371, MSE=0.4973389 
## 
## Node number 11: 731 observations,    complexity param=0.01035548
##   mean=6.0342, MSE=0.6294733 
##   left son=22 (614 obs) right son=23 (117 obs)
##   Primary splits:
##       density             < 0.99788  to the left,  improve=0.09057531, (0 missing)
##       residual.sugar      < 12.575   to the left,  improve=0.08646014, (0 missing)
##       alcohol             < 9.05     to the right, improve=0.06988401, (0 missing)
##       free.sulfur.dioxide < 10.5     to the left,  improve=0.04391815, (0 missing)
##       fixed.acidity       < 8.45     to the right, improve=0.03138537, (0 missing)
##   Surrogate splits:
##       residual.sugar < 12.75    to the left,  agree=0.933, adj=0.581, (0 split)
##       alcohol        < 9.15     to the right, agree=0.910, adj=0.436, (0 split)
## 
## Node number 14: 822 observations
##   mean=6.19708, MSE=0.6594562 
## 
## Node number 15: 877 observations
##   mean=6.597491, MSE=0.6144977 
## 
## Node number 22: 614 observations
##   mean=5.929967, MSE=0.55047 
## 
## Node number 23: 117 observations
##   mean=6.581197, MSE=0.6878516

Aynı belirlemeyi bar grafik cinsinden elde edelim:

#burada degiskenlerin onemini gosteriyor
barplot(agacmodeli$variable.importance)

Bu kez karar ağacımız ile kaliteyi etkileyen faktörler için parametre etkilerini görelim:

#gorsel olarak agac
rpart.plot(agacmodeli)

Son olarak bazı değişkenlerimizin birbiri ile korele olabileceğini düşündük. Bu nedenle her değişkenin kendi içindeki korelasyonuna bakıp değişken sayısını düşürebiliriz.

#korelasyon matrisi
cor(white)
##                      fixed.acidity volatile.acidity  citric.acid
## fixed.acidity           1.00000000      -0.02269729  0.289180698
## volatile.acidity       -0.02269729       1.00000000 -0.149471811
## citric.acid             0.28918070      -0.14947181  1.000000000
## residual.sugar          0.08902070       0.06428606  0.094211624
## chlorides               0.02308564       0.07051157  0.114364448
## free.sulfur.dioxide    -0.04939586      -0.09701194  0.094077221
## total.sulfur.dioxide    0.09106976       0.08926050  0.121130798
## density                 0.26533101       0.02711385  0.149502571
## pH                     -0.42585829      -0.03191537 -0.163748211
## sulphates              -0.01714299      -0.03572815  0.062330940
## alcohol                -0.12088112       0.06771794 -0.075728730
## quality                -0.11366283      -0.19472297 -0.009209091
##                      residual.sugar   chlorides free.sulfur.dioxide
## fixed.acidity            0.08902070  0.02308564       -0.0493958591
## volatile.acidity         0.06428606  0.07051157       -0.0970119393
## citric.acid              0.09421162  0.11436445        0.0940772210
## residual.sugar           1.00000000  0.08868454        0.2990983537
## chlorides                0.08868454  1.00000000        0.1013923521
## free.sulfur.dioxide      0.29909835  0.10139235        1.0000000000
## total.sulfur.dioxide     0.40143931  0.19891030        0.6155009650
## density                  0.83896645  0.25721132        0.2942104109
## pH                      -0.19413345 -0.09043946       -0.0006177961
## sulphates               -0.02666437  0.01676288        0.0592172458
## alcohol                 -0.45063122 -0.36018871       -0.2501039415
## quality                 -0.09757683 -0.20993441        0.0081580671
##                      total.sulfur.dioxide     density            pH
## fixed.acidity                 0.091069756  0.26533101 -0.4258582910
## volatile.acidity              0.089260504  0.02711385 -0.0319153683
## citric.acid                   0.121130798  0.14950257 -0.1637482114
## residual.sugar                0.401439311  0.83896645 -0.1941334540
## chlorides                     0.198910300  0.25721132 -0.0904394560
## free.sulfur.dioxide           0.615500965  0.29421041 -0.0006177961
## total.sulfur.dioxide          1.000000000  0.52988132  0.0023209718
## density                       0.529881324  1.00000000 -0.0935914935
## pH                            0.002320972 -0.09359149  1.0000000000
## sulphates                     0.134562367  0.07449315  0.1559514973
## alcohol                      -0.448892102 -0.78013762  0.1214320987
## quality                      -0.174737218 -0.30712331  0.0994272457
##                        sulphates     alcohol      quality
## fixed.acidity        -0.01714299 -0.12088112 -0.113662831
## volatile.acidity     -0.03572815  0.06771794 -0.194722969
## citric.acid           0.06233094 -0.07572873 -0.009209091
## residual.sugar       -0.02666437 -0.45063122 -0.097576829
## chlorides             0.01676288 -0.36018871 -0.209934411
## free.sulfur.dioxide   0.05921725 -0.25010394  0.008158067
## total.sulfur.dioxide  0.13456237 -0.44889210 -0.174737218
## density               0.07449315 -0.78013762 -0.307123313
## pH                    0.15595150  0.12143210  0.099427246
## sulphates             1.00000000 -0.01743277  0.053677877
## alcohol              -0.01743277  1.00000000  0.435574715
## quality               0.05367788  0.43557472  1.000000000
#korelasyon matrisi
cor_White = 
    as.data.frame(cor(white)) %>% mutate(val1=rownames(.)) %>% gather(key=val2,value=correlation,-val1) %>% filter(abs(correlation) > 0.5 & abs(correlation) < 1) %>% arrange(desc(abs(correlation)))

Analiz bize yoğunluk ve toplam kükürt verilerinin şeker ve serbest kükürt verileri ile korele olduğunu gösterdi. Bu durumda analiz verilerimizde yoğunluk ve serbest kükürtü kullanmamalıyız.

#korelasyon matrisi
library(tidyverse)
        mutate_white<-white %>%
  select(-density,-free.sulfur.dioxide)
library(rpart)
library(rpart.plot)

agacmodelim=rpart(quality~.,mutate_white)
agacmodelim
## n= 4898 
## 
## node), split, n, deviance, yval
##       * denotes terminal node
## 
##  1) root 4898 3840.99000 5.877909  
##    2) alcohol< 10.85 3085 1844.90600 5.605511  
##      4) volatile.acidity>=0.2525 1610  775.33480 5.360870 *
##      5) volatile.acidity< 0.2525 1475  868.03800 5.872542  
##       10) volatile.acidity>=0.2075 744  370.02020 5.713710 *
##       11) volatile.acidity< 0.2075 731  460.14500 6.034200  
##         22) residual.sugar< 12.575 581  326.86750 5.915663 *
##         23) residual.sugar>=12.575 150   93.49333 6.493333 *
##    3) alcohol>=10.85 1813 1377.65900 6.341423  
##      6) alcohol< 11.74167 881  670.00450 6.121453  
##       12) volatile.acidity>=0.465 16   13.93750 4.562500 *
##       13) volatile.acidity< 0.465 865  616.46240 6.150289 *
##      7) alcohol>=11.74167 932  624.72960 6.549356 *
summary(agacmodelim)
## Call:
## rpart(formula = quality ~ ., data = mutate_white)
##   n= 4898 
## 
##           CP nsplit rel error    xerror       xstd
## 1 0.16100651      0 1.0000000 1.0002889 0.02127153
## 2 0.05246918      1 0.8389935 0.8427783 0.02008410
## 3 0.02158948      2 0.7865243 0.7968718 0.01960356
## 4 0.01031104      3 0.7649348 0.7747400 0.01895395
## 5 0.01010898      4 0.7546238 0.7673959 0.01881474
## 6 0.01000000      6 0.7344058 0.7662752 0.01879213
## 
## Variable importance
##              alcohol     volatile.acidity            chlorides 
##                   44                   18                   14 
## total.sulfur.dioxide       residual.sugar            sulphates 
##                    9                    7                    2 
##          citric.acid                   pH        fixed.acidity 
##                    2                    1                    1 
## 
## Node number 1: 4898 observations,    complexity param=0.1610065
##   mean=5.877909, MSE=0.7841955 
##   left son=2 (3085 obs) right son=3 (1813 obs)
##   Primary splits:
##       alcohol              < 10.85    to the left,  improve=0.16100650, (0 missing)
##       chlorides            < 0.0395   to the right, improve=0.07812748, (0 missing)
##       total.sulfur.dioxide < 158.5    to the right, improve=0.04360317, (0 missing)
##       citric.acid          < 0.235    to the left,  improve=0.03463660, (0 missing)
##       volatile.acidity     < 0.2275   to the right, improve=0.02876092, (0 missing)
##   Surrogate splits:
##       chlorides            < 0.0375   to the right, agree=0.755, adj=0.339, (0 split)
##       total.sulfur.dioxide < 114.5    to the right, agree=0.692, adj=0.169, (0 split)
##       residual.sugar       < 5.05     to the right, agree=0.667, adj=0.099, (0 split)
##       sulphates            < 0.345    to the right, agree=0.643, adj=0.036, (0 split)
##       fixed.acidity        < 5.45     to the right, agree=0.638, adj=0.021, (0 split)
## 
## Node number 2: 3085 observations,    complexity param=0.05246918
##   mean=5.605511, MSE=0.5980247 
##   left son=4 (1610 obs) right son=5 (1475 obs)
##   Primary splits:
##       volatile.acidity     < 0.2525   to the right, improve=0.10923780, (0 missing)
##       citric.acid          < 0.235    to the left,  improve=0.03184711, (0 missing)
##       alcohol              < 10.11667 to the left,  improve=0.03071881, (0 missing)
##       pH                   < 3.315    to the left,  improve=0.01537558, (0 missing)
##       total.sulfur.dioxide < 172.5    to the right, improve=0.01385645, (0 missing)
##   Surrogate splits:
##       total.sulfur.dioxide < 159.5    to the right, agree=0.596, adj=0.155, (0 split)
##       citric.acid          < 0.265    to the left,  agree=0.582, adj=0.127, (0 split)
##       alcohol              < 9.85     to the left,  agree=0.563, adj=0.086, (0 split)
##       pH                   < 3.295    to the left,  agree=0.561, adj=0.081, (0 split)
##       sulphates            < 0.675    to the left,  agree=0.548, adj=0.056, (0 split)
## 
## Node number 3: 1813 observations,    complexity param=0.02158948
##   mean=6.341423, MSE=0.7598782 
##   left son=6 (881 obs) right son=7 (932 obs)
##   Primary splits:
##       alcohol              < 11.74167 to the left,  improve=0.06019267, (0 missing)
##       total.sulfur.dioxide < 67.5     to the left,  improve=0.03123416, (0 missing)
##       fixed.acidity        < 7.35     to the right, improve=0.02877365, (0 missing)
##       residual.sugar       < 1.275    to the left,  improve=0.02534408, (0 missing)
##       pH                   < 3.245    to the left,  improve=0.01926295, (0 missing)
##   Surrogate splits:
##       volatile.acidity     < 0.2675   to the left,  agree=0.633, adj=0.244, (0 split)
##       chlorides            < 0.0365   to the right, agree=0.633, adj=0.244, (0 split)
##       residual.sugar       < 1.675    to the left,  agree=0.572, adj=0.119, (0 split)
##       total.sulfur.dioxide < 128.5    to the right, agree=0.571, adj=0.117, (0 split)
##       fixed.acidity        < 6.85     to the right, agree=0.546, adj=0.065, (0 split)
## 
## Node number 4: 1610 observations
##   mean=5.36087, MSE=0.4815744 
## 
## Node number 5: 1475 observations,    complexity param=0.01010898
##   mean=5.872542, MSE=0.5885003 
##   left son=10 (744 obs) right son=11 (731 obs)
##   Primary splits:
##       volatile.acidity < 0.2075   to the right, improve=0.04363035, (0 missing)
##       fixed.acidity    < 9.1      to the right, improve=0.01926279, (0 missing)
##       alcohol          < 10.11667 to the left,  improve=0.01690201, (0 missing)
##       residual.sugar   < 12.525   to the left,  improve=0.01643145, (0 missing)
##       citric.acid      < 0.265    to the left,  improve=0.01336636, (0 missing)
##   Surrogate splits:
##       residual.sugar       < 6.75     to the right, agree=0.612, adj=0.218, (0 split)
##       total.sulfur.dioxide < 131.5    to the right, agree=0.586, adj=0.166, (0 split)
##       alcohol              < 9.775    to the left,  agree=0.572, adj=0.135, (0 split)
##       pH                   < 3.255    to the left,  agree=0.570, adj=0.133, (0 split)
##       fixed.acidity        < 6.85     to the left,  agree=0.551, adj=0.093, (0 split)
## 
## Node number 6: 881 observations,    complexity param=0.01031104
##   mean=6.121453, MSE=0.7605046 
##   left son=12 (16 obs) right son=13 (865 obs)
##   Primary splits:
##       volatile.acidity     < 0.465    to the right, improve=0.05911096, (0 missing)
##       citric.acid          < 0.235    to the left,  improve=0.03617176, (0 missing)
##       total.sulfur.dioxide < 67.5     to the left,  improve=0.03361096, (0 missing)
##       residual.sugar       < 0.975    to the left,  improve=0.03310966, (0 missing)
##       fixed.acidity        < 7.05     to the right, improve=0.02851510, (0 missing)
##   Surrogate splits:
##       citric.acid          < 0.065    to the left,  agree=0.984, adj=0.125, (0 split)
##       total.sulfur.dioxide < 279.25   to the right, agree=0.984, adj=0.125, (0 split)
##       residual.sugar       < 0.85     to the left,  agree=0.983, adj=0.063, (0 split)
## 
## Node number 7: 932 observations
##   mean=6.549356, MSE=0.6703107 
## 
## Node number 10: 744 observations
##   mean=5.71371, MSE=0.4973389 
## 
## Node number 11: 731 observations,    complexity param=0.01010898
##   mean=6.0342, MSE=0.6294733 
##   left son=22 (581 obs) right son=23 (150 obs)
##   Primary splits:
##       residual.sugar       < 12.575   to the left,  improve=0.08646014, (0 missing)
##       alcohol              < 9.05     to the right, improve=0.06988401, (0 missing)
##       fixed.acidity        < 8.45     to the right, improve=0.03138537, (0 missing)
##       chlorides            < 0.0555   to the left,  improve=0.02805891, (0 missing)
##       total.sulfur.dioxide < 56.5     to the left,  improve=0.02700982, (0 missing)
##   Surrogate splits:
##       alcohol < 9.15     to the right, agree=0.845, adj=0.247, (0 split)
##       pH      < 2.965    to the right, agree=0.803, adj=0.040, (0 split)
## 
## Node number 12: 16 observations
##   mean=4.5625, MSE=0.8710938 
## 
## Node number 13: 865 observations
##   mean=6.150289, MSE=0.7126733 
## 
## Node number 22: 581 observations
##   mean=5.915663, MSE=0.5625946 
## 
## Node number 23: 150 observations
##   mean=6.493333, MSE=0.6232889
rpart.plot(agacmodelim)

barplot(agacmodelim$variable.importance)

Son olarak korele verileri elimine ederek test yaptık. Bu test sonucuna göre; Kalite için alkol miktarı en önemli değişkendir-ne kaadr ekmek o kadar köfte diyelim

Üst düzeyde kalite etkenlerinden diğerleri asit, klorit ve kükürttür yani boğazı yakan şarap kalite olarak iyisidir…

Berk Orbay ve Mustafa Gökçe Baydoğan Hocalarımıza paylaştıkları bu değerli bilgiler, emekleri ve yardımları için teşekkür eder; organizasyonda emeği geçenlere şükranlarımızı sunarız.

Alkol dostunuz değil; düşmanınız da… ;}

COMETOBEŞİKTAŞ