%% Perform Automatic Binning for a Categorical Predictor Using Name-Value Pair Arguments
% This example shows how to modify the data (for this example only) to illustrate binning categorical predictors using the Monotone algorithm.
% Create a creditscorecard object using the CreditCardData.mat file to load the data (using a dataset from Refaat 2011).

load CreditCardData

%
% Add two new categories and updating the response variable.

newdata = data;
rng('default'); %for reproducibility
Predictor = 'ResStatus';
Status    = newdata.status;
NumObs    = length(newdata.(Predictor));
Ind1 = randi(NumObs,100,1);
Ind2 = randi(NumObs,100,1);
newdata.(Predictor)(Ind1) = 'Subtenant';
newdata.(Predictor)(Ind2) = 'CoOwner';
Status(Ind1) = randi(2,100,1)-1;
Status(Ind2) = randi(2,100,1)-1;

newdata.status = Status;

%
% Update the creditscorecard object using the newdata and plot the bins for a later comparison.

scnew = creditscorecard(newdata,'IDVar','CustID');
[bi,cg] = bininfo(scnew,Predictor)
plotbins(scnew,Predictor)

%
% Perform automatic binning for the categorical Predictor using the default Monotone algorithm with the AlgorithmOptions name-value pair arguments for 'SortCategories' and 'Trend'.

AlgoOptions = {'SortCategories','Goods','Trend','Increasing'};

scnew = autobinning(scnew,Predictor,'Algorithm','Monotone',...
    'AlgorithmOptions',AlgoOptions);

%
% Use bininfo to display the bin information. The second output parameter 'cg' captures the bin membership, which is the bin number that each group belongs to.

[bi,cg] = bininfo(scnew,Predictor)

%
% Plot bins and compare with the histogram plotted pre-binning.

plotbins(scnew,Predictor)

% Copyright 2015 The MathWorks, Inc.