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()
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:
pH: pH is the measure of acidity/alkalinity in water.
Hardness: Hardness is the amount of calcium and magnesium salts dissolved in the water measured in mg/L.
Solids: Solids is the Total Dissolved Solids in water which could consist of Potassium, calcium, sodium etc. measured in ppm.
Chloramines: Disinfectants used to treat drinking water; it is a combination of chlorine and ammonia, measured in ppm.
Sulfate: The amount of sulfates dissolved in the water, measured in mg/L.
Conductivity: Measures the electrical conductivity of water, measured in μS/cm.
Organic_carbon: Is the Total Organic Carbon found in water measured in ppm.
Trihalomethanes: A liquid byproduct found when water is treated with chlorine, measured in μg/L.
Turbidity: Turbidity is the cloudiness in water caused by a large amount of suspended solids, measured in NTU.
Potability: Potability is a categorization of whether water is safe for human consumption or not. 1 being safe, 0 being unsafe.
# 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
WaterQuality$PotableFactor <- factor(WaterQuality$Potability,
levels = c(1, 0),
labels = c("Safe", "Unsafe"))
#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
For pH the mean value is 7.09 meaning that the average acidity is at the 7.09 level.
The median Hardness is 197.19 representing the middle value of all hardness observations when arranged in ascending order. It means 50% of the observations lie before the median and 50% after the median
For solids the standard deviation (std.dev) is 8642.24 which shows us the spread of amount of solids around the mean.
The coefficient of variation (coef.var) for Chloramines is 0.22 which is a measurement for the relative variation of Chloramines compared to the mean. This statistic is good for comparing different variables.
The range for sulfates is 352.03 which is the largest value subtracted by the smallest value.
The min for conductivity is 201.62 meaning that the lowest observation of electrical conductivity in water is 201.62 μS/cm.
The max for organic carbon is 27.01 which means the water observation with the highest amount of Total Organic Carbon is 27.01 ppm.
Trihalomethanes has a sum of 133532.13 which is all of the observations added up together.
Turbidity has an nbr.val of 2011.00 and all of the variables have the same nbr.val because the data has been cleaned to remove all rows with null values hence all variables have the same number of observations.
Potability cannot be explained well by any of the statistics as it is a binary variable consisting of 1s and 0s indicating whether water is safe or unsafe therefore stat.desc gives nonsensical results for potability.