#III. Tiền xử lý ddữ liệu
water_potability <- read.csv("~/Zalo Received Files/LOP L07_NHÓM 14.csv")
save(water_potability, file="water_potability.rda")
head(water_potability,10)
##           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
## 7  10.223862 248.0717 28749.72    7.513408 393.6634     283.6516      13.789695
## 8   8.635849 203.3615 13672.09    4.563009 303.3098     474.6076      12.363817
## 9         NA 118.9886 14285.58    7.804174 268.6469     389.3756      12.706049
## 10 11.180284 227.2315 25484.51    9.077200 404.0416     563.8855      17.927806
##    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
## 7         84.60356  2.672989          0
## 8         62.79831  4.401425          0
## 9         53.92885  3.595017          0
## 10        71.97660  4.370562          0
water_potability$X=NULL
water_potability$X.1=NULL
water_potability$X.2=NULL


str(water_potability)
## 'data.frame':    3276 obs. of  10 variables:
##  $ ph             : num  NA 3.72 8.1 8.32 9.09 ...
##  $ Hardness       : num  205 129 224 214 181 ...
##  $ Solids         : num  20791 18630 19910 22018 17979 ...
##  $ Chloramines    : num  7.3 6.64 9.28 8.06 6.55 ...
##  $ Sulfate        : num  369 NA NA 357 310 ...
##  $ Conductivity   : num  564 593 419 363 398 ...
##  $ Organic_carbon : num  10.4 15.2 16.9 18.4 11.6 ...
##  $ Trihalomethanes: num  87 56.3 66.4 100.3 32 ...
##  $ Turbidity      : num  2.96 4.5 3.06 4.63 4.08 ...
##  $ Potability     : int  0 0 0 0 0 0 0 0 0 0 ...
colSums(is.na(water_potability))
##              ph        Hardness          Solids     Chloramines         Sulfate 
##             491               0               0               0             781 
##    Conductivity  Organic_carbon Trihalomethanes       Turbidity      Potability 
##               0               0             162               0               0
colMeans(is.na(water_potability))
##              ph        Hardness          Solids     Chloramines         Sulfate 
##      0.14987790      0.00000000      0.00000000      0.00000000      0.23840049 
##    Conductivity  Organic_carbon Trihalomethanes       Turbidity      Potability 
##      0.00000000      0.00000000      0.04945055      0.00000000      0.00000000
ph_1<-mean(water_potability[water_potability$Potability == 0 & water_potability$Hardness <= 150, "ph"],na.rm=T)
ph_1
## [1] 6.721973
ph_2<-mean(water_potability[water_potability$Potability == 0 & water_potability$Hardness > 150, "ph"],na.rm=T)
ph_2
## [1] 7.112512
ph_3<-mean(water_potability[water_potability$Potability == 1 & water_potability$Hardness <= 150, "ph"],na.rm=T)
ph_3
## [1] 7.098154
ph_4<-mean(water_potability[water_potability$Potability == 1 & water_potability$Hardness > 150, "ph"],na.rm=T)
ph_4
## [1] 7.071429
water_potability[ water_potability$Potability == 0 & water_potability$Hardness <= 150 & is.na(water_potability$ph), "ph"] <- ph_1
water_potability[ water_potability$Potability == 0 & water_potability$Hardness > 150 & is.na(water_potability$ph), "ph"] <- ph_2
water_potability[ water_potability$Potability == 1 & water_potability$Hardness <= 150 & is.na(water_potability$ph), "ph"] <- ph_3
water_potability[ water_potability$Potability == 1 & water_potability$Hardness > 150 & is.na(water_potability$ph), "ph"] <- ph_4

Sulfate_1<-mean(water_potability[water_potability$Potability == 0, "Sulfate"],na.rm=T)
Sulfate_1
## [1] 334.5643
Sulfate_2<-mean(water_potability[water_potability$Potability == 1, "Sulfate"],na.rm=T)
Sulfate_2
## [1] 332.567
water_potability[water_potability$Potability == 0 & is.na(water_potability$Sulfate), "Sulfate"] <- Sulfate_1
water_potability[water_potability$Potability == 1 & is.na(water_potability$Sulfate), "Sulfate"] <- Sulfate_2



