-
Notifications
You must be signed in to change notification settings - Fork 4
/
load_spec.m
94 lines (83 loc) · 2.94 KB
/
load_spec.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
function Spec = load_spec(specfile)
%loadSpec Load model specification for a dynamic factor model (DFM)
%
% Description:
%
% Load model specification 'Spec' from a Microsoft Excel workbook file
% given by 'filename'.
%
% Input Arguments:
%
% filename -
%
% Output Arguments:
%
% spec - 1 x 1 structure with the following fields:
% . series_id
% . name
% . frequency
% . units
% . transformation
% . category
% . blocks
% . BlockNames
%
Spec = struct;
[~,~,raw] = xlsread(specfile,'','','basic');
header = strrep(raw(1,:),' ','');
raw = raw(2:end,:);
% Find and drop series from Spec that are not in Model
Model = cell2mat(raw(:,strcmpi('Model',header)));
raw(Model==0,:) = [];
% Parse fields given by column names in Excel worksheet
fldnms = {'SeriesID','SeriesName','Frequency','Units','Transformation','Category'};
for iField = 1:numel(fldnms)
fld = fldnms{iField};
jCol = find(strcmpi(fld,header),1);
if isempty(jCol)
error([fld ' column missing from model specification.']);
% I guess Title and Units are not necessary though
else
Spec.(fld) = raw(:,jCol);
end
end
% Parse blocks.
jColBlock = strncmpi('Block',header,length('Block'));
Blocks = cell2mat(raw(:,jColBlock));
Blocks(isnan(Blocks)) = 0;
if ~all(Blocks(:,1)==1)
error('All variables must load on global block.');
else
Spec.Blocks = Blocks;
end
% Sort all fields of 'Spec' in order of decreasing frequency
frequency = {'d','w','m','q','sa','a'};
permutation = [];
for iFreq = 1:numel(frequency)
permutation = [permutation; find(strcmp(frequency{iFreq},Spec.Frequency))]; %#ok<AGROW>
end
fldnms = fieldnames(Spec);
for iField = 1:numel(fldnms)
fld = fldnms{iField};
Spec.(fld) = Spec.(fld)(permutation,:);
end
Spec.BlockNames = regexprep(header(jColBlock),'Block\d-','');
Spec.UnitsTransformed = Spec.Transformation;
Spec.UnitsTransformed = strrep(Spec.UnitsTransformed,'lin','Levels (No Transformation)');
Spec.UnitsTransformed = strrep(Spec.UnitsTransformed,'chg','Change (Difference)');
Spec.UnitsTransformed = strrep(Spec.UnitsTransformed,'ch1','Year over Year Change (Difference)');
Spec.UnitsTransformed = strrep(Spec.UnitsTransformed,'pch','Percent Change');
Spec.UnitsTransformed = strrep(Spec.UnitsTransformed,'pc1','Year over Year Percent Change');
Spec.UnitsTransformed = strrep(Spec.UnitsTransformed,'pca','Percent Change (Annual Rate)');
Spec.UnitsTransformed = strrep(Spec.UnitsTransformed,'cch','Continuously Compounded Rate of Change');
Spec.UnitsTransformed = strrep(Spec.UnitsTransformed,'cca','Continuously Compounded Annual Rate of Change');
Spec.UnitsTransformed = strrep(Spec.UnitsTransformed,'log','Natural Log');
% Summarize model specification
fprintf('Table 1: Model specification \n');
try
tabular = table(Spec.SeriesID,Spec.SeriesName,Spec.Units,Spec.UnitsTransformed,...
'VariableNames',{'SeriesID','SeriesName','Units','Transformation'});
disp(tabular);
catch
end
end