function Example_RegDisp(stats,varargin)
%EXAMPLE_REGDISP Display multiple linear regression results
%
% Syntax:
%
%   Example_RegDisp(stats)
%   Example_RegDisp(stats,param,val,...)
%
% Description:
%
%   Displays multiple linear regression results from REGSTATS.
%
% Input Arguments:
%
%   stats - Output structure of regression statistics from REGSTATS. The
%           field stats.tstat is required, in order to display basic
%           regression results (coefficient estimates, standard errors, t
%           statistics, and t p-values). Other fields are optional, but
%           must include those with information flagged for display by the
%           'diagnostics' parameter.
%
% Optional Input Parameter Name/Value Pairs:
%
%   NAME            VALUE
%
%   'varNames'      String or cell vector of strings providing variable
%                   names for the display, of length equal to the number of
%                   coefficients estimated by REGSTATS. Names are truncated
%                   to the first five characters. By default, REGSTATS adds
%                   an initial intercept term to all models, so the default
%                   variable names are {'Const','var1','var2',...}.
%
%	'diagnostics'	String or cell vector of strings indicating which
%                   regression diagnostics to display. Options are limited
%                   to scalar statistics computed by REGSTATS (with the
%                   exception of the basic regression results, which are
%                   always displayed). Valid strings, and required fields
%                   for the stats structure, are:
%
%                   STRING  REQUIRED FIELD    DIAGNOSTIC
%
%                   'SSE'   stats.fstat       Error sum of squares
%                   'SSR'   stats.fstat       Regression sum of squares
%                   'MSE'   stats.mse         Mean-squared error
%                   'R2'    stats.rsquare     R squared
%                   'AR2'   stats.adjrsquare  Adjusted R squared
%                   'DFE'   stats.fstat       Error degrees of freedom
%                   'DFR'   stats.fstat       Regression degrees of freedom
%                   'F'     stats.fstat       F statistic
%                   'DW'    stats.dwstat      Durbin-Watson statistic
%
%                   An empty string '' displays no diagnostics (only the
%                   basic regression results are displayed). The string
%                   'all' displays all diagnostics. The default is ''.
%
% Example:
%
%   load acetylene
%   X = [x1,x2,x3];
%   stats = regstats(y,X);
%   Example_RegDisp(stats,'varNames',{'C','x1','x2','x3'},'diagnostics','F')
%
% See also REGSTATS.

% Copyright 2012 The MathWorks, Inc.

% Parse inputs and set defaults:

parseObj = inputParser;
parseObj.addRequired('stats',@statsCheck);
parseObj.addParamValue('varNames',{},@varNamesCheck);
parseObj.addParamValue('diagnostics','',@diagnosticsCheck);

parseObj.parse(stats,varargin{:});

stats = parseObj.Results.stats;
varNames = parseObj.Results.varNames;
diagnostics = parseObj.Results.diagnostics;

% Get number of coefficients in stats structure:

numCoeffs = length(stats.tstat.beta);

% Create variable names:

