Carregando as libraries necessarias

## Loading required package: gdata
## gdata: read.xls support for 'XLS' (Excel 97-2004) files ENABLED.
## 
## gdata: read.xls support for 'XLSX' (Excel 2007+) files ENABLED.
## 
## Attaching package: 'gdata'
## The following object is masked from 'package:stats':
## 
##     nobs
## The following object is masked from 'package:utils':
## 
##     object.size
## The following object is masked from 'package:base':
## 
##     startsWith
## Loading required package: ggplot2

Importando a base de dados

setwd("~/Área de Trabalho/FIA/tcc/credito/data")

df <- read.xls('default of credit card clients.xls', sheet = 1)    

01 Analise Descritiva

Verificando a estrutura da base

str(df)
## 'data.frame':    30000 obs. of  25 variables:
##  $ ID                        : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ LIMIT_BAL                 : int  20000 120000 90000 50000 50000 50000 500000 100000 140000 20000 ...
##  $ SEX                       : int  2 2 2 2 1 1 1 2 2 1 ...
##  $ EDUCATION                 : int  2 2 2 2 2 1 1 2 3 3 ...
##  $ MARRIAGE                  : int  1 2 2 1 1 2 2 2 1 2 ...
##  $ AGE                       : int  24 26 34 37 57 37 29 23 28 35 ...
##  $ PAY_0                     : int  2 -1 0 0 -1 0 0 0 0 -2 ...
##  $ PAY_2                     : int  2 2 0 0 0 0 0 -1 0 -2 ...
##  $ PAY_3                     : int  -1 0 0 0 -1 0 0 -1 2 -2 ...
##  $ PAY_4                     : int  -1 0 0 0 0 0 0 0 0 -2 ...
##  $ PAY_5                     : int  -2 0 0 0 0 0 0 0 0 -1 ...
##  $ PAY_6                     : int  -2 2 0 0 0 0 0 -1 0 -1 ...
##  $ BILL_AMT1                 : int  3913 2682 29239 46990 8617 64400 367965 11876 11285 0 ...
##  $ BILL_AMT2                 : int  3102 1725 14027 48233 5670 57069 412023 380 14096 0 ...
##  $ BILL_AMT3                 : int  689 2682 13559 49291 35835 57608 445007 601 12108 0 ...
##  $ BILL_AMT4                 : int  0 3272 14331 28314 20940 19394 542653 221 12211 0 ...
##  $ BILL_AMT5                 : int  0 3455 14948 28959 19146 19619 483003 -159 11793 13007 ...
##  $ BILL_AMT6                 : int  0 3261 15549 29547 19131 20024 473944 567 3719 13912 ...
##  $ PAY_AMT1                  : int  0 0 1518 2000 2000 2500 55000 380 3329 0 ...
##  $ PAY_AMT2                  : int  689 1000 1500 2019 36681 1815 40000 601 0 0 ...
##  $ PAY_AMT3                  : int  0 1000 1000 1200 10000 657 38000 0 432 0 ...
##  $ PAY_AMT4                  : int  0 1000 1000 1100 9000 1000 20239 581 1000 13007 ...
##  $ PAY_AMT5                  : int  0 0 1000 1069 689 1000 13750 1687 1000 1122 ...
##  $ PAY_AMT6                  : int  0 2000 5000 1000 679 800 13770 1542 1000 0 ...
##  $ default.payment.next.month: int  1 1 0 0 0 0 0 0 0 0 ...

Ajustando as colunas que devem ser fatores:

df$SEX <- as.factor(df$SEX)
df$EDUCATION <- as.factor(df$EDUCATION)
df$MARRIAGE <- as.factor(df$MARRIAGE)
df$PAY_0 <- as.factor(df$PAY_0)
df$PAY_2 <- as.factor(df$PAY_2)
df$PAY_3 <- as.factor(df$PAY_3)
df$PAY_4 <- as.factor(df$PAY_4)
df$PAY_5 <- as.factor(df$PAY_5)
df$PAY_6 <- as.factor(df$PAY_6)
df$defaul <- as.factor(df$default.payment.next.month)

Variavel de Limite de credito

##Verificando se existem dados Faltantes:
NROW(df[is.na(df$LIMIT_BAL),])
## [1] 0
##Sumarizacao das metricas
summary(df$LIMIT_BAL)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   10000   50000  140000  167500  240000 1000000
##Plots

