Keeping missing data
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.
In the video, we illustrated the idea of coarse classification for employment length. The code from that example has been reproduced in the R script to the right and can be adapted to coarse classify the int_rate variable.
Este exercício faz parte do curso
Credit Risk Modeling in R
Instruções do exercício
- Make the necessary changes to the code provided to coarse classify
int_rate, saving the result to a new variable calledir_cat.- First, replace
loan_data$emp_catbyloan_data$ir_catwhere it occurs in the R script, as well as replacingloan_data$emp_lengthbyloan_data$int_rate. - Next, the variables should be binned in categories
"0-8","8-11","11-13.5", and"13.5+"(replacing"0-15","15-30","30-45"and"45+"). Usage of>and<=is exactly as in the video. Make sure to change the numbers in the conditional statements too (15, 30 and 45 should be changed to 8, 11 and 13.5 respectively).
- First, replace
- Look at your new variable
ir_catusingplot(loan_data$ir_cat).
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# Make the necessary replacements in the coarse classification example below
loan_data$emp_cat <- rep(NA, length(loan_data$emp_length))
loan_data$emp_cat[which(loan_data$emp_length <= 15)] <- "0-15"
loan_data$emp_cat[which(loan_data$emp_length > 15 & loan_data$emp_length <= 30)] <- "15-30"
loan_data$emp_cat[which(loan_data$emp_length > 30 & loan_data$emp_length <= 45)] <- "30-45"
loan_data$emp_cat[which(loan_data$emp_length > 45)] <- "45+"
loan_data$emp_cat[which(is.na(loan_data$emp_length))] <- "Missing"
loan_data$emp_cat <- as.factor(loan_data$emp_cat)
# Look at your new variable using plot()