water_potability[is.na(water_potability$Trihalomethanes),"Trihalomethanes"] <- mean(water_potability$Trihalomethanes,na.rm=T)

colSums(is.na(water_potability))
##              ph        Hardness          Solids     Chloramines         Sulfate 
##               0               0               0               0               0 
##    Conductivity  Organic_carbon Trihalomethanes       Turbidity      Potability 
##               0               0               0               0               0
#IV. Thống kê mô tả

water_potability$Potability<-as.factor(water_potability$Potability)
summary(water_potability)
##        ph            Hardness          Solids         Chloramines    
##  Min.   : 0.000   Min.   : 47.43   Min.   :  320.9   Min.   : 0.352  
##  1st Qu.: 6.278   1st Qu.:176.85   1st Qu.:15666.7   1st Qu.: 6.127  
##  Median : 7.104   Median :196.97   Median :20927.8   Median : 7.130  
##  Mean   : 7.081   Mean   :196.37   Mean   :22014.1   Mean   : 7.122  
##  3rd Qu.: 7.870   3rd Qu.:216.67   3rd Qu.:27332.8   3rd Qu.: 8.115  
##  Max.   :14.000   Max.   :323.12   Max.   :61227.2   Max.   :13.127  
##     Sulfate       Conductivity   Organic_carbon  Trihalomethanes  
##  Min.   :129.0   Min.   :181.5   Min.   : 2.20   Min.   :  0.738  
##  1st Qu.:317.1   1st Qu.:365.7   1st Qu.:12.07   1st Qu.: 56.648  
##  Median :334.6   Median :421.9   Median :14.22   Median : 66.396  
##  Mean   :333.8   Mean   :426.2   Mean   :14.28   Mean   : 66.396  
##  3rd Qu.:350.4   3rd Qu.:481.8   3rd Qu.:16.56   3rd Qu.: 76.667  
##  Max.   :481.0   Max.   :753.3   Max.   :28.30   Max.   :124.000  
##    Turbidity     Potability
##  Min.   :1.450   0:1998    
##  1st Qu.:3.440   1:1278    
##  Median :3.955             
##  Mean   :3.967             
##  3rd Qu.:4.500             
##  Max.   :6.739
table (water_potability$Potability)
## 
##    0    1 
## 1998 1278
barplot ( table ( water_potability $ Potability ) , xlab =" Potability ", ylab =" Frequency ", main ="Barplot of Potability ",col =c(" grey "," lightblue "))

par(mfrow=c(1,3))
hist(water_potability$ph,xlab="pH",main="Histogram of pH",col="blue",labels=T,ylim=c(0,1200))
hist(water_potability$Hardness,xlab="Hardness",main="Histogram of Hardness",col="blue",labels=T,ylim=c(0,1000))
hist(water_potability$Solids,xlab="Solids",main="Histogram of Solids",col="blue",labels=T,ylim=c(0,800))

hist(water_potability$Chloramines,xlab="Chloramines",main="Histogram of Chloramines",col="blue",labels=T,ylim=c(0,1000))
hist(water_potability$Sulfate,xlab="Sulfate",main="Histogram of Sulfate",col="blue",labels=T,ylim=c(0,1400))
hist(water_potability$Conductivity,xlab="Conductivity",main="Histogram of Conductivity",col="blue",labels=T,ylim=c(0,800))

hist(water_potability$Organic_carbon,xlab="Organic_carbon",main="Histogram of Organic_carbon",col="blue",labels=T,ylim=c(0,800))
hist(water_potability$Trihalomethanes,xlab="Trihalomethanes",main="Histogram of Trihalomethanes",col="blue",labels=T,ylim=c(0,1000))
hist(water_potability$Turbidity,xlab="Turbidity",main="Histogram of Turbidity",col="blue",labels=T,ylim=c(0,1000))

boxplot ( water_potability $ ph ~ water_potability $ Potability , xlab = " Potability " , ylab = " ph " , main =" pH and Potability " , col = c ( " grey " ," lightblue " ) )
boxplot ( water_potability $ Hardness ~ water_potability $ Potability , xlab = " Potability " , ylab = "Hardness " , main = " Hardness and Potability " , col = c ( " grey " ," lightblue " ) )
boxplot ( water_potability $ Solids ~ water_potability $ Potability , xlab = " Potability " , ylab = "Solids " , main = " Solids and Potability " , col = c ( " grey " ," lightblue " ) )