if isempty(varNames)

    varNames =  ['Const';strcat({'var'},num2str((1:numCoeffs-1)','%-u'))];

else

    if length(varNames) < numCoeffs

        error(message('econ:Example_RegDisp:VarNamesTooFew'))

    elseif length(varNames) > numCoeffs

        error(message('econ:Example_RegDisp:VarNamesTooMany'))

    else

        % Truncate variable names to first five characters:
        varNames = cellfun(@(s)[s,'     '],varNames,'UniformOutput',false);
        varNames = cellfun(@(s)s(1:5),varNames,'UniformOutput',false);

    end

end

% Print basic regression results:

fprintf('\n')
fprintf('REGRESSION RESULTS')
fprintf('\n\n')

fprintf('%-8s','      |')
fprintf('%-36s',['Coeff     ','SE        ','tStat     ','pVal  '])
fprintf('\n')

fprintf(repmat('=',1,44))
fprintf('\n')

for varNum = 1:numCoeffs
    
	fprintf('%-5s',varNames{varNum})
    fprintf('%-3s',' | ')
    fprintf('%-10.4f',stats.tstat.beta(varNum))
    fprintf('%-10.4f',stats.tstat.se(varNum))
    fprintf('%-10.4f',stats.tstat.t(varNum))
    fprintf('%-6.4f',stats.tstat.pval(varNum))
    fprintf('\n')
    
end

fprintf('\n')

% Print specified diagnostics:

if ~isempty(diagnostics)
    
    fprintf('Diagnostics:')
    fprintf('\n\n')
    
else
    
    return

end

if any(strcmpi('all',diagnostics))
    
    diagnostics = {'SSE','SSR','MSE','R2','AR2','DFE','DFR','F','DW'};
        
end
    
if any(strcmpi('SSE',diagnostics))

    fprintf('%-5s','SSE:')
    fprintf('%-5.4f',stats.fstat.sse)
    fprintf('\n')
    
end

if any(strcmpi('SSR',diagnostics))
    
    fprintf('%-5s','SSR:')
    fprintf('%-5.4f',stats.fstat.ssr)
    fprintf('\n')
    
end

if any(strcmpi('MSE',diagnostics))
    
    fprintf('%-5s','MSE:')
    fprintf('%-5.4f',stats.mse)
    fprintf('\n')
    
end

if any(strcmpi('R2',diagnostics))
    
    fprintf('%-5s','R2 :')
    fprintf('%-5.4f',stats.rsquare)
    fprintf('\n')

end

if any(strcmpi('AR2',diagnostics))
    
    fprintf('%-5s','AR2:')
    fprintf('%-5.4f',stats.adjrsquare)
    fprintf('\n')

end

if any(strcmpi('DFE',diagnostics))
    
    fprintf('%-5s','DFE:')
    fprintf('%-5u',stats.fstat.dfe)
    fprintf('\n')

end

if any(strcmpi('DFR',diagnostics))
    
    fprintf('%-5s','DFR:')
    fprintf('%-5u',stats.fstat.dfr)
    fprintf('\n')

end

if any(strcmpi('F',diagnostics))
    
    fprintf('%-5s','F  :')
    fprintf('%-5.4f\n',stats.fstat.f)
    fprintf('%-5s','')
    fprintf('%-5.4f',stats.fstat.pval)
    fprintf('%-9s','(p-value)')
    fprintf('\n')

end

if any(strcmpi('DW',diagnostics))
    
    fprintf('%-5s','DW :')
    fprintf('%-5.4f\n',stats.dwstat.dw)
    fprintf('%-5s','')
    fprintf('%-5.4f',stats.dwstat.pval)
    fprintf('%-9s','(p-value)')
    fprintf('\n')

end

fprintf('\n')

%-------------------------------------------------------------------------
% Check input stats
function OK = statsCheck(stats)
            
if ~isstruct(stats)

    error(message('econ:Example_RegDisp:StatsNonStructure'))
      
elseif ~isfield(stats,'tstat')

    error(message('econ:Example_RegDisp:StatsMissingTStat'))

else

    OK = true;

end

%-------------------------------------------------------------------------
% Check value of 'varNames' parameter
function OK = varNamesCheck(varNames)
    
if ~isvector(varNames)

    error(message('econ:Example_RegDisp:VarNamesNonVector'))

elseif isnumeric(varNames) || (iscell(varNames) && any(cellfun(@isnumeric,varNames)))

    error(message('econ:Example_RegDisp:VarNamesNumeric'))

else

    OK = true;

end

%-------------------------------------------------------------------------
% Check value of 'diagnostics' parameter
function OK = diagnosticsCheck(diagnostics)
    
if ~isempty(diagnostics) && ~isvector(diagnostics)

    error(message('econ:Example_RegDisp:DiagnosticsNonVector'))

elseif isnumeric(diagnostics) || (iscell(diagnostics) && any(cellfun(@isnumeric,diagnostics)))

    error(message('econ:Example_RegDisp:DiagnosticsNumeric'))

elseif ~all(ismember(upper(diagnostics),{'SSE','SSR','MSE','R2','AR2','DFE','DFR','F','DW','ALL',''}))

    error(message('econ:Example_RegDisp:DiagnosticsInvalid'))

else

    OK = true;

end