%% Scores for Missing or Out-of-Range Data
% This example shows how to use formatpoints to score missing or out-of-range data. When data is scored, some observations can be either missing (NaN, or undefined) or out of range. You will need to decide whether or not points are assigned to these cases. Use the name-value pair argument Missing to do so.
% Create a creditscorecard object using the CreditCardData.mat file to load the data (using a dataset from Refaat 2011). Use the 'IDVar' argument in creditscorecard 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 to bin for all predictors.

sc = autobinning(sc);

%
% Indicate that the minimum allowed value for 'CustAge' is zero. This makes any negative values for age invalid or out-of-range.

sc = modifybins(sc,'CustAge','MinValue',0);

%
% Fit a linear regression model using default parameters.

sc = fitmodel(sc);

%
% Suppose there are missing or out of range observations in the data that you want to score. Notice that by default, the points and score assigned to the missing value is NaN.

% Set up a data set with missing and out of range data for illustration purposes
newdata = data(1:5,:);
newdata.CustAge(1) = NaN; % missing
newdata.CustAge(2) = -100; % invalid
newdata.ResStatus(3) = '<undefined>'; % missing
newdata.ResStatus(4) = 'House'; % invalid
disp(newdata)
[Scores,Points] = score(sc,newdata);
disp(Scores)
disp(Points)

%
% Use the name-value pair argument Missing to replace NaN with points corresponding to a zero Weight-of-Evidence (WOE).

sc = formatpoints(sc,'Missing','ZeroWOE');
[Scores,Points] = score(sc,newdata);
disp(Scores)
disp(Points)

%
% Alternatively, use the name-value pair argument Missing to replace the missing value with the minimum points for the predictors that have the missing values.

sc = formatpoints(sc,'Missing','MinPoints');
[Scores,Points] = score(sc,newdata);
disp(Scores)
disp(Points)

%
% As a third alternative, use the name-value pair argument Missing to replace the missing value with the maximum points for the predictors that have the missing values.

sc = formatpoints(sc,'Missing','MaxPoints');
[Scores,Points] = score(sc,newdata);
disp(Scores)
disp(Points)

%
% Verify that the minimum and maximum points assigned to the missing data correspond to the minimum and maximum points for the corresponding predictors. The points for 'CustAge' are reported in the first seven rows of the points information table. For 'ResStatus' the points are in rows 8 through 10.

PointsInfo = displaypoints(sc);
PointsInfo(1:7,:)
min(PointsInfo.Points(1:7))
max(PointsInfo.Points(1:7))
PointsInfo(8:10,:)
min(PointsInfo.Points(8:10))
max(PointsInfo.Points(8:10))

% Copyright 2012 The MathWorks, Inc.