dyn_ols: reference tag name in printing if available
parent
99344e0bcf
commit
edc38fea3f
|
@ -1,4 +1,4 @@
|
||||||
function [lhs, rhs, linenum, sample] = getEquationsByTags(jsonmodel, varargin)
|
function [lhs, rhs, linenum, sample, tagvalue] = getEquationsByTags(jsonmodel, varargin)
|
||||||
%function [lhs, rhs, linenum, sample] = getEquationByTag(jsonmodel, varargin)
|
%function [lhs, rhs, linenum, sample] = getEquationByTag(jsonmodel, varargin)
|
||||||
% Return the lhs, rhs of an equation and the line it was defined
|
% Return the lhs, rhs of an equation and the line it was defined
|
||||||
% on given its tag
|
% on given its tag
|
||||||
|
@ -13,6 +13,7 @@ function [lhs, rhs, linenum, sample] = getEquationsByTags(jsonmodel, varargin)
|
||||||
% rhs [cellstring array] right hand side of eq
|
% rhs [cellstring array] right hand side of eq
|
||||||
% linenum [cellstring array] eq line in .mod file
|
% linenum [cellstring array] eq line in .mod file
|
||||||
% sample [cell array of dates] sample range
|
% sample [cell array of dates] sample range
|
||||||
|
% tagvalue [cellstring array] tags associated with equations
|
||||||
%
|
%
|
||||||
% SPECIAL REQUIREMENTS
|
% SPECIAL REQUIREMENTS
|
||||||
% none
|
% none
|
||||||
|
@ -41,14 +42,24 @@ if nargin == 1
|
||||||
rhs = cell(1, length(jsonmodel));
|
rhs = cell(1, length(jsonmodel));
|
||||||
linenum = cell(1, length(jsonmodel));
|
linenum = cell(1, length(jsonmodel));
|
||||||
sample = cell(1, length(jsonmodel));
|
sample = cell(1, length(jsonmodel));
|
||||||
|
tagvalue = cell(1, length(jsonmodel));
|
||||||
for i=1:length(jsonmodel)
|
for i=1:length(jsonmodel)
|
||||||
lhs{i} = jsonmodel{i}.lhs;
|
lhs{i} = jsonmodel{i}.lhs;
|
||||||
rhs{i} = jsonmodel{i}.rhs;
|
rhs{i} = jsonmodel{i}.rhs;
|
||||||
linenum{i} = jsonmodel{i}.line;
|
linenum{i} = jsonmodel{i}.line;
|
||||||
if isfield(jsonmodel{i}, 'tags') && ...
|
if isfield(jsonmodel{i}, 'tags') && ...
|
||||||
isfield(jsonmodel{i}.tags, 'sample')
|
isfield(jsonmodel{i}.tags, 'name')
|
||||||
tmp = strsplit(jsonmodel{i}.tags.sample, ':');
|
tagvalue{i} = jsonmodel{i}.tags.('name');
|
||||||
sample{i} = dates(tmp{1}):dates(tmp{2});
|
else
|
||||||
|
tagvalue{i} = ['eq_line_no_' num2str(linenum{i})];
|
||||||
|
end
|
||||||
|
if isfield(jsonmodel{i}, 'tags')
|
||||||
|
if isfield(jsonmodel{i}.tags, 'sample')
|
||||||
|
tmp = strsplit(jsonmodel{i}.tags.sample, ':');
|
||||||
|
sample{i} = dates(tmp{1}):dates(tmp{2});
|
||||||
|
end
|
||||||
|
else
|
||||||
|
tagvalue{i} = ['eq_line_no_' num2str(linenum{i})];
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return
|
return
|
||||||
|
|
|
@ -49,16 +49,16 @@ jsonmodel = loadjson(jsonfile);
|
||||||
jsonmodel = jsonmodel.model;
|
jsonmodel = jsonmodel.model;
|
||||||
|
|
||||||
if nargin == 1
|
if nargin == 1
|
||||||
[lhs, rhs, lineno, sample] = getEquationsByTags(jsonmodel);
|
[lhs, rhs, lineno, sample, tags] = getEquationsByTags(jsonmodel);
|
||||||
fitted_names_dict = {};
|
fitted_names_dict = {};
|
||||||
else
|
else
|
||||||
assert(isempty(fitted_names_dict) || ...
|
assert(isempty(fitted_names_dict) || ...
|
||||||
(iscell(fitted_names_dict) && columns(fitted_names_dict) == 2), ...
|
(iscell(fitted_names_dict) && columns(fitted_names_dict) == 2), ...
|
||||||
'dyn_ols: the second argument must be an Nx2 cell array');
|
'dyn_ols: the second argument must be an Nx2 cell array');
|
||||||
if nargin == 2
|
if nargin == 2
|
||||||
[lhs, rhs, lineno, sample] = getEquationsByTags(jsonmodel);
|
[lhs, rhs, lineno, sample, tags] = getEquationsByTags(jsonmodel);
|
||||||
else
|
else
|
||||||
[lhs, rhs, lineno, sample] = getEquationsByTags(jsonmodel, 'name', eqtags);
|
[lhs, rhs, lineno, sample, tags] = getEquationsByTags(jsonmodel, 'name', eqtags);
|
||||||
end
|
end
|
||||||
if isempty(lhs)
|
if isempty(lhs)
|
||||||
disp('dyn_ols: Nothing to estimate')
|
disp('dyn_ols: Nothing to estimate')
|
||||||
|
@ -195,24 +195,15 @@ for i = 1:length(lhs)
|
||||||
|
|
||||||
%% Estimation
|
%% Estimation
|
||||||
% From LeSage, James P. "Applied Econometrics using MATLAB"
|
% From LeSage, James P. "Applied Econometrics using MATLAB"
|
||||||
if nargin == 3
|
|
||||||
if iscell(eqtags)
|
|
||||||
tagv = eqtags{i};
|
|
||||||
else
|
|
||||||
tagv = eqtags;
|
|
||||||
end
|
|
||||||
else
|
|
||||||
tagv = ['eq_line_no_' num2str(lineno{i})];
|
|
||||||
end
|
|
||||||
[nobs, nvars] = size(X);
|
[nobs, nvars] = size(X);
|
||||||
oo_.ols.(tagv).dof = nobs - nvars;
|
oo_.ols.(tags{i}).dof = nobs - nvars;
|
||||||
|
|
||||||
% Estimated Parameters
|
% Estimated Parameters
|
||||||
[q, r] = qr(X, 0);
|
[q, r] = qr(X, 0);
|
||||||
xpxi = (r'*r)\eye(nvars);
|
xpxi = (r'*r)\eye(nvars);
|
||||||
oo_.ols.(tagv).beta = r\(q'*Y.data);
|
oo_.ols.(tags{i}).beta = r\(q'*Y.data);
|
||||||
for j = 1:length(pnames)
|
for j = 1:length(pnames)
|
||||||
M_.params(strcmp(M_param_names_trim, pnames{j})) = oo_.ols.(tagv).beta(j);
|
M_.params(strcmp(M_param_names_trim, pnames{j})) = oo_.ols.(tags{i}).beta(j);
|
||||||
end
|
end
|
||||||
|
|
||||||
% Yhat
|
% Yhat
|
||||||
|
@ -224,59 +215,60 @@ for i = 1:length(lhs)
|
||||||
yhatname = fitted_names_dict{idx, 2};
|
yhatname = fitted_names_dict{idx, 2};
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
oo_.ols.(tagv).Yhat = dseries(X*oo_.ols.(tagv).beta, fp, yhatname);
|
oo_.ols.(tags{i}).Yhat = dseries(X*oo_.ols.(tags{i}).beta, fp, yhatname);
|
||||||
|
|
||||||
% Residuals
|
% Residuals
|
||||||
oo_.ols.(tagv).resid = Y - oo_.ols.(tagv).Yhat;
|
oo_.ols.(tags{i}).resid = Y - oo_.ols.(tags{i}).Yhat;
|
||||||
|
|
||||||
% Correct Yhat reported back to user for given
|
% Correct Yhat reported back to user for given
|
||||||
for j = 1:lhssub.vobs
|
for j = 1:lhssub.vobs
|
||||||
oo_.ols.(tagv).Yhat = oo_.ols.(tagv).Yhat + lhssub{j}(fp:lp);
|
oo_.ols.(tags{i}).Yhat = oo_.ols.(tags{i}).Yhat + lhssub{j}(fp:lp);
|
||||||
end
|
end
|
||||||
ds = [ds oo_.ols.(tagv).Yhat];
|
ds = [ds oo_.ols.(tags{i}).Yhat];
|
||||||
|
|
||||||
%% Calculate statistics
|
%% Calculate statistics
|
||||||
% Estimate for sigma^2
|
% Estimate for sigma^2
|
||||||
SS_res = oo_.ols.(tagv).resid.data'*oo_.ols.(tagv).resid.data;
|
SS_res = oo_.ols.(tags{i}).resid.data'*oo_.ols.(tags{i}).resid.data;
|
||||||
oo_.ols.(tagv).s2 = SS_res/oo_.ols.(tagv).dof;
|
oo_.ols.(tags{i}).s2 = SS_res/oo_.ols.(tags{i}).dof;
|
||||||
|
|
||||||
% R^2
|
% R^2
|
||||||
ym = Y.data - mean(Y);
|
ym = Y.data - mean(Y);
|
||||||
SS_tot = ym'*ym;
|
SS_tot = ym'*ym;
|
||||||
oo_.ols.(tagv).R2 = 1 - SS_res/SS_tot;
|
oo_.ols.(tags{i}).R2 = 1 - SS_res/SS_tot;
|
||||||
|
|
||||||
% Adjusted R^2
|
% Adjusted R^2
|
||||||
oo_.ols.(tagv).adjR2 = oo_.ols.(tagv).R2 - (1 - oo_.ols.(tagv).R2)*nvars/(oo_.ols.(tagv).dof-1);
|
oo_.ols.(tags{i}).adjR2 = oo_.ols.(tags{i}).R2 - (1 - oo_.ols.(tags{i}).R2)*nvars/(oo_.ols.(tags{i}).dof-1);
|
||||||
|
|
||||||
% Durbin-Watson
|
% Durbin-Watson
|
||||||
ediff = oo_.ols.(tagv).resid.data(2:nobs) - oo_.ols.(tagv).resid.data(1:nobs-1);
|
ediff = oo_.ols.(tags{i}).resid.data(2:nobs) - oo_.ols.(tags{i}).resid.data(1:nobs-1);
|
||||||
oo_.ols.(tagv).dw = (ediff'*ediff)/SS_res;
|
oo_.ols.(tags{i}).dw = (ediff'*ediff)/SS_res;
|
||||||
|
|
||||||
% Standard Error
|
% Standard Error
|
||||||
oo_.ols.(tagv).stderr = sqrt(oo_.ols.(tagv).s2*diag(xpxi));
|
oo_.ols.(tags{i}).stderr = sqrt(oo_.ols.(tags{i}).s2*diag(xpxi));
|
||||||
|
|
||||||
% T-Stat
|
% T-Stat
|
||||||
oo_.ols.(tagv).tstat = oo_.ols.(tagv).beta./oo_.ols.(tagv).stderr;
|
oo_.ols.(tags{i}).tstat = oo_.ols.(tags{i}).beta./oo_.ols.(tags{i}).stderr;
|
||||||
|
|
||||||
%% Print Output
|
%% Print Output
|
||||||
if ~options_.noprint
|
if ~options_.noprint
|
||||||
title = sprintf('OLS Estimation of equation `%s`', tagv);
|
|
||||||
if nargin == 3
|
if nargin == 3
|
||||||
title = [title sprintf(' [%s = %s]', 'name', tagv)];
|
title = ['OLS Estimation of equation ''' tags{i} ''' [name = ''' tags{i} ''']'];
|
||||||
|
else
|
||||||
|
title = ['OLS Estimation of equation ''' tags{i} ''''];
|
||||||
end
|
end
|
||||||
|
|
||||||
preamble = {sprintf('Dependent Variable: %s', lhs{i}), ...
|
preamble = {sprintf('Dependent Variable: %s', lhs{i}), ...
|
||||||
sprintf('No. Independent Variables: %d', nvars), ...
|
sprintf('No. Independent Variables: %d', nvars), ...
|
||||||
sprintf('Observations: %d from %s to %s\n', nobs, fp.char, lp.char)};
|
sprintf('Observations: %d from %s to %s\n', nobs, fp.char, lp.char)};
|
||||||
|
|
||||||
afterward = {sprintf('R^2: %f', oo_.ols.(tagv).R2), ...
|
afterward = {sprintf('R^2: %f', oo_.ols.(tags{i}).R2), ...
|
||||||
sprintf('R^2 Adjusted: %f', oo_.ols.(tagv).adjR2), ...
|
sprintf('R^2 Adjusted: %f', oo_.ols.(tags{i}).adjR2), ...
|
||||||
sprintf('s^2: %f', oo_.ols.(tagv).s2), ...
|
sprintf('s^2: %f', oo_.ols.(tags{i}).s2), ...
|
||||||
sprintf('Durbin-Watson: %f', oo_.ols.(tagv).dw)};
|
sprintf('Durbin-Watson: %f', oo_.ols.(tags{i}).dw)};
|
||||||
|
|
||||||
dyn_table(title, preamble, afterward, vnames, ...
|
dyn_table(title, preamble, afterward, vnames, ...
|
||||||
{'Coefficients','t-statistic','Std. Error'}, 4, ...
|
{'Coefficients','t-statistic','Std. Error'}, 4, ...
|
||||||
[oo_.ols.(tagv).beta oo_.ols.(tagv).tstat oo_.ols.(tagv).stderr]);
|
[oo_.ols.(tags{i}).beta oo_.ols.(tags{i}).tstat oo_.ols.(tags{i}).stderr]);
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue