SOURCE CODES:
setwd("F:/project")
> Telecom = read_excel("Cellphone-1.xlsx",sheet = "Data")
> str(Telecom)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 3333 obs. of 11 variables:
$ Churn : num 0 0 0 0 0 0 0 0 0 0 ...
$ AccountWeeks : num 128 107 137 84 75 118 121 147 117 141 ...
$ ContractRenewal: num 1 1 1 0 0 0 1 0 1 0 ...
$ DataPlan : num 1 1 0 0 0 0 1 0 0 1 ...
$ DataUsage : num 2.7 3.7 0 0 0 0 2.03 0 0.19 3.02 ...
$ CustServCalls : num 1 1 0 2 3 0 3 0 1 0 ...
$ DayMins : num 265 162 243 299 167 ...
$ DayCalls : num 110 123 114 71 113 98 88 79 97 84 ...
$ MonthlyCharge : num 89 82 52 57 41 57 87.3 36 63.9 93.2 ...
$ OverageFee : num 9.87 9.78 6.06 3.1 7.42 ...
$ RoamMins : num 10 13.7 12.2 6.6 10.1 6.3 7.5 7.1 8.7 11.2 ...
> dim(Telecom)
[1] 3333 11
> sum(is.na(Telecom))
[1] 0
> min(Telecom$AccountWeeks); max(Telecom$AccountWeeks)
[1] 1
[1] 243
> summary(Telecom)
Churn AccountWeeks ContractRenewal DataPlan
Min. :0.0000 Min. : 1.0 Min. :0.0000 Min. :0.0000
1st Qu.:0.0000 1st Qu.: 74.0 1st Qu.:1.0000 1st Qu.:0.0000
Median :0.0000 Median :101.0 Median :1.0000 Median :0.0000
Mean :0.1449 Mean :101.1 Mean :0.9031 Mean :0.2766
3rd Qu.:0.0000 3rd Qu.:127.0 3rd Qu.:1.0000 3rd Qu.:1.0000
Max. :1.0000 Max. :243.0 Max. :1.0000 Max. :1.0000
DataUsage CustServCalls DayMins DayCalls MonthlyCharge
Min. :0.0000 Min. :0.000 Min. : 0.0 Min. : 0.0 Min. : 14.00
1st Qu.:0.0000 1st Qu.:1.000 1st Qu.:143.7 1st Qu.: 87.0 1st Qu.: 45.00
Median :0.0000 Median :1.000 Median :179.4 Median :101.0 Median : 53.50
Mean :0.8165 Mean :1.563 Mean :179.8 Mean :100.4 Mean : 56.31
3rd Qu.:1.7800 3rd Qu.:2.000 3rd Qu.:216.4 3rd Qu.:114.0 3rd Qu.: 66.20
Max. :5.4000 Max. :9.000 Max. :350.8 Max. :165.0 Max. :111.30
OverageFee RoamMins
Min. : 0.00 Min. : 0.00
1st Qu.: 8.33 1st Qu.: 8.50
Median :10.07 Median :10.30
Mean :10.05 Mean :10.24
3rd Qu.:11.77 3rd Qu.:12.10
Max. :18.19 Max. :20.00
> boxplot(Telecom)
> par(mfrow=c(3,3))
> hist(Telecom$AccountWeeks)
> hist(Telecom$DataUsage)
> hist(Telecom$CustServCalls)
> hist(Telecom$DayMins)
> hist(Telecom$DayCalls)
> hist(Telecom$DataUsage)
> hist(Telecom$MonthlyCharge)
> hist(Telecom$OverageFee)
> hist(Telecom$RoamMins)
> attach(Telecom)
The following objects are masked from Telecom (pos = 4):
AccountWeeks, Churn, ContractRenewal, CustServCalls, DataPlan,
DataUsage, DayCalls, DayMins, MonthlyCharge, OverageFee, RoamMins
> library(ggplot2)
> library(corrplot)
> corrplot(cor(Telecom))
> qplot(AccountWeeks,DataUsage,data=Telecom)
> qplot(AccountWeeks,DayMins,data=Telecom)
> qplot(AccountWeeks,MonthlyCharge,data=Telecom)
> qplot(AccountWeeks,OverageFee,data=Telecom)
> qplot(AccountWeeks,RoamMins,data=Telecom)
> hist(Churn,CustServCalls,Telecom)
Error in freq && !equidist : invalid 'x' type in 'x && y'
> numeric.var = sapply(Telecom, is.numeric)
> corr.matrix <- cor(Telecom[,numeric.var])
> corrplot(corr.matrix, main="\n\nCorrelation Plot for Numerical Variables", method="number")
> Telecom$Dataplan = NULL
> Telecom$DataUsage = NULL
> str(Telecom)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 3333 obs. of 10 variables:
$ Churn : num 0 0 0 0 0 0 0 0 0 0 ...
$ AccountWeeks : num 128 107 137 84 75 118 121 147 117 141 ...
$ ContractRenewal: num 1 1 1 0 0 0 1 0 1 0 ...
$ DataPlan : num 1 1 0 0 0 0 1 0 0 1 ...
$ CustServCalls : num 1 1 0 2 3 0 3 0 1 0 ...
$ DayMins : num 265 162 243 299 167 ...
$ DayCalls : num 110 123 114 71 113 98 88 79 97 84 ...
$ MonthlyCharge : num 89 82 52 57 41 57 87.3 36 63.9 93.2 ...
$ OverageFee : num 9.87 9.78 6.06 3.1 7.42 ...
$ RoamMins : num 10 13.7 12.2 6.6 10.1 6.3 7.5 7.1 8.7 11.2 ...
> churnrate = table(Telecom$Churn) / nrow(Telecom)
> churnrate
0 1
0.8550855 0.1449145
> library(class)
> library(gmodels)
> library(datasets)
> summary(Telecom)
Churn AccountWeeks ContractRenewal DataPlan CustServCalls
Min. :0.0000 Min. : 1.0 Min. :0.0000 Min. :0.0000 Min. :0.000
1st Qu.:0.0000 1st Qu.: 74.0 1st Qu.:1.0000 1st Qu.:0.0000 1st Qu.:1.000
Median :0.0000 Median :101.0 Median :1.0000 Median :0.0000 Median :1.000
Mean :0.1449 Mean :101.1 Mean :0.9031 Mean :0.2766 Mean :1.563
3rd Qu.:0.0000 3rd Qu.:127.0 3rd Qu.:1.0000 3rd Qu.:1.0000 3rd Qu.:2.000
Max. :1.0000 Max. :243.0 Max. :1.0000 Max. :1.0000 Max. :9.000
DayMins DayCalls MonthlyCharge OverageFee RoamMins
Min. : 0.0 Min. : 0.0 Min. : 14.00 Min. : 0.00 Min. : 0.00
1st Qu.:143.7 1st Qu.: 87.0 1st Qu.: 45.00 1st Qu.: 8.33 1st Qu.: 8.50
Median :179.4 Median :101.0 Median : 53.50 Median :10.07 Median :10.30
Mean :179.8 Mean :100.4 Mean : 56.31 Mean :10.05 Mean :10.24
3rd Qu.:216.4 3rd Qu.:114.0 3rd Qu.: 66.20 3rd Qu.:11.77 3rd Qu.:12.10
Max. :350.8 Max. :165.0 Max. :111.30 Max. :18.19 Max. :20.00
> Telecom$Churn = as.factor(Telecom$Churn)
> mydata=createDataPartition(y=Telecom$Churn,p=0.7,list=FALSE)
> set.seed(123)
> trainData=Telecom[mydata,]
> testData=Telecom[-mydata,]
> dim(trainData)
[1] 2334 10
> dim(testData)
[1] 999 10
> print(sum(trainData$Churn=="1")/nrow(trainData))
[1] 0.1452442
> lmodel=glm(Churn~.,family = binomial(link = "logit"),data=trainData)
> print(summary(lmodel))
Call:
glm(formula = Churn ~ ., family = binomial(link = "logit"), data = trainData)
Deviance Residuals:
Min 1Q Median 3Q Max
-2.0553 -0.5045 -0.3431 -0.1992 3.0525
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -6.2861356 0.6535782 -9.618 < 2e-16 ***
AccountWeeks 0.0003457 0.0016766 0.206 0.83665
ContractRenewal -1.9676217 0.1713300 -11.484 < 2e-16 ***
DataPlan -1.3577425 0.6468784 -2.099 0.03582 *
CustServCalls 0.5539090 0.0483978 11.445 < 2e-16 ***
DayMins 0.0107140 0.0039687 2.700 0.00694 **
DayCalls 0.0039345 0.0033077 1.190 0.23424
MonthlyCharge 0.0119062 0.0220000 0.541 0.58838
OverageFee 0.1372180 0.0460986 2.977 0.00291 **
RoamMins 0.0833328 0.0261358 3.188 0.00143 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 1934.3 on 2333 degrees of freedom
Residual deviance: 1515.6 on 2324 degrees of freedom
AIC: 1535.6
Number of Fisher Scoring iterations: 6
> vif(lmodel)
AccountWeeks ContractRenewal DataPlan CustServCalls DayMins
1.003981 1.055954 13.715112 1.100637 10.060967
DayCalls MonthlyCharge OverageFee RoamMins
1.007451 26.125413 2.898266 1.178697
> lrtest(lmodel)
Likelihood ratio test
Model 1: Churn ~ AccountWeeks + ContractRenewal + DataPlan + CustServCalls +
DayMins + DayCalls + MonthlyCharge + OverageFee + RoamMins
Model 2: Churn ~ 1
#Df LogLik Df Chisq Pr(>Chisq)
1 10 -757.81
2 1 -967.14 -9 418.66 < 2.2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> anova(lmodel, test="Chisq")
Analysis of Deviance Table
Model: binomial, link: logit
Response: Churn
Terms added sequentially (first to last)
Df Deviance Resid. Df Resid. Dev Pr(>Chi)
NULL 2333 1934.3
AccountWeeks 1 0.115 2332 1934.2 0.734750
ContractRenewal 1 121.008 2331 1813.2 < 2.2e-16 ***
DataPlan 1 33.934 2330 1779.2 5.702e-09 ***
CustServCalls 1 111.989 2329 1667.2 < 2.2e-16 ***
DayMins 1 104.501 2328 1562.7 < 2.2e-16 ***
DayCalls 1 1.206 2327 1561.5 0.272175
MonthlyCharge 1 31.286 2326 1530.2 2.227e-08 ***
OverageFee 1 4.268 2325 1526.0 0.038842 *
RoamMins 1 10.354 2324 1515.6 0.001292 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> pr2(lmodel)
Error in pr2(lmodel) : could not find function "pr2"
> ROCRpred=prediction(predTest,testData$Churn)
Error in is.data.frame(predictions) : object 'predTest' not found
> fitted.results = predict(lmodel,newdata=testing,type='response')
> fitted.results = ifelse(fitted.results > 0.5,1,0)
> misClasificError = mean(fitted.results != testing$Churn)
> print(paste('Logistic Regression Accuracy',1-misClasificError))
[1] "Logistic Regression Accuracy 0.85985985985986"
> print("Confusion Matrix for Logistic Regression"); table(testing$Churn, fitted.results > 0.5)
[1] "Confusion Matrix for Logistic Regression"
FALSE TRUE
0 827 28
1 112 32
> ROCRpred=prediction(lmodel,testData$Churn)
Error in prediction(lmodel, testData$Churn) :
Number of cross-validation runs must be equal for predictions and labels.
> lmodel2=glm(formula = Churn ~ AccountWeeks + ContractRenewal + CustServCalls + DayCalls + DayMins +
OverageFee + RoamMins, family = binomial, data = trainData)
> summary(lmodel2)
Call:
glm(formula = Churn ~ AccountWeeks + ContractRenewal + CustServCalls +
DayCalls + DayMins + OverageFee + RoamMins, family = binomial,
data = trainData)
Deviance Residuals:
Min 1Q Median 3Q Max
-1.9708 -0.5214 -0.3588 -0.2287 2.9352
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -6.308e+00 6.421e-01 -9.825 < 2e-16 ***
AccountWeeks -1.046e-05 1.670e-03 -0.006 0.995000
ContractRenewal -1.920e+00 1.682e-01 -11.418 < 2e-16 ***
CustServCalls 5.448e-01 4.768e-02 11.425 < 2e-16 ***
DayCalls 3.590e-03 3.263e-03 1.100 0.271201
DayMins 1.244e-02 1.266e-03 9.829 < 2e-16 ***
OverageFee 1.451e-01 2.706e-02 5.363 8.2e-08 ***
RoamMins 8.795e-02 2.411e-02 3.648 0.000264 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 1934.3 on 2333 degrees of freedom
Residual deviance: 1554.7 on 2326 degrees of freedom
AIC: 1570.7
Number of Fisher Scoring iterations: 5
> lrtest(lmodel2)
Likelihood ratio test
Model 1: Churn ~ AccountWeeks + ContractRenewal + CustServCalls + DayCalls +
DayMins + OverageFee + RoamMins
Model 2: Churn ~ 1
#Df LogLik Df Chisq Pr(>Chisq)
1 8 -777.35
2 1 -967.14 -7 379.59 < 2.2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> exp(coefficients(lmodel2))
(Intercept) AccountWeeks ContractRenewal CustServCalls DayCalls
0.001821575 0.999989535 0.146577427 1.724210932 1.003596874
DayMins OverageFee RoamMins
1.012522052 1.156162339 1.091929137
> exp(coefficients(lmodel))
(Intercept) AccountWeeks ContractRenewal DataPlan CustServCalls
0.001861941 1.000345730 0.139788918 0.257240832 1.740041634
DayMins DayCalls MonthlyCharge OverageFee RoamMins
1.010771582 1.003942288 1.011977335 1.147078233 1.086903477
> library(pscl)
> vif(lmodel2)
AccountWeeks ContractRenewal CustServCalls DayCalls DayMins
1.001995 1.048199 1.090312 1.005826 1.042210
OverageFee RoamMins
1.027652 1.013871
> exp(cbind(OR=coef(lmodel), confint(lmodel)))
Waiting for profiling to be done...
OR 2.5 % 97.5 %
(Intercept) 0.001861941 0.0005079769 0.006595388
AccountWeeks 1.000345730 0.9970605030 1.003639008
ContractRenewal 0.139788918 0.0997948023 0.195471858
DataPlan 0.257240832 0.0705683186 0.894293449
CustServCalls 1.740041634 1.5838161710 1.915049172
DayMins 1.010771582 1.0029369394 1.018685566
DayCalls 1.003942288 0.9974623414 1.010487466
MonthlyCharge 1.011977335 0.9693289613 1.056782262
OverageFee 1.147078233 1.0480111034 1.255799098
RoamMins 1.086903477 1.0329465685 1.144463931
> pR2(lmodel2)
fitting null model for pseudo-r2
llh llhNull G2 McFadden r2ML r2CU
-777.3450017 -967.1400908 379.5901782 0.1962436 0.1500987 0.2664153
> confusionMatrix(trainData$Churn)
Error in is.factor(reference) :
argument "reference" is missing, with no default
> pred=predict(rf_model, newdata = training)
> predictglm=predict(lmodel2,type="response",newdata=training)
> predictglm
1 2 3 4 5 6 7
0.055244690 0.264484362 0.407305938 0.151309272 0.451610888 0.235784283 0.030726392
8 9 10 11 12 13 14
0.018147025 0.189685333 0.285866309 0.752697181 0.179901815 0.065177658 0.093862034
15 16 17 18 19 20 21
0.038882490 0.064498135 0.014993121 0.023610962 0.014389223 0.050086694 0.096266915
22 23 24 25 26 27 28
0.045420345 0.037623708 0.029241858 0.106680486 0.100776266 0.331898667 0.029496974
29 30 31 32 33 34 35
0.034770305 0.628862013 0.065855263 0.067852410 0.085904364 0.027090311 0.047714705
36 37 38 39 40 41 42
0.339919383 0.235915811 0.115243485 0.122749579 0.293088537 0.072206629 0.023103544
43 44 45 46 47 48 49
0.068755674 0.107258171 0.062356680 0.233918361 0.175688550 0.037159184 0.391755142
50 51 52 53 54 55 56
0.100665390 0.264915201 0.273501072 0.194784200 0.087740128 0.062530112 0.037378493
57 58 59 60 61 62 63
0.146971395 0.027046757 0.059015681 0.187948891 0.725707846 0.105595218 0.218262238
64 65 66 67 68 69 70
0.132380745 0.047674796 0.036137849 0.142325863 0.268645629 0.069811745 0.199005195
71 72 73 74 75 76 77
0.194956148 0.013916877 0.038506972 0.048765050 0.387496103 0.266489613 0.024343726
78 79 80 81 82 83 84
0.024871205 0.183219678 0.096123930 0.235649561 0.517287827 0.047058360 0.208073535
85 86 87 88 89 90 91
0.036194044 0.126098241 0.159986837 0.039723922 0.093496455 0.055224029 0.161811037
92 93 94 95 96 97 98
0.030465634 0.181316399 0.029836842 0.131283236 0.244423852 0.398195386 0.284073480
99 100 101 102 103 104 105
0.248531610 0.043941016 0.097410965 0.088841575 0.048787379 0.142159181 0.119183266
106 107 108 109 110 111 112
0.283530117 0.065401609 0.084791377 0.068645908 0.363509855 0.056871813 0.098102928
113 114 115 116 117 118 119
0.013242985 0.089082268 0.225866596 0.017493632 0.049219592 0.237068474 0.069551693
120 121 122 123 124 125 126
0.041921542 0.171720096 0.084526869 0.087977763 0.032940211 0.194580366 0.026153588
127 128 129 130 131 132 133
0.219521111 0.241060657 0.762075293 0.083909669 0.062600188 0.207278711 0.789047769
134 135 136 137 138 139 140
0.016631223 0.292780404 0.035143823 0.015275471 0.026653373 0.111883725 0.031016663
141 142 143 144 145 146 147
0.046155016 0.350597917 0.926931809 0.010573622 0.181747645 0.241962715 0.157692682
148 149 150 151 152 153 154
0.029392371 0.125658262 0.177618800 0.195229385 0.360053575 0.038908328 0.760832267
155 156 157 158 159 160 161
0.151540349 0.023785342 0.116293464 0.258268498 0.026703529 0.152719263 0.028099236
162 163 164 165 166 167 168
0.063538300 0.220623179 0.667106591 0.456912242 0.276637517 0.038336591 0.360824030
169 170 171 172 173 174 175
0.264649262 0.069435437 0.050729734 0.007846075 0.050510584 0.241910201 0.168620148
176 177 178 179 180 181 182
0.011803533 0.004501644 0.349314742 0.230604473 0.238407022 0.326986708 0.032011701
183 184 185 186 187 188 189
0.042373584 0.086136350 0.047603529 0.041957878 0.035501890 0.048040539 0.076507024
190 191 192 193 194 195 196
0.177169140 0.124984047 0.004982398 0.344389586 0.085891097 0.026834074 0.039612861
197 198 199 200 201 202 203
0.283699857 0.313397460 0.048954975 0.704088870 0.096035930 0.131108052 0.082956333
204 205 206 207 208 209 210
0.144662528 0.080838339 0.187333127 0.231163200 0.067178640 0.008113975 0.223663979
211 212 213 214 215 216 217
0.032393913 0.069800638 0.043567910 0.323541553 0.045544018 0.053760643 0.100422342
218 219 220 221 222 223 224
0.082025230 0.230814034 0.635182022 0.342601214 0.191417453 0.108833795 0.037941719
225 226 227 228 229 230 231
0.024760009 0.040554960 0.045093920 0.067987668 0.512189007 0.034698620 0.033083750
232 233 234 235 236 237 238
0.033992687 0.199479764 0.007344073 0.018553966 0.145279889 0.067899431 0.089805036
239 240 241 242 243 244 245
0.291716069 0.529706008 0.046149097 0.044368502 0.052001398 0.048817521 0.083746201
246 247 248 249 250 251 252
0.222010938 0.061457142 0.101930015 0.063779283 0.008963478 0.025205278 0.035988246
253 254 255 256 257 258 259
0.131659734 0.069192076 0.025670472 0.146963937 0.677764954 0.028008689 0.059112358
260 261 262 263 264 265 266
0.245769507 0.047702691 0.060391792 0.097754383 0.019859884 0.110079405 0.148790580
267 268 269 270 271 272 273
0.186701843 0.091566627 0.051067727 0.073327087 0.421820893 0.093967491 0.047720898
274 275 276 277 278 279 280
0.095241813 0.410089077 0.440256011 0.145673854 0.078252866 0.040211502 0.114668484
281 282 283 284 285 286 287
0.083595511 0.021607127 0.441161198 0.135887351 0.066100090 0.017839388 0.100228649
288 289 290 291 292 293 294
0.040797082 0.035699478 0.038523187 0.090705746 0.256346923 0.330205186 0.008689705
295 296 297 298 299 300 301
0.178790320 0.256751325 0.072053409 0.055850657 0.033022396 0.078903850 0.016887315
302 303 304 305 306 307 308
0.236878939 0.157705586 0.056657884 0.201511924 0.195214200 0.244325728 0.069549922
309 310 311 312 313 314 315
0.356959161 0.022384058 0.023406153 0.088546835 0.112903551 0.126570640 0.074486048
316 317 318 319 320 321 322
0.115206912 0.071966148 0.120784989 0.137523109 0.052839818 0.171174775 0.031812958
323 324 325 326 327 328 329
0.157120053 0.375048244 0.130018240 0.163758053 0.036175161 0.388910162 0.083999078
330 331 332 333 334 335 336
0.030718091 0.253350041 0.296944575 0.030085922 0.143304982 0.179147616 0.134483134
337 338 339 340 341 342 343
0.013436083 0.012622716 0.061707906 0.243976253 0.068859392 0.141560797 0.131064937
344 345 346 347 348 349 350
0.077109757 0.031233184 0.093523742 0.101698761 0.028275708 0.019808672 0.129548349
351 352 353 354 355 356 357
0.330306931 0.011827513 0.092578852 0.063689055 0.067160162 0.095760121 0.516835537
358 359 360 361 362 363 364
0.384648454 0.060844342 0.358178674 0.080512026 0.131313819 0.549272782 0.043170854
365 366 367 368 369 370 371
0.205028509 0.336616986 0.038359969 0.386802642 0.087850969 0.042053132 0.043804469
372 373 374 375 376 377 378
0.065288147 0.076177816 0.129870078 0.819740301 0.115140541 0.065458851 0.038779886
379 380 381 382 383 384 385
0.064897628 0.073824579 0.184708962 0.018368215 0.049767902 0.068669428 0.049738631
386 387 388 389 390 391 392
0.196966385 0.104084141 0.055240760 0.755780497 0.691388371 0.011179481 0.158721290
393 394 395 396 397 398 399
0.185452785 0.540372360 0.118610348 0.077125596 0.111474047 0.029832098 0.065655459
400 401 402 403 404 405 406
0.041668254 0.207477816 0.128785187 0.089776488 0.785909207 0.062827538 0.152759576
407 408 409 410 411 412 413
0.271167549 0.275608648 0.073192003 0.058423416 0.211570590 0.037851647 0.133983514
414 415 416 417 418 419 420
0.010586634 0.014919180 0.291019280 0.017113857 0.295582580 0.041698017 0.185006069
421 422 423 424 425 426 427
0.033757680 0.042376542 0.049343992 0.204814090 0.040603883 0.069740630 0.081959784
428 429 430 431 432 433 434
0.492757919 0.056541035 0.056353265 0.128402337 0.060638015 0.206503794 0.504395376
435 436 437 438 439 440 441
0.181328684 0.047927543 0.410189562 0.230635460 0.061372823 0.566582137 0.079306236
442 443 444 445 446 447 448
0.022075318 0.092072928 0.151580504 0.165950037 0.186677652 0.024058537 0.230974666
449 450 451 452 453 454 455
0.049346235 0.016802157 0.051112585 0.132075791 0.017606055 0.137138541 0.054926393
456 457 458 459 460 461 462
0.052471262 0.120419045 0.130678044 0.267725487 0.116735984 0.032830993 0.004515576
463 464 465 466 467 468 469
0.260903087 0.087296282 0.145539310 0.170663317 0.068673838 0.028636655 0.100114064
470 471 472 473 474 475 476
0.015378516 0.250767188 0.146583601 0.075410983 0.052502929 0.117346309 0.162583630
477 478 479 480 481 482 483
0.022354389 0.133321091 0.068050663 0.298611951 0.069482714 0.130991001 0.053613388
484 485 486 487 488 489 490
0.054510543 0.035400347 0.603773333 0.012477149 0.045527016 0.080858437 0.572201442
491 492 493 494 495 496 497
0.101549542 0.249642783 0.100272577 0.547769355 0.040363230 0.037064198 0.053612929
498 499 500 501 502 503 504
0.009279076 0.046005113 0.178914896 0.048003890 0.130240348 0.086240414 0.224315640
505 506 507 508 509 510 511
0.334086963 0.127250400 0.074363794 0.081502235 0.051187962 0.117681238 0.020427403
512 513 514 515 516 517 518
0.204631687 0.112390843 0.176760007 0.081415639 0.027802758 0.091079226 0.314291368
519 520 521 522 523 524 525
0.048397026 0.114590760 0.323024505 0.169127550 0.252156622 0.413063662 0.059215987
526 527 528 529 530 531 532
0.077879184 0.013445082 0.512858401 0.090383770 0.050593136 0.016639312 0.144084379
533 534 535 536 537 538 539
0.089148524 0.016870498 0.157170655 0.029481452 0.049012165 0.050315934 0.292595507
540 541 542 543 544 545 546
0.080060454 0.099802575 0.143259295 0.255198264 0.031420870 0.101434017 0.099734475
547 548 549 550 551 552 553
0.056768117 0.040785549 0.218881882 0.049759606 0.076689857 0.203116424 0.702288028
554 555 556 557 558 559 560
0.298404572 0.075386207 0.200189282 0.343310217 0.051259071 0.262333356 0.097556974
561 562 563 564 565 566 567
0.401595756 0.142149074 0.628581201 0.048858757 0.030990742 0.355123658 0.246828140
568 569 570 571 572 573 574
0.183786492 0.137537135 0.085895422 0.026680668 0.030197217 0.183002643 0.058935122
575 576 577 578 579 580 581
0.116399413 0.034162708 0.087450128 0.044101705 0.245074349 0.020762078 0.110456583
582 583 584 585 586 587 588
0.059331014 0.056080306 0.006392524 0.022499427 0.099287127 0.277851488 0.040444591
589 590 591 592 593 594 595
0.127400171 0.403337370 0.393022072 0.063365405 0.027588084 0.015896014 0.059417748
596 597 598 599 600 601 602
0.076074284 0.163792589 0.099529669 0.468768516 0.093713675 0.062607481 0.178328210
603 604 605 606 607 608 609
0.056512397 0.166022979 0.652362395 0.325124660 0.100371013 0.181098196 0.166234320
610 611 612 613 614 615 616
0.050742361 0.071275577 0.440271499 0.085478669 0.071258644 0.133803831 0.066207624
617 618 619 620 621 622 623
0.115753716 0.285901437 0.108238486 0.021329189 0.033548901 0.404235298 0.037380945
624 625 626 627 628 629 630
0.527983673 0.337173131 0.062047026 0.460939761 0.574681056 0.046739109 0.074948827
631 632 633 634 635 636 637
0.213511817 0.207478864 0.013521014 0.096568355 0.047390321 0.013631621 0.068123581
638 639 640 641 642 643 644
0.385794553 0.059397666 0.037574501 0.110157384 0.064561368 0.516626507 0.035791886
645 646 647 648 649 650 651
0.156104606 0.064150668 0.025691328 0.019826151 0.870969058 0.046615157 0.179577146
652 653 654 655 656 657 658
0.231422343 0.054331540 0.020354341 0.015703282 0.030272247 0.034600567 0.009766684
659 660 661 662 663 664 665
0.040313997 0.022634282 0.066388700 0.065001022 0.025910000 0.147385722 0.021322268
666 667 668 669 670 671 672
0.460894248 0.043328927 0.074302729 0.092664538 0.028826325 0.090902708 0.224537166
673 674 675 676 677 678 679
0.349346390 0.045447735 0.153818801 0.249957797 0.085044128 0.032154403 0.215266773
680 681 682 683 684 685 686
0.284275882 0.059741925 0.230554440 0.136153900 0.102890510 0.146407229 0.064655570
687 688 689 690 691 692 693
0.104293634 0.050120403 0.082614253 0.120370904 0.091548822 0.464095945 0.208053715
694 695 696 697 698 699 700
0.014291362 0.143606244 0.702829603 0.011861065 0.196868026 0.043365849 0.095935161
701 702 703 704 705 706 707
0.021692951 0.641114855 0.152973779 0.238219922 0.061890195 0.012133507 0.011772023
708 709 710 711 712 713 714
0.385152067 0.040717945 0.019106829 0.033455971 0.398671353 0.020445233 0.036199755
715 716 717 718 719 720 721
0.030913369 0.402744780 0.196981646 0.025867278 0.037543807 0.150791665 0.064641790
722 723 724 725 726 727 728
0.035839589 0.054449273 0.008998777 0.231321807 0.035123905 0.017298641 0.049854094
729 730 731 732 733 734 735
0.271969628 0.283013776 0.067389200 0.494069296 0.091757982 0.022677274 0.069215485
736 737 738 739 740 741 742
0.143734788 0.049047430 0.121503905 0.098539631 0.152438770 0.056412464 0.123884164
743 744 745 746 747 748 749
0.139577436 0.728612014 0.186436357 0.027617033 0.053720263 0.025214128 0.089330288
750 751 752 753 754 755 756
0.055761180 0.081723445 0.036745017 0.047795021 0.090475437 0.078496215 0.289658734
757 758 759 760 761 762 763
0.068827249 0.066517769 0.047085502 0.023542616 0.183547428 0.034109594 0.179474841
764 765 766 767 768 769 770
0.166219619 0.087586151 0.013935566 0.228296095 0.290569959 0.716692596 0.278684979
771 772 773 774 775 776 777
0.091997574 0.019383710 0.162521896 0.025176350 0.107899791 0.023027805 0.042284561
778 779 780 781 782 783 784
0.268195189 0.046032070 0.194509178 0.099388659 0.328725099 0.205683692 0.165204730
785 786 787 788 789 790 791
0.181004216 0.100503012 0.017511757 0.083239679 0.040955164 0.012872295 0.153244943
792 793 794 795 796 797 798
0.080980627 0.069216534 0.193664543 0.437437505 0.286724639 0.039086665 0.095001313
799 800 801 802 803 804 805
0.064515478 0.031718156 0.508399832 0.024433819 0.108218421 0.022968617 0.102919432
806 807 808 809 810 811 812
0.695424263 0.165732849 0.074769216 0.060808014 0.236330408 0.062178118 0.087987223
813 814 815 816 817 818 819
0.017952592 0.031221275 0.074410547 0.153715332 0.316919269 0.039609447 0.061261657
820 821 822 823 824 825 826
0.015913538 0.081837718 0.350905712 0.015391834 0.417963266 0.076271538 0.084308482
827 828 829 830 831 832 833
0.078915767 0.040287659 0.142686134 0.049011829 0.091794810 0.122478773 0.024131257
834 835 836 837 838 839 840
0.022891047 0.256923866 0.101175222 0.092967749 0.060649210 0.070700636 0.481395291
841 842 843 844 845 846 847
0.078613784 0.023966153 0.141670887 0.031765072 0.030604023 0.336171145 0.737413748
848 849 850 851 852 853 854
0.060198744 0.067186194 0.397953746 0.067549034 0.103228962 0.143979105 0.141553701
855 856 857 858 859 860 861
0.610640456 0.058139319 0.021628348 0.183543729 0.277010566 0.117473260 0.059010232
862 863 864 865 866 867 868
0.098846184 0.050449927 0.091981248 0.076930585 0.297327270 0.113753119 0.197683652
869 870 871 872 873 874 875
0.072535585 0.017313348 0.129242538 0.046353321 0.046390800 0.036297903 0.107598888
876 877 878 879 880 881 882
0.109949583 0.099996575 0.005240661 0.044428191 0.244511979 0.097073966 0.670557540
883 884 885 886 887 888 889
0.161875695 0.555341745 0.788206184 0.078673037 0.084469293 0.090972156 0.025138654
890 891 892 893 894 895 896
0.202254588 0.017900915 0.118212852 0.137135507 0.718907295 0.110697673 0.046952688
897 898 899 900 901 902 903
0.103581246 0.104755291 0.047255782 0.030190023 0.177159171 0.087825884 0.176964353
904 905 906 907 908 909 910
0.049527152 0.517720016 0.294024629 0.163768734 0.033300223 0.042670774 0.253442833
911 912 913 914 915 916 917
0.146721622 0.111974181 0.023014225 0.640304240 0.397892165 0.024624501 0.066609066
918 919 920 921 922 923 924
0.080090031 0.085770391 0.088218455 0.101774922 0.026308832 0.027552576 0.166926027
925 926 927 928 929 930 931
0.183724774 0.317935248 0.175258574 0.173883497 0.098281480 0.091488167 0.049010400
932 933 934 935 936 937 938
0.391877467 0.053223817 0.139469657 0.123599283 0.102991691 0.209328468 0.067801031
939 940 941 942 943 944 945
0.085537026 0.026272967 0.137510452 0.218556388 0.196464580 0.034824059 0.347503893
946 947 948 949 950 951 952
0.233009247 0.614716439 0.133299729 0.050411393 0.039712532 0.076358791 0.163084807
953 954 955 956 957 958 959
0.053503264 0.034872990 0.222578124 0.019071954 0.610168779 0.135699380 0.086189952
960 961 962 963 964 965 966
0.105195210 0.040097157 0.123135249 0.041250915 0.077570777 0.220445317 0.089465788
967 968 969 970 971 972 973
0.054125361 0.452053801 0.409814622 0.054349257 0.015942602 0.074959195 0.409008359
974 975 976 977 978 979 980
0.292535984 0.020753053 0.076227445 0.285119706 0.082700712 0.011914811 0.017006853
981 982 983 984 985 986 987
0.686214642 0.015953760 0.113926752 0.027630818 0.003481304 0.033025758 0.048363274
988 989 990 991 992 993 994
0.061074090 0.340140317 0.057761806 0.039837531 0.037809861 0.036982725 0.475055911
995 996 997 998 999 1000
0.020358315 0.562393239 0.103616311 0.031433559 0.111200190 0.022832089
[ reached getOption("max.print") -- omitted 1334 entries ]
> testing$Churn=as.character(testing$Churn)
> testing$Churn[testing$Churn=="No"] <- "0"
> testing$Churn[testing$Churn=="Yes"] <- "1"
> fitted.results=predict(lmodel,newdata=testing,type='response')
> fitted.results=ifelse(fitted.results > 0.5,1,0)
> misClasificError=mean(fitted.results != testing$Churn)
> print(paste('Logistic Regression Accuracy',1-misClasificError))
[1] "Logistic Regression Accuracy 0.85985985985986"
> print("Confusion Matrix for Logistic Regression"); table(testing$Churn, fitted.results > 0.5)
[1] "Confusion Matrix for Logistic Regression"
FALSE TRUE
0 827 28
1 112 32
> sum(Telecom$Churn=="1")/nrow(Telecom)
[1] 0.1449145
> str(Telecom)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 3333 obs. of 10 variables:
$ Churn : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
$ AccountWeeks : num 128 107 137 84 75 118 121 147 117 141 ...
$ ContractRenewal: num 1 1 1 0 0 0 1 0 1 0 ...
$ DataPlan : num 1 1 0 0 0 0 1 0 0 1 ...
$ CustServCalls : num 1 1 0 2 3 0 3 0 1 0 ...
$ DayMins : num 265 162 243 299 167 ...
$ DayCalls : num 110 123 114 71 113 98 88 79 97 84 ...
$ MonthlyCharge : num 89 82 52 57 41 57 87.3 36 63.9 93.2 ...
$ OverageFee : num 9.87 9.78 6.06 3.1 7.42 ...
$ RoamMins : num 10 13.7 12.2 6.6 10.1 6.3 7.5 7.1 8.7 11.2 ...
> Trainknn=trainData
> Testknn=testData
> TrainBayes=trainData
> TestBayes=testData
> lmodel=glm(Churn~.,family = binomial(link = "logit"),data=trainData)
> print(summary(lmodel))
Call:
glm(formula = Churn ~ ., family = binomial(link = "logit"), data = trainData)
Deviance Residuals:
Min 1Q Median 3Q Max
-2.0553 -0.5045 -0.3431 -0.1992 3.0525
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -6.2861356 0.6535782 -9.618 < 2e-16 ***
AccountWeeks 0.0003457 0.0016766 0.206 0.83665
ContractRenewal -1.9676217 0.1713300 -11.484 < 2e-16 ***
DataPlan -1.3577425 0.6468784 -2.099 0.03582 *
CustServCalls 0.5539090 0.0483978 11.445 < 2e-16 ***
DayMins 0.0107140 0.0039687 2.700 0.00694 **
DayCalls 0.0039345 0.0033077 1.190 0.23424
MonthlyCharge 0.0119062 0.0220000 0.541 0.58838
OverageFee 0.1372180 0.0460986 2.977 0.00291 **
RoamMins 0.0833328 0.0261358 3.188 0.00143 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 1934.3 on 2333 degrees of freedom
Residual deviance: 1515.6 on 2324 degrees of freedom
AIC: 1535.6
Number of Fisher Scoring iterations: 6
> view(lmodel2)
> print(summary(lmodel2))
Call:
glm(formula = Churn ~ AccountWeeks + ContractRenewal + CustServCalls +
DayCalls + DayMins + OverageFee + RoamMins, family = binomial,
data = trainData)
Deviance Residuals:
Min 1Q Median 3Q Max
-1.9708 -0.5214 -0.3588 -0.2287 2.9352
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -6.308e+00 6.421e-01 -9.825 < 2e-16 ***
AccountWeeks -1.046e-05 1.670e-03 -0.006 0.995000
ContractRenewal -1.920e+00 1.682e-01 -11.418 < 2e-16 ***
CustServCalls 5.448e-01 4.768e-02 11.425 < 2e-16 ***
DayCalls 3.590e-03 3.263e-03 1.100 0.271201
DayMins 1.244e-02 1.266e-03 9.829 < 2e-16 ***
OverageFee 1.451e-01 2.706e-02 5.363 8.2e-08 ***
RoamMins 8.795e-02 2.411e-02 3.648 0.000264 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 1934.3 on 2333 degrees of freedom
Residual deviance: 1554.7 on 2326 degrees of freedom
AIC: 1570.7
Number of Fisher Scoring iterations: 5
> dim(Trainknn)
[1] 2334 10
> dim(Testknn)
[1] 999 10
> head(Trainknn)
# A tibble: 6 x 10
Churn AccountWeeks ContractRenewal DataPlan CustServCalls DayMins DayCalls
<fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
10 128 1 1 1 265. 110
20 107 1 1 1 162. 123
30 137 1 0 0 243. 114
40 84 0 0 2 299. 71
50 75 0 0 3 167. 113
60 118 0 0 0 223. 98
# ... with 3 more variables: MonthlyCharge <dbl>, OverageFee <dbl>, RoamMins <dbl>
> pred = knn(Trainknn[-1], Testknn[-1], Trainknn[,1], k = 3)
Error in knn(Trainknn[-1], Testknn[-1], Trainknn[, 1], k = 3) :
'train' and 'class' have different lengths
> Telecom_test_pred=knn(train = Trainknn,
+ test = Testknn,
+ cl= Telecom_train_labels,
+ k = 3,
+ prob=TRUE)
Error in knn(train = Trainknn, test = Testknn, cl = Telecom_train_labels, :
'train' and 'class' have different lengths
> pred=knn(Trainknn[-1],Testknn[-1],Trainknn[1],k=19)
Error in knn(Trainknn[-1], Testknn[-1], Trainknn[1], k = 19) :
'train' and 'class' have different lengths
> table.knn = table(Testknn[,1], pred)
Error: Must use a vector in `[`, not an object of class matrix.
Call `rlang::last_error()` to see a backtrace.
> sum(diag(table.knn)/sum(table.knn))
Error in diag(table.knn) : object 'table.knn' not found
> dim(TrainBayes)
[1] 2334 10
> dim(TestBayes)
[1] 999 10
> train.NB=train.NB[-c(8,10)]
Error: Negative column indexes in `[` must match number of columns:
* `.data` has 9 columns
* Position 2 equals -10
Call `rlang::last_error()` to see a backtrace.
> train.NB=train.NB[-c(8,9)]
> test.NB=test.NB[-c(8,9)]
> train.NB$Churn=as.factor(train.NB$Churn)
> test.NB$Churn=as.factor(test.NB$Churn)
> NB=naiveBayes(x=TrainBayes[-1], y=TrainBayes$Churn)
> NB
Naive Bayes Classifier for Discrete Predictors
Call:
naiveBayes.default(x = TrainBayes[-1], y = TrainBayes$Churn)
A-priori probabilities:
TrainBayes$Churn
0 1
0.8547558 0.1452442
Conditional probabilities:
AccountWeeks
TrainBayes$Churn [,1] [,2]
0 101.1975 39.29269
1 101.9794 39.24309
ContractRenewal
TrainBayes$Churn [,1] [,2]
0 0.9328321 0.2503753
1 0.7109145 0.4540078
DataPlan
TrainBayes$Churn [,1] [,2]
0 0.2972431 0.4571591
1 0.1563422 0.3637163
CustServCalls
TrainBayes$Churn [,1] [,2]
0 1.445614 1.142954
1 2.259587 1.809865
DayMins
TrainBayes$Churn [,1] [,2]
0 175.3046 49.97021
1 206.3634 69.97969
DayCalls
TrainBayes$Churn [,1] [,2]
0 100.5719 19.50113
1 101.4926 22.51053
MonthlyCharge
TrainBayes$Churn [,1] [,2]
0 55.81414 16.38930
1 58.84041 16.13284
OverageFee
TrainBayes$Churn [,1] [,2]
0 9.928416 2.503162
1 10.614248 2.594425
RoamMins
TrainBayes$Churn [,1] [,2]
0 10.18050 2.76674
1 10.76047 2.84053
> predNB=predict(NB,TestBayes,type = "class")
> predNB1=predict(NB,TrainBayes,type = "class")
> y_pred.NB=predict(NB,newdata=TestBayes[-1])
> tab.NB=table(test.NB[,1],y_pred.NB)
Error: Must use a vector in `[`, not an object of class matrix.
Call `rlang::last_error()` to see a backtrace.
> tab.NB
[1] 0.8358358
> mean(predNB==TestBayes$Churn)
[1] 0.8438438
> tab.NB=table(test.NB[,1],y_pred.NB)
Error: Must use a vector in `[`, not an object of class matrix.
Call `rlang::last_error()` to see a backtrace.
> CM=confusionMatrix(predNB, TestBayes[["Churn"]])
> CM
Confusion Matrix and Statistics
Reference
Prediction 0 1
0 785 86
1 70 58
Accuracy : 0.8438
95% CI : (0.8198, 0.8658)
No Information Rate : 0.8559
P-Value [Acc > NIR] : 0.8694
Kappa : 0.3365
Mcnemar's Test P-Value : 0.2298
Sensitivity : 0.9181
Specificity : 0.4028
Pos Pred Value : 0.9013
Neg Pred Value : 0.4531
Prevalence : 0.8559
Detection Rate : 0.7858
Detection Prevalence : 0.8719
Balanced Accuracy : 0.6605
'Positive' Class : 0
> CM2=confusionMatrix(predNB1,TrainBayes[["Churn"]])
> CM2
Confusion Matrix and Statistics
Reference
Prediction 0 1
0 1837 192
1 158 147
Accuracy : 0.85
95% CI : (0.8349, 0.8643)
No Information Rate : 0.8548
P-Value [Acc > NIR] : 0.75154
Kappa : 0.3698
Mcnemar's Test P-Value : 0.07774
Sensitivity : 0.9208
Specificity : 0.4336
Pos Pred Value : 0.9054
Neg Pred Value : 0.4820
Prevalence : 0.8548
Detection Rate : 0.7871
Detection Prevalence : 0.8693
Balanced Accuracy : 0.6772
'Positive' Class : 0
> normalize=function(x){return((x-min(x))/(max(x)-min(x)))}
> Telecom$norm.Churn=normalize(Churn)
> Telecom$norm.Accountweeks=normalize(AccountWeeks)
> Telecom$norm.daycalls=normalize(DayCalls)
> Telecom$norm.daymins=normalize(DayMins)
> Telecom$norm.overagefee=normalize(OverageFee)
> Telecom$norm.contractrenewal=normalize(ContractRenewal)
> Telecom$norm.dataplan=normalize(DataPlan)
> Telecom$norm.datausage=normalize(DataUsage)
> Telecom$norm.Cust=normalize(CustServCalls)
> Telecom$norm.monthlycharge=normalize(MonthlyCharge)
> Telecom$norm.roammins=normalize(RoamMins)
> view(Telecom)
> Telecom
# A tibble: 3,333 x 21
Churn AccountWeeks ContractRenewal DataPlan CustServCalls DayMins DayCalls
<fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
10 128 1 1 1 265. 110
20 107 1 1 1 162. 123
30 137 1 0 0 243. 114
40 84 0 0 2 299. 71
50 75 0 0 3 167. 113
60 118 0 0 0 223. 98
70 121 1 1 3 218. 88
80 147 0 0 0 157 79
90 117 1 0 1 184. 97
10 0 141 0 1 0 259. 84
# ... with 3,323 more rows, and 14 more variables: MonthlyCharge <dbl>,
# OverageFee <dbl>, RoamMins <dbl>, norm.Churn <dbl>, norm.Accountweeks <dbl>,
# norm.daycalls <dbl>, norm.daymins <dbl>, norm.overagefee <dbl>,
# norm.contractrenewal <dbl>, norm.dataplan <dbl>, norm.datausage <dbl>,
# norm.Cust <dbl>, norm.monthlycharge <dbl>, norm.roammins <dbl>
> pred = knn(Trainknn[-1], Testknn[-1], Trainknn[,1], k = 3)
Error in knn(Trainknn[-1], Testknn[-1], Trainknn[, 1], k = 3) :
'train' and 'class' have different lengths
> y_pred.3=knn(Trainknn=train.NB[,-1],test=test.NB[-1], cl=train.NB[,1],k=3)
Error in knn(Trainknn = train.NB[, -1], test = test.NB[-1], cl = train.NB[, :
unused argument (Trainknn = train.NB[, -1])
> set.seed(1234)
> pd=sample(2,nrow(Telecom),replace=TRUE, prob=c(0.7,0.3))
> train1=Telecom[pd==1,]
> test1=Telecom[pd==2,]
> train.NB=train1[,c(1,13:22)]
Error: Positive column indexes in `[` must match number of columns:
* `.data` has 21 columns
* Position 11 equals 22
Call `rlang::last_error()` to see a backtrace.
> test.NB=test1[,c(1,13:22)]
Error: Positive column indexes in `[` must match number of columns:
* `.data` has 21 columns
* Position 11 equals 22
Call `rlang::last_error()` to see a backtrace.
> str(train.NB)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 2362 obs. of 7 variables:
$ Churn : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 2 ...
$ norm.Accountweeks : num 0.525 0.438 0.562 0.343 0.483 ...
$ norm.daycalls : num 0.667 0.745 0.691 0.43 0.594 ...
$ norm.daymins : num 0.756 0.461 0.694 0.853 0.637 ...
$ norm.overagefee : num 0.543 0.538 0.333 0.17 0.606 ...
$ norm.contractrenewal: num 1 1 1 0 0 1 0 1 0 1 ...
$ norm.dataplan : num 1 1 0 0 0 1 0 0 1 0 ...
> table(train.NB$Churn)
0 1
2034 328
> table(test.NB$Churn)
0 1
816 155
> y_pred.3=knn(Trainknn=train.NB[,-1],Testknn=test.NB[-1], cl=train.NB[,1],k=3)