%% Modify a GLM Model Fitted with fitmodel
% This example shows how to use setmodel to make modifications to a logistic regression model initially fitted using the fitmodel function, and then set the new logistic regression model predictors and coefficients back into the creditscorecard object.
%
% Create a creditscorecard object using the CreditCardData.mat file to load the data (using a dataset from Refaat 2011).

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

%
% Perform automatic binning.

sc = autobinning(sc);

%
% The standard workflow is to use the fitmodel function to fit a logistic regression model using a stepwise method. However, fitmodel only supports limited options regarding the stepwise procedure. You can use the optional mdl output argument from fitmodel to get a copy of the fitted GeneralizedLinearModel object, to later modify.

[sc,mdl] = fitmodel(sc);

%
% Suppose you want to include, or "force," the predictor  'UtilRate' in the logistic regression model, even though the stepwise method did not include it in the fitted model. You can add 'UtilRate' to the logistic regression model using the GeneralizedLinearModel object mdl directly.

mdl = mdl.addTerms('UtilRate')

%
% Use setmodel to update the model predictors and model coefficients in the creditscorecard object. The ModelPredictors input argument does not explicitly include a string for the intercept. However, the ModelCoefficients input argument does have the intercept information as its first element.

ModelPredictors = mdl.PredictorNames
ModelCoefficients = mdl.Coefficients.Estimate

sc = setmodel(sc,ModelPredictors,ModelCoefficients);

%
% Verify that 'UtilRate' is part of the scorecard predictors by displaying the scorecard points.

pi = displaypoints(sc)

% Copyright 2015 The MathWorks, Inc.