getwd()
## [1] "C:/Users/monty/Desktop"
# Load the CSV file
wine <- read.csv("C:/Users/monty/Desktop/wine.csv")

# Print the structure of the dataset
str(wine)
## 'data.frame':    1599 obs. of  13 variables:
##  $ fixed.acidity       : num  8.9 9.4 10.2 7.3 8 8 7 11.6 5.3 7.6 ...
##  $ volatile.acidity    : num  0.48 0.685 0.23 0.73 0.62 0.67 0.43 0.44 0.57 0.54 ...
##  $ citric.acid         : num  0.53 0.11 0.37 0.24 0.35 0.3 0.02 0.64 0.01 0.13 ...
##  $ residual.sugar      : num  4 2.7 2.2 1.9 2.8 2 1.9 2.1 1.7 2.5 ...
##  $ chlorides           : num  0.101 0.077 0.057 0.108 0.086 ...
##  $ free.sulfur.dioxide : num  3 6 14 18 28 38 15 5 5 24 ...
##  $ total.sulfur.dioxide: num  10 31 36 102 52 62 28 15 27 66 ...
##  $ density             : num  0.996 0.998 0.996 0.997 0.997 ...
##  $ pH                  : num  3.21 3.19 3.23 3.26 3.31 3.26 3.35 3.21 3.57 3.39 ...
##  $ sulphates           : num  0.59 0.7 0.49 0.59 0.62 0.56 0.81 0.67 0.84 0.61 ...
##  $ alcohol             : num  12.1 10.1 9.3 9.3 10.8 10.2 10.6 10.2 12.5 9.4 ...
##  $ quality             : int  7 6 4 5 5 6 6 6 7 5 ...
##  $ kfold               : int  0 0 0 0 0 0 0 0 0 0 ...
# Display the first few rows of the dataset
head(wine)
##   fixed.acidity volatile.acidity citric.acid residual.sugar chlorides
## 1           8.9            0.480        0.53            4.0     0.101
## 2           9.4            0.685        0.11            2.7     0.077
## 3          10.2            0.230        0.37            2.2     0.057
## 4           7.3            0.730        0.24            1.9     0.108
## 5           8.0            0.620        0.35            2.8     0.086
## 6           8.0            0.670        0.30            2.0     0.060
##   free.sulfur.dioxide total.sulfur.dioxide density   pH sulphates alcohol
## 1                   3                   10 0.99586 3.21      0.59    12.1
## 2                   6                   31 0.99840 3.19      0.70    10.1
## 3                  14                   36 0.99614 3.23      0.49     9.3
## 4                  18                  102 0.99670 3.26      0.59     9.3
## 5                  28                   52 0.99700 3.31      0.62    10.8
## 6                  38                   62 0.99580 3.26      0.56    10.2
##   quality kfold
## 1       7     0
## 2       6     0
## 3       4     0
## 4       5     0
## 5       5     0
## 6       6     0
mean_fixed_acidity <- mean(wine$`fixed.acidity`)
print(mean_fixed_acidity)
## [1] 8.319637
#2How many wines have a chlorides content exceeding 0.1?
above_0.1_chlorides <- sum(wine$chlorides > 0.1)
print(above_0.1_chlorides)
## [1] 223
#3what is the mean ph of the wine whose quality is 9

quality_9_wines <- wine[wine$quality == 6, ]
ph_values <- quality_9_wines$pH
mean_ph <- mean(ph_values)
print(mean_ph)
## [1] 3.318072
#4 Calculate the difference between the maximum and minimum values of fixed acidity
diff_fixed_acidity <- max(wine[, "fixed.acidity"]) - min(wine[, "fixed.acidity"])
print(diff_fixed_acidity)
## [1] 11.3
#5How does the distribution of volatile acidity look like?histogram
library(ggplot2)
ggplot(wine, aes(x = `volatile.acidity`)) +
  geom_histogram(binwidth = 0.1, fill = "skyblue", color = "black") +
  labs(x = "Volatile Acidity", y = "Frequency")

# 6. Relationship between citric acid content and quality ratings,boxplot
library(ggplot2)
ggplot(wine, aes(x = `citric.acid`, y = quality, color = factor(quality))) +
  geom_boxplot() +
  labs(x = "Citric Acid", y = "Quality Rating", color = "Quality")

#7 Density across different quality ratings,boxplot
library(ggplot2)
ggplot(wine, aes(x = quality, y = density)) +
  geom_boxplot(fill = "skyblue", color = "black") +
  labs(x = "Quality Rating", y = "Density") +
  ggtitle("Boxplot of Density by Quality")
## Warning: Continuous x aesthetic
## ℹ did you forget `aes(group = ...)`?