hist(df$LIMIT_BAL/1000, col = "green", main = "Distribuição de LIMIT_BAL", xlab = "Limite em K")
abline(v=mean(df[df$defaul == 1,]$LIMIT_BAL/1000), col = c("red"), lwd=2)
abline(v=mean(df[df$defaul == 0,]$LIMIT_BAL/1000), col = c("blue"), lwd=2)

boxplot((df$LIMIT_BAL/1000) ~ df$defaul, main = 'Limite de Crédito por Default', ylab = 'Limite em K', xlab='Default de pagamento' )

Variavel Sexo

##Verificando se existem dados faltantes
NROW(df[is.na(df$SEX),])
## [1] 0
##Contagem
summary(df$SEX)
##     1     2 
## 11888 18112
##Table com Default
par(mfrow=c(1,2))

barplot(table(df$SEX, df$defaul), col = c('lightblue','pink'), main = "Sexo Vs Default de Pagamento", font.main = 4)

pie(table(df$SEX), labels = c("masculino","feminino"), col = c("lightblue","pink"), main = "SEX - Visao Geral", 
    legend = c("masculino","feminino"))
## Warning in text.default(1.1 * P$x, 1.1 * P$y, labels[i], xpd = TRUE, adj =
## ifelse(P$x < : "legend" não é um parâmetro gráfico

## Warning in text.default(1.1 * P$x, 1.1 * P$y, labels[i], xpd = TRUE, adj =
## ifelse(P$x < : "legend" não é um parâmetro gráfico
## Warning in title(main = main, ...): "legend" não é um parâmetro gráfico

par(mfrow=c(1,1))

Variavel Education

##Verificando se existem dados faltantes
NROW(df[is.na(df$EDUCATION),])
## [1] 0
##Contagem
summary(df$EDUCATION)
##     0     1     2     3     4     5     6 
##    14 10585 14030  4917   123   280    51
##De acordo com a documentação, deveria existir apenas os niveis 1-4. Os demais serão jogados no nível 4
df[df$EDUCATION %in% c(0,5,6),]$EDUCATION <- 4

#Table
table(df$EDUCATION, df$defaul)
##    
##         0     1
##   0     0     0
##   1  8549  2036
##   2 10700  3330
##   3  3680  1237
##   4   435    33
##   5     0     0
##   6     0     0
##Plot com Default
par(mfrow=c(1,2))
barplot(table(df$EDUCATION, df$defaul),  main = "Educação Vs Default de Pgto", font.main = 4,
        col = c("lightgreen","yellow","lightblue","red"))

pie(table(df$EDUCATION), labels = c("outros","pós graduação","faculdade","ensino médio"), col = c("lightgreen","yellow","lightblue","red"), main = "Education - Visao Geral")

par(mfrow=c(1,1))

Variavel Marriage

##Verificando a existência de Nulos
NROW(df[is.na(df$MARRIAGE),])
## [1] 0
##Traz um sumário com as variaveis
summary(df$MARRIAGE)
##     0     1     2     3 
##    54 13659 15964   323
##De acordo com a documentação, deveria existir apenas os niveis 1-3. Os demais serão jogados no nível 3
df[df$MARRIAGE %in% c(0),]$MARRIAGE <- 3

#Table
table(df$MARRIAGE, df$defaul)
##    
##         0     1
##   0     0     0
##   1 10453  3206
##   2 12623  3341
##   3   288    89
#Plot

par(mfrow=c(1,2))

barplot(table(df$MARRIAGE, df$defaul),  main = "Estado Civil Vs Default de Pgto", font.main = 4,
        col = c("orange","gray","green"))

pie(table(df$MARRIAGE), labels = c("outros","casado","solteiro"), col = c("orange","gray","green"), main = "Estado Civil - Visao Geral")

par(mfrow=c(1,1))

Variavel AGE

##Verificando se existem dados Faltantes:
NROW(df[is.na(df$AGE),])
## [1] 0
##Sumarizacao das metricas
summary(df$AGE)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   21.00   28.00   34.00   35.49   41.00   79.00
##Plots

hist(df$AGE, col = "blue", main = "Distribuição de AGE")
abline(v=mean(df[df$defaul == 1,]$AGE), col = c("red"), lwd=2)
abline(v=mean(df[df$defaul == 0,]$AGE), col = c("green"), lwd=2)

boxplot((df$AGE) ~ df$defaul, main = 'AGE vs Default', ylab = 'Idade', xlab='Default de pagamento', col = "orange" )