boxplot ( water_potability $ Chloramines ~ water_potability $ Potability , xlab = " Potability " , ylab =" Chloramines " , main = " Chloramines and Potability " , col = c ( " grey " ," lightblue " ) )
boxplot ( water_potability $ Sulfate ~ water_potability $ Potability , xlab = " Potability " , ylab = "Sulfate " , main = " Sulfate and Potability " , col = c ( " grey " ," lightblue " ) )
boxplot ( water_potability $ Conductivity ~ water_potability $ Potability , xlab = " Potability " , ylab= " Conductivity " , main = " Conductivity and Potability " , col = c ( " grey " ," lightblue " ) )

boxplot ( water_potability $ Organic_carbon ~ water_potability $ Potability , xlab = " Potability " ,ylab = " Organic_carbon " , main = " Organic_carbon and Potability " , col = c ( " grey " ," lightblue " ))
boxplot ( water_potability $ Trihalomethanes ~ water_potability $ Potability , xlab = " Potability " ,ylab = " T ri h al om e th an e s " , main = " Tr i ha lo m et h an es and Potability " , col = c ( " grey " ," lightblue ") )
boxplot ( water_potability $ Turbidity ~ water_potability $ Potability , xlab = " Potability " , ylab = "Turbidity " , main = " Turbidity and Potability " , col = c ( " grey " ," lightblue " ) )

round(cor(water_potability[,1:9]),3)
##                     ph Hardness Solids Chloramines Sulfate Conductivity
## ph               1.000    0.079 -0.082      -0.032   0.015        0.017
## Hardness         0.079    1.000 -0.047      -0.030  -0.093       -0.024
## Solids          -0.082   -0.047  1.000      -0.070  -0.150        0.014
## Chloramines     -0.032   -0.030 -0.070       1.000   0.023       -0.020
## Sulfate          0.015   -0.093 -0.150       0.023   1.000       -0.014
## Conductivity     0.017   -0.024  0.014      -0.020  -0.014        1.000
## Organic_carbon   0.040    0.004  0.010      -0.013   0.027        0.021
## Trihalomethanes  0.004   -0.013 -0.009       0.017  -0.026        0.001
## Turbidity       -0.036   -0.014  0.020       0.002  -0.010        0.006
##                 Organic_carbon Trihalomethanes Turbidity
## ph                       0.040           0.004    -0.036
## Hardness                 0.004          -0.013    -0.014
## Solids                   0.010          -0.009     0.020
## Chloramines             -0.013           0.017     0.002
## Sulfate                  0.027          -0.026    -0.010
## Conductivity             0.021           0.001     0.006
## Organic_carbon           1.000          -0.013    -0.027
## Trihalomethanes         -0.013           1.000    -0.022
## Turbidity               -0.027          -0.022     1.000
library(corrplot)
## corrplot 0.92 loaded
M=cor(water_potability[,1:9])
corrplot(M, method="circle")

