Professional Documents
Culture Documents
FRA Assignment - India Credit Model
FRA Assignment - India Credit Model
Using logistic regression, we are required to build this model. We shall be required to
perform following on the dataset :
EDA (Exploratory Data analytics)
Modelling on the data set
Evaluate model performance
The given dataset has 52 columns which captures various financial parameters.
While looking at the dataset, following are the initial inferences:
The “net worth for next year” in the given dataset needs to be converted into
the binary form so that the logistic regression can be modelled on the dataset.
The given dataset has very high skewness in the data set which need to be
removed before modelling. Also, the outlier in the given dataset needs
treatment so that model gives us better output.
2. Phase II – Data Preparation
From section above, following steps are done to understand the data in binary form :
In order to have data in binary form – we are considering that If the net worth
for the given parameter/ number is less than zero or negative (company
making losses) it is treated as zero & if the net worth is positive (company
making profit) the value is considered as 1.
With this we come to know that 234 sample values are there which are
default. They are rated as 0. The default rate comes out to be 234/3541 as
6.5%.
The Outlier has been treated with the capping logic. The highest value has been
treated to the 99% value & the lowest with the 1% value. So that we can shrink the
data & outlier can be removed. By doing this we have kept in mind regarding the
skewness factor. The missing values has been treated with the average value. For
treating the missing value we got the missing vs. observed graph ready which
shared that the 7% of the value are not available for the given dataset.
missing_values = lapply(data_training,function(x)
sum(is.na(x))) missing_values
missmap(data_training, main = "Missing values vs
observed")
Now we need to remove the attributes where the missing values are more than 50%.
#Remove columns for both test and training data for which more than 50% is missing
i.e. ones which have more 1416 missing
data_training <-subset(data_training,-c(data_training$`PE on
BSE`,data_training$`Deposits (accepted by commercial
banks)`,data_training$Investments))
data_validation <- subset(data_validation, select = -c(data_validation$`PE on
BSE`,data_validation$`Deposits (accepted by commercial
banks)`,data_validation$Investments))
## Now replacing the NA by the mean value for the given attribute:
mean_values = array()
##Replace the rest of missing NAs with mean values in TRAINING data.
for (i in 1:length(data_training)) {
mean_values[i] = mean(data_training[,i],na.rm=T)
data_training[,i][is.na(data_training[,i])] <-
mean_values[i]
}
#Replace the rest of missing NAs with mean values for given training &
validation data.
data_validation_non_missing = data_validation
for (i in 1:length(data_validation_non_missing)) {
data_validation_non_missing[,i]
[is.na(data_validation_non_missing[,i])] <-
mean(data_validation_non_missing[,i],na.rm=T)
}
Code for the capping for 99% & 1% values to reduce the
number of outliers:
## Remove Outliers
data_summary = summary(data_training)
data_summary
col_length = ncol(data_summary)
View(data_training)
data_training_pruned =
data_training
View(data_training_pruned)
#Remove anything above 99% and Remove anything below 1%
for (i in 2:col_length) {
# for (i in 2:18) {
if (colnames(data_training_pruned)[i] != "Default") {
topmost = quantile(data_training[,i], 0.99)
bottommost = quantile(data_training[,i], 0.01)
data_training_pruned = training_pruned[data_training_pruned[,i] < topmost,]
Inference :
With this the data readiness is completed & now we can go ahead with the logistic
model which need to be built on the shared dataset.
3. Phase III – Model Planning and Building
library(ISLR)
library(dplyr)
library(ggvis)
library(boot)
library(rJava)
str(data)
summary(data)
dim(data)
## [1] 3541 52
train <- data[,2:52]
val <- val[,2:52] str(train)
dim(train)
## [1] 3541 51
str(train)
#Remove the blank variable from the file
#Let's run first iteration
my_logit <- glm(Default~., family = "binomial", data = train)
Summary(my_logit)
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ##
## (Dispersion parameter for binomial family taken to be 1)
## Null deviance: 1723.67 on 3540 degrees of freedom
## Residual deviance: 875.68 on 3492 degrees of freedom
## AIC: 973.68
## Number of Fisher Scoring iterations: 14
#Now is the time to implement and check the performance of boruta
package. The syntax of boruta is almost similar to regression (lm)
method.
# install.packages("Boruta")
library(Boruta)
set.seed(123)
boruta.train <- Boruta(Default~., data = train, doTrace = 2)
summary(my_logit2_Val)
## Call:
## glm(formula = sig.formula, data = val)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -0.49273 -0.06993 -0.03881 0.00430 0.91737
## (Dispersion parameter for gaussian family taken to be 0.0283165) ##
## Null deviance: 15.355 on 396 degrees of freedom
## Residual deviance: 10.845 on 383 degrees of freedom
## (318 observations deleted due to missingness)
## AIC: -572.65
##
## Number of Fisher Scoring iterations: 2
The AIC Score on both Train and Test Dataset has a good match.