%% Apply a Weight of Evidence (WOE) Transformation to Data
% bindata supports the following types of WOE transformation:
% When the 'OutputType' name-value argument is set to 'WOE', bindata simply applies the WOE transformation to all predictors and keeps the rest of the variables in the original data in place and unchanged.
% When the 'OutputType' name-value pair argument is set to 'WOEModelInput', bindata returns a table that can be used directly as an input for fitting a logistic regression model for the scorecard. In this case, bindata:
% Applies WOE transformation to all predictors.
% Returns predictor variables, but no IDVar or unused variables are included in the output.
% Includes the mapped response variable as the last column.
% The fitmodel function calls bindata internally using the 'WOEModelInput' option to fit the logistic regression model for the creditscorecard model.
% Create a creditscorecard object using the CreditCardData.mat file to load the data (using a dataset from Refaat 2011). Use the 'IDVar' argument to indicate that 'CustID' contains ID information and should not be included as a predictor variable.

load CreditCardData 
sc = creditscorecard(data,'IDVar','CustID')

%
% Perform automatic binning.

sc = autobinning(sc);

%
% Show the bin information for 'CustAge'.

bininfo(sc,'CustAge')

%
% These are the first 10 age values in the original data, used to create the creditscorecard object.

data(1:10,'CustAge')

%
% Convert the test data to WOE values. To do this, set the bindata name-value pair argument for 'OutputType' to 'WOE'.

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

%
% These are the first 10 binned ages, in WOE format. The ages are mapped to the WOE values displayed internally by bininfo.

bdata(1:10,'CustAge')

%
% These are the first 10 binned ages, in WOE format. The ages are mapped to the WOE values displayed internally by bininfo.

bdata(1:10,'CustAge')

%
% The size of the original data and the size of bdata output are the same because bindata leaves unused variables (such as 'IDVar') unchanged and in place.

whos data bdata

%
% The response values are the same in the original data and in the binned data because, by default, bindata does not modify response values.

disp([data.status(1:10) bdata.status(1:10)])

%
% When fitting a logistic regression model with WOE data, set the 'OutputType' name-value pair argument to 'WOEModelInput'.

bdata = bindata(sc,'OutputType','WOEModelInput');

%
% The binned predictor data is the same as when the 'OutputType' name-value pair argument is set to 'WOE'.

bdata(1:10,'CustAge')

%
% However, the size of the original data and the size of bdata output are different. This is because bindata removes unused variables (such as 'IDVar').

whos data bdata

%
% The response values are also modified in this case and are mapped so that "Good" is 1 and "Bad" is 0.

disp([data.status(1:10) bdata.status(1:10)])

% Copyright 2015 The MathWorks, Inc.