#V. Thống kế suy diễn
library(caTools)
set.seed(8)
split = sample.split(water_potability$Potability, SplitRatio = 0.7)
train_data = subset(water_potability, split == TRUE)
test_data = subset(water_potability, split == FALSE)
dim(train_data)
## [1] 2294   10
dim(test_data)
## [1] 982  10
model = glm(Potability~., family="binomial"(link='logit'), data=train_data)
research = step(model)
## Start:  AIC=3070.69
## Potability ~ ph + Hardness + Solids + Chloramines + Sulfate + 
##     Conductivity + Organic_carbon + Trihalomethanes + Turbidity
## 
##                   Df Deviance    AIC
## - ph               1   3050.7 3068.7
## - Trihalomethanes  1   3050.7 3068.7
## - Conductivity     1   3050.8 3068.8
## - Sulfate          1   3050.9 3068.9
## - Turbidity        1   3051.4 3069.4
## - Hardness         1   3052.0 3070.0
## <none>                 3050.7 3070.7
## - Chloramines      1   3055.4 3073.4
## - Solids           1   3055.7 3073.7
## - Organic_carbon   1   3056.2 3074.2
## 
## Step:  AIC=3068.69
## Potability ~ Hardness + Solids + Chloramines + Sulfate + Conductivity + 
##     Organic_carbon + Trihalomethanes + Turbidity
## 
##                   Df Deviance    AIC
## - Trihalomethanes  1   3050.7 3066.7
## - Conductivity     1   3050.8 3066.8
## - Sulfate          1   3050.9 3066.9
## - Turbidity        1   3051.4 3067.4
## - Hardness         1   3052.0 3068.0
## <none>                 3050.7 3068.7
## - Chloramines      1   3055.4 3071.4
## - Solids           1   3055.7 3071.7
## - Organic_carbon   1   3056.2 3072.2
## 
## Step:  AIC=3066.73
## Potability ~ Hardness + Solids + Chloramines + Sulfate + Conductivity + 
##     Organic_carbon + Turbidity
## 
##                  Df Deviance    AIC
## - Conductivity    1   3050.8 3064.8
## - Sulfate         1   3050.9 3064.9
## - Turbidity       1   3051.4 3065.4
## - Hardness        1   3052.0 3066.0
## <none>                3050.7 3066.7
## - Chloramines     1   3055.4 3069.4
## - Solids          1   3055.7 3069.7
## - Organic_carbon  1   3056.2 3070.2
## 
## Step:  AIC=3064.84
## Potability ~ Hardness + Solids + Chloramines + Sulfate + Organic_carbon + 
##     Turbidity
## 
##                  Df Deviance    AIC
## - Sulfate         1   3051.0 3063.0
## - Turbidity       1   3051.6 3063.6
## - Hardness        1   3052.1 3064.1
## <none>                3050.8 3064.8
## - Chloramines     1   3055.6 3067.6
## - Solids          1   3055.8 3067.8
## - Organic_carbon  1   3056.4 3068.4
## 
## Step:  AIC=3063.03
## Potability ~ Hardness + Solids + Chloramines + Organic_carbon + 
##     Turbidity
## 
##                  Df Deviance    AIC
## - Turbidity       1   3051.8 3061.8
## - Hardness        1   3052.2 3062.2
## <none>                3051.0 3063.0
## - Chloramines     1   3055.7 3065.7
## - Solids          1   3056.5 3066.5
## - Organic_carbon  1   3056.7 3066.7
## 
## Step:  AIC=3061.75
## Potability ~ Hardness + Solids + Chloramines + Organic_carbon
## 
##                  Df Deviance    AIC
## - Hardness        1   3052.9 3060.9
## <none>                3051.8 3061.8
## - Chloramines     1   3056.4 3064.4
## - Solids          1   3057.2 3065.2
## - Organic_carbon  1   3057.5 3065.5
## 
## Step:  AIC=3060.93
## Potability ~ Solids + Chloramines + Organic_carbon
## 
##                  Df Deviance    AIC
## <none>                3052.9 3060.9
## - Chloramines     1   3057.7 3063.7
## - Solids          1   3058.7 3064.7
## - Organic_carbon  1   3058.9 3064.9
summary(research)
## 
## Call:
## glm(formula = Potability ~ Solids + Chloramines + Organic_carbon, 
##     family = binomial(link = "logit"), data = train_data)
## 
## Coefficients:
##                  Estimate Std. Error z value Pr(>|z|)  
## (Intercept)    -6.831e-01  2.974e-01  -2.297   0.0216 *
## Solids          1.174e-05  4.907e-06   2.393   0.0167 *
## Chloramines     5.946e-02  2.717e-02   2.189   0.0286 *
## Organic_carbon -3.153e-02  1.297e-02  -2.431   0.0151 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 3068.5  on 2293  degrees of freedom
## Residual deviance: 3052.9  on 2290  degrees of freedom
## AIC: 3060.9
## 
## Number of Fisher Scoring iterations: 4
confint(research)
## Waiting for profiling to be done...
##                        2.5 %        97.5 %
## (Intercept)    -1.267567e+00 -1.012542e-01
## Solids          2.124268e-06  2.137074e-05
## Chloramines     6.308056e-03  1.128470e-01
## Organic_carbon -5.702291e-02 -6.155123e-03
exp(cbind(OR = coef(research), confint(research)))
## Waiting for profiling to be done...
##                       OR     2.5 %    97.5 %
## (Intercept)    0.5050704 0.2815156 0.9037032
## Solids         1.0000117 1.0000021 1.0000214
## Chloramines    1.0612651 1.0063280 1.1194606
## Organic_carbon 0.9689596 0.9445724 0.9938638
predicted <- predict(research,test_data, type="response")
test_data$predicted<-round(predicted)
head(test_data,10)
##           ph Hardness   Solids Chloramines  Sulfate Conductivity Organic_carbon
## 3   8.099124 224.2363 19909.54    9.275884 334.5643     418.6062      16.868637
## 6   5.584087 188.3133 28748.69    7.544869 326.6784     280.4679       8.399735
## 8   8.635849 203.3615 13672.09    4.563009 303.3098     474.6076      12.363817
## 9   6.721973 118.9886 14285.58    7.804174 268.6469     389.3756      12.706049
## 16  6.347272 186.7329 41065.23    9.629596 364.4877     516.7433      11.539781
## 26  6.514415 198.7674 21218.70    8.670937 323.5963     413.2905      14.900000
## 30  6.721973 148.1531 15193.41    9.046833 307.0118     563.8047      16.568656
## 33 10.433291 117.7912 22326.89    8.161505 307.7075     412.9868      12.890709
## 37  3.641630 183.9087 24752.07    5.538314 286.0596     456.8601       9.034067
## 38  5.618064 304.2359 17281.98    6.101084 334.5643     399.4716      12.265002
##    Trihalomethanes Turbidity Potability predicted
## 3         66.42009  3.055934          0         0
## 6         54.91786  2.559708          0         0
## 8         62.79831  4.401425          0         0
## 9         53.92885  3.595017          0         0
## 16        75.07162  4.376348          0         1
## 26        79.84784  5.200885          0         0
## 30        52.67619  6.038185          0         0
## 33        65.73348  5.057311          0         0
## 37        73.59466  3.464353          0         0
## 38        81.58899  2.896547          0         0
Observation<-table(test_data$Potability)
Predicted_value<-table(test_data$predicted)
rbind(Observation,Predicted_value)
##                   0   1
## Observation     599 383
## Predicted_value 974   8
table(test_data$Potability, test_data$predicted)
##    
##       0   1
##   0 594   5
##   1 380   3
predictions = predict(research, newdata=test_data, type='response')
predicted_classes = ifelse(predicted > 0.5, 1, 0)

