Con base en los datos de rotación realizar los puntos 1 a 4:
library(readxl)
datos = read_excel("~/Desktop/Datos_Creditos.xlsx")
names(datos)
## [1] "DEFAULT" "DIAS_MORA" "ANTIUEDAD" "EDAD"
## [5] "CUOTA_TOTAL" "INGRESOS" "CARTERA_TOTAL"
datos$COMPROMISO=round(datos$CUOTA_TOTAL/datos$INGRESOS*100,1)
# datos$y=as.numeric(datos$Rotación=="Si")
# table(datos$Rotación,datos$y)
Seleccionar 3 variables categóricas (distintas de rotación) y 3 variables cuantitativas, que consideren estén relacionadas con la rotación. Nota: Justificar por que estas variables están relacionadas y que tipo de relación se espera (Hipótesis). Ejemplo: Se espera que las horas extra se relacionen con la rotación ya que las personas podrían desgastarse mas al trabajar horas extra y descuidan aspectos personales. La hipótesis es que las personas que trabajan horas extra tienen mayor posibilidad de rotar que las que no trabajan extra. (serian 6, una por variable).
Realizar un análisis univariado (caracterización). Nota: Los indicadores o gráficos se usan dependiendo del tipo de variable (cuanti o cuali). Incluir interpretaciones de la rotación.
# require(ggplot2)
# require(ggpubr)
#
# g1=ggplot(datos,aes(x=Edad))+geom_histogram()+theme_bw()
# g2=ggplot(datos,aes(x=`Viaje de Negocios`))+geom_bar()+theme_bw()
#
# ggarrange(g1, g2,labels = c("A", "B"),ncol = 2, nrow = 1)
names(datos)
## [1] "DEFAULT" "DIAS_MORA" "ANTIUEDAD" "EDAD"
## [5] "CUOTA_TOTAL" "INGRESOS" "CARTERA_TOTAL" "COMPROMISO"
require(table1)
y <- table1::table1(~ ANTIUEDAD+ EDAD+COMPROMISO| DEFAULT, data = datos)
y
0 (N=741) |
1 (N=39) |
Overall (N=780) |
|
---|---|---|---|
ANTIUEDAD | |||
Mean (SD) | 18.2 (11.9) | 14.5 (11.6) | 18.0 (11.9) |
Median [Min, Max] | 15.5 [0.255, 37.3] | 9.99 [1.37, 37.3] | 15.1 [0.255, 37.3] |
EDAD | |||
Mean (SD) | 57.1 (12.5) | 55.2 (12.4) | 57.0 (12.5) |
Median [Min, Max] | 58.0 [26.6, 92.4] | 53.7 [30.4, 78.9] | 57.9 [26.6, 92.4] |
COMPROMISO | |||
Mean (SD) | 17.0 (11.9) | 26.7 (21.8) | 17.4 (12.7) |
Median [Min, Max] | 15.3 [0, 85.8] | 23.1 [0.800, 130] | 15.6 [0, 130] |
t.test(datos$COMPROMISO~datos$DEFAULT)
##
## Welch Two Sample t-test
##
## data: datos$COMPROMISO by datos$DEFAULT
## t = -2.7594, df = 39.204, p-value = 0.008756
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -16.800108 -2.589635
## sample estimates:
## mean in group 0 mean in group 1
## 16.96154 26.65641
t.test(datos$ANTIUEDAD~datos$DEFAULT)
##
## Welch Two Sample t-test
##
## data: datos$ANTIUEDAD by datos$DEFAULT
## t = 1.9332, df = 42.35, p-value = 0.05992
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -0.1610822 7.5375264
## sample estimates:
## mean in group 0 mean in group 1
## 18.21966 14.53144
t.test(datos$EDAD~datos$DEFAULT)
##
## Welch Two Sample t-test
##
## data: datos$EDAD by datos$DEFAULT
## t = 0.94189, df = 42.162, p-value = 0.3516
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -2.196093 6.040982
## sample estimates:
## mean in group 0 mean in group 1
## 57.08114 55.15869
datos$EDAD_GRUPO=cut(datos$EDAD,breaks = c(0,20,40,60,80,100))
require(CGPfunctions)
PlotXTabs2(data = datos,x = EDAD_GRUPO,y = DEFAULT)
modelo=glm(DEFAULT~EDAD+ANTIUEDAD+COMPROMISO,data = datos,family = binomial(link="logit"))
summary(modelo)
##
## Call:
## glm(formula = DEFAULT ~ EDAD + ANTIUEDAD + COMPROMISO, family = binomial(link = "logit"),
## data = datos)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.1700 -0.3526 -0.2798 -0.2167 2.9410
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -4.27244 0.94783 -4.508 6.56e-06 ***
## EDAD 0.02054 0.01913 1.074 0.283
## ANTIUEDAD -0.04685 0.02158 -2.171 0.030 *
## COMPROMISO 0.04523 0.01140 3.968 7.24e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 309.68 on 779 degrees of freedom
## Residual deviance: 288.08 on 776 degrees of freedom
## AIC: 296.08
##
## Number of Fisher Scoring iterations: 6
OR=exp(modelo$coefficients)
OR
## (Intercept) EDAD ANTIUEDAD COMPROMISO
## 0.01394766 1.02075494 0.95423016 1.04626372
##Ejemplo vamos a predeccir la probabilidad de que un asociado presente default si tiene:
# 30 años, lleva 5 años en la empresa y su nivel de compromiso es de 50%
lineal=-4.27244+(0.02054*30)-(0.04685*5)+(0.04523*50)
exp(lineal)/(1+exp(lineal))
## [1] 0.1639688
predict(modelo,list(EDAD =30,ANTIUEDAD=5, COMPROMISO=50),type = "response")
## 1
## 0.1639472
table(datos$DEFAULT)/780
##
## 0 1
## 0.95 0.05
probas=predict(modelo,list(EDAD =datos$EDAD,ANTIUEDAD=datos$ANTIUEDAD, COMPROMISO=datos$COMPROMISO),type = "response")
default_modelo=probas>0.45
table(datos$DEFAULT,default_modelo)
## default_modelo
## FALSE TRUE
## 0 739 2
## 1 38 1
(471+25)/780
## [1] 0.6358974
library(pROC)
## Type 'citation("pROC")' for a citation.
##
## Attaching package: 'pROC'
## The following objects are masked from 'package:stats':
##
## cov, smooth, var
ROC_res=roc(datos$DEFAULT~probas, percent = T, ci=T)
## Setting levels: control = 0, case = 1
## Setting direction: controls < cases
ROC_res
##
## Call:
## roc.formula(formula = datos$DEFAULT ~ probas, percent = T, ci = T)
##
## Data: probas in 741 controls (datos$DEFAULT 0) < 39 cases (datos$DEFAULT 1).
## Area under the curve: 70.37%
## 95% CI: 62.31%-78.42% (DeLong)
plot(ROC_res,print.auc=T,print.thres = "best",col="red"
,xlab = "Specificity", ylab = "Sensitivity")
predict(modelo,list(EDAD =20,ANTIUEDAD=1, COMPROMISO=10),type = "response")
## 1
## 0.03058482