Podstawowe operacje w R - część 4.

Czyszczenie danych

Zadanie domowe

Korzystając z paczki danych “germancredit” dotyczącą oceny kredytowej (creditability) wybranych klientów pewnego banku:

Czy w zbiorze danych mamy obserwacje brakujące?

Proszę dokonać kategoryzacji zmiennej “age.in.years” (wiek w latach) wg oceny kredytowej “creditability”.

Podaj i zinterpretuj wskaźniki informacyjne. Oceń skośność zmiennych ilościowych.

Sprawdź, czy nie mamy obserwacji odstających dla zmiennej “age.in.years” (wiek w latach). Jeśli są - dokonaj imputacji wybraną przez siebie metodą.

data("germancredit")

summary(germancredit)
##                                      status.of.existing.checking.account
##  ... < 0 DM                                            :274             
##  0 <= ... < 200 DM                                     :269             
##  ... >= 200 DM / salary assignments for at least 1 year: 63             
##  no checking account                                   :394             
##                                                                         
##                                                                         
##  duration.in.month
##  Min.   : 4.0     
##  1st Qu.:12.0     
##  Median :18.0     
##  Mean   :20.9     
##  3rd Qu.:24.0     
##  Max.   :72.0     
##                                                      credit.history
##  no credits taken/ all credits paid back duly               : 40   
##  all credits at this bank paid back duly                    : 49   
##  existing credits paid back duly till now                   :530   
##  delay in paying off in the past                            : 88   
##  critical account/ other credits existing (not at this bank):293   
##                                                                    
##    purpose          credit.amount                 savings.account.and.bonds
##  Length:1000        Min.   :  250   ... < 100 DM               :603        
##  Class :character   1st Qu.: 1366   100 <= ... < 500 DM        :103        
##  Mode  :character   Median : 2320   500 <= ... < 1000 DM       : 63        
##                     Mean   : 3271   ... >= 1000 DM             : 48        
##                     3rd Qu.: 3972   unknown/ no savings account:183        
##                     Max.   :18424                                          
##        present.employment.since
##  unemployed        : 62        
##  ... < 1 year      :172        
##  1 <= ... < 4 years:339        
##  4 <= ... < 7 years:174        
##  ... >= 7 years    :253        
##                                
##  installment.rate.in.percentage.of.disposable.income
##  Min.   :1.00                                       
##  1st Qu.:2.00                                       
##  Median :3.00                                       
##  Mean   :2.97                                       
##  3rd Qu.:4.00                                       
##  Max.   :4.00                                       
##                         personal.status.and.sex other.debtors.or.guarantors
##  male : divorced/separated          : 50        none        :907           
##  female : divorced/separated/married:310        co-applicant: 41           
##  male : single                      :548        guarantor   : 52           
##  male : married/widowed             : 92                                   
##  female : single                    :  0                                   
##                                                                            
##  present.residence.since
##  Min.   :1.00           
##  1st Qu.:2.00           
##  Median :3.00           
##  Mean   :2.85           
##  3rd Qu.:4.00           
##  Max.   :4.00           
##                                                  property    age.in.years 
##  real estate                                         :282   Min.   :19.0  
##  building society savings agreement/ life insurance  :232   1st Qu.:27.0  
##  car or other, not in attribute Savings account/bonds:332   Median :33.0  
##  unknown / no property                               :154   Mean   :35.5  
##                                                             3rd Qu.:42.0  
##                                                             Max.   :75.0  
##  other.installment.plans     housing    number.of.existing.credits.at.this.bank
##  bank  :139              rent    :179   Min.   :1.00                           
##  stores: 47              own     :713   1st Qu.:1.00                           
##  none  :814              for free:108   Median :1.00                           
##                                         Mean   :1.41                           
##                                         3rd Qu.:2.00                           
##                                         Max.   :4.00                           
##                                                             job     
##  unemployed/ unskilled - non-resident                         : 22  
##  unskilled - resident                                         :200  
##  skilled employee / official                                  :630  
##  management/ self-employed/ highly qualified employee/ officer:148  
##                                                                     
##                                                                     
##  number.of.people.being.liable.to.provide.maintenance.for
##  Min.   :1.00                                            
##  1st Qu.:1.00                                            
##  Median :1.00                                            
##  Mean   :1.16                                            
##  3rd Qu.:1.00                                            
##  Max.   :2.00                                            
##                                     telephone   foreign.worker creditability
##  none                                    :596   yes:963        bad :300     
##  yes, registered under the customers name:404   no : 37        good:700     
##                                                                             
##                                                                             
##                                                                             
## 
data("germancredit")

