This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
library(class)
project <- read.csv("C:\\Users\\hussain7108\\Desktop\\churn (1).csv")
str(project)
## 'data.frame': 3333 obs. of 21 variables:
## $ State : chr "KS" "OH" "NJ" "OH" ...
## $ Account.Length: int 128 107 137 84 75 118 121 147 117 141 ...
## $ Area.Code : int 415 415 415 408 415 510 510 415 408 415 ...
## $ Phone : chr "382-4657" "371-7191" "358-1921" "375-9999" ...
## $ Int.l.Plan : chr "no" "no" "no" "yes" ...
## $ VMail.Plan : chr "yes" "yes" "no" "no" ...
## $ VMail.Message : int 25 26 0 0 0 0 24 0 0 37 ...
## $ Day.Mins : num 265 162 243 299 167 ...
## $ Day.Calls : int 110 123 114 71 113 98 88 79 97 84 ...
## $ Day.Charge : num 45.1 27.5 41.4 50.9 28.3 ...
## $ Eve.Mins : num 197.4 195.5 121.2 61.9 148.3 ...
## $ Eve.Calls : int 99 103 110 88 122 101 108 94 80 111 ...
## $ Eve.Charge : num 16.78 16.62 10.3 5.26 12.61 ...
## $ Night.Mins : num 245 254 163 197 187 ...
## $ Night.Calls : int 91 103 104 89 121 118 118 96 90 97 ...
## $ Night.Charge : num 11.01 11.45 7.32 8.86 8.41 ...
## $ Intl.Mins : num 10 13.7 12.2 6.6 10.1 6.3 7.5 7.1 8.7 11.2 ...
## $ Intl.Calls : int 3 3 5 7 3 6 7 6 4 5 ...
## $ Intl.Charge : num 2.7 3.7 3.29 1.78 2.73 1.7 2.03 1.92 2.35 3.02 ...
## $ CustServ.Calls: int 1 1 0 2 3 0 3 0 1 0 ...
## $ Churn. : chr "False." "False." "False." "False." ...
project$Churn. <- factor(project$Churn.)
project$Int.l.Plan <- factor(project$Int.l.Plan)
project$VMail.Plan <- factor(project$VMail.Plan)
# Select the specified columns from the project data frame
project <- project[, c("Int.l.Plan", "VMail.Plan", "VMail.Message",
"Day.Mins", "Day.Calls", "Day.Charge",
"Eve.Mins", "Eve.Calls", "Eve.Charge",
"Night.Mins", "Night.Calls", "Night.Charge",
"Intl.Mins", "Intl.Calls", "Intl.Charge",
"CustServ.Calls", "Churn.")]
summary(project)
## Int.l.Plan VMail.Plan VMail.Message Day.Mins Day.Calls
## no :3010 no :2411 Min. : 0.000 Min. : 0.0 Min. : 0.0
## yes: 323 yes: 922 1st Qu.: 0.000 1st Qu.:143.7 1st Qu.: 87.0
## Median : 0.000 Median :179.4 Median :101.0
## Mean : 8.099 Mean :179.8 Mean :100.4
## 3rd Qu.:20.000 3rd Qu.:216.4 3rd Qu.:114.0
## Max. :51.000 Max. :350.8 Max. :165.0
## Day.Charge Eve.Mins Eve.Calls Eve.Charge
## Min. : 0.00 Min. : 0.0 Min. : 0.0 Min. : 0.00
## 1st Qu.:24.43 1st Qu.:166.6 1st Qu.: 87.0 1st Qu.:14.16
## Median :30.50 Median :201.4 Median :100.0 Median :17.12
## Mean :30.56 Mean :201.0 Mean :100.1 Mean :17.08
## 3rd Qu.:36.79 3rd Qu.:235.3 3rd Qu.:114.0 3rd Qu.:20.00
## Max. :59.64 Max. :363.7 Max. :170.0 Max. :30.91
## Night.Mins Night.Calls Night.Charge Intl.Mins
## Min. : 23.2 Min. : 33.0 Min. : 1.040 Min. : 0.00
## 1st Qu.:167.0 1st Qu.: 87.0 1st Qu.: 7.520 1st Qu.: 8.50
## Median :201.2 Median :100.0 Median : 9.050 Median :10.30
## Mean :200.9 Mean :100.1 Mean : 9.039 Mean :10.24
## 3rd Qu.:235.3 3rd Qu.:113.0 3rd Qu.:10.590 3rd Qu.:12.10
## Max. :395.0 Max. :175.0 Max. :17.770 Max. :20.00
## Intl.Calls Intl.Charge CustServ.Calls Churn.
## Min. : 0.000 Min. :0.000 Min. :0.000 False.:2850
## 1st Qu.: 3.000 1st Qu.:2.300 1st Qu.:1.000 True. : 483
## Median : 4.000 Median :2.780 Median :1.000
## Mean : 4.479 Mean :2.765 Mean :1.563
## 3rd Qu.: 6.000 3rd Qu.:3.270 3rd Qu.:2.000
## Max. :20.000 Max. :5.400 Max. :9.000
project[1:3,]
## Int.l.Plan VMail.Plan VMail.Message Day.Mins Day.Calls Day.Charge Eve.Mins
## 1 no yes 25 265.1 110 45.07 197.4
## 2 no yes 26 161.6 123 27.47 195.5
## 3 no no 0 243.4 114 41.38 121.2
## Eve.Calls Eve.Charge Night.Mins Night.Calls Night.Charge Intl.Mins Intl.Calls
## 1 99 16.78 244.7 91 11.01 10.0 3
## 2 103 16.62 254.4 103 11.45 13.7 3
## 3 110 10.30 162.6 104 7.32 12.2 5
## Intl.Charge CustServ.Calls Churn.
## 1 2.70 1 False.
## 2 3.70 1 False.
## 3 3.29 0 False.
summary(project)
## Int.l.Plan VMail.Plan VMail.Message Day.Mins Day.Calls
## no :3010 no :2411 Min. : 0.000 Min. : 0.0 Min. : 0.0
## yes: 323 yes: 922 1st Qu.: 0.000 1st Qu.:143.7 1st Qu.: 87.0
## Median : 0.000 Median :179.4 Median :101.0
## Mean : 8.099 Mean :179.8 Mean :100.4
## 3rd Qu.:20.000 3rd Qu.:216.4 3rd Qu.:114.0
## Max. :51.000 Max. :350.8 Max. :165.0
## Day.Charge Eve.Mins Eve.Calls Eve.Charge
## Min. : 0.00 Min. : 0.0 Min. : 0.0 Min. : 0.00
## 1st Qu.:24.43 1st Qu.:166.6 1st Qu.: 87.0 1st Qu.:14.16
## Median :30.50 Median :201.4 Median :100.0 Median :17.12
## Mean :30.56 Mean :201.0 Mean :100.1 Mean :17.08
## 3rd Qu.:36.79 3rd Qu.:235.3 3rd Qu.:114.0 3rd Qu.:20.00
## Max. :59.64 Max. :363.7 Max. :170.0 Max. :30.91
## Night.Mins Night.Calls Night.Charge Intl.Mins
## Min. : 23.2 Min. : 33.0 Min. : 1.040 Min. : 0.00
## 1st Qu.:167.0 1st Qu.: 87.0 1st Qu.: 7.520 1st Qu.: 8.50
## Median :201.2 Median :100.0 Median : 9.050 Median :10.30
## Mean :200.9 Mean :100.1 Mean : 9.039 Mean :10.24
## 3rd Qu.:235.3 3rd Qu.:113.0 3rd Qu.:10.590 3rd Qu.:12.10
## Max. :395.0 Max. :175.0 Max. :17.770 Max. :20.00
## Intl.Calls Intl.Charge CustServ.Calls Churn.
## Min. : 0.000 Min. :0.000 Min. :0.000 False.:2850
## 1st Qu.: 3.000 1st Qu.:2.300 1st Qu.:1.000 True. : 483
## Median : 4.000 Median :2.780 Median :1.000
## Mean : 4.479 Mean :2.765 Mean :1.563
## 3rd Qu.: 6.000 3rd Qu.:3.270 3rd Qu.:2.000
## Max. :20.000 Max. :5.400 Max. :9.000
#Scale data
x <- scale(project[,c(13,14,15)])
x[1:3,]
## Intl.Mins Intl.Calls Intl.Charge
## [1,] -0.08499548 -0.6011049 -0.08567764
## [2,] 1.24029559 -0.6011049 1.24098239
## [3,] 0.70301543 0.2115021 0.69705178
#Training and Predicting Datset
train <- sample(1:3333,3000)
xtrain <- x[train,]
xnew <- x[-train,]
ytrain <- project$Churn.[train]
ynew <- project$Churn.[-train]
## k-nearest neighbor method
nearest1 <- knn(train=xtrain, test=xnew, cl=ytrain, k=1)
nearest3 <- knn(train=xtrain, test=xnew, cl=ytrain, k=3)
data.frame(ynew, nearest1, nearest3) [1:10,]
## ynew nearest1 nearest3
## 1 False. False. False.
## 2 False. False. False.
## 3 False. False. False.
## 4 True. False. False.
## 5 False. True. True.
## 6 False. False. False.
## 7 False. False. False.
## 8 False. False. False.
## 9 False. False. False.
## 10 False. False. False.
# Calculate proportion of correct classification
pcorrn1=100*sum(ynew==nearest1)/100
pcorrn3=100*sum(ynew==nearest3)/100
pcorrn1
## [1] 267
pcorrn3
## [1] 274
#Plot for 3nn
plot(xtrain[,c("Intl.Calls","Intl.Mins")],
col=c(4,3,6,2)[project[train, "Intl.Charge"]],
pch=c(1,2) [as.numeric(ytrain)],
main="Predicted Churn, by 3 nearest neighbors", cex.main=.95)
points(xnew[,c("Intl.Calls", "Intl.Mins")],
bg=c(4,3,6,2) [project[train, "Intl.Charge"]],
pch=c(21,24)[as.numeric(nearest3)], cex=1.2, col=grey(.7))
legend("bottomright", pch=c(1,16,2,17), bg=c(1,1,1,1),
legend=c("data 0", "pred 0", "data 1", "pred 1"),
title="Churn", bty="n", cex=.8)
legend("topleft", fill=c(4,3,6,2), legend=c(1,2,3,4),
title = "Intl.Charge %", horiz=TRUE, bty="n", col=grey(.7), cex=.8)
#Cross-Validate
pcorr=dim(10)
for (k in 1:10) {
pred=knn.cv(x,cl=project$Churn.,k)
pcorr[k]=100*sum(project$Churn.==pred)/1000
}
pcorr
## [1] 270.2 272.4 276.8 278.9 281.2 282.6 282.6 283.9 284.8 284.8