Skip to main content

Learn from credit risk modeling - p1

Credit Analysis

# View the structure of loan_data
str(loan_data)

# Load the gmodels package
library(gmodels)

# Call CrossTable() on loan_status
CrossTable(loan_data$loan_status)

# Call CrossTable() on grade and loan_status
#CrossTable(x=loan_data$grade,y=loan_data$loan_status,prop.r=TRUE, prop.c=FALSE,prop.t=FALSE,Prop.chisq=FALSE)
CrossTable(loan_data$grade, loan_data$loan_status, prop.r = TRUE,
           prop.c = FALSE, prop.t = FALSE, prop.chisq = FALSE)

we only want row-wise proportions, so set prop.r to TRUE, but prop.c , prop.t and prop.chisq to FALSE (default values here are TRUE, and this would lead to inclusion of column proportions, table proportions and chi-square contributions for each cell. We do not need these here.)# Create histogram of loan_amnt: hist_1
hist_1<-hist(loan_data$loan_amnt)

# Print locations of the breaks in hist_1
hist_1$breaks
#Knowing the location of the breaks is important because if they are poorly chosen, the histogram may be misleading.

# Change number of breaks and add labels: hist_2
hist_2 <- hist(loan_data$loan_amnt, breaks = 200, xlab = "Loan Amount",
               main = "Histogram of the loan Amount")

# Plot the age variable
plot(loan_data$age,ylab="Age")

# Save the outlier's index to index_highage
index_highage <- which(loan_data$age > 122)

# Create data set new_data with outlier deleted
new_data <- loan_data[-index_highage, ]

# Make bivariate scatterplot of age and annual income
plot(loan_data$age, loan_data$annual_inc, xlab = "Age", ylab = "Annual Income")

# Look at summary of loan_data
summary(loan_data$int_rate)

# Get indices of missing interest rates: na_index
na_index <- which(is.na(loan_data$int_rate))

# Remove observations with missing interest rates: loan_data_delrow_na
loan_data_delrow_na <- loan_data[-na_index, ]

# Make copy of loan_data
loan_data_delcol_na <- loan_data

# Delete interest rate column from loan_data_delcol_na
loan_data_delcol_na$int_rate <- NULL

# Compute the median of int_rate
median_ir <- median(loan_data$int_rate,na.rm=TRUE)

# Make copy of loan_data
loan_data_replace <- loan_data

# Replace missing interest rates with median
loan_data_replace$int_rate[na_index] <- median_ir

# Check if the NAs are gone
summary(loan_data_replace$int_rate)


In some situations, the fact that an input is missing is important information in itself. NAs can be kept in a separate "missing" category using coarse classification.

Coarse classification allows you to simplify your data and improve the interpretability of your model. Coarse classification requires you to bin your responses into groups that contain ranges of values. You can use this binning technique to place all NAs in their own bin.


# Make the necessary replacements in the coarse classification example below
loan_data$ir_cat <- rep(NA, length(loan_data$int_rate))

loan_data$ir_cat[which(loan_data$int_rate <= 8)] <- "0-8"
loan_data$ir_cat[which(loan_data$int_rate > 8 & loan_data$int_rate <= 11)] <- "8-11"
loan_data$ir_cat[which(loan_data$int_rate > 11 & loan_data$int_rate <= 13.5)] <- "11-13.5"
loan_data$ir_cat[which(loan_data$int_rate > 13.5)] <- "13.5+"
loan_data$ir_cat[which(is.na(loan_data$int_rate))] <- "Missing"

loan_data$ir_cat <- as.factor(loan_data$ir_cat)

# Look at your new variable using plot()
plot(loan_data$ir_cat)


To make your training and test sets, you should first set a seed using set.seed(). Seeds allow you to create a starting point for randomly generated numbers, so that each time your code is run the same answer is generated. The advantage of doing this in your sampling is that you or anyone else can recreate the exact same training and test sets by using the same seed.

# Set seed of 567
set.seed(567)

# Store row numbers for training set: index_train
index_train <- sample(1:nrow(loan_data),2/3*nrow(loan_data))

# Create training set: training_set
training_set <- loan_data[index_train, ]

# Create test set: test_set
test_set <- loan_data[-index_train, ]

# Create confusion matrix
conf_matrix <- table(test_set$loan_status,model_pred)

# Compute classification accuracy
(conf_matrix[1,1]+conf_matrix[2,2])/(conf_matrix[1,1]+conf_matrix[2,2]+conf_matrix[1,2]+conf_matrix[2,1])

# Compute sensitivity
(conf_matrix[2,2])/(conf_matrix[2,2]+conf_matrix[2,1])

Comments

Popular posts from this blog

Installing OpenERP on Ubuntu 12.04

1. Install Ubuntu Server with OpenSSH Server.So that you can run your commands from a remote server. 2. Run the following commands to install required depedency sudo apt-get install postgresql postgresql-client python-dateutil python-feedparser python-gdata python-ldap python-libxslt1 python-lxml python-mako python-openid python-psycopg2 python-pybabel python-pychart python-pydot python-pyparsing python-reportlab python-simplejson python-tz python-vatnumber python-vobject python-webdav python-werkzeug python-xlwt python-yaml python-zsi 3. Download Latest Openerp all file wget http://nightly.openerp.com/6.1/releases/openerp_6.1-1-1_all.deb 4. Install openErp server. sudo dpkg -i openerp_6.1-1-1_all.deb sudo apt-get install openerp -f 5. Reboot 6. Access the Web Client using http://ip:8069

20 Extensions for VSCode For Node+Angular2 Development

Here is the list of Extensions which I found important and Interesting. The names are in the form of  Author.Name christian-kohler.npm-intellisense CoenraadS.bracket-pair-colorizer DavidAnson.vscode-markdownlint dbaeumer.jshint donjayamanne.githistory ecmel.vscode-html-css eg2.tslint eg2.vscode-npm-script fknop.vscode-npm formulahendry.auto-close-tag formulahendry.auto-rename-tag HookyQR.beautify humao.rest-client lonefy.vscode-JS-CSS-HTML-formatter mkaufman.HTMLHint msjsdiag.debugger-for-chrome prashaantt.node-tdd rbbit.typescript-hero robertohuertasm.vscode-icons Zignd.html-css-class-completion See the following link to quickly install these extensions. Here is the extension.json . https://code.visualstudio.com/docs/editor/extension-gallery#_workspace-recommended-extensions

OpenERP 7 on Ubuntu

install the following sudo apt-get install graphviz ghostscript postgresql-client python-dateutil python-feedparser python-matplotlib python-ldap python-libxslt1 python-lxml python-mako python-openid python-psycopg2 python-pybabel python-pychart python-pydot python-pyparsing python-reportlab python-simplejson python-tz python-vatnumber python-vobject python-webdav python-werkzeug python-xlwt python-yaml python-imaging gcc python-dev mc bzr python-setuptools python-babel python-feedparser python-reportlab-accel python-zsi python-openssl python-egenix-mxdatetime python-jinja2 python-unittest2 python-mock python-docutils lptools make python-psutil python-paramiko poppler-utils python-pdftools antiword postgresql sudo -u postgres createuser -s openerp install using To install the Debian/Ubuntu package, add the following line to your /etc/apt/sources.list: deb http://nightly.odoo.com/7.0/nightly/deb/ ./ And type: sudo apt-get update sudo apt-get install openerp