library(readxl)
Telco <- read_excel("C:/Users/DELL/Desktop/DA/Data.xlsx",
sheet = "Main")
View(Telco)
library(ggplot2)
library(ggpie)
library(dplyr)
library(tidyverse)
library(corrplot)
library(lmtest)
library(patchwork)
library(caTools)
library(sjPlot)
library(car)
library(caret)
library(stargazer)
library(rpart)
library(rpart.plot)
| Variables | Data type |
|---|---|
| Gender | Qualitative data |
| Age group | Qualitative data |
| Married | Qualitative data |
| Number of Dependents | Quantitative data |
| Number of Referrals | Quantitative data |
| Tenure in Months | Time series |
| Offer | Qualitative data |
| Phone Service | Qualitative data |
| Avg Monthly Long Distance Charges | Quantitative data |
| Multiple Lines | Quantitative data |
| Internet Service | Qualitative data |
| Internet Type | Qualitative data |
| Avg Monthly GB Download | Quantitative data |
| Online Security | Qualitative data |
| Online Backup | Qualitative data |
| Device Protection Plan | Qualitative data |
| Premium Tech Support | Qualitative data |
| Streaming TV | Qualitative data |
| Streaming Movies | Qualitative data |
| Streaming Music | Qualitative data |
| Unlimited Data | Qualitative data |
| Contract | Qualitative data |
| Paperless Billing | Qualitative data |
| Payment Method | Qualitative data |
| Monthly Charge | Quantitative data |
| Total Charges | Quantitative data |
| Total Refunds | Quantitative data |
| Total Extra Data Charges | Quantitative data |
| Total Long Distance Charges | Quantitative data |
| Total Revenue | Quantitative data |
| Customer Status | Qualitative data |
## tibble [7,043 × 33] (S3: tbl_df/tbl/data.frame)
## $ Gender : chr [1:7043] "Female" "Male" "Male" "Male" ...
## $ Cus ID : chr [1:7043] "0002-ORFBO" "0003-MKNFE" "0004-TLHLJ" "0011-IGKFF" ...
## $ Age : num [1:7043] 37 46 50 78 75 23 67 52 68 43 ...
## $ Age Group : chr [1:7043] "30 - 59" "30 - 59" "30 - 59" "60 - 89" ...
## $ Married : chr [1:7043] "Yes" "No" "No" "Yes" ...
## $ Number of Dependents : num [1:7043] 0 0 0 0 0 3 0 0 0 1 ...
## $ Number of Referrals : num [1:7043] 2 0 0 1 3 0 1 8 0 3 ...
## $ Tenure in Months : num [1:7043] 9 9 4 13 3 9 71 63 7 65 ...
## $ Offer : chr [1:7043] "None" "None" "Offer E" "Offer D" ...
## $ Phone Service : chr [1:7043] "Yes" "Yes" "Yes" "Yes" ...
## $ Avg Monthly Long Distance Charges: num [1:7043] 42.39 10.69 33.65 27.82 7.38 ...
## $ Multiple Lines : chr [1:7043] "No" "Yes" "No" "No" ...
## $ Internet Service : chr [1:7043] "Yes" "Yes" "Yes" "Yes" ...
## $ Internet Type : chr [1:7043] "Cable" "Cable" "Fiber Optic" "Fiber Optic" ...
## $ Avg Monthly GB Download : num [1:7043] 16 10 30 4 11 73 14 7 21 14 ...
## $ Online Security : chr [1:7043] "No" "No" "No" "No" ...
## $ Online Backup : chr [1:7043] "Yes" "No" "No" "Yes" ...
## $ Device Protection Plan : chr [1:7043] "No" "No" "Yes" "Yes" ...
## $ Premium Tech Support : chr [1:7043] "Yes" "No" "No" "No" ...
## $ Streaming TV : chr [1:7043] "Yes" "No" "No" "Yes" ...
## $ Streaming Movies : chr [1:7043] "No" "Yes" "No" "Yes" ...
## $ Streaming Music : chr [1:7043] "No" "Yes" "No" "No" ...
## $ Unlimited Data : chr [1:7043] "Yes" "No" "Yes" "Yes" ...
## $ Contract : chr [1:7043] "One Year" "Month-to-Month" "Month-to-Month" "Month-to-Month" ...
## $ Paperless Billing : chr [1:7043] "Yes" "No" "Yes" "Yes" ...
## $ Payment Method : chr [1:7043] "Credit Card" "Credit Card" "Bank Withdrawal" "Bank Withdrawal" ...
## $ Monthly Charge : num [1:7043] 65.6 -4 73.9 98 83.9 ...
## $ Total Charges : num [1:7043] 593 542 281 1238 267 ...
## $ Total Refunds : num [1:7043] 0 38.3 0 0 0 ...
## $ Total Extra Data Charges : num [1:7043] 0 10 0 0 0 0 0 20 0 0 ...
## $ Total Long Distance Charges : num [1:7043] 381.5 96.2 134.6 361.7 22.1 ...
## $ Total Revenue : num [1:7043] 975 610 415 1600 290 ...
## $ Customer Status : chr [1:7043] "Stayed" "Stayed" "Churned" "Churned" ...
sum(duplicated(Telco))
## [1] 0
=> No duplicate values
sapply(Telco, function(x) sum(is.na(x)))
## Gender Cus ID
## 0 0
## Age Age Group
## 0 0
## Married Number of Dependents
## 0 0
## Number of Referrals Tenure in Months
## 0 0
## Offer Phone Service
## 0 0
## Avg Monthly Long Distance Charges Multiple Lines
## 682 682
## Internet Service Internet Type
## 0 1526
## Avg Monthly GB Download Online Security
## 1526 1526
## Online Backup Device Protection Plan
## 1526 1526
## Premium Tech Support Streaming TV
## 1526 1526
## Streaming Movies Streaming Music
## 1526 1526
## Unlimited Data Contract
## 1526 0
## Paperless Billing Payment Method
## 0 0
## Monthly Charge Total Charges
## 0 0
## Total Refunds Total Extra Data Charges
## 0 0
## Total Long Distance Charges Total Revenue
## 0 0
## Customer Status
## 0
telco_use_both = Telco[complete.cases(Telco),]
#Phone service: Yes/ Internet service: Yes.
miss_inter = c( "Internet Service",
"Internet Type",
"Avg Monthly GB Download",
"Online Security",
"Online Backup",
"Device Protection Plan",
"Premium Tech Support",
"Streaming TV",
"Streaming Movies",
"Streaming Music",
"Unlimited Data")
telco_use_phone = Telco[is.na(Telco$`Internet Type`),
-which(names(Telco) %in% miss_inter)]
#Only use phone
miss_phone = c("Phone Service",
"Avg Monthly Long Distance Charges",
"Multiple Lines")
telco_use_inter = Telco[is.na(Telco$`Multiple Lines`),
-which(names(Telco) %in% miss_phone)]
#Only use internet
quan_val1= c("Number of Dependents",
"Number of Referrals",
"Avg Monthly Long Distance Charges",
"Avg Monthly GB Download",
"Monthly Charge",
"Total Revenue")
quan_val2= c("Number of Dependents",
"Number of Referrals",
"Avg Monthly Long Distance Charges",
"Monthly Charge",
"Total Revenue")
quan_val3= c("Number of Dependents",
"Number of Referrals",
"Avg Monthly GB Download",
"Monthly Charge",
"Total Revenue")
end_of_quarter = my("06-2022")
# Calculate join date
telco_use_both$Join_Date = end_of_quarter - months(telco_use_both$`Tenure in Months`)
telco_use_both$Join_Date = format(telco_use_both$Join_Date, "%Y-%m")
#Churn
telco_use_both$Churn = case_when(
telco_use_both$`Customer Status`== "Churned" ~ "Yes",
TRUE ~ "No")
summary(telco_use_both)
## Gender Cus ID Age Age Group
## Length:4835 Length:4835 Min. :19.00 Length:4835
## Class :character Class :character 1st Qu.:33.00 Class :character
## Mode :character Mode :character Median :47.00 Mode :character
## Mean :47.76
## 3rd Qu.:62.00
## Max. :80.00
## Married Number of Dependents Number of Referrals Tenure in Months
## Length:4835 Min. :0.000 Min. : 0.000 Min. : 1.00
## Class :character 1st Qu.:0.000 1st Qu.: 0.000 1st Qu.: 9.00
## Mode :character Median :0.000 Median : 0.000 Median :30.00
## Mean :0.376 Mean : 1.895 Mean :33.04
## 3rd Qu.:0.000 3rd Qu.: 3.000 3rd Qu.:56.00
## Max. :8.000 Max. :11.000 Max. :72.00
## Offer Phone Service Avg Monthly Long Distance Charges
## Length:4835 Length:4835 Min. : 1.01
## Class :character Class :character 1st Qu.:13.04
## Mode :character Mode :character Median :25.82
## Mean :25.56
## 3rd Qu.:37.97
## Max. :49.99
## Multiple Lines Internet Service Internet Type
## Length:4835 Length:4835 Length:4835
## Class :character Class :character Class :character
## Mode :character Mode :character Mode :character
##
##
##
## Avg Monthly GB Download Online Security Online Backup
## Min. : 2.00 Length:4835 Length:4835
## 1st Qu.:13.00 Class :character Class :character
## Median :21.00 Mode :character Mode :character
## Mean :26.09
## 3rd Qu.:30.00
## Max. :85.00
## Device Protection Plan Premium Tech Support Streaming TV
## Length:4835 Length:4835 Length:4835
## Class :character Class :character Class :character
## Mode :character Mode :character Mode :character
##
##
##
## Streaming Movies Streaming Music Unlimited Data Contract
## Length:4835 Length:4835 Length:4835 Length:4835
## Class :character Class :character Class :character Class :character
## Mode :character Mode :character Mode :character Mode :character
##
##
##
## Paperless Billing Payment Method Monthly Charge Total Charges
## Length:4835 Length:4835 Min. :-10.00 Min. : 42.9
## Class :character Class :character 1st Qu.: 69.40 1st Qu.: 659.5
## Mode :character Mode :character Median : 81.70 Median :2347.9
## Mean : 80.31 Mean :2901.1
## 3rd Qu.: 95.60 3rd Qu.:4870.3
## Max. :118.75 Max. :8684.8
## Total Refunds Total Extra Data Charges Total Long Distance Charges
## Min. : 0.000 Min. : 0.000 Min. : 1.13
## 1st Qu.: 0.000 1st Qu.: 0.000 1st Qu.: 139.97
## Median : 0.000 Median : 0.000 Median : 517.80
## Mean : 2.081 Mean : 8.695 Mean : 848.09
## 3rd Qu.: 0.000 3rd Qu.: 0.000 3rd Qu.:1360.96
## Max. :49.570 Max. :150.000 Max. :3536.64
## Total Revenue Customer Status Join_Date Churn
## Min. : 46.92 Length:4835 Length:4835 Length:4835
## 1st Qu.: 899.48 Class :character Class :character Class :character
## Median : 3081.23 Mode :character Mode :character Mode :character
## Mean : 3755.82
## 3rd Qu.: 6284.90
## Max. :11979.34
sapply(telco_use_both, function(x) sd(x))
## Gender Cus ID
## NA NA
## Age Age Group
## 17.3026737 NA
## Married Number of Dependents
## NA 0.8756364
## Number of Referrals Tenure in Months
## 2.9407402 24.6352469
## Offer Phone Service
## NA NA
## Avg Monthly Long Distance Charges Multiple Lines
## 14.2414400 NA
## Internet Service Internet Type
## NA NA
## Avg Monthly GB Download Online Security
## 19.5609900 NA
## Online Backup Device Protection Plan
## NA NA
## Premium Tech Support Streaming TV
## NA NA
## Streaming Movies Streaming Music
## NA NA
## Unlimited Data Contract
## NA NA
## Paperless Billing Payment Method
## NA NA
## Monthly Charge Total Charges
## 21.3796392 2415.4266589
## Total Refunds Total Extra Data Charges
## 8.1352920 28.0598134
## Total Long Distance Charges Total Revenue
## 864.4759327 3087.3517910
## Customer Status Join_Date
## NA NA
## Churn
## NA
by(telco_use_both$`Total Revenue`,telco_use_both$Offer,summary)
## telco_use_both$Offer: None
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 52.92 861.99 2960.85 3622.36 5958.91 11688.90
## ------------------------------------------------------------
## telco_use_both$Offer: Offer A
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 4229 7188 8346 8321 9465 11979
## ------------------------------------------------------------
## telco_use_both$Offer: Offer B
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 2514 5102 6072 6143 7148 10185
## ------------------------------------------------------------
## telco_use_both$Offer: Offer C
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1308 2700 3245 3361 3971 6028
## ------------------------------------------------------------
## telco_use_both$Offer: Offer D
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 419.3 1209.4 1561.2 1633.8 1980.6 3273.1
## ------------------------------------------------------------
## telco_use_both$Offer: Offer E
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 46.92 114.80 273.58 371.48 556.04 1351.16
\[ p=P(y=1)=\frac{\epsilon^{\beta_1 + \beta_2*X_2 + ... + \beta_n*X_n}}{1+\epsilon^{\beta_1 + \beta_2*X_2 + ... + \beta_n*X_n}} \]
\[ ln(\frac{p}{1-p}) = \beta_1 + \beta_2*X_2 + \beta_3*X_3 + ... + \beta_n*X_n \]
When \(X_j\) changes 1 unit => odd ratio \(\frac{p}{1-p}\) changes \(\epsilon^{\beta_j}\) times
telco_use_both = mutate(telco_use_both,
Churn = ifelse(Churn == "Yes", 1, 0))
telco_use_both$Churn_text = ifelse(telco_use_both$Churn == 0,
"Stayed","Churned")
set.seed(2610)
split = sample.split(telco_use_both$Churn,SplitRatio = 0.8)
train = subset(telco_use_both, split == TRUE)
test = subset(telco_use_both, split == FALSE)
train1 = subset(train,
select = - c(`Cus ID`, Age, `Phone Service`,
`Internet Service`, `Join_Date`,`Customer Status`,
`Total Charges`, `Total Refunds`,
`Total Extra Data Charges`,
`Total Long Distance Charges`,Churn_text))
train2 = subset(train,
select = - c(`Cus ID`, Age, `Phone Service`,
`Internet Service`, `Join_Date`,`Customer Status`,
`Total Charges`, `Total Refunds`,
`Total Extra Data Charges`,
`Total Long Distance Charges`,Churn))
test1 = subset(test,
select = - c(`Cus ID`, Age, `Phone Service`,
`Internet Service`, `Join_Date`,`Customer Status`,
`Total Charges`, `Total Refunds`,
`Total Extra Data Charges`,
`Total Long Distance Charges`,Churn_text))
test2 = subset(test,
select = - c(`Cus ID`, Age, `Phone Service`,
`Internet Service`, `Join_Date`,`Customer Status`,
`Total Charges`, `Total Refunds`,
`Total Extra Data Charges`,
`Total Long Distance Charges`,Churn))
md1 = glm(Churn ~ ., data = train1, family = "binomial")
md2 = glm(Churn ~ . + Gender*`Total Revenue` +
`Number of Dependents`*`Unlimited Data`+
`Contract`*`Avg Monthly GB Download`+
I(`Tenure in Months`^2),
data = train1, family = "binomial")
md3 = glm(Churn ~ . + Gender*`Total Revenue` +
`Number of Dependents`*`Unlimited Data`+
`Contract`*`Avg Monthly GB Download`+
I(`Tenure in Months`^2) - Offer,
data = train1, family = "binomial")
##
## ===========================================================================
## Dependent variable:
## --------------------------------
## Churn
## (1) (2) (3)
## ---------------------------------------------------------------------------
## GenderMale -0.091 -0.285** -0.277**
## (0.091) (0.129) (0.129)
##
## `Age Group`30 - 59 0.323* 0.291 0.273
## (0.191) (0.190) (0.189)
##
## `Age Group`60 - 89 0.911*** 0.906*** 0.896***
## (0.214) (0.213) (0.212)
##
## MarriedYes 1.648*** 1.656*** 1.648***
## (0.139) (0.139) (0.139)
##
## `Number of Dependents` -0.787*** -0.326* -0.317*
## (0.088) (0.172) (0.171)
##
## `Number of Referrals` -0.522*** -0.520*** -0.517***
## (0.041) (0.041) (0.040)
##
## `Tenure in Months` -0.072*** -0.090*** -0.101***
## (0.012) (0.013) (0.012)
##
## OfferOffer A 0.909*** 0.455
## (0.302) (0.322)
##
## OfferOffer B -0.302 -0.304
## (0.191) (0.190)
##
## OfferOffer C -0.346* -0.135
## (0.196) (0.208)
##
## OfferOffer D -0.566*** -0.468***
## (0.155) (0.159)
##
## OfferOffer E 0.113 -0.002
## (0.141) (0.148)
##
## `Avg Monthly Long Distance Charges` -0.011*** -0.008** -0.008**
## (0.004) (0.004) (0.004)
##
## `Multiple Lines`Yes 0.234** 0.267** 0.257**
## (0.106) (0.108) (0.107)
##
## `Internet Type`DSL -0.366** -0.331* -0.334*
## (0.172) (0.173) (0.173)
##
## `Internet Type`Fiber Optic -0.084 -0.002 0.004
## (0.180) (0.183) (0.183)
##
## `Avg Monthly GB Download` 0.004 0.001 0.001
## (0.004) (0.004) (0.004)
##
## `Online Security`Yes -0.417*** -0.419*** -0.437***
## (0.113) (0.114) (0.114)
##
## `Online Backup`Yes -0.301*** -0.297*** -0.290***
## (0.107) (0.108) (0.108)
##
## `Device Protection Plan`Yes -0.060 -0.021 -0.018
## (0.107) (0.109) (0.108)
##
## `Premium Tech Support`Yes -0.426*** -0.410*** -0.398***
## (0.115) (0.116) (0.115)
##
## `Streaming TV`Yes 0.144 0.173 0.175
## (0.118) (0.119) (0.119)
##
## `Streaming Movies`Yes -0.227 -0.181 -0.154
## (0.181) (0.184) (0.183)
##
## `Streaming Music`Yes 0.475*** 0.477*** 0.455***
## (0.172) (0.174) (0.173)
##
## `Unlimited Data`Yes 0.013 0.093 0.090
## (0.127) (0.132) (0.132)
##
## ContractOne Year -1.204*** -1.612*** -1.607***
## (0.146) (0.218) (0.217)
##
## ContractTwo Year -2.914*** -3.407*** -3.343***
## (0.245) (0.363) (0.360)
##
## `Paperless Billing`Yes 0.256** 0.264** 0.259**
## (0.105) (0.106) (0.105)
##
## `Payment Method`Credit Card -0.339*** -0.334*** -0.327***
## (0.106) (0.107) (0.106)
##
## `Payment Method`Mailed Check 0.595*** 0.619*** 0.596***
## (0.219) (0.222) (0.219)
##
## `Monthly Charge` 0.012*** 0.012*** 0.012***
## (0.004) (0.004) (0.004)
##
## `Total Revenue` 0.0004*** 0.0002** 0.0002**
## (0.0001) (0.0001) (0.0001)
##
## I(`Tenure in Months`2) 0.001*** 0.001***
## (0.0002) (0.0001)
##
## GenderMale:`Total Revenue` 0.0001** 0.0001**
## (0.00003) (0.00003)
##
## `Number of Dependents`:`Unlimited Data`Yes -0.566*** -0.579***
## (0.195) (0.194)
##
## `Avg Monthly GB Download`:ContractOne Year 0.015** 0.015**
## (0.006) (0.006)
##
## `Avg Monthly GB Download`:ContractTwo Year 0.015 0.015
## (0.011) (0.011)
##
## Constant -0.350 -0.156 -0.118
## (0.404) (0.411) (0.406)
##
## ---------------------------------------------------------------------------
## Observations 3,868 3,868 3,868
## Log Likelihood -1,500.350 -1,485.281 -1,491.863
## Akaike Inf. Crit. 3,066.699 3,046.562 3,049.727
## ===========================================================================
## Note: *p<0.1; **p<0.05; ***p<0.01
anova(md1,md2,test = "LR")
## Analysis of Deviance Table
##
## Model 1: Churn ~ Gender + `Age Group` + Married + `Number of Dependents` +
## `Number of Referrals` + `Tenure in Months` + Offer + `Avg Monthly Long Distance Charges` +
## `Multiple Lines` + `Internet Type` + `Avg Monthly GB Download` +
## `Online Security` + `Online Backup` + `Device Protection Plan` +
## `Premium Tech Support` + `Streaming TV` + `Streaming Movies` +
## `Streaming Music` + `Unlimited Data` + Contract + `Paperless Billing` +
## `Payment Method` + `Monthly Charge` + `Total Revenue`
## Model 2: Churn ~ Gender + `Age Group` + Married + `Number of Dependents` +
## `Number of Referrals` + `Tenure in Months` + Offer + `Avg Monthly Long Distance Charges` +
## `Multiple Lines` + `Internet Type` + `Avg Monthly GB Download` +
## `Online Security` + `Online Backup` + `Device Protection Plan` +
## `Premium Tech Support` + `Streaming TV` + `Streaming Movies` +
## `Streaming Music` + `Unlimited Data` + Contract + `Paperless Billing` +
## `Payment Method` + `Monthly Charge` + `Total Revenue` + Gender *
## `Total Revenue` + `Number of Dependents` * `Unlimited Data` +
## Contract * `Avg Monthly GB Download` + I(`Tenure in Months`^2)
## Resid. Df Resid. Dev Df Deviance Pr(>Chi)
## 1 3835 3000.7
## 2 3830 2970.6 5 30.137 1.386e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
anova(md2,md3,test = "LR")
## Analysis of Deviance Table
##
## Model 1: Churn ~ Gender + `Age Group` + Married + `Number of Dependents` +
## `Number of Referrals` + `Tenure in Months` + Offer + `Avg Monthly Long Distance Charges` +
## `Multiple Lines` + `Internet Type` + `Avg Monthly GB Download` +
## `Online Security` + `Online Backup` + `Device Protection Plan` +
## `Premium Tech Support` + `Streaming TV` + `Streaming Movies` +
## `Streaming Music` + `Unlimited Data` + Contract + `Paperless Billing` +
## `Payment Method` + `Monthly Charge` + `Total Revenue` + Gender *
## `Total Revenue` + `Number of Dependents` * `Unlimited Data` +
## Contract * `Avg Monthly GB Download` + I(`Tenure in Months`^2)
## Model 2: Churn ~ Gender + `Age Group` + Married + `Number of Dependents` +
## `Number of Referrals` + `Tenure in Months` + Offer + `Avg Monthly Long Distance Charges` +
## `Multiple Lines` + `Internet Type` + `Avg Monthly GB Download` +
## `Online Security` + `Online Backup` + `Device Protection Plan` +
## `Premium Tech Support` + `Streaming TV` + `Streaming Movies` +
## `Streaming Music` + `Unlimited Data` + Contract + `Paperless Billing` +
## `Payment Method` + `Monthly Charge` + `Total Revenue` + Gender *
## `Total Revenue` + `Number of Dependents` * `Unlimited Data` +
## Contract * `Avg Monthly GB Download` + I(`Tenure in Months`^2) -
## Offer
## Resid. Df Resid. Dev Df Deviance Pr(>Chi)
## 1 3830 2970.6
## 2 3835 2983.7 -5 -13.164 0.02189 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
=> Choose model 3
exp(coef(md3))
## (Intercept)
## 0.88885446
## GenderMale
## 0.75768636
## `Age Group`30 - 59
## 1.31383579
## `Age Group`60 - 89
## 2.44987857
## MarriedYes
## 5.19869541
## `Number of Dependents`
## 0.72827531
## `Number of Referrals`
## 0.59622598
## `Tenure in Months`
## 0.90427703
## `Avg Monthly Long Distance Charges`
## 0.99187532
## `Multiple Lines`Yes
## 1.29266186
## `Internet Type`DSL
## 0.71621831
## `Internet Type`Fiber Optic
## 1.00405994
## `Avg Monthly GB Download`
## 1.00069004
## `Online Security`Yes
## 0.64603932
## `Online Backup`Yes
## 0.74804590
## `Device Protection Plan`Yes
## 0.98193415
## `Premium Tech Support`Yes
## 0.67171692
## `Streaming TV`Yes
## 1.19144175
## `Streaming Movies`Yes
## 0.85713362
## `Streaming Music`Yes
## 1.57653697
## `Unlimited Data`Yes
## 1.09413278
## ContractOne Year
## 0.20048895
## ContractTwo Year
## 0.03534785
## `Paperless Billing`Yes
## 1.29569950
## `Payment Method`Credit Card
## 0.72129729
## `Payment Method`Mailed Check
## 1.81477815
## `Monthly Charge`
## 1.01217938
## `Total Revenue`
## 1.00021258
## I(`Tenure in Months`^2)
## 1.00069977
## GenderMale:`Total Revenue`
## 1.00006928
## `Number of Dependents`:`Unlimited Data`Yes
## 0.56019747
## `Avg Monthly GB Download`:ContractOne Year
## 1.01501412
## `Avg Monthly GB Download`:ContractTwo Year
## 1.01513211
pr1 = predict(md3, test1, type = "response")
predicted = round(pr1)
tbl1 = table(Prediction = predicted, Actual = test1$Churn)
tbl1
## Actual
## Prediction 0 1
## 0 547 96
## 1 103 221
spec = 547/(547+103)*100 #Specificity
sens = 221/(221+96)*100 #Sensitivity
FP = 100 - sens #False Positive
FN = 100 - spec #False Negative
spec #Specificity 84.15%
## [1] 84.15385
sens #Sensitivity 69.72%
## [1] 69.71609
FP #False Positive 30.28%
## [1] 30.28391
FN #False Negative 15.85%
## [1] 15.84615
84.15% of customers are predicted “Stayed” and the result is true negative which means predicted correct.
69.72% of customers are predicted “Churned” and the result is true positive which means predicted correct.
30.28% of customers have churned but the result is predicted “Stayed”
15.85% of customers have stayed but the result is predicted “Churned”
#Note: ?confusionMatrix
confusionMatrix(tbl1, positive = "1", mode = "everything")
## Confusion Matrix and Statistics
##
## Actual
## Prediction 0 1
## 0 547 96
## 1 103 221
##
## Accuracy : 0.7942
## 95% CI : (0.7673, 0.8193)
## No Information Rate : 0.6722
## P-Value [Acc > NIR] : <2e-16
##
## Kappa : 0.5357
##
## Mcnemar's Test P-Value : 0.6706
##
## Sensitivity : 0.6972
## Specificity : 0.8415
## Pos Pred Value : 0.6821
## Neg Pred Value : 0.8507
## Precision : 0.6821
## Recall : 0.6972
## F1 : 0.6895
## Prevalence : 0.3278
## Detection Rate : 0.2285
## Detection Prevalence : 0.3351
## Balanced Accuracy : 0.7693
##
## 'Positive' Class : 1
##
dtr = rpart(Churn_text ~ ., data = train2, method = "class")
Initially, probability of stay decision is 0.67,
first sample size divides into 57% and 43%. In layer 2,
probability of stay decision is 0.49, 57% out of first sample size
continues to divide into 48% and 9%.
pr2 = predict(dtr, test2, type = "class")
tbl2= table(Prediction = pr2, Actual = test2$Churn_text)
#Note: ?confusionMatrix
confusionMatrix(tbl2, positive = "Churned", mode = "everything")
## Confusion Matrix and Statistics
##
## Actual
## Prediction Churned Stayed
## Churned 200 92
## Stayed 117 558
##
## Accuracy : 0.7839
## 95% CI : (0.7566, 0.8094)
## No Information Rate : 0.6722
## P-Value [Acc > NIR] : 1.149e-14
##
## Kappa : 0.4995
##
## Mcnemar's Test P-Value : 0.09689
##
## Sensitivity : 0.6309
## Specificity : 0.8585
## Pos Pred Value : 0.6849
## Neg Pred Value : 0.8267
## Precision : 0.6849
## Recall : 0.6309
## F1 : 0.6568
## Prevalence : 0.3278
## Detection Rate : 0.2068
## Detection Prevalence : 0.3020
## Balanced Accuracy : 0.7447
##
## 'Positive' Class : Churned
##
vif(md3, type ="predictor")
## GVIF Df GVIF^(1/(2*Df))
## Gender 2.012650 1 1.418679
## `Age Group` 3.227297 2 1.340324
## Married 2.066820 1 1.437644
## `Number of Dependents` 4.347828 1 2.085145
## `Number of Referrals` 1.762788 1 1.327700
## `Tenure in Months` 32.719743 1 5.720117
## `Avg Monthly Long Distance Charges` 1.669636 1 1.292144
## `Multiple Lines` 1.392603 1 1.180086
## `Internet Type` 2.494508 2 1.256742
## `Avg Monthly GB Download` 2.999940 1 1.732034
## `Online Security` 1.181064 1 1.086768
## `Online Backup` 1.282129 1 1.132311
## `Device Protection Plan` 1.294678 1 1.137839
## `Premium Tech Support` 1.205969 1 1.098166
## `Streaming TV` 1.702718 1 1.304882
## `Streaming Movies` 4.055482 1 2.013823
## `Streaming Music` 3.552718 1 1.884866
## `Unlimited Data` 1.115683 1 1.056259
## Contract 8.871002 2 1.725811
## `Paperless Billing` 1.070768 1 1.034779
## `Payment Method` 1.084166 2 1.020408
## `Monthly Charge` 4.081645 1 2.020308
## `Total Revenue` 43.994619 1 6.632844
## I(`Tenure in Months`^2) 17.120265 1 4.137664
## Gender:`Total Revenue` 3.153862 1 1.775912
## `Number of Dependents`:`Unlimited Data` 4.297086 1 2.072941
## `Avg Monthly GB Download`:Contract 8.313728 2 1.698044