In this exercise, we fit a neural network on the Default data, available in the ISLR2 library. Have a look at labs 10.9.1-10.9.2 for guidance. Compare the classification performance of the deep NN model with that of logistic regression.
Data preprocessing:
default
from character (“yes” & “no”) to numeric (1 & 0).train.idx
.x
that can be processed by a NN. Use the model.matrix()
function and scale the input variables.
Use income
and balance
as independent variables. Also store the dependent variable default
in a vector y
.Model building:
nn.model
with 2 hidden layers of 10 hidden units each, and a relu activation function.Model compiling:
modnn
model from the previous exercise as follows:
binary_crossentropy
as the loss functionoptimizer_rmsprop()
as you optimizertf$keras$metrics$AUC()
as a metric.Model fitting:
history
.Model evaluation:
nn.pred
.pROC::auc(pROC::roc(drop(response), as.numeric(drop(predictor))))
, with response
and predictor
obtained in the previous steps.Logistic regression:
lr.model
and the predictions in lr.pred
.