Commit f89b5cfe authored by Stéphane Adjemian's avatar Stéphane Adjemian
Browse files

Allow instantiation of dynSeries classw with a datafile (csv, mat or m). Fixed various bugs.

For instance, the follwing syntax is allowed:

data = dynSeries('mydata.csv');

assuming that the csv file is in the matlab's path.
parent 16cba161
......@@ -93,7 +93,8 @@ ts.vobs = 0;
ts.name = {};
ts.tex = {};
ts.freq = [];
ts.init = dynDate();%ts.Time = dynTime();
ts.init = dynDate();
ts.time = dynDates();
ts = class(ts,'dynSeries');
......@@ -110,6 +111,7 @@ switch nargin
ts.init = varargin{1};
ts.freq = varargin{1}.freq;
end
return
elseif ischar(varargin{1})
% Create a dynSeries object loading data in a file (*.csv, *.m, *.mat).
if check_file_extension(varargin{1},'m')
......@@ -121,11 +123,25 @@ switch nargin
else
error(['dynSeries:: I''m not able to load data from ' inputname(1) '!'])
end
ts.init = init;
ts.freq = freq;
ts.data = data;
ts.name = varlist;
ts.vobs = length(varlist);
ts.nobs = size(data,1);
end
case {2,4}
if nargin==2
c = [];
a = varargin{1};
b = varargin{2};
if nargin<4
d = [];
else
d = varargin{4};
end
if nargin<3
c = [];
else
c = varargin{3};
end
% Get data, number of observations and number of variables.
ts.data = a;
......@@ -137,6 +153,7 @@ switch nargin
quaterly = findstr('Q',b);
monthly = findstr('M',b);
weekly = findstr('W',b);
yearly = findstr('Y',b);
if ~isempty(quaterly)
ts.freq = 4;
end
......@@ -146,46 +163,58 @@ switch nargin
if ~isempty(weekly)
ts.freq = 52;
end
if isempty(quaterly) && isempty(monthly) && isempty(weekly)
error('dynSeries:: Using a string as a second input argument, I can only handle weekly (W), monthly (M) or quaterly (Q) data!');
if ~isempty(yearly)
ts.freq = 1;
end
elseif isa(b,'dynDate')% If b is not a string then yearly data are assumed.
if isempty(quaterly) && isempty(monthly) && isempty(weekly) && isempty(yearly)
error('dynSeries:: Using a string as a second input argument, I can only handle weekly (W), monthly (M), quaterly (Q) or yearly (Y) data!');
end
ts.init = dynDate(b);
elseif isa(b,'dynDate') && ~isempty(b)
ts.freq = b.freq;
ts.init = b;
elseif isnumeric(b) && isreal(b) && isint(b)
ts.freq = 1;
ts.init = dynDate(b);
else
error('dynSeries::dynSeries: Wrong calling sequence!');
end
ts.Time = ts.Time.setFreq(ts.freq);
ts.Time = ts.Time.setTime(dynDate(b):dynDate(b)+ts.nobs);
else% If b is empty.
ts.freq = 1;
ts.Time = ts.Time.setFreq(1);
ts.Time = ts.Time.setTime([transpose(1:ts.nobs) ones(ts.nobs,1)]);
ts.init = dynDate(1);
end
% Get the names of the variables.
if ~isempty(c)
if ts.vobs==size(c,1)
ts.name = c;
if ts.vobs==length(c)
for i=1:ts.vobs
ts.name = vertcat(ts.name, c(i) );
end
else
error('dynSeries:: The number of declared names does not match the number of variables!')
error('dynSeries::dynSeries: The number of declared names does not match the number of variables!')
end
else
for i=1:ts.vobs
ts.name = char(ts.name,'--NA--');
ts.name = vertcat(ts.name, {'--NA--'});
end
end
if ~isempty(d)
if ts.vobs==size(d,1)
ts.tex = d;
if ts.vobs==length(d)
for i=1:ts.vobs
ts.tex = vertcat(ts.tex, d(i));
end
else
error('dynSeries:: The number of declared tex names does not match the number of variables!')
error('dynSeries::dynSeries: The number of declared tex names does not match the number of variables!')
end
else
for i=1:ts.vobs
ts.tex = char(ts.tex,'--NA--');
ts.tex = vertcat(ts.tex, {'--NA--'});
end
end
otherwise
error('dynSeries:: Can''t instantiate the class, wrong calling sequence!')
error('dynSeries::dynSeries: Can''t instantiate the class, wrong calling sequence!')
end
ts.time = ts.init:(ts.init+ts.nobs);
%@test:1
%$ % Test if we can instantiate an empty dynSeries object.
......@@ -200,39 +229,153 @@ end
%@eof:1
%@test:2
%$ % Define a data set.
%$ A = transpose(1:10);
%$
%$ % Define initial date
%$ B1 = 1950;
%$ B2 = '1950Q2';
%$ B3 = '1950M10';
%$ B4 = '1950W50';
%$
%$ % Define expected results.
%$ e1.Time = transpose([1950 1951 1952 1953 1954 1955 1956 1957 1958 1959]);
%$ e1.freq = 1;
%$ e2.Time = char('1950Q2','1950Q3','1950Q4','1951Q1','1951Q2','1951Q3','1951Q4','1952Q1','1952Q2','1952Q3');
%$ e2.freq = 4;
%$ e3.Time = char('1950M10','1950M11','1950M12','1951M1','1951M2','1951M3','1951M4','1951M5','1951M6','1951M7');
%$ e3.freq = 12;
%$ e4.Time = char('1950W50','1950W51','1950W52','1951W1','1951W2','1951W3','1951W4','1951W5','1951W6','1951W7');
%$ e4.freq = 52;
%$
%$ % Call the tested routine.
%$ ts1 = dynSeries(A,B1);
%$ ts2 = dynSeries(A,B2);
%$ ts3 = dynSeries(A,B3);
%$ ts4 = dynSeries(A,B4);
%$
%$ % Check the results.
%$ t(1) = dyn_assert(getTime(ts1),e1.Time);
%$ t(2) = dyn_assert(getTime(ts2),e2.Time);
%$ t(3) = dyn_assert(getTime(ts3),e3.Time);
%$ t(4) = dyn_assert(getTime(ts4),e4.Time);
%$ t(5) = dyn_assert(ts1.freq,e1.freq);
%$ t(6) = dyn_assert(ts2.freq,e2.freq);
%$ t(7) = dyn_assert(ts3.freq,e3.freq);
%$ t(8) = dyn_assert(ts4.freq,e4.freq);
%$ t = zeros(4,1);
%$
%$ try
%$ aa = dynDate('1938M11');
%$ ts = dynSeries(aa);
%$ t(1) = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$ if length(t)>1
%$ t(2) = dyn_assert(ts.freq,12);
%$ t(3) = dyn_assert(ts.init.freq,12);
%$ t(4) = dyn_assert(ts.init.time,[1938, 11]);
%$ end
%$
%$ T = all(t);
%@eof:2
%@test:3
%$ t = zeros(6,1);
%$
%$ try
%$ ts = dynSeries('dynseries_test_data.m');
%$ t(1) = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$ if length(t)>1
%$ t(2) = dyn_assert(ts.freq,4);
%$ t(3) = dyn_assert(ts.init.freq,4);
%$ t(4) = dyn_assert(ts.init.time,[1994, 3]);
%$ t(5) = dyn_assert(ts.vobs,2);
%$ t(6) = dyn_assert(ts.nobs,100);
%$ end
%$
%$ T = all(t);
%@eof:3
%@test:4
%$ t = zeros(6,1);
%$
%$ try
%$ ts = dynSeries('dynseries_test_data.mat');
%$ t(1) = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$ if length(t)>1
%$ t(2) = dyn_assert(ts.freq,4);
%$ t(3) = dyn_assert(ts.init.freq,4);
%$ t(4) = dyn_assert(ts.init.time,[1994, 3]);
%$ t(5) = dyn_assert(ts.vobs,2);
%$ t(6) = dyn_assert(ts.nobs,100);
%$ end
%$
%$ T = all(t);
%@eof:1
\ No newline at end of file
%@eof:4
%@test:5
%$ t = zeros(7,1);
%$
%$ try
%$ ts = dynSeries('dynseries_test_data.csv');
%$ t(1) = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$ if length(t)>1
%$ t(2) = dyn_assert(ts.freq,4);
%$ t(3) = dyn_assert(ts.init.freq,4);
%$ t(4) = dyn_assert(ts.init.time,[1990, 1]);
%$ t(5) = dyn_assert(ts.vobs,4);
%$ t(6) = dyn_assert(ts.nobs,4);
%$ t(7) = dyn_assert(ts.name,{'azert';'yuiop';'qsdfg';'jklm'});
%$ end
%$
%$ T = all(t);
%@eof:5
%@test:6
%$ t = zeros(7,1);
%$
%$ try
%$ ts = dynSeries(transpose(1:5),[]);
%$ t(1) = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$ if length(t)>1
%$ t(2) = dyn_assert(ts.freq,1);
%$ t(3) = dyn_assert(ts.init.freq,1);
%$ t(4) = dyn_assert(ts.init.time,[1, 1]);
%$ t(5) = dyn_assert(ts.vobs,1);
%$ t(6) = dyn_assert(ts.nobs,5);
%$ t(7) = dyn_assert(ts.name,{'--NA--'});
%$ end
%$
%$ T = all(t);
%@eof:6
%@test:7
%$ t = zeros(7,1);
%$
%$ try
%$ ts = dynSeries(transpose(1:5),'1950Q1');
%$ t(1) = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$ if length(t)>1
%$ t(2) = dyn_assert(ts.freq,4);
%$ t(3) = dyn_assert(ts.init.freq,4);
%$ t(4) = dyn_assert(ts.init.time,[1950, 1]);
%$ t(5) = dyn_assert(ts.vobs,1);
%$ t(6) = dyn_assert(ts.nobs,5);
%$ t(7) = dyn_assert(ts.name,{'--NA--'});
%$ end
%$
%$ T = all(t);
%@eof:7
%@test:8
%$ t = zeros(7,1);
%$
%$ try
%$ ts = dynSeries([transpose(1:5), transpose(6:10)],'1950Q1',{'Output'; 'Consumption'}, {'Y_t'; 'C_t'});
%$ t(1) = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$ if length(t)>1
%$ t(2) = dyn_assert(ts.freq,4);
%$ t(3) = dyn_assert(ts.init.freq,4);
%$ t(4) = dyn_assert(ts.init.time,[1950, 1]);
%$ t(5) = dyn_assert(ts.vobs,2);
%$ t(6) = dyn_assert(ts.nobs,5);
%$ t(7) = dyn_assert(ts.name,{'Output'; 'Consumption'});
%$ end
%$
%$ T = all(t);
%@eof:8
......@@ -69,9 +69,9 @@ function us = subsref(ts, S)
% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr
if isequal(S.type,'.') & length(S)==1
if length(S)==1 && isequal(S.type,'.')
switch S.subs
case {'data','nobs','vobs','name','tex','freq','time','init','last','Time'} % Public members.
case {'data','nobs','vobs','name','tex','freq','time','init'} % Public members.
us = builtin('subsref', ts, S);
case {'log','exp'} % Give "dot access" to public methods.
us = feval(S.subs,ts);
......@@ -85,9 +85,7 @@ if isequal(S.type,'.') & length(S)==1
us.nobs = ts.nobs;
us.vobs = 1;
us.freq = ts.freq;
us.time = ts.time;
us.init = ts.init;
us.last = ts.last;
return
else
error('dynSeries::subsref: Unknown public method, public member or variable!')
......@@ -95,7 +93,8 @@ if isequal(S.type,'.') & length(S)==1
end
return
end
if isequal(S.type,'()') & length(S)==1 % Extract a sub-object by selecting a sub-sample.
if length(S)==1 && isequal(S.type,'()') % Extract a sub-object by selecting a sub-sample.
us = dynSeries();
if size(ts.data,2)>1
S.subs = [S.subs, ':'];
......@@ -105,15 +104,15 @@ if isequal(S.type,'()') & length(S)==1
us.vobs = ts.vobs;
us.freq = ts.freq;
us.time = builtin('subsref', ts.time, S);
us.init = us.time(1,:);
us.last = us.time(end,:);
us.init = ts.init+S.subs{1}(1);
us.name = ts.name;
us.tex = ts.tex;
return
end
if (length(S)==2) & (isequal(S(1).subs,'Time'))
if isequal(S(2).type,'.')
us = builtin('subsref', ts.Time, S(2));
if (length(S)==2) && (isequal(S(1).subs,'init'))
if isequal(S(2).type,'.') && ( isequal(S(2).subs,'freq') || isequal(S(2).subs,'time') )
us = builtin('subsref', ts.init, S(2));
else
error('dynSeries:subsref:: I don''t understand what you are trying to do!')
end
......@@ -125,7 +124,7 @@ end
%$ A = [transpose(1:10),2*transpose(1:10)];
%$
%$ % Define names
%$ A_name = char('A1','A2');
%$ A_name = {'A1';'A2'};
%$
%$ % Instantiate a time series object.
%$ ts1 = dynSeries(A,[],A_name,[]);
......@@ -137,20 +136,16 @@ end
%$ e.data = [transpose(2:9),2*transpose(2:9)];
%$ e.nobs = 8;
%$ e.vobs = 2;
%$ e.name = char('A1','A2');
%$ e.name = {'A1';'A2'};
%$ e.freq = 1;
%$ tmp = ts1.time; e.time = tmp(2:9,:);
%$ e.init = e.time(1,:);
%$ e.last = e.time(end,:);
%$ e.init = dynDate(2);
%$
%$ % Check the results.
%$ t(1) = dyn_assert(a.data,e.data);
%$ t(2) = dyn_assert(a.time,e.time);
%$ t(3) = dyn_assert(a.nobs,e.nobs);
%$ t(4) = dyn_assert(a.vobs,e.vobs);
%$ t(5) = dyn_assert(a.freq,e.freq);
%$ t(6) = dyn_assert(a.init,e.init);
%$ t(7) = dyn_assert(a.last,e.last);
%$ t(2) = dyn_assert(a.nobs,e.nobs);
%$ t(3) = dyn_assert(a.vobs,e.vobs);
%$ t(4) = dyn_assert(a.freq,e.freq);
%$ t(5) = dyn_assert(a.init,e.init);
%$ T = all(t);
%@eof:1
......@@ -160,7 +155,7 @@ end
%$ A = [transpose(1:10),2*transpose(1:10)];
%$
%$ % Define names
%$ A_name = char('A1','A2');
%$ A_name = {'A1';'A2'};
%$
%$ % Instantiate a time series object.
%$ ts1 = dynSeries(A,[],A_name,[]);
......@@ -172,20 +167,16 @@ end
%$ e.data = transpose(1:10);
%$ e.nobs = 10;
%$ e.vobs = 1;
%$ e.name = char('A1');
%$ e.name = {'A1'};
%$ e.freq = 1;
%$ e.time = [transpose(1:10),ones(10,1)];
%$ e.init = e.time(1,:);
%$ e.last = e.time(end,:);
%$ e.init = dynDate(1);
%$
%$ % Check the results.
%$ t(1) = dyn_assert(a.data,e.data);
%$ t(2) = dyn_assert(a.time,e.time);
%$ t(2) = dyn_assert(a.init,e.init);
%$ t(3) = dyn_assert(a.nobs,e.nobs);
%$ t(4) = dyn_assert(a.vobs,e.vobs);
%$ t(5) = dyn_assert(a.freq,e.freq);
%$ t(6) = dyn_assert(a.init,e.init);
%$ t(7) = dyn_assert(a.last,e.last);
%$ T = all(t);
%@eof:2
......@@ -195,7 +186,7 @@ end
%$ A = [transpose(1:10),2*transpose(1:10)];
%$
%$ % Define names
%$ A_name = char('A1','A2');
%$ A_name = {'A1';'A2'};
%$
%$ % Instantiate a time series object.
%$ ts1 = dynSeries(A,[],A_name,[]);
......@@ -207,19 +198,15 @@ end
%$ e.data = log(A);
%$ e.nobs = 10;
%$ e.vobs = 2;
%$ e.name = char('A1','A2');
%$ e.name = {'A1';'A2'};
%$ e.freq = 1;
%$ tmp = ts1.time; e.time = tmp(1:10,:);
%$ e.init = e.time(1,:);
%$ e.last = e.time(end,:);
%$ e.init = dynDate(1);
%$
%$ % Check the results.
%$ t(1) = dyn_assert(a.data,e.data);
%$ t(2) = dyn_assert(a.time,e.time);
%$ t(3) = dyn_assert(a.nobs,e.nobs);
%$ t(4) = dyn_assert(a.vobs,e.vobs);
%$ t(5) = dyn_assert(a.freq,e.freq);
%$ t(6) = dyn_assert(a.init,e.init);
%$ t(7) = dyn_assert(a.last,e.last);
%$ t(2) = dyn_assert(a.nobs,e.nobs);
%$ t(3) = dyn_assert(a.vobs,e.vobs);
%$ t(4) = dyn_assert(a.freq,e.freq);
%$ t(5) = dyn_assert(a.init,e.init);
%$ T = all(t);
%@eof:3
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment