%% Fill Missing Data in a creditscorecard Object
% This example shows how to use fillmissing to replace missing values in the CustAge and ResStatus predictors with user-defined values. For additional information on alternative approaches for "treating" missing data, see Credit Scorecard Modeling with Missing Values.
% Load the credit scorecard data and use dataMissing for the training data.

load CreditCardData.mat
disp(head(dataMissing));

% Create a creditscorecard object with 'BinMissingData' set to true.

sc = creditscorecard(dataMissing,'BinMissingData',true);
sc = autobinning(sc);

% Use bininfo and plotbins to display the CustAge and ResStatus predictors with missing data.

bininfo(sc,'CustAge')
plotbins(sc,'CustAge');

bininfo(sc,'ResStatus')
plotbins(sc,'ResStatus');

% Use fillmissing to replace NaN values in CustAge with the median value and to replace the <missing> values in ResStatus with 'Tenant'. Use predictorinfo to verify the filled values.

sc = fillmissing(sc,{'CustAge'},'median');
sc = fillmissing(sc,{'ResStatus'},'constant','Tenant');
predictorinfo(sc,'CustAge')
predictorinfo(sc,'ResStatus')

% Use bininfo and plotbins to display the CustAge and ResStatus predictors to verify that the missing data has been replaced with the values defined by fillmissing.

bininfo(sc,'CustAge')
plotbins(sc,'CustAge');

bininfo(sc,'ResStatus')
plotbins(sc,'ResStatus');

% Use fitmodel and then run formatpoints, displaypoints, and score.

sc = fitmodel(sc,'Display','off');
sc = formatpoints(sc,'WorstAndBest',[300 800]);
t = displaypoints(sc)

% When a validation data set has missing values and you use fillmissing with the training dataset, the missing values in the validation data set are assigned the same points as the corresponding bins containing the filled values.
% As the table shows, the '<missing>' bin for the CustAge predictor is assigned the same points as the '[40,46)' bin because the missing data is filled with the median value 45.
% The points assigned to the '<missing>' bin for the EmpStatus predictor are NaN because fillmissing is not used for that predictor. The assigned points are decided by the default 'NoScore' for the 'Missing' name-value pair argument in formatpoints.
% Create a test validation data set (tdata) and add missing values.

tdata = data(1:10,:);
tdata.CustAge(1) = NaN;
tdata.ResStatus(2) = '<undefined>';
[scr,pts] = score(sc,tdata)

% Copyright 2019 The MathWorks, Inc.