Predicting Loan Defaults with R

Can you predict whether someone will default on a loan? I built two machine learning models in R to find out.

I used anonymized loan default data from Kaggle. The dataset had 36 variables, including annual income and loan term. Before modeling, I cleaned the data. I dropped ID-style columns and rows with missing values, then converted the 'repay failure' variable from a number into a two-level factor. That makes it a proper classification target and simplifies the analysis.

Next, I split the data: 80% for training, 20% for testing. With a dataset this large, that split keeps training manageable while leaving plenty of data to evaluate the models fairly on cases they haven't seen.

I trained two models: k-Nearest Neighbors and Random Forest. kNN classifies each loan based on the most similar loans in the training data. Random Forest builds many decision trees and combines their votes, which makes it flexible without being fragile. I used cross-validation on both to tune them and guard against overfitting.

Both models predicted repayment failure from annual income and loan amount. I generated predictions on the test set with predict() and evaluated each model with a confusion matrix. Cross-validation improved accuracy in both cases.

The results: kNN reached 84.84% accuracy, Random Forest 84.78% with cross-validation. Without cross-validation, Random Forest performed better.

One important caveat. Defaults were rare in this dataset, so a model can score high accuracy just by predicting "repaid" most of the time. Overall accuracy tells only part of the story here, and the confusion matrix matters more than the headline number.

The dataset and R code are on GitHub: https://github.com/eafinnigan/EdX_Capstone_CYO

Next
Next

Yelp III: Are cities really different, or do they just have more reviews?