%% Modify Bin Information When Using Missing Data
% Create a creditscorecard object using the CreditCardData.mat file to load the dataMissing with missing values.

load CreditCardData.mat 
head(dataMissing,5)

fprintf('Number of rows: %d\n',height(dataMissing))
fprintf('Number of missing values CustAge: %d\n',sum(ismissing(dataMissing.CustAge)))
fprintf('Number of missing values ResStatus: %d\n',sum(ismissing(dataMissing.ResStatus)))

% Use creditscorecard with the name-value argument 'BinMissingData' set to true to bin the missing data in a separate bin.

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

disp(sc)

%
% Display 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')

% For the numeric predictor CustAge, remove cut points 48 and 51 and then use modifybins to define a 'MinValue'of 0 to manually change the binning and notice that this does not affect the data in the <missing> bin and the <missing> bin remains at the end.

cp(cp==48) = [];
cp(cp==51) = []; 
sc = modifybins(sc,'CustAge','CutPoints',cp,'MinValue',0); 
bi = bininfo(sc,'CustAge');
disp(bi)
plotbins(sc,'CustAge')

% Display 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 categorical predictor ResStatus, use modifybins to manually merge 'HomeOwner' and 'Other' into a single group by assigning the same bin number to these categories. Notice that this does not affect the data in the <missing> bin and the <missing> bin remains at the end.

cg.BinNumber(3) = 2; 
sc = modifybins(sc,'ResStatus','CatGrouping',cg);
[bi,cg] = bininfo(sc,'ResStatus'); 
disp(bi) 
disp(cg) 

% Copyright 2018 The MathWorks, Inc.