Packages Used

library(pastecs)
## Warning: package 'pastecs' was built under R version 4.2.3
library(tidyr)
## Warning: package 'tidyr' was built under R version 4.2.2
## 
## Attaching package: 'tidyr'
## The following object is masked from 'package:pastecs':
## 
##     extract
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.3
library(RColorBrewer)

library(RColorBrewer) options(repr.plot.width = 10, repr.plot.height = 6) # Adjust width and height as needed display.brewer.all()

Importing data and explaining variables

WaterQuality <- read.table(file = "water_potability.csv", header = TRUE, sep = ",", dec = ".")
head(WaterQuality)
##         ph Hardness   Solids Chloramines  Sulfate Conductivity Organic_carbon
## 1       NA 204.8905 20791.32    7.300212 368.5164     564.3087      10.379783
## 2 3.716080 129.4229 18630.06    6.635246       NA     592.8854      15.180013
## 3 8.099124 224.2363 19909.54    9.275884       NA     418.6062      16.868637
## 4 8.316766 214.3734 22018.42    8.059332 356.8861     363.2665      18.436524
## 5 9.092223 181.1015 17978.99    6.546600 310.1357     398.4108      11.558279
## 6 5.584087 188.3133 28748.69    7.544869 326.6784     280.4679       8.399735
##   Trihalomethanes Turbidity Potability
## 1        86.99097  2.963135          0
## 2        56.32908  4.500656          0
## 3        66.42009  3.055934          0
## 4       100.34167  4.628771          0
## 5        31.99799  4.075075          0
## 6        54.91786  2.559708          0

Descriptions of Variables:

Source: Kaggle

Carrying out data manipulation

# Cleaning data as there are missing values within the dataset represented by NA
WaterQuality <- drop_na(WaterQuality)

head(WaterQuality)
##          ph Hardness   Solids Chloramines  Sulfate Conductivity Organic_carbon
## 1  8.316766 214.3734 22018.42    8.059332 356.8861     363.2665      18.436524
## 2  9.092223 181.1015 17978.99    6.546600 310.1357     398.4108      11.558279
## 3  5.584087 188.3133 28748.69    7.544869 326.6784     280.4679       8.399735
## 4 10.223862 248.0717 28749.72    7.513408 393.6634     283.6516      13.789695
## 5  8.635849 203.3615 13672.09    4.563009 303.3098     474.6076      12.363817
## 6 11.180284 227.2315 25484.51    9.077200 404.0416     563.8855      17.927806
##   Trihalomethanes Turbidity Potability
## 1       100.34167  4.628771          0
## 2        31.99799  4.075075          0
## 3        54.91786  2.559708          0
## 4        84.60356  2.672989          0
## 5        62.79831  4.401425          0
## 6        71.97660  4.370562          0

Converting categorical variables into factors

WaterQuality$PotableFactor <- factor(WaterQuality$Potability, 
                             levels = c(1, 0), 
                             labels = c("Safe", "Unsafe"))

Presenting descriptive statistics

#Descriptive Statistics for the selected variables using stat.desc from pastec package
round(stat.desc(WaterQuality),2)
##                    ph  Hardness      Solids Chloramines   Sulfate Conductivity
## nbr.val       2011.00   2011.00     2011.00     2011.00   2011.00      2011.00
## nbr.null         0.00      0.00        0.00        0.00      0.00         0.00
## nbr.na           0.00      0.00        0.00        0.00      0.00         0.00
## min              0.23     73.49      320.94        1.39    129.00       201.62
## max             14.00    317.34    56488.67       13.13    481.03       753.34
## range           13.77    243.85    56167.73       11.74    352.03       551.72
## sum          14249.93 394091.79 44075974.60    14347.15 670114.82    857744.61
## median           7.03    197.19    20933.51        7.14    332.23       423.46
## mean             7.09    195.97    21917.44        7.13    333.22       426.53
## SE.mean          0.04      0.73      192.72        0.04      0.92         1.80
## CI.mean.0.95     0.07      1.43      377.95        0.07      1.80         3.53
## var              2.48   1065.05 74688309.02        2.51   1697.87      6514.52
## std.dev          1.57     32.64     8642.24        1.58     41.21        80.71
## coef.var         0.22      0.17        0.39        0.22      0.12         0.19
##              Organic_carbon Trihalomethanes Turbidity Potability PotableFactor
## nbr.val             2011.00         2011.00   2011.00    2011.00            NA
## nbr.null               0.00            0.00      0.00    1200.00            NA
## nbr.na                 0.00            0.00      0.00       0.00            NA
## min                    2.20            8.58      1.45       0.00            NA
## max                   27.01          124.00      6.49       1.00            NA
## range                 24.81          115.42      5.04       1.00            NA
## sum                28873.35       133532.13   7983.12     811.00            NA
## median                14.32           66.54      3.97       0.00            NA
## mean                  14.36           66.40      3.97       0.40            NA
## SE.mean                0.07            0.36      0.02       0.01            NA
## CI.mean.0.95           0.15            0.70      0.03       0.02            NA
## var                   11.06          258.47      0.61       0.24            NA
## std.dev                3.32           16.08      0.78       0.49            NA
## coef.var               0.23            0.24      0.20       1.22            NA

Explaining the descriptive statistics

Visually representing the variables