%% Fit a Stepwise Logistic Model 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 numeric or categorical data in a separate bin.

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

% Use fitmodel to fit a logistic regression model using Weight of Evidence (WOE) data. fitmodel internally transforms all the predictor variables into WOE values, using the bins found with the automatic binning process. fitmodel then fits a logistic regression model using a stepwise method (by default). For predictors that have missing data, there is an explicit <missing> bin, with a corresponding WOE value computed from the data. When using fitmodel, the corresponding WOE value for the <missing> bin is applied when performing the WOE transformation. For example, a missing value for customer age (CustAge) is replaced with -0.15787 which is the WOE value for the <missing> bin for the CustAge predictor. However, when 'BinMissingData' is false, a missing value for CustAge remains as missing (NaN) when applying the WOE transformation.

[sc,mdl] = fitmodel(sc);

% Copyright 2018 The MathWorks, Inc.