accuracy = sum(predicted_classes == test_data$Potability) / nrow(test_data)
print(paste('Accuracy:', accuracy))
## [1] "Accuracy: 0.607942973523422"
library ( ROCR )
ROCRpred=prediction(predictions, test_data$Potability)
ROCRperf=performance( ROCRpred , "tpr", "fpr")
plot(ROCRperf)

as.numeric(performance(ROCRpred, "auc")@y.values) 
## [1] 0.4854392
qqnorm(water_potability$ph)
qqline(water_potability$ph)

qqnorm(water_potability$Organic_carbon)
qqline(water_potability$Organic_carbon)


water_potability$ph_category <- cut(water_potability$ph, breaks = c(0, 6.5, 8.5, 14), labels = c("Acidic", "Neutral", "Alkaline"))
water_potability$organic_carbon_category <- cut(water_potability$Organic_carbon, breaks = c(0, 10, 20, 30, Inf), labels = c("Low", "Moderate", "High", "Very High"))
head(water_potability)
##         ph Hardness   Solids Chloramines  Sulfate Conductivity Organic_carbon
## 1 7.112512 204.8905 20791.32    7.300212 368.5164     564.3087      10.379783
## 2 3.716080 129.4229 18630.06    6.635246 334.5643     592.8854      15.180013
## 3 8.099124 224.2363 19909.54    9.275884 334.5643     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 ph_category organic_carbon_category
## 1        86.99097  2.963135          0     Neutral                Moderate
## 2        56.32908  4.500656          0      Acidic                Moderate
## 3        66.42009  3.055934          0     Neutral                Moderate
## 4       100.34167  4.628771          0     Neutral                Moderate
## 5        31.99799  4.075075          0    Alkaline                Moderate
## 6        54.91786  2.559708          0      Acidic                     Low
library(car)
## Loading required package: carData
water_potability$Potability <- as.numeric(water_potability$Potability)
str(water_potability$Potability)
##  num [1:3276] 1 1 1 1 1 1 1 1 1 1 ...
leveneTest(Potability ~ ph_category * organic_carbon_category , data = water_potability)
## Levene's Test for Homogeneity of Variance (center = median)
##         Df F value  Pr(>F)  
## group    8  2.0831 0.03411 *
##       3266                  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
anova<-aov(Potability~ph_category*organic_carbon_category, data=water_potability)
summary(anova)
##                                       Df Sum Sq Mean Sq F value   Pr(>F)    
## ph_category                            2    3.6  1.7771   7.486 0.000571 ***
## organic_carbon_category                2    0.1  0.0258   0.109 0.897022    
## ph_category:organic_carbon_category    4    0.4  0.0876   0.369 0.830876    
## Residuals                           3266  775.3  0.2374                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 1 observation deleted due to missingness
#VI. Thảo luận và mở rộng

