print table in dyn_table

time-shift
Houtan Bastani 2017-07-17 17:40:12 +02:00
parent ea1757e4d1
commit 5eda6f3885
3 changed files with 126 additions and 60 deletions

92
matlab/dyn_table.m Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
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

View File

@ -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

View File

@ -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