Załadowanie potrzebnych bibliotek:
library(e1071) # do kurtozy
library(dplyr) # do lepszego zarzadzania danymi
library(ggplot2) # pakiet graficzny
library(ggcorrplot) # do korelacji
library(knitr) # tabelki
library(DT) # tabelki
Wczytanie zbioru danych i podglądnięcie tabeli z danymi (interaktywna tabela):
danewina <- read.csv2("winequality.csv", header= T)
datatable(danewina, width = "100%", options=list(scrollX = T))
Objaśnienie 15 zmiennych:
region - regiony włoskie
color - kolor wina
fixed.acidity - kwasowość pierwotna wina
volatile.acidity - kwasowość powstała podczas procesu dojrzewania
citric.acid - stężenie kwasu cytrynowego
residual.sugar - zawartość cukru pozostała po procesie fermentacji
chlorides – zawartość chlorku sodu
free.sulfur.dioxide - zawartość wolnego dwutlenku siarki
total.sulfur.dioxide - całkowita zawartość dwutlenku siarki
density – gęstość wina
pH – odczyn wina wyrażony przez poziom pH
sulphates - zawartość siarczanu potasu
sulphates.after.filtering - zawartość siarczanu potasu po filtracji
alcohol - procentowa zawartość alkoholu
quality - wynik oceny eksperckiej wina (w skali 0-10)
Analiza została przeprowadzona na podstawie zadań.
Lista zadań:
Wybrane zmienne:
pH - odczyn wina wyrażony przez poziom pH
alcohol - procentowa zawartość alkoholu
chlorides - zawartość chlorku sodu
quality - wynik oceny ekspertów od wina (w skali 0-10)
citric.acid - stężenie kwasu cytrynowego
Catania_Wine <- danewina[danewina$region == "Catania", ]
Bolonia_Wine <- danewina[danewina$region == "Bolonia", ]
EmiliaRomana_Wine <- danewina[danewina$region == "Emilia Romana", ]
Funkcja do wyboru regionu, interesującej nas zmiennej i wartości statystycznej (do agregacji):
aggregateFunction <- function(x,y,z){
aggregate(list("value"= x), list(Color=y), FUN= z)
}
Wyświetlenie funkcji dla wartości pH. W dalszych agregacjach zmienia się tylko odpowiednie parametry.
(dlatego nie są wyświetlone kody do nich - dla przejrzystości raportu)
# srednia
aggregateFunction(Bolonia_Wine$pH, Bolonia_Wine$color, mean)
aggregateFunction(Catania_Wine$pH, Catania_Wine$color, mean)
aggregateFunction(EmiliaRomana_Wine$pH, EmiliaRomana_Wine$color, mean)
# mediana
aggregateFunction(Bolonia_Wine$pH, Bolonia_Wine$color, median)
aggregateFunction(Catania_Wine$pH, Catania_Wine$color, median)
aggregateFunction(EmiliaRomana_Wine$pH, EmiliaRomana_Wine$color, median)
# kwantyle
aggregateFunction(Bolonia_Wine$pH, Bolonia_Wine$color, quantile)
aggregateFunction(Catania_Wine$pH, Catania_Wine$color, quantile)
aggregateFunction(EmiliaRomana_Wine$pH, EmiliaRomana_Wine$color, quantile)
# wartosc min
aggregateFunction(Bolonia_Wine$pH, Bolonia_Wine$color, min)
aggregateFunction(Catania_Wine$pH, Catania_Wine$color, min)
aggregateFunction(EmiliaRomana_Wine$pH, EmiliaRomana_Wine$color, min)
# wartosc max
aggregateFunction(Bolonia_Wine$pH, Bolonia_Wine$color, max)
aggregateFunction(Catania_Wine$pH, Catania_Wine$color, max)
aggregateFunction(EmiliaRomana_Wine$pH, EmiliaRomana_Wine$color, max)
# odchylenie standardowe
aggregateFunction(Bolonia_Wine$pH, Bolonia_Wine$color, sd)
aggregateFunction(Catania_Wine$pH, Catania_Wine$color, sd)
aggregateFunction(EmiliaRomana_Wine$pH, EmiliaRomana_Wine$color, sd)
# kurtoza
aggregateFunction(Bolonia_Wine$pH, Bolonia_Wine$color, kurtosis)
aggregateFunction(Catania_Wine$pH, Catania_Wine$color, kurtosis)
aggregateFunction(EmiliaRomana_Wine$pH, EmiliaRomana_Wine$color, kurtosis)
# skosnosc - trzeci moment centralny
aggregateFunction(Bolonia_Wine$pH, Bolonia_Wine$color, skewness)
aggregateFunction(Catania_Wine$pH, Catania_Wine$color, skewness)
aggregateFunction(EmiliaRomana_Wine$pH, EmiliaRomana_Wine$color, skewness)
| Kolor | Srednia | Mediana | 1.Kwartyl | 3.Kwartyl | Min | Max | OdchStand | Kurtoza | Skosnosc |
|---|---|---|---|---|---|---|---|---|---|
| red | 3.312410 | 3.31 | 3.21 | 3.4 | 2.74 | 3.90 | 0.1514665 | 0.4221712 | 0.1145628 |
| white | 3.207308 | 3.20 | 3.10 | 3.3 | 2.77 | 3.75 | 0.1512020 | 0.2374281 | 0.2953784 |
| Kolor | Srednia | Mediana | 1.Kwartyl | 3.Kwartyl | Min | Max | OdchStand | Kurtoza | Skosnosc |
|---|---|---|---|---|---|---|---|---|---|
| red | 3.290805 | 3.30 | 3.20 | 3.38 | 2.98 | 3.58 | 0.1258295 | -0.4019733 | -0.1346111 |
| white | 3.167300 | 3.16 | 3.08 | 3.25 | 2.72 | 3.82 | 0.1417800 | 0.6259314 | 0.4638217 |
| Kolor | Srednia | Mediana | 1.Kwartyl | 3.Kwartyl | Min | Max | OdchStand | Kurtoza | Skosnosc |
|---|---|---|---|---|---|---|---|---|---|
| red | 3.311880 | 3.32 | 3.2 | 3.41 | 2.86 | 4.01 | 0.1689181 | 1.415209 | 0.3745481 |
| white | 3.215596 | 3.21 | 3.1 | 3.32 | 2.74 | 3.81 | 0.1617499 | 0.432570 | 0.4339952 |
| Kolor | Srednia | Mediana | 1.Kwartyl | 3.Kwartyl | Min | Max | OdchStand | Kurtoza | Skosnosc |
|---|---|---|---|---|---|---|---|---|---|
| red | 10.45844 | 10.2 | 9.5 | 11.1 | 8.4 | 14 | 1.069283 | 0.0067919 | 0.8055947 |
| white | 11.30525 | 11.3 | 10.4 | 12.3 | 8.4 | 14 | 1.231523 | -0.9049161 | -0.1235458 |
| Kolor | Srednia | Mediana | 1.Kwartyl | 3.Kwartyl | Min | Max | OdchStand | Kurtoza | Skosnosc |
|---|---|---|---|---|---|---|---|---|---|
| red | 10.59502 | 10.5 | 9.533333 | 11.2 | 8.8 | 14.90 | 1.191275 | 0.7035332 | 0.8269375 |
| white | 10.06764 | 9.7 | 9.200000 | 10.7 | 8.0 | 14.05 | 1.116616 | 0.1767042 | 0.9679575 |
| Kolor | Srednia | Mediana | 1.Kwartyl | 3.Kwartyl | Min | Max | OdchStand | Kurtoza | Skosnosc |
|---|---|---|---|---|---|---|---|---|---|
| red | 10.27157 | 10.0 | 9.5 | 10.90 | 8.4 | 14.0 | 1.008544 | 0.5197576 | 1.0122887 |
| white | 10.82942 | 10.8 | 10.0 | 11.45 | 8.0 | 14.2 | 1.055224 | -0.4657661 | 0.2799252 |
| Kolor | Srednia | Mediana | 1.Kwartyl | 3.Kwartyl | Min | Max | OdchStand | Kurtoza | Skosnosc |
|---|---|---|---|---|---|---|---|---|---|
| red | 0.0860349 | 0.080 | 0.072 | 0.091 | 0.012 | 0.61 | 0.037512 | 61.78649 | 6.505329 |
| white | 0.0427297 | 0.039 | 0.033 | 0.047 | 0.012 | 0.24 | 0.021442 | 31.14294 | 4.837587 |
| Kolor | Srednia | Mediana | 1.Kwartyl | 3.Kwartyl | Min | Max | OdchStand | Kurtoza | Skosnosc |
|---|---|---|---|---|---|---|---|---|---|
| red | 0.0912069 | 0.080 | 0.071 | 0.098 | 0.050 | 0.235 | 0.0371327 | 4.755622 | 2.158151 |
| white | 0.0471888 | 0.045 | 0.038 | 0.052 | 0.014 | 0.346 | 0.0205159 | 55.725233 | 5.929303 |
| Kolor | Srednia | Mediana | 1.Kwartyl | 3.Kwartyl | Min | Max | OdchStand | Kurtoza | Skosnosc |
|---|---|---|---|---|---|---|---|---|---|
| red | 0.0910463 | 0.075 | 0.066 | 0.084 | 0.034 | 0.611 | 0.0701904 | 18.95325 | 4.164993 |
| white | 0.0451429 | 0.040 | 0.034 | 0.048 | 0.009 | 0.271 | 0.0242614 | 20.99898 | 4.088909 |
| Kolor | Srednia | Mediana | 1.Kwartyl | 3.Kwartyl | Min | Max | OdchStand | Kurtoza | Skosnosc |
|---|---|---|---|---|---|---|---|---|---|
| red | 5.636681 | 6 | 5 | 6 | 3 | 8 | 0.7893851 | 0.2354520 | 0.1844736 |
| white | 6.157051 | 6 | 6 | 7 | 3 | 9 | 0.9511679 | 0.2836353 | -0.1827279 |
| Kolor | Srednia | Mediana | 1.Kwartyl | 3.Kwartyl | Min | Max | OdchStand | Kurtoza | Skosnosc |
|---|---|---|---|---|---|---|---|---|---|
| red | 5.724138 | 6 | 5 | 6 | 3 | 8 | 0.9607368 | -0.3533579 | 0.1752395 |
| white | 5.797396 | 6 | 5 | 6 | 3 | 9 | 0.8350149 | 0.3809388 | 0.3386658 |
| Kolor | Srednia | Mediana | 1.Kwartyl | 3.Kwartyl | Min | Max | OdchStand | Kurtoza | Skosnosc |
|---|---|---|---|---|---|---|---|---|---|
| red | 5.613079 | 6 | 5 | 6 | 3 | 8 | 0.8249147 | 0.5157174 | 0.2929675 |
| white | 5.840118 | 6 | 5 | 6 | 3 | 9 | 0.8958611 | 0.1316467 | -0.0028826 |
| Kolor | Srednia | Mediana | 1.Kwartyl | 3.Kwartyl | Min | Max | OdchStand | Kurtoza | Skosnosc |
|---|---|---|---|---|---|---|---|---|---|
| red | 0.2732838 | 0.26 | 0.10 | 0.43 | 0 | 1.00 | 0.1952662 | -0.7859698 | 0.3156556 |
| white | 0.3178312 | 0.30 | 0.27 | 0.36 | 0 | 1.66 | 0.1070923 | 32.8351815 | 3.2992694 |
| Kolor | Srednia | Mediana | 1.Kwartyl | 3.Kwartyl | Min | Max | OdchStand | Kurtoza | Skosnosc |
|---|---|---|---|---|---|---|---|---|---|
| red | 0.3372414 | 0.34 | 0.19 | 0.455 | 0 | 0.78 | 0.2038564 | -0.7173157 | 0.2591322 |
| white | 0.3402604 | 0.31 | 0.26 | 0.400 | 0 | 1.00 | 0.1232746 | 2.5833781 | 1.1082348 |
| Kolor | Srednia | Mediana | 1.Kwartyl | 3.Kwartyl | Min | Max | OdchStand | Kurtoza | Skosnosc |
|---|---|---|---|---|---|---|---|---|---|
| red | 0.2480654 | 0.23 | 0.07 | 0.40 | 0 | 0.76 | 0.1874908 | -1.001612 | 0.2995886 |
| white | 0.3337972 | 0.32 | 0.27 | 0.39 | 0 | 1.00 | 0.1246834 | 2.948669 | 0.6647969 |
### Region - Bolonia ----
par(oma = c(2,1,1,1), mfrow = c(2, 2), mar = c(2, 2, 1, 1))
for (i in 1:4){
boxplot(Bolonia_Wine$pH ~ Bolonia_Wine$color, col=c("purple", "green"), main = "pH Value")
boxplot(Bolonia_Wine$alcohol ~ Bolonia_Wine$color, col=c("purple", "green"), main = "Alcohol Value")
boxplot(Bolonia_Wine$quality ~ Bolonia_Wine$color, col=c("purple", "green"), main = "Quality Value")
boxplot(Bolonia_Wine$citric.acid ~ Bolonia_Wine$color, col=c("purple", "green"), main = "Citric Acid Value")
}
par(fig = c(0, 1, 0, 1), oma = c(0, 0, 0, 0), mar = c(0, 0, 0, 0), new = TRUE)
plot(0, 0, type = 'l', main ="\n\nRegion - Bolonia", col.main="red", cex.main=1.3)
legend( x ="bottom", legend = c("Wino Czerwone","Wino Białe"), col=c("purple", "green"),
lty=c(1,1), lwd=3, cex=0.9, horiz=T, bty = "o")
### Region - Catania ----
par(oma = c(2,1,1,1), mfrow = c(2, 2), mar = c(2, 2, 1, 1))
for (i in 1:4){
boxplot(Catania_Wine$pH ~ Catania_Wine$color, col=c("orange", "blue"), main = "pH Value")
boxplot(Catania_Wine$alcohol ~ Catania_Wine$color, col=c("orange", "blue"), main = "Alcohol Value")
boxplot(Catania_Wine$quality ~ Catania_Wine$color, col=c("orange", "blue"), main = "Quality Value")
boxplot(Catania_Wine$citric.acid ~ Catania_Wine$color, col=c("orange", "blue"), main = "Citric Acid Value")
}
par(fig = c(0, 1, 0, 1), oma = c(0, 0, 0, 0), mar = c(0, 0, 0, 0), new = TRUE)
plot(0, 0, type = 'l', main ="\n\nRegion - Catania", col.main="red", cex.main=1.3)
legend( x ="bottom", legend = c("Wino Czerwone","Wino Białe"), col=c("orange", "blue"),
lty=c(1,1), lwd=3, cex=0.9, horiz=T, bty = "o")
### Region - Emilia Romana ----
par(oma = c(2,1,1,1), mfrow = c(2, 2), mar = c(2, 2, 1, 1))
for (i in 1:4){
boxplot(EmiliaRomana_Wine$pH ~ EmiliaRomana_Wine$color, col=c("brown", "yellow"), main = "pH Value")
boxplot(EmiliaRomana_Wine$alcohol ~ EmiliaRomana_Wine$color, col=c("brown", "yellow"), main = "Alcohol Value")
boxplot(EmiliaRomana_Wine$quality ~ EmiliaRomana_Wine$color, col=c("brown", "yellow"), main = "Quality Value")
boxplot(EmiliaRomana_Wine$citric.acid ~ EmiliaRomana_Wine$color, col=c("brown", "yellow"), main = "Citric Acid Value")
}
par(fig = c(0, 1, 0, 1), oma = c(0, 0, 0, 0), mar = c(0, 0, 0, 0), new = TRUE)
plot(0, 0, type = 'l', main ="\n\nRegion - Emilia Romana", col.main="red", cex.main=1.3)
legend( x ="bottom", legend = c("Wino Czerwone","Wino Białe"), col=c("brown", "yellow"),
lty=c(1,1), lwd=3, cex=0.9, horiz=T, bty = "o")
par(oma = c(2,1,1,1), mfrow = c(2, 2), mar = c(2, 2, 1, 1))
for (i in 1:4){
boxplot(danewina$pH ~ danewina$region, col=c("mediumvioletred", "gold1", "cadetblue2"), main = "pH Value", xlab="Color", ylab= "pH value")
boxplot(danewina$alcohol ~ danewina$region, col=c("mediumvioletred", "gold1", "cadetblue2"), main = "Alcohol Value", xlab="Color", ylab= "Chlorides value")
boxplot(danewina$sulphates ~ danewina$region, col=c("mediumvioletred", "gold1", "cadetblue2"), main = "Sulphates Value", xlab="Color", ylab= "Quality value")
boxplot(danewina$citric.acid ~ danewina$region, col=c("mediumvioletred", "gold1", "cadetblue2"), main = "Citric Acid Value", xlab="Color", ylab= "Citric Acid value")
}
par(mfrow=c(1,1))
plot(pH ~ sulphates, data = danewina[danewina$color == "white", ], col= "darkgrey",
ylim=range(danewina$pH), xlim=range(danewina$sulphates), pch= 1,
main = "Scatter Plot - pH ~ Sulphates")
points(pH ~ sulphates, data = danewina[danewina$color == "red", ], col= "red", pch= 2)
grid()
legend( x ="topright", legend = c("White Wine","Red Wine"),
title ="Type of Wine: ", box.lty = 3,
col=c("darkgrey","red"), lty=c(1,1),
lwd=3, cex=0.9, horiz=F, bty = "o")
par(mfrow=c(1,1))
plot(pH ~ alcohol, data = danewina[danewina$color == "white", ], col= "darkgrey",
ylim=range(danewina$pH), xlim=range(danewina$alcohol), pch= 1,
main = "Scatter Plot - pH ~ Alcohol")
points(pH ~ alcohol, data = danewina[danewina$color == "red", ], col= "red", pch= 2)
grid()
legend( x ="topright", legend = c("White Wine","Red Wine"),
title ="Type of Wine: ", box.lty = 3,
col=c("darkgrey","red"), lty=c(1,1),
lwd=3, cex=0.9, horiz=F, bty = "o")
par(mfrow=c(1,1))
plot(chlorides ~ sulphates, data = danewina[danewina$region == "Bolonia", ], col= "red",
ylim=range(danewina$chlorides), xlim=range(danewina$sulphates), pch= 1,
main = "Scatter Plot - Chlorides ~ Sulphates")
points(chlorides ~ sulphates, data = danewina[danewina$region == "Catania", ], col= "blue", pch= 2)
points(chlorides ~ sulphates, data = danewina[danewina$region == "Emilia Romana", ], col= "green", pch= 3)
grid()
legend( x ="topleft", legend = c("Bolonia","Catania", "Emilia Romana"),
title ="Region: ", box.lty = 2,
col=c("red","blue", "green"), lty=c(1,1,1),
lwd=3, cex=0.9, horiz=F, bty = "o")
par(mfrow=c(1,1))
plot(citric.acid ~ alcohol, data = danewina[danewina$region == "Bolonia", ], col= "red",
ylim=range(danewina$citric.acid), xlim=range(danewina$alcohol), pch= 1,
main = "Scatter Plot - Citric.acid ~ Alcohol")
points(citric.acid ~ alcohol, data = danewina[danewina$region == "Catania", ], col= "blue", pch= 2)
points(citric.acid ~ alcohol, data = danewina[danewina$region == "Emilia Romana", ], col= "green", pch= 3)
grid()
legend( x ="topleft", legend = c("Bolonia","Catania", "Emilia Romana"),
title ="Region: ", box.lty = 2,
col=c("red","blue", "green"), lty=c(1,1,1),
lwd=3, cex=0.9, horiz=F, bty = "o")
### test dla sredniej ----
# dla ogolnych danych
wines_ttest <- t.test(x = danewina$pH, y= danewina$citric.acid, conf.level = 0.95)
# dla regionu Bolonia
winesB_ttest <- t.test(x = Bolonia_Wine$pH, y= Bolonia_Wine$citric.acid, conf.level = 0.95)
# dla regionu Catania
winesC_ttest <- t.test(x = Catania_Wine$pH, y= Catania_Wine$citric.acid, conf.level = 0.95)
# dla regionu Emilia Romana
winesER_ttest <- t.test(x = EmiliaRomana_Wine$pH, y= EmiliaRomana_Wine$citric.acid,conf.level = 0.95)
# Ramka danych dla przedzialu wariancji
meanintervaldf <- data.frame( c("Ogółem","Bolonia","Catania","Emilia Romana"),
c(round(wines_ttest$conf.int[1],5), round(winesB_ttest$conf.int[1],5),
round(winesC_ttest$conf.int[1],5), round(winesER_ttest$conf.int[1],5)),
c(round(wines_ttest$conf.int[2],5), round(winesB_ttest$conf.int[2],5),
round(winesC_ttest$conf.int[2],5), round(winesER_ttest$conf.int[2],5)))
names(meanintervaldf) <- c("Region:","Min","Max")
meanintervaldf
## Region: Min Max
## 1 Ogółem 2.89460 2.90514
## 2 Bolonia 2.96199 2.98164
## 3 Catania 2.82391 2.83833
## 4 Emilia Romana 2.91019 2.93117
### test dla wariancji ----
# dla ogolnych danych
wines_vartest <- var.test(x = danewina$pH, y= danewina$citric.acid, conf.level =0.95)
# Dla regionu Bolonia
winesB_vartest <- var.test(x = Bolonia_Wine$pH, y= Bolonia_Wine$citric.acid, conf.level =0.95)
# Dla regionu Catania
winesC_vartest <- var.test(x = Catania_Wine$pH, y= Catania_Wine$citric.acid, conf.level =0.95)
# Dla regionu Emilia Romana
winesER_vartest <- var.test(x = EmiliaRomana_Wine$pH, y= EmiliaRomana_Wine$citric.acid, conf.level =0.95)
# Ramka danych dla przedzialu wariancji
varintervaldf <- data.frame( c("Ogółem","Bolonia","Catania","Emilia Romana"),
c(round(wines_vartest$conf.int[1],5), round(winesB_vartest$conf.int[1],5),
round(winesC_vartest$conf.int[1],5), round(winesER_vartest$conf.int[1],5)),
c(round(wines_vartest$conf.int[2],5), round(winesB_vartest$conf.int[2],5),
round(winesC_vartest$conf.int[2],5), round(winesER_vartest$conf.int[2],5)))
names(varintervaldf) <- c("Region:","Min","Max")
varintervaldf
## Region: Min Max
## 1 Ogółem 1.16611 1.28525
## 2 Bolonia 0.88358 1.04935
## 3 Catania 1.18191 1.37455
## 4 Emilia Romana 1.22491 1.48013
# ogolne dane
t.test(x = danewina$sulphates,
y = danewina$sulphates.after.filtering,
conf.level = 0.95)
##
## Welch Two Sample t-test
##
## data: danewina$sulphates and danewina$sulphates.after.filtering
## t = 21.39, df = 12850, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 0.04825836 0.05799529
## sample estimates:
## mean of x mean of y
## 0.5312683 0.4781414
# wino biale
t.test(x = danewina$sulphates[danewina$color == "white"],
y = danewina$sulphates.after.filtering[danewina$color == "white"],
conf.level = 0.95)
##
## Welch Two Sample t-test
##
## data: danewina$sulphates[danewina$color == "white"] and danewina$sulphates.after.filtering[danewina$color == "white"]
## t = 22.328, df = 9687.3, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 0.04468421 0.05328516
## sample estimates:
## mean of x mean of y
## 0.4898469 0.4408622
# wino czerwone
t.test(x = danewina$sulphates[danewina$color == "red"],
y = danewina$sulphates.after.filtering[danewina$color == "red"],
conf.level = 0.95)
##
## Welch Two Sample t-test
##
## data: danewina$sulphates[danewina$color == "red"] and danewina$sulphates.after.filtering[danewina$color == "red"]
## t = 11.54, df = 3161.2, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 0.05463295 0.07699682
## sample estimates:
## mean of x mean of y
## 0.6581488 0.5923340
Wniosek: We wszystkich przypadkach mamy podstawy do odrzucenia hipotezy zerowej mówiącej że średnia przed filtracją jest równa średniej po filtracji. (Zwracamy uwagę na wartość p. Jest ona mniejsza od 0.05 [przedział ufności ustawiony na 0.95], a więc odrzucamy hipotezę zerową). Oznacza to że średnie każdego z ww. przypadków istotnie różnią się od siebie - a więc również i poziom zmienił się istotnie.
## zmienna Sulphates
t.test(danewina$sulphates[danewina$color == "white"], danewina$sulphates[danewina$color == "red"],
conf.level = 0.95)
##
## Welch Two Sample t-test
##
## data: danewina$sulphates[danewina$color == "white"] and danewina$sulphates[danewina$color == "red"]
## t = -37.056, df = 2091, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.177209 -0.159395
## sample estimates:
## mean of x mean of y
## 0.4898469 0.6581488
## zmienna pH
t.test(danewina$pH[danewina$color == "white"], danewina$pH[danewina$color == "red"],
conf.level = 0.95)
##
## Welch Two Sample t-test
##
## data: danewina$pH[danewina$color == "white"] and danewina$pH[danewina$color == "red"]
## t = -27.775, df = 2667.1, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.1315191 -0.1141740
## sample estimates:
## mean of x mean of y
## 3.188267 3.311113
## zmienna Alcohol
t.test(danewina$alcohol[danewina$color == "white"], danewina$alcohol[danewina$color == "red"],
conf.level = 0.95)
##
## Welch Two Sample t-test
##
## data: danewina$alcohol[danewina$color == "white"] and danewina$alcohol[danewina$color == "red"]
## t = 2.859, df = 3100.5, p-value = 0.004278
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 0.02868117 0.15388669
## sample estimates:
## mean of x mean of y
## 10.51427 10.42298
## zmienna Citric.acid
t.test(x = danewina$citric.acid[danewina$color == "white"], danewina$citric.acid[danewina$color == "red"],
conf.level = 0.95)
##
## Welch Two Sample t-test
##
## data: danewina$citric.acid[danewina$color == "white"] and danewina$citric.acid[danewina$color == "red"]
## t = 12.229, df = 2015.6, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 0.05307807 0.07335372
## sample estimates:
## mean of x mean of y
## 0.3341915 0.2709756
## zmienna Quality
t.test(x = danewina$quality[danewina$color == "white"], danewina$quality[danewina$color == "red"],
conf.level = 0.95)
##
## Welch Two Sample t-test
##
## data: danewina$quality[danewina$color == "white"] and danewina$quality[danewina$color == "red"]
## t = 10.149, df = 2950.8, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 0.1951564 0.2886173
## sample estimates:
## mean of x mean of y
## 5.877909 5.636023
Wniosek: We wszystkich przypadkach mamy podstawy do odrzucenia hipotezy zerowej mowiącej że różnica średnich każdej badanej pary zmiennych wynosi 0 (średnie są sobie równe).
Jak można zauważyć, wartości parametru p są bardzo małe w większości wyników. Wyjątkiem jest test dla zmiennej Alcohol, gdzie wartość p wynosi 0.004278. Jest to jednak dalej wartość mniejsza od 0.05 - a więc również są podstawy do odrzucenia hipotezy zerowej.
## Zmienna Quality
var.test(danewina$quality[danewina$color=="red"], danewina$quality[danewina$color== "white"])
##
## F test to compare two variances
##
## data: danewina$quality[danewina$color == "red"] and danewina$quality[danewina$color == "white"]
## F = 0.83147, num df = 1598, denom df = 4897, p-value = 8.561e-06
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
## 0.7682689 0.9013500
## sample estimates:
## ratio of variances
## 0.8314702
## Zmienna pH
var.test(danewina$pH[danewina$color=="red"], danewina$pH[danewina$color== "white"])
##
## F test to compare two variances
##
## data: danewina$pH[danewina$color == "red"] and danewina$pH[danewina$color == "white"]
## F = 1.0453, num df = 1598, denom df = 4897, p-value = 0.2716
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
## 0.965890 1.133203
## sample estimates:
## ratio of variances
## 1.045349
## Zmienna Chlorides
var.test(danewina$chlorides[danewina$color=="red"], danewina$chlorides[danewina$color== "white"])
##
## F test to compare two variances
##
## data: danewina$chlorides[danewina$color == "red"] and danewina$chlorides[danewina$color == "white"]
## F = 4.6407, num df = 1598, denom df = 4897, p-value < 2.2e-16
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
## 4.287915 5.030676
## sample estimates:
## ratio of variances
## 4.640658
## Zmienna Sulphates
var.test(danewina$sulphates[danewina$color=="red"], danewina$sulphates[danewina$color== "white"])
##
## F test to compare two variances
##
## data: danewina$sulphates[danewina$color == "red"] and danewina$sulphates[danewina$color == "white"]
## F = 2.206, num df = 1598, denom df = 4897, p-value < 2.2e-16
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
## 2.038327 2.391410
## sample estimates:
## ratio of variances
## 2.206009
## Zmienna Density
var.test(danewina$density[danewina$color=="red"], danewina$density[danewina$color== "white"])
##
## F test to compare two variances
##
## data: danewina$density[danewina$color == "red"] and danewina$density[danewina$color == "white"]
## F = 0.39819, num df = 1598, denom df = 4897, p-value < 2.2e-16
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
## 0.3679241 0.4316567
## sample estimates:
## ratio of variances
## 0.3981912
Wniosek: Wartość p dla testu zmiennej pH wynosi 0.2716 i jest większa od poziomu istotności 0.05. W takim przypadku pozostajemy przy hipotezie zerowej mówiącej że współczynnik wariancji jest równy 1.
Podsumowując, nie ma istotnej różnicy pomiędzy obiema wariancjami dla koloru wina i zmiennej pH.
W pozostałych przypadkach odrzucamy hipotezę zerową - jest istotna różnica pomiędzy wariancjami.
t.test(Catania_Wine$sulphates, Bolonia_Wine$sulphates)
##
## Welch Two Sample t-test
##
## data: Catania_Wine$sulphates and Bolonia_Wine$sulphates
## t = -22.883, df = 3484.2, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.10594675 -0.08922395
## sample estimates:
## mean of x mean of y
## 0.4899110 0.5874964
t.test(Catania_Wine$pH, Bolonia_Wine$pH)
##
## Welch Two Sample t-test
##
## data: Catania_Wine$pH and Bolonia_Wine$pH
## t = -21.044, df = 4199.9, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.10259815 -0.08511089
## sample estimates:
## mean of x mean of y
## 3.171282 3.265137
t.test(Catania_Wine$alcohol, Bolonia_Wine$alcohol)
##
## Welch Two Sample t-test
##
## data: Catania_Wine$alcohol and Bolonia_Wine$alcohol
## t = -21.948, df = 4277.6, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.8220888 -0.6872636
## sample estimates:
## mean of x mean of y
## 10.08465 10.83932
t.test(Catania_Wine$citric.acid, Bolonia_Wine$citric.acid)
##
## Welch Two Sample t-test
##
## data: Catania_Wine$citric.acid and Bolonia_Wine$citric.acid
## t = 10.823, df = 3824.9, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 0.03835685 0.05532828
## sample estimates:
## mean of x mean of y
## 0.3401631 0.2933205
t.test(Catania_Wine$quality, Bolonia_Wine$quality)
##
## Welch Two Sample t-test
##
## data: Catania_Wine$quality and Bolonia_Wine$quality
## t = -2.9615, df = 4300.5, p-value = 0.003078
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.12581615 -0.02558758
## sample estimates:
## mean of x mean of y
## 5.795033 5.870735
Wniosek: Dla każdego testu wartość p okazała się być bardzo mała. Odrzucamy hipotezę zerową mówiącą że różnica średnich wynosi 0. Odpowiedź - średni poziom różni się istotnie statystycznie.
t.test(Catania_Wine$sulphates, EmiliaRomana_Wine$sulphates)
##
## Welch Two Sample t-test
##
## data: Catania_Wine$sulphates and EmiliaRomana_Wine$sulphates
## t = -8.9344, df = 2914.1, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.04658017 -0.02981427
## sample estimates:
## mean of x mean of y
## 0.4899110 0.5281083
t.test(Catania_Wine$pH, EmiliaRomana_Wine$pH)
##
## Welch Two Sample t-test
##
## data: Catania_Wine$pH and EmiliaRomana_Wine$pH
## t = -13.245, df = 3228.8, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.07448610 -0.05527732
## sample estimates:
## mean of x mean of y
## 3.171282 3.236164
t.test(Catania_Wine$alcohol, EmiliaRomana_Wine$alcohol)
##
## Welch Two Sample t-test
##
## data: Catania_Wine$alcohol and EmiliaRomana_Wine$alcohol
## t = -18.582, df = 3784.6, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.6916135 -0.5595990
## sample estimates:
## mean of x mean of y
## 10.08465 10.71025
t.test(Catania_Wine$citric.acid, EmiliaRomana_Wine$citric.acid)
##
## Welch Two Sample t-test
##
## data: Catania_Wine$citric.acid and EmiliaRomana_Wine$citric.acid
## t = 5.795, df = 3299.2, p-value = 7.474e-09
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 0.01632976 0.03303016
## sample estimates:
## mean of x mean of y
## 0.3401631 0.3154831
t.test(Catania_Wine$quality, EmiliaRomana_Wine$quality)
##
## Welch Two Sample t-test
##
## data: Catania_Wine$quality and EmiliaRomana_Wine$quality
## t = 0.12747, df = 3510.5, p-value = 0.8986
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.0491154 0.0559458
## sample estimates:
## mean of x mean of y
## 5.795033 5.791618
Wniosek: Dla testu badającego średnie zmiennej Quality wartość p wynosi 0.8986. Oznacza to, że odrzucamy hipotezę zerową na rzecz hipotezy alternatywnej, mowiącej że różnica pomiędzy średnimi nie jest równa 0.
Odpowiedź - średni poziom pomiędzy regionami Catania i Emilia Romana dla zmiennej Quality nie różni się od siebie. Dla pozostałych przypadków - odrzucamy hipotezę zerową (średni poziom różni się istotnie).
t.test(EmiliaRomana_Wine$sulphates, Bolonia_Wine$sulphates)
##
## Welch Two Sample t-test
##
## data: EmiliaRomana_Wine$sulphates and Bolonia_Wine$sulphates
## t = -11.447, df = 3759.8, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.06955960 -0.04921666
## sample estimates:
## mean of x mean of y
## 0.5281083 0.5874964
t.test(EmiliaRomana_Wine$pH, Bolonia_Wine$pH)
##
## Welch Two Sample t-test
##
## data: EmiliaRomana_Wine$pH and Bolonia_Wine$pH
## t = -5.4047, df = 3591, p-value = 6.916e-08
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.03948317 -0.01846245
## sample estimates:
## mean of x mean of y
## 3.236164 3.265137
t.test(EmiliaRomana_Wine$alcohol, Bolonia_Wine$alcohol)
##
## Welch Two Sample t-test
##
## data: EmiliaRomana_Wine$alcohol and Bolonia_Wine$alcohol
## t = -3.4727, df = 3783.2, p-value = 0.000521
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.20193879 -0.05620096
## sample estimates:
## mean of x mean of y
## 10.71025 10.83932
t.test(EmiliaRomana_Wine$citric.acid, Bolonia_Wine$citric.acid)
##
## Welch Two Sample t-test
##
## data: EmiliaRomana_Wine$citric.acid and Bolonia_Wine$citric.acid
## t = 4.4337, df = 3777.4, p-value = 9.525e-06
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 0.01236234 0.03196286
## sample estimates:
## mean of x mean of y
## 0.3154831 0.2933205
t.test(EmiliaRomana_Wine$quality, Bolonia_Wine$quality)
##
## Welch Two Sample t-test
##
## data: EmiliaRomana_Wine$quality and Bolonia_Wine$quality
## t = -2.7151, df = 3687.6, p-value = 0.006656
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.13624829 -0.02198583
## sample estimates:
## mean of x mean of y
## 5.791618 5.870735
Wniosek: Dla każdego testu wartość p okazała się być bardzo mała. Odrzucamy hipotezę zerową mówiącą że różnica średnich wynosi 0. Odpowiedź - średni poziom różni się istotnie statystycznie.
Do analizy korelacji potrzebujemy danych typu numeric i oczyszczonych z braków danych ( NA ).
wine_numeric <- select_if(danewina, is.numeric)
res <- as.data.frame(cor(na.omit(wine_numeric)))
Sprawdzenie korelacji pomiędzy zmiennymi:
Współczynnik korelacji R zawiera się w przedziale [-1; 1]
korelacja dodatnia - informacja, że wzrostowi wartości jednej cechy towarzyszy wzrost wartości drugiej cechy
korelacja ujemna - informacja, że wzrostowi wartości jednej cechy towarzyszy spadek wartości drugiej cechy
Siła związku pomiędzy zmiennymi:
od 0 do 0.3 - słaby związek
od 0.3 do 0.5 - związek umiarkowanie silny
od 0.5 do 1 - związek silny lub bardzo silny
ggcorrplot(res, hc.order= T, type= "lower", outline.color = "white", lab= T)
Odczytujemy z korelogramu interesującą nas zmienną - w naszym przypadku zmienna Quality.
Możemy zauważyć że istotnie zmienna Quality skorelowana jest ze zmiennymi:
Alcohol (współczynnik korelacji równy 0.44)
Density (współczynnik korelacji równy -0.31)
Oznacza to że są to umiarkowanie silne powiązania pomiędzy tymi zmiennymi.
Sprawdzimy, czy jakość wina zależy od jego rodzaju (koloru).
Tabelka, która ukazuje liczbę win o danej wartości jakości, z ograniczeniem na kolor wina:
freqcolordf <- data.frame(table(danewina$quality[danewina$color=="white"]),
table(factor(danewina$quality[danewina$color=="red"], levels=3:9)))
freqcolordf <- freqcolordf[,c(2,4)]
names(freqcolordf) <- c("white","red")
rownames(freqcolordf) <- 3:9
datatable(freqcolordf)
Sprawdzenie testem niezależności chi- kwadrat
chisq.test(unlist(danewina$quality[danewina$color=="red"]),unlist(danewina$quality[danewina$color=="red"]))
##
## Pearson's Chi-squared test
##
## data: unlist(danewina$quality[danewina$color == "red"]) and unlist(danewina$quality[danewina$color == "red"])
## X-squared = 7995, df = 25, p-value < 2.2e-16
Wniosek: Hipoteza zerowa: badane dane są niezależne.
Wartosc p jest bardzo mała, dlatego odrzucamy hipotezę zerową mówiącą że zmienne podlegające badaniu są niezależne i skłaniamy się ku hipotezie alternatywnej, mówiącej że są one zależne od siebie.
Wynik sugeruje że istnieje statystyczne powiązanie pomiędzy kolorem wina, a jego jakością (co możemy odczytać również bezpośrednio z tabelki - widzimy np. że wina białe dostały ocenę 9 [prawie najwyższą], gdzie wina czerwone nie mogą się taką oceną pochwalić wcale).
Sprawdzimy, czy jakość wina zależy od jego regionu.
Tabelki, które ukazują liczbę win o danej wartości jakości, oraz ich procentowy udział z ograniczeniem na 3 regiony:
qualCat <- as.data.frame(table(Catania_Wine$quality))
qualBol <- as.data.frame(table(Bolonia_Wine$quality))
qualEmiR <- as.data.frame(table(EmiliaRomana_Wine$quality))
qualData <- qualCat
qualData <- cbind(qualData, qualBol$Freq)
qualData <- cbind(qualData, qualEmiR$Freq)
row.names(qualData) <- qualData$Var1
qualData <- qualData[,2:4]
names(qualData) <- c("Catania","Bolonia","Emilia Romana")
## Funkcja zamiany wartosci na procenty w zaleznosci od wierszy
percentSumRows <- function(x){
percentqualData <- data.frame(matrix(NA, ncol = ncol(x)))
for (i in 1:nrow(x)){
percentqualData <- rbind(percentqualData, paste0(round(as.numeric(x[i,]/rowSums(x[i,]))*100,1),"%"))
}
## Wynik procentowy jakosci wina z podzialem na regiony
percentqualData <- percentqualData[2:8,]
row.names(percentqualData) <- row.names(x)
names(percentqualData) <- names(x)
return(percentqualData)
}
datatable(qualData)
datatable(percentSumRows(qualData), options = list(
columnDefs = list(list(className = 'dt-right', targets = 1:3))
))
barplot(t(qualData[c("Catania", "Bolonia", "Emilia Romana")]),
beside= T, ylab= "Frequency", xlab= "Quality")
grid()
barplot(t(qualData[c("Catania", "Bolonia", "Emilia Romana")]),
beside= T, ylab= "Frequency", xlab= "Quality", add = T,
col= c("deeppink","pink2", "pink4"))
box()
legend(x ="topright", legend = c("Catania","Bolonia", "Emilia Romana"),
title ="Region: ", box.lty = 2,
col= c("deeppink","pink2", "pink4"), lty=c(1,1,1),
lwd=3, cex=0.9, horiz=F, bty = "o")
Wniosek: Możemy zauważyć że jakość wina zależy w pewnym stopniu od regionu pochodzenia.
Największy odsetek wszystkich ocen 9 [czyli tych niemal najlepszych] stanowiły wina z regionu Bolonia.
Jednakże wśród win średnich [oceny 5, czy 6] przeważały wina pochodzące z regionu Catania.
Region Emilia Romana w tym zestawieniu wypada najgorzej - możemy zatem stwierdzić, że mało prawdopodobne jest trafienie na dobre wino z regionu Emilia Romana.