library(rpart)
library(rpart.plot)
fit=rpart(Potability~., data=train_data, method="class")
rpart.plot(fit, extra="auto")

test_data$predicted_2=predict(fit, test_data, type="class")
test_data$predicted_2=as.numeric(as.character(test_data$predicted_2))
head(test_data, 10)
##           ph Hardness   Solids Chloramines  Sulfate Conductivity Organic_carbon
## 3   8.099124 224.2363 19909.54    9.275884 334.5643     418.6062      16.868637
## 6   5.584087 188.3133 28748.69    7.544869 326.6784     280.4679       8.399735
## 8   8.635849 203.3615 13672.09    4.563009 303.3098     474.6076      12.363817
## 9   6.721973 118.9886 14285.58    7.804174 268.6469     389.3756      12.706049
## 16  6.347272 186.7329 41065.23    9.629596 364.4877     516.7433      11.539781
## 26  6.514415 198.7674 21218.70    8.670937 323.5963     413.2905      14.900000
## 30  6.721973 148.1531 15193.41    9.046833 307.0118     563.8047      16.568656
## 33 10.433291 117.7912 22326.89    8.161505 307.7075     412.9868      12.890709
## 37  3.641630 183.9087 24752.07    5.538314 286.0596     456.8601       9.034067
## 38  5.618064 304.2359 17281.98    6.101084 334.5643     399.4716      12.265002
##    Trihalomethanes Turbidity Potability predicted predicted_2
## 3         66.42009  3.055934          0         0           0
## 6         54.91786  2.559708          0         0           0
## 8         62.79831  4.401425          0         0           0
## 9         53.92885  3.595017          0         0           0
## 16        75.07162  4.376348          0         1           0
## 26        79.84784  5.200885          0         0           0
## 30        52.67619  6.038185          0         0           0
## 33        65.73348  5.057311          0         0           1
## 37        73.59466  3.464353          0         0           0
## 38        81.58899  2.896547          0         0           0
Predicted_value_2<-table(test_data$predicted_2)
rbind(Observation,Predicted_value_2)
##                     0   1
## Observation       599 383
## Predicted_value_2 690 292
table(test_data$Potability, test_data$predicted_2)
##    
##       0   1
##   0 537  62
##   1 153 230
predictions_2 = predict(fit, newdata=test_data, type='class')
accuracy = sum(predictions_2 == test_data$Potability) / nrow(test_data)
print(paste('Accuracy:', accuracy))
## [1] "Accuracy: 0.781059063136456"
ROCRpred2 = prediction ( test_data$predicted_2, test_data$Potability )
ROCRperf2 = performance ( ROCRpred2 , "tpr", "fpr")
plot(ROCRperf2)

as.numeric(performance(ROCRpred2,"auc")@y.values )
## [1] 0.7485082