%% Bin Additional "Test" Data When Using the 'BinMissingData' Option
% Create a creditscorecard object using the CreditCardData.mat file to load the data with missing values. The variables CustAge and ResStatus have missing values.

load CreditCardData.mat 
head(dataMissing,5)

%
% Use creditscorecard with the name-value argument 'BinMissingData' set to true to bin the missing numeric or categorical data in a separate bin. Apply automatic binning.

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

disp(sc)

%
% Display and plot bin information for numeric data for 'CustAge' that includes missing data in a separate bin labelled <missing>.

[bi,cp] = bininfo(sc,'CustAge');
disp(bi)
plotbins(sc,'CustAge')

% Display and plot bin information for categorical data for 'ResStatus' that includes missing data in a separate bin labelled <missing>.

[bi,cg] = bininfo(sc,'ResStatus');
disp(bi)
plotbins(sc,'ResStatus')

% For the 'CustAge' and 'ResStatus' predictors, there is missing data (NaNs and <undefined>) in the training data, and the binning process estimates a WOE value of -0.15787 and 0.026469 respectively for missing data in these predictors, as shown above.
%
% For the purpose of illustration, take a few rows from the original data as test data and introduce some missing data.

tdata = dataMissing(11:14,:);
tdata.CustAge(1) = NaN;
tdata.TmAtAddress(2) = NaN;
tdata.ResStatus(3) = '<undefined>';
tdata.EmpStatus(4) = '<undefined>';
disp(tdata)

%
% Convert the test data to WOE values. To do this, set the bindata name-value pair argument for 'OutputType' to 'WOE', passing the test data tdata as an optional input.

bdata = bindata(sc,tdata,'OutputType','WOE');
disp(bdata)

% For the 'CustAge' and 'ResStatus' predictors, because there is missing data in the training data, the missing values in the test data get mapped to the WOE value estimated for the <missing> bin. Therefore, a missing value for 'CustAge' is replaced with -0.15787, and a missing value for 'ResStatus' is replaced with 0.026469.
% For 'TmAtAddress' and 'EmpStatus', the training data has no missing values, therefore there is no bin for missing data, and there is no way to estimate a WOE value for missing data. Therefore, for these predictors, the WOE transformation leaves missing values as missing (that is, sets a WOE value of NaN).
% These rules apply when 'OutputType' is set to 'WOE' or 'WOEModelInput'. The rationale is that if a data-based WOE value exists for missing data, it should be used for the WOE transformation and for subsequent steps (for example, fitting a logistic model or scoring).
% On the other hand, when 'OutputType' is set to 'BinNumber' or 'Categorical', bindata leaves missing values as missing, since this allows you to subsequently treat the missing data as you see fit.
% For example, when 'OutputType' is set to 'BinNumber', missing values are set to NaN:

bdata = bindata(sc,tdata,'OutputType','BinNumber');
disp(bdata)

% And when 'OutputType' is set to 'Categorical', missing values are set to '<undefined>':

bdata = bindata(sc,tdata,'OutputType','Categorical');
disp(bdata)

%
% Copyright 2018 The MathWorks, Inc.