VIM:: aggr(germancredit)

#kategoryzacja zmiennej "age.in.years"
germancredit$age.in.years <-as.numeric(germancredit$age.in.years)

bin <- binning_by(germancredit, y="creditability",x="age.in.years")
## Warning in binning_by(germancredit, y = "creditability", x = "age.in.years"): The factor y has been changed to a numeric vector consisting of 0 and 1.
## 'good' changed to 1 (positive) and 'bad' changed to 0 (negative).
summary(bin)
## ¦¦ Binning Table ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦ Several Metrics ¦¦ 
##       Bin CntRec CntPos CntNeg RatePos RateNeg    Odds      WoE      IV     JSD
## 1 [19,25]    190    110     80 0.15714 0.26667 1.37500 -0.52884 0.05792 0.00716
## 2 (25,75]    810    590    220 0.84286 0.73333 2.68182  0.13920 0.01525 0.00190
## 3   Total   1000    700    300 1.00000 1.00000 2.33333       NA 0.07317 0.00906
##       AUC
## 1 0.02095
## 2 0.42429
## 3 0.44524
## 
## ¦¦ General Metrics ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦ 
## • Gini index                       :  -0.10952
## • IV (Jeffrey)                     :  0.07317
## • JS (Jensen-Shannon) Divergence   :  0.00906
## • Kolmogorov-Smirnov Statistics    :  0.10952
## • HHI (Herfindahl-Hirschman Index) :  0.6922
## • HHI (normalized)                 :  0.3844
## • Cramer's V                       :  0.12794 
## 
## ¦¦ Significance Tests ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦ Chisquare Test ¦¦ 
##     Bin A   Bin B statistics      p_value
## 1 [19,25] (25,75]    16.3681 0.0000521562
plot(bin)

#ocena skośności
find_skewness(germancredit, value = TRUE ,thres = 0.1)
##                                        duration.in.month 
##                                                    1.093 
##                                            credit.amount 
##                                                    1.947 
##      installment.rate.in.percentage.of.disposable.income 
##                                                   -0.531 
##                                  present.residence.since 
##                                                   -0.272 
##                                             age.in.years 
##                                                    1.019 
##                  number.of.existing.credits.at.this.bank 
##                                                    1.271 
## number.of.people.being.liable.to.provide.maintenance.for 
##                                                    1.907

W większości przypadków skośność jest dodatnia co świadczy o prawoskośności rozkładu. W “oprocentowaniu raty jako procenta dochodu” i “Obecnym miejscu zamieszkania” występuje rozkład lewoskośny, czyli wydłużone lewe ramię rozkładu. Największa skośność występuje w “Wartości kredytu” co świadczy o zróżnicowanych kwotach kredytów w zbiorze obserwacji.

#odstające obserwacje dla "age.in.years"
wiek<-imputate_outlier(germancredit, age.in.years, method="capping")
summary(wiek)
## Impute outliers with capping
## 
## * Information of Imputation (before vs after)
##             Original  Imputation
## n        1000.000000 1000.000000
## na          0.000000    0.000000
## mean       35.546000   35.350000
## sd         11.375469   10.852985
## se_mean     0.359724    0.343202
## IQR        15.000000   15.000000
## skewness    1.020739    0.821878
## kurtosis    0.595780   -0.132573
## p00        19.000000   19.000000
## p01        20.000000   20.000000
## p05        22.000000   22.000000
## p10        23.000000   23.000000
## p20        26.000000   26.000000
## p25        27.000000   27.000000
## p30        28.000000   28.000000
## p40        30.000000   30.000000
## p50        33.000000   33.000000
## p60        36.000000   36.000000
## p70        39.000000   39.000000
## p75        42.000000   42.000000
## p80        45.000000   45.000000
## p90        52.000000   52.000000
## p95        60.000000   60.000000
## p99        67.010000   63.000000
## p100       75.000000   64.000000