Explorar, analisar e visualizar um conjunto de dados wine quality para extrair insights relevantes e compreender os fatores que influenciam na sua qualidade.
library("pacman")
pacman::p_load(hrbrthemes,tidyr,readr,dplyr,
metan, janitor, naniar,ggplot2, DT, grid,
gridExtra,GGally,ggExtra,purrr,plotly,ggrepel, corrplot)
wine = read.table ("wine_quality.txt", header = T, sep = ",")%>%
dplyr::rename (cor = color,
branco = white,
acidez_fixa = fixed.acidity,
acidez_volatil = volatile.acidity,
acido_citrico = citric.acid,
acucar_residual = residual.sugar,
Cloretos = chlorides,
dioxido_enxofre_livre = free.sulfur.dioxide,
dioxido_enxofre_total = total.sulfur.dioxide,
densidade = density,
pH = pH,
sulfatos = sulphates,
alcool = alcohol,
qualidade = quality,
sensorial = good)
O banco de dados possui 6497 linhas e 15 colunas, não tem nenhuma informação faltante e os dados são referentes as características das propriedades dos vinhos avaliados, incluindo sua classificação de qualidade.
dim(wine) # O banco de dados possui 6497 linhas e 15 coluas
## [1] 6497 15
datatable(wine[1:15])
metan::inspect(wine)
## # A tibble: 15 × 10
## Variable Class Missing Levels Valid_n Min Median Max Outlier Text
## <chr> <chr> <chr> <chr> <int> <dbl> <dbl> <dbl> <int> <chr>
## 1 cor char… No 0 6497 NA NA NA NA Line…
## 2 branco inte… No - 6497 0 1 1 1599 <NA>
## 3 acidez_fixa nume… No - 6497 3.8 7 15.9 357 <NA>
## 4 acidez_volatil nume… No - 6497 0.08 0.29 1.58 377 <NA>
## 5 acido_citrico nume… No - 6497 0 0.31 1.66 509 <NA>
## 6 acucar_residu… nume… No - 6497 0.6 3 65.8 118 <NA>
## 7 Cloretos nume… No - 6497 0.01 0.05 0.61 286 <NA>
## 8 dioxido_enxof… nume… No - 6497 1 29 289 62 <NA>
## 9 dioxido_enxof… nume… No - 6497 6 118 440 10 <NA>
## 10 densidade nume… No - 6497 0.99 0.99 1.04 3 <NA>
## 11 pH nume… No - 6497 2.72 3.21 4.01 73 <NA>
## 12 sulfatos nume… No - 6497 0.22 0.51 2 191 <NA>
## 13 alcool nume… No - 6497 8 10.3 14.9 3 <NA>
## 14 qualidade inte… No - 6497 3 6 9 228 <NA>
## 15 sensorial char… No 0 6497 NA NA NA NA Line…
naniar::as_shadow(wine)
## # A tibble: 6,497 × 15
## cor_NA branco_NA acidez_fixa_NA acidez_volatil_NA acido_citrico_NA
## <fct> <fct> <fct> <fct> <fct>
## 1 !NA !NA !NA !NA !NA
## 2 !NA !NA !NA !NA !NA
## 3 !NA !NA !NA !NA !NA
## 4 !NA !NA !NA !NA !NA
## 5 !NA !NA !NA !NA !NA
## 6 !NA !NA !NA !NA !NA
## 7 !NA !NA !NA !NA !NA
## 8 !NA !NA !NA !NA !NA
## 9 !NA !NA !NA !NA !NA
## 10 !NA !NA !NA !NA !NA
## # ℹ 6,487 more rows
## # ℹ 10 more variables: acucar_residual_NA <fct>, Cloretos_NA <fct>,
## # dioxido_enxofre_livre_NA <fct>, dioxido_enxofre_total_NA <fct>,
## # densidade_NA <fct>, pH_NA <fct>, sulfatos_NA <fct>, alcool_NA <fct>,
## # qualidade_NA <fct>, sensorial_NA <fct>
naniar::miss_var_summary(wine) # Não tem nenhuma informação faltante no banco de dados
## # A tibble: 15 × 3
## variable n_miss pct_miss
## <chr> <int> <dbl>
## 1 cor 0 0
## 2 branco 0 0
## 3 acidez_fixa 0 0
## 4 acidez_volatil 0 0
## 5 acido_citrico 0 0
## 6 acucar_residual 0 0
## 7 Cloretos 0 0
## 8 dioxido_enxofre_livre 0 0
## 9 dioxido_enxofre_total 0 0
## 10 densidade 0 0
## 11 pH 0 0
## 12 sulfatos 0 0
## 13 alcool 0 0
## 14 qualidade 0 0
## 15 sensorial 0 0
resumo_quali_dplyr = wine %>% dplyr::group_by(qualidade) %>%
dplyr::summarise(quantidade = n())%>% dplyr::arrange(quantidade)
datatable(resumo_quali_dplyr)
resumo_quali_janitor = wine%>%
janitor::tabyl(qualidade) %>% dplyr::arrange(n) %>%
janitor::adorn_totals()
datatable(resumo_quali_janitor)
Em resumo: Os vinhos foram classificados de 3 a 9 em uma escala sensorial. Cerca de 44% de todos os vinhos avaliados é de qualidade 6, considerados vinhos bons
wine_id = wine %>% dplyr::mutate(ID = row_number())
datatable (wine_id[1:15])
wine_bruto = wine_id %>% metan::column_to_first(ID)
datatable (wine_bruto[1:15])
wine_final = wine_bruto %>% janitor::clean_names()
names(wine_final)
## [1] "id" "cor" "branco"
## [4] "acidez_fixa" "acidez_volatil" "acido_citrico"
## [7] "acucar_residual" "cloretos" "dioxido_enxofre_livre"
## [10] "dioxido_enxofre_total" "densidade" "p_h"
## [13] "sulfatos" "alcool" "qualidade"
## [16] "sensorial"
metan::desc_stat(wine, stats = "main")
## # A tibble: 13 × 10
## variable cv max mean median min sd.amo se ci.t n.valid
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Cloretos 62.5 0.611 0.056 0.047 0.009 0.035 0.0004 0.0009 6497
## 2 acidez_fixa 18.0 15.9 7.22 7 3.8 1.30 0.0161 0.0315 6497
## 3 acidez_vol… 48.5 1.58 0.340 0.29 0.08 0.165 0.002 0.004 6497
## 4 acido_citr… 45.6 1.66 0.319 0.31 0 0.145 0.0018 0.0035 6497
## 5 acucar_res… 87.4 65.8 5.44 3 0.6 4.76 0.059 0.116 6497
## 6 alcool 11.4 14.9 10.5 10.3 8 1.19 0.0148 0.029 6497
## 7 branco 57.1 1 0.754 1 0 0.431 0.0053 0.0105 6497
## 8 densidade 0.302 1.04 0.995 0.995 0.987 0.003 0 0.0001 6497
## 9 dioxido_en… 58.1 289 30.5 29 1 17.7 0.220 0.432 6497
## 10 dioxido_en… 48.8 440 116. 118 6 56.5 0.701 1.37 6497
## 11 pH 5.00 4.01 3.22 3.21 2.72 0.161 0.002 0.0039 6497
## 12 qualidade 15.0 9 5.82 6 3 0.873 0.0108 0.0212 6497
## 13 sulfatos 28.0 2 0.531 0.51 0.22 0.149 0.0018 0.0036 6497
metan::desc_stat(wine, stats = "all")
## # A tibble: 13 × 34
## variable av.dev ci.t ci.z cv gmean hmean iqr kurt mad
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Cloretos 0.0206 0.0009 0.0009 62.5 0.0503 0.0462 2.7e-2 50.9 0.0163
## 2 acidez_fi… 0.914 0.0315 0.0315 18.0 7.11 7.02 1.3e+0 5.06 0.890
## 3 acidez_vo… 0.124 0.004 0.004 48.5 0.307 0.280 1.7e-1 2.83 0.119
## 4 acido_cit… 0.106 0.0035 0.0035 45.6 0.295 0 1.4e-1 2.40 0.104
## 5 acucar_re… 3.90 0.116 0.116 87.4 3.76 2.72 6.3e+0 4.36 2.52
## 6 alcool 0.997 0.029 0.029 11.4 10.4 10.4 1.8e+0 -0.532 1.33
## 7 branco 0.371 0.0105 0.0105 57.1 1 0 0 -0.610 0
## 8 densidade 0.0025 0.0001 0.0001 0.302 0.995 0.995 4.7e-3 6.61 0.0034
## 9 dioxido_e… 14.0 0.432 0.432 58.1 24.9 18.6 2.4e+1 7.91 17.8
## 10 dioxido_e… 45.7 1.37 1.37 48.8 96.0 67.9 7.9e+1 -0.372 57.8
## 11 pH 0.127 0.0039 0.0039 5.00 3.21 3.21 2.1e-1 0.368 0.163
## 12 qualidade 0.686 0.0212 0.0212 15.0 5.75 5.68 1 e+0 0.232 1.48
## 13 sulfatos 0.110 0.0036 0.0036 28.0 0.513 0.497 1.7e-1 8.65 0.119
## # ℹ 24 more variables: max <dbl>, mean <dbl>, median <dbl>, min <dbl>, n <dbl>,
## # n.valid <dbl>, n.missing <dbl>, n.unique <dbl>, ps <dbl>, q2.5 <dbl>,
## # q25 <dbl>, q75 <dbl>, q97.5 <dbl>, range <dbl>, sd.amo <dbl>, sd.pop <dbl>,
## # se <dbl>, skew <dbl>, sum <dbl>, sum.dev <dbl>, ave.dev <dbl>,
## # sum.sq.dev <dbl>, var.amo <dbl>, var.pop <dbl>
metan::desc_stat(wine, stats = "robust")
## # A tibble: 13 × 5
## variable n median iqr ps
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 Cloretos 6497 0.047 0.027 0.02
## 2 acidez_fixa 6497 7 1.3 0.963
## 3 acidez_volatil 6497 0.29 0.17 0.126
## 4 acido_citrico 6497 0.31 0.14 0.104
## 5 acucar_residual 6497 3 6.3 4.67
## 6 alcool 6497 10.3 1.8 1.33
## 7 branco 6497 1 0 0
## 8 densidade 6497 0.995 0.0047 0.0034
## 9 dioxido_enxofre_livre 6497 29 24 17.8
## 10 dioxido_enxofre_total 6497 118 79 58.5
## 11 pH 6497 3.21 0.21 0.156
## 12 qualidade 6497 6 1 0.741
## 13 sulfatos 6497 0.51 0.17 0.126
metan::desc_stat(wine, stats = "quantile")
## # A tibble: 13 × 7
## variable n min q25 median q75 max
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Cloretos 6497 0.009 0.038 0.047 0.065 0.611
## 2 acidez_fixa 6497 3.8 6.4 7 7.7 15.9
## 3 acidez_volatil 6497 0.08 0.23 0.29 0.4 1.58
## 4 acido_citrico 6497 0 0.25 0.31 0.39 1.66
## 5 acucar_residual 6497 0.6 1.8 3 8.1 65.8
## 6 alcool 6497 8 9.5 10.3 11.3 14.9
## 7 branco 6497 0 1 1 1 1
## 8 densidade 6497 0.987 0.992 0.995 0.997 1.04
## 9 dioxido_enxofre_livre 6497 1 17 29 41 289
## 10 dioxido_enxofre_total 6497 6 77 118 156 440
## 11 pH 6497 2.72 3.11 3.21 3.32 4.01
## 12 qualidade 6497 3 5 6 6 9
## 13 sulfatos 6497 0.22 0.43 0.51 0.6 2
metan::desc_stat(wine, by =sensorial)
## # A tibble: 26 × 11
## sensorial variable cv max mean median min sd.amo se ci.t
## <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Bad Cloretos 66.5 0.611 6.44e-2 0.052 0.009 0.0428 0.0009 0.0017
## 2 Bad acidez_… 17.3 15.9 7.33e+0 7.1 4.2 1.27 0.026 0.0509
## 3 Bad acidez_… 47.3 1.58 3.97e-1 0.338 0.1 0.188 0.0038 0.0075
## 4 Bad acido_c… 53.6 1 3.04e-1 0.295 0 0.163 0.0033 0.0065
## 5 Bad acucar_… 87.1 23.5 5.65e+0 2.9 0.6 4.92 0.101 0.198
## 6 Bad alcool 8.53 14.9 9.87e+0 9.6 8 0.842 0.0172 0.0338
## 7 Bad branco 67.4 1 6.88e-1 1 0 0.463 0.0095 0.0186
## 8 Bad densida… 0.248 1.00 9.96e-1 0.996 0.987 0.0025 0.0001 0.0001
## 9 Bad dioxido… 67.3 289 2.95e+1 26 2 19.8 0.406 0.797
## 10 Bad dioxido… 51.9 440 1.19e+2 124 6 61.9 1.27 2.49
## # ℹ 16 more rows
## # ℹ 1 more variable: n.valid <dbl>
metan::desc_stat(wine, alcool, by =sensorial)
## # A tibble: 2 × 11
## sensorial variable cv max mean median min sd.amo se ci.t n.valid
## <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Bad alcool 8.53 14.9 9.87 9.6 8 0.842 0.0172 0.0338 2384
## 2 Good alcool 11.2 14.2 10.9 10.8 8.4 1.22 0.019 0.0373 4113
metan::desc_stat(wine, stats = "main", hist = T)
## # A tibble: 13 × 10
## variable cv max mean median min sd.amo se ci.t n.valid
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Cloretos 62.5 0.611 0.056 0.047 0.009 0.035 0.0004 0.0009 6497
## 2 acidez_fixa 18.0 15.9 7.22 7 3.8 1.30 0.0161 0.0315 6497
## 3 acidez_vol… 48.5 1.58 0.340 0.29 0.08 0.165 0.002 0.004 6497
## 4 acido_citr… 45.6 1.66 0.319 0.31 0 0.145 0.0018 0.0035 6497
## 5 acucar_res… 87.4 65.8 5.44 3 0.6 4.76 0.059 0.116 6497
## 6 alcool 11.4 14.9 10.5 10.3 8 1.19 0.0148 0.029 6497
## 7 branco 57.1 1 0.754 1 0 0.431 0.0053 0.0105 6497
## 8 densidade 0.302 1.04 0.995 0.995 0.987 0.003 0 0.0001 6497
## 9 dioxido_en… 58.1 289 30.5 29 1 17.7 0.220 0.432 6497
## 10 dioxido_en… 48.8 440 116. 118 6 56.5 0.701 1.37 6497
## 11 pH 5.00 4.01 3.22 3.21 2.72 0.161 0.002 0.0039 6497
## 12 qualidade 15.0 9 5.82 6 3 0.873 0.0108 0.0212 6497
## 13 sulfatos 28.0 2 0.531 0.51 0.22 0.149 0.0018 0.0036 6497
c1 <- ggplot(wine, aes(x = sensorial, fill = sensorial)) +
geom_bar(alpha = 0.5) +
labs(title = "Gráfico de barras da variável sensorial")+
theme_classic()
c2 <- ggplot(wine, aes(x = as.character(qualidade), fill = sensorial)) +
geom_bar(alpha = 0.5) +
labs(title = "Gráfico de barras da variável qualidade")+
xlab("qualidade")+
theme_classic()
c3 <- ggplot(wine, aes(x = cor, fill = sensorial)) +
geom_bar(alpha = 0.5) +
labs(title = "Gráfico de barras da variável cor")+
theme_classic()
grid.arrange(c1, c2, c3)
g1 <- ggplot(wine, aes(x = acidez_fixa, fill = sensorial)) +
geom_density(alpha = 0.5) +
labs(title = "Acidez fixa")
g2 <- ggplot(wine, aes(x = acidez_volatil, fill = sensorial)) +
geom_density(alpha = 0.5) +
labs(title = "Acidez volátil")
g3 <- ggplot(wine, aes(x = acido_citrico, fill = sensorial)) +
geom_density(alpha = 0.5) +
labs(title = "Acido cítrico")
g4 <- ggplot(wine, aes(x = acucar_residual, fill = sensorial)) +
geom_density(alpha = 0.5) +
labs(title = "Açúcar residual")
g5 <- ggplot(wine, aes(x = Cloretos, fill = sensorial)) +
geom_density(alpha = 0.5) +
labs(title = "cloretos")
g6 <- ggplot(wine, aes(x = dioxido_enxofre_livre, fill = sensorial)) +
geom_density(alpha = 0.5) +
labs(title = "Dióxido de enxofre livre")
g7 <- ggplot(wine, aes(x = dioxido_enxofre_total, fill = sensorial)) +
geom_density(alpha = 0.5) +
labs(title = "Dióxido de enxofre total")
g8 <- ggplot(wine, aes(x = densidade, fill = sensorial)) +
geom_density(alpha = 0.5) +
labs(title = "Densidade")
g9 <- ggplot(wine, aes(x = pH, fill = sensorial)) +
geom_density(alpha = 0.5) +
labs(title = "pH")
g10 <- ggplot(wine, aes(x = sulfatos, fill = sensorial)) +
geom_density(alpha = 0.5) +
labs(title = "Sulfatos")
g11 <- ggplot(wine, aes(x = alcool, fill = sensorial)) +
geom_density(alpha = 0.5) +
labs(title = "Alcool")
# Organizando os gráficos em um grid usando grid.arrange
grid.arrange(g1, g2, g3, g4, g5, g6,g7, g8, g9, g10, g11)
Em resumo: a partir da análise visual dos gráficos de densidade, podemos constatar que a variável “Alcool” é a que possui maior diferença (menor sobreposição) entre os vinhos classificados como bons e ruins
g11
b1 <- ggplot(data = wine, aes( x=sensorial, y=acidez_fixa , fill=sensorial)) +
geom_boxplot() +
xlab("Sensorial") +
ylab("Acidez fixa") +
theme(legend.position="none") +
xlab("") +
xlab("")
b2 <- ggplot(data = wine, aes( x=sensorial, y=acidez_volatil , fill=sensorial)) +
geom_boxplot() +
xlab("Sensorial") +
ylab("Acidez volátil") +
theme(legend.position="none") +
xlab("") +
xlab("")
b3 <- ggplot(data = wine, aes( x=sensorial, y=acido_citrico , fill=sensorial)) +
geom_boxplot() +
xlab("Sensorial") +
ylab("Ácido cítrico") +
theme(legend.position="none") +
xlab("") +
xlab("")
b4 <- ggplot(data = wine, aes( x=sensorial, y=acucar_residual , fill=sensorial)) +
geom_boxplot() +
xlab("Sensorial") +
ylab("Açúcar residual") +
theme(legend.position="none") +
xlab("") +
xlab("")
b5 <- ggplot(data = wine, aes( x=sensorial, y=Cloretos , fill=sensorial)) +
geom_boxplot() +
xlab("Sensorial") +
ylab("Cloretos") +
theme(legend.position="none") +
xlab("") +
xlab("")
b6 <- ggplot(data = wine, aes( x=sensorial, y=dioxido_enxofre_livre , fill=sensorial)) +
geom_boxplot() +
xlab("Sensorial") +
ylab("Dióxido de enxofre livre") +
theme(legend.position="none") +
xlab("") +
xlab("")
b7 <- ggplot(data = wine, aes( x=sensorial, y=dioxido_enxofre_total , fill=sensorial)) +
geom_boxplot() +
xlab("Sensorial") +
ylab("Dióxido de enxofre total") +
theme(legend.position="none") +
xlab("") +
xlab("")
b8<- ggplot(data = wine, aes( x=sensorial, y=densidade , fill=sensorial)) +
geom_boxplot() +
xlab("Sensorial") +
ylab("Densidade") +
theme(legend.position="none") +
xlab("") +
xlab("")
b9 <- ggplot(data = wine, aes( x=sensorial, y=pH , fill=sensorial)) +
geom_boxplot() +
xlab("Sensorial") +
ylab("pH")+
theme(legend.position="none") +
xlab("") +
xlab("")
b10 <- ggplot(data = wine, aes( x=sensorial, y=sulfatos , fill=sensorial)) +
geom_boxplot() +
xlab("Sensorial") +
ylab("Sulfatos")+
theme(legend.position="none") +
xlab("") +
xlab("")
b11 <- ggplot(data = wine, aes( x=sensorial, y=alcool , fill=sensorial)) +
geom_boxplot() +
xlab("Sensorial") +
ylab("Alcool")+
theme(legend.position="none") +
xlab("") +
xlab("")
# Organizando os gráficos em um grid usando grid.arrange
grid.arrange(b1, b2, b3, b4, b5, b6,b7, b8, b9, b10, b11)
Em resumo: é possível observar nos gráficos boxplots que a variável “Alcool” possui maior concentração nos vinhos considerados bons e portanto, pode ser uma variável a influenciar na qualidade do vinho
b11
# Data - variáveis numéricas da tabela wine
data <- wine[ , c(3:14)]
# Matriz de correlação
M<-cor(data)
head(round(M,2))
## acidez_fixa acidez_volatil acido_citrico acucar_residual
## acidez_fixa 1.00 0.22 0.32 -0.11
## acidez_volatil 0.22 1.00 -0.38 -0.20
## acido_citrico 0.32 -0.38 1.00 0.14
## acucar_residual -0.11 -0.20 0.14 1.00
## Cloretos 0.30 0.38 0.04 -0.13
## dioxido_enxofre_livre -0.28 -0.35 0.13 0.40
## Cloretos dioxido_enxofre_livre dioxido_enxofre_total
## acidez_fixa 0.30 -0.28 -0.33
## acidez_volatil 0.38 -0.35 -0.41
## acido_citrico 0.04 0.13 0.20
## acucar_residual -0.13 0.40 0.50
## Cloretos 1.00 -0.20 -0.28
## dioxido_enxofre_livre -0.20 1.00 0.72
## densidade pH sulfatos alcool qualidade
## acidez_fixa 0.46 -0.25 0.30 -0.10 -0.08
## acidez_volatil 0.27 0.26 0.23 -0.04 -0.27
## acido_citrico 0.10 -0.33 0.06 -0.01 0.09
## acucar_residual 0.55 -0.27 -0.19 -0.36 -0.04
## Cloretos 0.36 0.04 0.40 -0.26 -0.20
## dioxido_enxofre_livre 0.03 -0.15 -0.19 -0.18 0.06
corrplot(M, method="circle")
Em resumo: as variáveis “Alcool” e “densidade” possuem uma correlação de -0.68, e as variáveis “dioxido_enxofre_livre” e “dioxido_enxofre_total” possuem uma correlação de 0.72.
Dentre essas variáveis, “Alcool” teve a maior correção com a variável “qualidade” (0.44), porém a correlação não foi significativa.
ggpairs(wine, columns = c("alcool", "densidade", "dioxido_enxofre_livre","dioxido_enxofre_total", "qualidade" ), ggplot2::aes(colour=sensorial))
Conclusão: A variável que se mais se correlaciona com a qualidade do vinho é “Álcool”. Vinhos que foram classificados como “Bons” tem maiores concentrações de álcool. Porém essa corelação não foi significativa