From 5eda6f3885c54338937b60851a01b49e49762e6c Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 17 Jul 2017 17:40:12 +0200 Subject: [PATCH] print table in dyn_table --- matlab/dyn_table.m | 92 ++++++++++++++++++++++++++++++++++++++++++++++ matlab/olseqs.m | 43 ++++++++-------------- matlab/sur.m | 51 ++++++++++--------------- 3 files changed, 126 insertions(+), 60 deletions(-) create mode 100644 matlab/dyn_table.m diff --git a/matlab/dyn_table.m b/matlab/dyn_table.m new file mode 100644 index 000000000..dab7c96c0 --- /dev/null +++ b/matlab/dyn_table.m @@ -0,0 +1,92 @@ +function dyn_table(title, preamble, afterward, rows, cols, indent, data) +%function dyn_table(title, rows, cols, indent, data) +% Print Table +% +% INPUTS +% title [char] +% preamble [cell string] +% afterward [cell string] +% rows [cell string] +% cols [cell string] +% indent [integer] +% data [matrix] +% +% OUTPUTS +% None +% +% SPECIAL REQUIREMENTS +% none + +% Copyright (C) 2017 Dynare Team +% +% This file is part of Dynare. +% +% Dynare is free software: you can redistribute it and/or modify +% it under the terms of the GNU General Public License as published by +% the Free Software Foundation, either version 3 of the License, or +% (at your option) any later version. +% +% Dynare is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU General Public License +% along with Dynare. If not, see . + +assert(ischar(title), 'title must be a char') +assert(iscellstr(preamble) && iscellstr(afterward) && iscellstr(rows) && iscellstr(cols), ... + 'preamble, afterward, rows, and cols must be cell arrays of strings') +assert(size(data, 1) == length(rows), 'must have the same number of rows') +assert(size(data, 2) == length(cols), 'must have the same number of columns') +assert(isint(indent), 'indent must be an integer') + +%% Print Output +rowstrlens = cellfun(@length, rows); +colstrlens = cellfun(@length, cols); +maxrowstrlen = max(rowstrlens); + +colbegin = repmat(' ', 1, 2*indent + maxrowstrlen); +colrow = sprintf('%s', colbegin); +colrow2 = colrow; +format = [' %-' num2str(maxrowstrlen) 's']; +for i = 1:length(cols) + if colstrlens(i) < 12 + colrow = [colrow repmat(' ', 1, floor((12-mod(colstrlens(i), 12))/2)) cols{i} repmat(' ', 1, ceil((12-mod(colstrlens(i), 12))/2))]; + colrow2 = [colrow2 repmat('_', 1, 12)]; + else + colrow = [colrow cols{i}]; + colrow2 = [colrow2 repmat('_', 1, colstrlens(i))]; + end + if i ~= length(cols) + colrow = [colrow repmat(' ', 1, indent)]; + colrow2 = [colrow2 repmat(' ', 1, indent)]; + end + format = [format repmat(' ', 1, indent) '%12.5f']; +end + +% Center title +if length(title) >= length(colrow) + fprintf('%s\n\n', title) +else + fprintf('%s%s\n\n', repmat(' ', 1, floor((length(colrow)+indent-length(title))/2)), title); +end +spaces = repmat(' ', 1, indent); +for i = 1:length(preamble) + fprintf('%s%s\n', spaces, preamble{i}); +end + +fprintf('%s\n', colrow); +fprintf('%s\n\n', colrow2); + +format = [format '\n']; +for i = 1:length(rows) + fprintf(format, rows{i}, data(i, :)); +end + +fprintf('\n'); +for i = 1:length(afterward) + fprintf('%s%s\n', spaces, afterward{i}); +end +fprintf('%s\n\n', repmat('_', 1, length(colbegin) + sum(colstrlens) + length(colstrlens)*indent)); +end \ No newline at end of file diff --git a/matlab/olseqs.m b/matlab/olseqs.m index cd8bc83cd..097e2d978 100644 --- a/matlab/olseqs.m +++ b/matlab/olseqs.m @@ -133,35 +133,22 @@ for i = 1:length(lhs) oo_.ols.(tagv).tstat = oo_.ols.(tagv).beta./oo_.ols.(tagv).stderr; %% Print Output - fprintf('OLS Estimation of equation on line %d', lineno{i}); + title = sprintf('OLS Estimation of equation on line %d', lineno{i}); if nargin == 3 - fprintf(' [%s = %s]', varargin{1}, tagv); + title = [title sprintf(' [%s = %s]', varargin{1}, tagv)]; end - fprintf('\n Dependent Variable: %s\n', lhs{i}); - fprintf(' No. Independent Variables: %d\n', nvars); - fprintf(' Observations: %d\n', nobs); - maxstrlen = 0; - for j=1:length(vwlags) - slen = length(vwlags{j}); - if slen > maxstrlen - maxstrlen = slen; - end - end - titlespacing = repmat(' ', 1, 4 + maxstrlen + 4) ; - fprintf('%sCoefficients t-statistic Std. Error\n', titlespacing); - fprintf('%s____________ ____________ ____________\n\n', titlespacing); - format = [' %-' num2str(maxstrlen) 's']; - for j = 1:length(vwlags) - fprintf(format, vwlags{j}); - fprintf('%12.5f %12.5f %12.5f\n', ... - oo_.ols.(tagv).beta(j), ... - oo_.ols.(tagv).tstat(j), ... - oo_.ols.(tagv).stderr(j)); - end - fprintf('\n R^2: %f\n', oo_.ols.(tagv).R2); - fprintf(' R^2 Adjusted: %f\n', oo_.ols.(tagv).adjR2); - fprintf(' s^2: %f\n', oo_.ols.(tagv).s2); - fprintf(' Durbin-Watson: %f\n', oo_.ols.(tagv).dw); - fprintf('%s\n\n', repmat('-', 1, 4 + maxstrlen + 4 + 44)); + + preamble = {sprintf('Dependent Variable: %s', lhs{i}), ... + sprintf('No. Independent Variables: %d', nvars), ... + sprintf('Observations: %d', nobs)}; + + afterward = {sprintf('R^2: %f', oo_.ols.(tagv).R2), ... + sprintf('R^2 Adjusted: %f', oo_.ols.(tagv).adjR2), ... + sprintf('s^2: %f', oo_.ols.(tagv).s2), ... + sprintf('Durbin-Watson: %f', oo_.ols.(tagv).dw)}; + + dyn_table(title, preamble, afterward, vwlags, ... + {'Coefficients','t-statistic','Std. Error'}, 4, ... + [oo_.ols.(tagv).beta oo_.ols.(tagv).tstat oo_.ols.(tagv).stderr]); end end diff --git a/matlab/sur.m b/matlab/sur.m index bb488f443..80fe4e3b5 100644 --- a/matlab/sur.m +++ b/matlab/sur.m @@ -167,43 +167,30 @@ oo_.sur.stderr = sqrt(oo_.sur.s2*diag(xpxi)); oo_.sur.tstat = oo_.sur.beta./oo_.sur.stderr; %% Print Output -fprintf('SUR Estimation'); +title = sprintf('SUR Estimation'); if nargin == 1 - fprintf(' of all equations') + title = [title sprintf(' of all equations')]; else - fprintf(' [%s = {', varargin{1}); + title = [title s(' [%s = {', varargin{1})]; for i = 1:length(varargin{2}) if i ~= 1 - fprintf(', '); + title = [title sprintf(', ')]; end - fprintf('%s', varargin{2}{i}); + title = [title sprintf('%s', varargin{2}{i})]; end - fprintf('}]'); + title = [title sprintf('}]')]; end -fprintf('\n Dependent Variable: %s\n', lhs{i}); -fprintf(' No. Independent Variables: %d\n', nvars); -fprintf(' Observations: %d\n', nobs); -maxstrlen = 0; -for j=1:length(vwlagsall) - slen = length(vwlagsall{j}); - if slen > maxstrlen - maxstrlen = slen; - end -end -titlespacing = repmat(' ', 1, 4 + maxstrlen + 4) ; -fprintf('%sCoefficients t-statistic Std. Error\n', titlespacing); -fprintf('%s____________ ____________ ____________\n\n', titlespacing); -format = [' %-' num2str(maxstrlen) 's']; -for j = 1:length(vwlagsall) - fprintf(format, vwlagsall{j}); - fprintf('%12.5f %12.5f %12.5f\n', ... - oo_.sur.beta(j), ... - oo_.sur.tstat(j), ... - oo_.sur.stderr(j)); -end -fprintf('\n R^2: %f\n', oo_.sur.R2); -fprintf(' R^2 Adjusted: %f\n', oo_.sur.adjR2); -fprintf(' s^2: %f\n', oo_.sur.s2); -fprintf(' Durbin-Watson: %f\n', oo_.sur.dw); -fprintf('%s\n\n', repmat('-', 1, 4 + maxstrlen + 4 + 44)); + +preamble = {sprintf('Dependent Variable: %s', lhs{i}), ... + sprintf('No. Independent Variables: %d', nvars), ... + sprintf('Observations: %d', nobs)}; + +afterward = {sprintf('R^2: %f', oo_.sur.R2), ... + sprintf('R^2 Adjusted: %f', oo_.sur.adjR2), ... + sprintf('s^2: %f', oo_.sur.s2), ... + sprintf('Durbin-Watson: %f', oo_.sur.dw)}; + +dyn_table(title, preamble, afterward, vwlagsall, ... + {'Coefficients','t-statistic','Std. Error'}, 4, ... + [oo_.sur.beta oo_.sur.tstat oo_.sur.stderr]); end