#8 Correlation between pH levels and alcohol content,scatterplot
library(ggplot2)
ggplot(wine, aes(x = pH, y = alcohol)) +
  geom_point(color = "skyblue") +
  geom_smooth(method = "lm", se = FALSE, color = "black") +
  labs(x = "pH", y = "Alcohol Content") +
  ggtitle("Scatterplot of pH vs. Alcohol Content")
## `geom_smooth()` using formula = 'y ~ x'

# 9. Alcohol content across different quality ratings
library(ggplot2)
ggplot(wine, aes(x = quality, y = alcohol)) +
  geom_boxplot(fill = "skyblue", color = "black") +
  labs(x = "Quality Rating", y = "Alcohol Content") +
  ggtitle("Boxplot of Alcohol Content by Quality")
## Warning: Continuous x aesthetic
## ℹ did you forget `aes(group = ...)`?

#10How do the selected variables "fixed acidity", "volatile acidity", "citric acid", "residual sugar", and "quality" relate to each other in pairs?
library(GGally)
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
library(ggplot2)
subset_data <- wine[, c("fixed.acidity", "volatile.acidity", "citric.acid", "residual.sugar", "quality")]
pair_plot <- ggpairs(subset_data, columns = c("fixed.acidity", "volatile.acidity", "citric.acid", "residual.sugar", "quality"),
                     title = "Pair Plot of Selected Variables")
print(pair_plot)

#11 Relationship between chlorides content and quality ratings
library(ggplot2)
ggplot(wine, aes(x = chlorides, y = quality)) +
  geom_point(color = "skyblue") +geom_smooth(method = "lm", se = FALSE, color = "black") +
  labs(x = "Chlorides Content", y = "Quality Rating") +
  ggtitle("Scatterplot of Chlorides Content vs. Quality")
## `geom_smooth()` using formula = 'y ~ x'

#12 Distribution of sulphates content
library(ggplot2)
ggplot(wine, aes(x = sulphates)) +
  geom_histogram(binwidth = 0.1, fill = "orange", color = "black") +
  labs(x = "Sulphates Content", y = "Frequency")

#13 Alcohol content vs. quality ratings
correlation_alcohol_quality <- cor(wine$alcohol, wine$quality)
print(correlation_alcohol_quality)
## [1] 0.4761663
#14 Residual sugar vs. density
correlation_residual_sugar_density <- cor(wine$`residual.sugar`, wine$density)
print(correlation_residual_sugar_density)
## [1] 0.3552834
#15. Proportion of different kfold values (pie chart)
ggplot(wine, aes(x = "", fill = factor(kfold))) +
  geom_bar(width = 1) +
  coord_polar("y", start = 0) +
  labs(fill = "kfold")

#16 Regression analysis to predict density based on residual sugar content:s
lm_model <- lm(density ~ `residual.sugar`, data = wine)
summary(lm_model)
## 
## Call:
## lm(formula = density ~ residual.sugar, data = wine)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -0.0073843 -0.0010304  0.0000242  0.0010768  0.0059011 
## 
## Coefficients:
##                 Estimate Std. Error  t value Pr(>|t|)    
## (Intercept)    9.955e-01  9.092e-05 10949.38   <2e-16 ***
## residual.sugar 4.756e-04  3.131e-05    15.19   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.001765 on 1597 degrees of freedom
## Multiple R-squared:  0.1262, Adjusted R-squared:  0.1257 
## F-statistic: 230.7 on 1 and 1597 DF,  p-value: < 2.2e-16
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
#17 Comparison of mean pH across different quality ratings
wine %>%
  group_by(quality) %>%
  summarise(mean_pH = mean(pH))
## # A tibble: 6 × 2
##   quality mean_pH
##     <int>   <dbl>
## 1       3    3.40
## 2       4    3.38
## 3       5    3.30
## 4       6    3.32
## 5       7    3.29
## 6       8    3.27
#18Multiple regression of quality ratings on multiple variables
library(dplyr)
lm_model_multiple <- lm(quality ~ `fixed.acidity` + `volatile.acidity` + `citric.acid`, data = wine)
summary(lm_model_multiple)
## 
## Call:
## lm(formula = quality ~ fixed.acidity + volatile.acidity + citric.acid, 
##     data = wine)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.81361 -0.54026 -0.01076  0.46798  2.95735 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       6.45014    0.12130   53.18   <2e-16 ***
## fixed.acidity     0.01395    0.01468    0.95    0.342    
## volatile.acidity -1.74590    0.12690  -13.76   <2e-16 ***
## citric.acid      -0.03196    0.15220   -0.21    0.834    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7438 on 1595 degrees of freedom
## Multiple R-squared:  0.1532, Adjusted R-squared:  0.1516 
## F-statistic: 96.17 on 3 and 1595 DF,  p-value: < 2.2e-16
#19 Calculate Pearson correlation coefficient between fixed acidity and quality
pearson_correlation <- cor(wine$`fixed.acidity`, wine$quality, method = "pearson")
print(pearson_correlation)
## [1] 0.1240516