Computing a bad rate given a fixed acceptance rate
In the video, you learned how to compute the bad rate (or, the percentage of defaults) in the loan portfolio of a bank when given:
- a specific model
- the acceptance rate
In this exercise, you will compute the bad rate that a bank can expect when using the pruned tree ptree_prior
that you fitted before, and an acceptance rate of 80%. As a reminder, the tree is plotted on your right hand side.
This is a part of the course
“Credit Risk Modeling in R”
Exercise instructions
- In the script, you are provided the code to make predictions for the probability of default using the pruned tree and
test_set
. Remember that if you use thepredict()
function for a tree, the probability of default can be found in the second column. Therefore[,2]
was pasted to thepredict()
function. - Obtain the cut-off that leads to an acceptance rate of 80%, using
prob_default_prior
. You can use the quantile()- function to do this, setting the second argument to 0.8. Assign the namecutoff_prior
. - The code to obtain the actual binary default predictions (0 or 1) is provided. ifelse() here. Name the object
bin_pred_prior_80
. - The code to select the default indicators of
test_set
for the accepted loans acording to a 80% acceptance rate is provided. - Compute the percentage of defaults (or the "bad rate") for the accepted loans. This is the number of occurences of
1
inaccepted_status_prior_80
, divided by the total number of instances in this vector. Print the solution to your R-console.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Make predictions for the probability of default using the pruned tree and the test set.
prob_default_prior <- predict(ptree_prior, newdata = test_set)[ ,2]
# Obtain the cutoff for acceptance rate 80%
# Obtain the binary predictions.
bin_pred_prior_80 <- ifelse(prob_default_prior > cutoff_prior, 1, 0)
# Obtain the actual default status for the accepted loans
accepted_status_prior_80 <- test_set$loan_status[bin_pred_prior_80 == 0]
# Obtain the bad rate for the accepted loans
This exercise is part of the course
Credit Risk Modeling in R
Apply statistical modeling in a real-life setting using logistic regression and decision trees to model credit risk.
In this chapter, you'll learn how you can evaluate and compare the results obtained through several credit risk models.
Exercise 1: Finding the right cut-off: the strategy curveExercise 2: Computing a bad rate given a fixed acceptance rateExercise 3: The strategy table and strategy curveExercise 4: To tree or not to tree?Exercise 5: The ROC-curveExercise 6: ROC-curves for comparison of logistic regression modelsExercise 7: ROC-curves for comparison of tree-based modelsExercise 8: Input selection based on the AUCExercise 9: Another round of pruning based on AUCExercise 10: Best of fourExercise 11: Further model reduction?Exercise 12: Course wrap-upWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.