-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmjdemetra.m
701 lines (544 loc) · 23.4 KB
/
mjdemetra.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
function [output,rslts]= mjdemetra(data,varargin)
%% Performs seasonal adjustment on the data according to the options chosen
%
% Inputs:
%
% [CLASS ][ARGUMENT NAME]
% ------------------------------------------------- ---------------------
% [TsData (JD+) ] data
% [char (Matlab) ] Method
% [char (Matlab) ] CalendarOption
% [char (Matlab) ] eMethod
% [logical (Matlab) ] plot
% [integer (Matlab) ] forecastHorizon
%
%
% https://blogs.mathworks.com/loren/2009/05/05/nice-way-to-set-function-defaults/
%
% Outputs:
%
% [CLASS ][ARGUMENT NAME]
% ------------------------------------------------- ---------------------
% [CompositeResults object of JDemetra+ ) ] rslts
% [structure (Matlab) ] output
%
% [double array (Matlab) with s. adjusted data ] output.sa
% [double array (Matlab) with non adjusted data ] output.nsa
% [double array (Matlab) with non adjusted forecasts ] output.Fnsa
% [timeseries (Matlab) with s. adjusted data + outliers info ] output.saTs
% [timeseries (Matlab) with non adjusted data ] output.nsaTs
% [timeseries (Matlab) with non adjusted forecasts ] output.FnsaTs
% [timeseries (Matlab) with 'linearised adjusted series' and
% a measure of the uncertainty around it; only for TramoSeats &
% excludes parameter uncertainty: data, upperbound, lowerbound] output.saLinTs/saLinTsF
%
%
%%
% Note 1: Make sure Matlab uses the appropiate Java version
% (type version -java to find out which version is used)
% If Matlab is using a version of Java that is not compatible with
% JDemetra+, then
% Note 2: Edit the classpath.txt file (type which classpath.txt to find its location)
% to make sure the paths containing your .jar libraries are listed. For example,
% my classpath.txt file includes the following path, containing the java compiled sofware:
% L:\DSXNPAPER\Project 2017\R model\models\JDinMATLAB\demetra-tstoolkit-2.2.2.jar
% If you don't want to modify the classpath.txt file, then just
% uncomment this line:
% javaclasspath('L:\DSXNPAPER\Project 2017\R model\models\JDinMATLAB\')
%
%% Param1 Param2 Param3 ...
% Examples: __________ __________________ ______________________
% / \ / \ / \
% [sa, rslts]= mjdemetra(data,'horizon',20,'Method','TramoSeats','CalendarOption','RSAfull')
% [sa, rslts]= mjdemetra(data2, 'Method','X13' );
% [sa, rslts]= mjdemetra(data,'horizon',20,'Method','TramoSeats','CalendarOption','RSA5')
% [sa, rslts]= mjdemetra(data,'horizon',20,'Method','X13' ,'CalendarOption','RSA5c')
% [sa, rslts]= mjdemetra(data)
% [sa, rslts]= mjdemetra(data, ,'CalendarOption','RSA0')
% [sa, rslts]= mjdemetra(data, , 'plot',false)
% [sa, rslts]= mjdemetra(data, , 'eMethod','KalmanSmoother')
%
% By default, the method 'TramoSeats' is used (with specification'RSAfull', unless otherwise stated)
% By default, a graph is plotted with
% 1) raw data,
% 2) adjusted data,
% 3) linearized data (only for TramoSeats; it only coincideds with the adjusted data when there are no outliers,
% 4) uncertainty around the linearized data (and forecasts)
% 5) outliers
%
%% OPTIONS check JDemetra+ documentation here:
% https://jdemetradocumentation.github.io/JDemetra-documentation/pages/reference-manual/sa-specifications.html
% *Calendar options for TramoSeats:
% RSAfull (default):
% RSA0 :
% RSA1 :
% RSA2 :
% RSA3 :
% RSA4 :
% RSA5 :
% *Calendar options for X13:
% RSAX11 (default) :
% RSA0 :
% RSA1 :
% RSA2C :
% RSA3 :
% RSA4C :
% RSA5C :
%
%
% Code written by David de Antonio Liedo
% Feel free to modify this function or create alternative outputs
% Don't hesitate to contact me if you need assistance
% Contact email: david.deantonioliedo@nbb.be
% Github : https://github.com/Liedo
p = inputParser;
% Data matrix is a required input
addRequired(p,'data',@ismatrix);
% Seasonal adjusment method is option (TrameSeats is the default)
defaultSaMethod = 'TramoSeats';
validSaMethods = {'TramoSeats','X13'};
checkSaMethod = @(x) any(validatestring(x,validSaMethods));
addParameter(p,'Method',defaultSaMethod,checkSaMethod);
% Seasonal adjusment options
defaultCalendarOption = 'RSAfull';
validCalendarOption = {'RSAfull','RSA0','RSA1','RSA2','RSA3','RSA4','RSA5','RSA4c','RSA5c','RSA2c','RSAX11'};
checkCalendarOption = @(x) any(validatestring(x,validCalendarOption));
addParameter(p,'CalendarOption',defaultCalendarOption,checkCalendarOption);
defaultHorizon=12 ;% 12 months, 12 years, 12 quarters...
addParameter(p,'horizon',defaultHorizon,@isnumeric);
defaultPlot=true ;% 12 months, 12 years, 12 quarters...
addParameter(p,'plot',defaultPlot,@islogical);
eMethod_default = 'Burman';
valid_eMethods = {'KalmanSmoother','Burman','McElroyMatrix'};
check_eMethods = @(x) any(validatestring(x,valid_eMethods));
addParameter(p,'eMethod',eMethod_default,check_eMethods);
p.KeepUnmatched = true;
%Pass the values of all of the function inputs.
parse(p,data,varargin{:});
horizon=p.Results.horizon; % parameter
saMethod=p.Results.Method; % optional
saOption=p.Results.CalendarOption; % optional
grafico = p.Results.plot ;
eMethod = p.Results.eMethod;
disp('...........................................................................')
disp(['Seasonal Adjustment Method: ',p.Results.Method])
disp(['Options for calendar effects and outlier-detection: ',p.Results.CalendarOption])
disp('...........................................................................')
if ~isempty(fieldnames(p.Unmatched))
disp('Extra inputs:')
disp(p.Unmatched)
end
if ~isempty(p.UsingDefaults)
disp('Using defaults: ')
disp(p.UsingDefaults)
end
%% Here's where the seasonal adjustment happens
if strcmp(saOption,'RSAX11') | strcmp(saOption,'RSA2c')| strcmp(saOption,'RSA4c')| strcmp(saOption,'RSA5c')
saOptionJD = ec.satoolkit.x13.X13Specification.fromString(saOption);
rslts = ec.satoolkit.algorithm.implementation. ...
X13ProcessingFactory.process(data, saOptionJD);
if strcmp(saMethod,'TramoSeasts')
disp('The SA options used correspond to the X13 method, so your call for the TramoSeats method is being ignored')
end
elseif strcmp(saMethod,'X13')
saOptionJD = ec.satoolkit.x13.X13Specification.fromString(saOption);
rslts = ec.satoolkit.algorithm.implementation. ...
X13ProcessingFactory.process(data, saOptionJD);
else %saMethod=='TramoSeats'
saOptionJD = ec.satoolkit.tramoseats.TramoSeatsSpecification.fromString(saOption);
saOptionJD.getSeatsSpecification().setPredictionLength(horizon);
%--------
KalmanSmoother=javaMethod('valueOf','ec.satoolkit.seats.SeatsSpecification$EstimationMethod', 'KalmanSmoother');
Burman=javaMethod('valueOf','ec.satoolkit.seats.SeatsSpecification$EstimationMethod', 'Burman');
McElroyMatrix=javaMethod('valueOf','ec.satoolkit.seats.SeatsSpecification$EstimationMethod', 'McElroyMatrix');
if strcmp(eMethod,'KalmanSmoother')
saOptionJD.getSeatsSpecification().setMethod(KalmanSmoother)
elseif strcmp(eMethod,'Burman')
saOptionJD.getSeatsSpecification().setMethod(Burman)
else % McElroyMatrix
saOptionJD.getSeatsSpecification().setMethod(McElroyMatrix)
end
%TramoSeatsSpec=ec.satoolkit.tramoseats.TramoSeatsSpecification();
%SeatSpec =TramoSeatsSpec.getSeatsSpecification()
% SeatSpec.setMethod(KalmanSmoother)
% SeatSpec.setMethod(Burman)
% SeatSpec.setMethod(McElroyMatrix)
%--------
rslts = ec.satoolkit.algorithm.implementation. ...
TramoSeatsProcessingFactory.process(data, saOptionJD);
end
%ec.satoolkit.seats.
% public static enum EstimationMethod {
% Burman, KalmanSmoother, McElroyMatrix
% }
%% Outliers Identification
% This java object has the following properties: description,
% coefficient, stdError, pValue
frecuencia = data.getFrequency().intValue() ;
multiplicative=rslts.getData('log',java.lang.Boolean(1).getClass());
temp = java.lang.Integer(1); % generate a Java integer (output of the function next line)
nout= rslts.getData('preprocessing.regression.nout', temp.getClass()); % number of outliers
temp=ec.tstoolkit.information.RegressionItem('descripcion',0,0,0); % generate Regression item (e.g. outlier)
for i=1:nout % extract all outliers
eval(['outlier{i}=char(rslts.getData(''preprocessing.regression.out(',num2str(i),')'', temp.getClass()))']);
description{i}=outlier{i}(1:2);
idx1=find(outlier{i}=='(');
idx2=find(outlier{i}==')');
dateString{i}= outlier{i}(idx1+1:idx2-1) ;
if frecuencia==4
yearO =outlier{i}(idx2-4:idx2-1) ;
quarterO=outlier{i}(idx1+1:idx2-5) ;
if strcmp(quarterO,'I-')
quarter='-Q1';
elseif strcmp(quarterO,'II-')
quarter='-Q2';
elseif strcmp(quarterO,'III-')
quarter='-Q3';
elseif strcmp(quarterO,'IV-')
quarter='-Q4' ;
end
dateOutlier(i)=datenum([year,quarter],'YYYY-QQ');
else
% dateOutlierStr{i}=outlier{i}(idx1+1:idx2-1);
dateOutlier(i)=datenum(dateString{i},'mm-YYYY');
end
end
%% Forecasts and estimation uncertainty
if strcmp(saMethod,'TramoSeats')
% Adjusted data
% You can find the whole list of outputs you can get with the getData function
% https://jdemetradocumentation.github.io/JDemetra-documentation/pages/theory/output.html
adjusted = rslts.getData('sa', data.getClass());
T=adjusted.getLength();
% Non SA F
nsaTsF = rslts.getData('y_f', data.getClass());
nsaF = nan(T+horizon,1); % initialization with NAN
for i=T:(T+horizon-1)
nsaF(i+1,1)= nsaTsF.get(i-T);
end
% SA data F
saTsF_lin = rslts.getData('decomposition.sa_lin_f', data.getClass());
saTsF = rslts.getData('sa_f', data.getClass()); % not corrected for outliers (otherwise the graph is confusing because we dont show the linearlized series)
saTsF_se = rslts.getData('decomposition.sa_lin_ef', data.getClass()); % the standard errors are only for the linearized series
y_ef = rslts.getData('y_ef', data.getClass()); % the standard errors are only for the linearized series
ReplaceBy_ef=false;
if isempty(saTsF_se)
display(['The model is multimplicative? ' ,string(multiplicative)])
display(['decomposition.sa_lin_ef is empty'])
ReplaceBy_ef=true;
end
% take y_ef instead
saF_lin = nan(T+horizon,1); % initialization with NAN
saF = nan(T+horizon,1); % initialization with NAN
saF_se = nan(T+horizon,1); % initialization with NAN
if multiplicative
if ReplaceBy_ef
for i=T:(T+horizon-1)
saF_lin(i+1,1)= saTsF_lin.get(i-T) ;
saF(i+1,1)= saTsF.get(i-T);
%temp = y_ef.get(i-T);
%saF_se(i+1,1)=ec.tstoolkit.modelling.arima.LogForecasts.expStdev(temp,saF(i+1,1));
saF_se(i+1,1)=y_ef.get(i-T);
end
else
for i=T:(T+horizon-1)
saF_lin(i+1,1)= saTsF_lin.get(i-T) ;
saF(i+1,1)= saTsF.get(i-T);
temp = saTsF_se.get(i-T);
%saF_se(i+1,1)=ec.tstoolkit.modelling.arima.LogForecasts.expStdev(temp,saF(i+1,1));
saF_se(i+1,1)=ec.tstoolkit.modelling.arima.LogForecasts.expStdev(temp,saF_lin(i+1,1));
end
end
else % non multiplicative
if ReplaceBy_ef
for i=T:(T+horizon-1)
saF(i+1,1)= saTsF.get(i-T);
% temp = saTsF_se.get(i-T);
% saF_se(i+1,1)=ec.tstoolkit.modelling.arima.LogForecasts.expStdev(temp,saF(i+1,1));
saF_se(i+1,1)=y_ef.get(i-T);
end
else
for i=T:(T+horizon-1)
saF(i+1,1)= saTsF.get(i-T);
% temp = saTsF_se.get(i-T);
% saF_se(i+1,1)=ec.tstoolkit.modelling.arima.LogForecasts.expStdev(temp,saF(i+1,1));
saF_se(i+1,1)=saTsF_se.get(i-T);
end
end
end
% SA data
saTs = rslts.getData('decomposition.sa_lin', data.getClass());
saTs_se = rslts.getData('decomposition.sa_lin_e', data.getClass());
ycalTs = rslts.getData('ycal', data.getClass());
replaceByZeros=false;
if isempty(saTs_se)
display(['The model is multimplicative? ' ,string(multiplicative)])
display(['decomposition.sa_lin_e is empty'])
replaceByZeros=true;
end
ycal = nan(T+horizon,1);
adj = nan(T+horizon,1); % initialization with NAN
adj2_F = nan(T+horizon,1); % initialization with NAN
sa = nan(T+horizon,1); % initialization with NAN
sa_se = nan(T+horizon,1); % initialization with NAN
% saF_se = nan(T+horizon,1); % initialization with NAN
if multiplicative
if replaceByZeros
for i=0:T-1
adj(i+1,1) =adjusted.get(i);
sa(i+1,1) =saTs.get(i);
%temp =saTs_se.get(i);
%sa_se(i+1,1)=ec.tstoolkit.modelling.arima.LogForecasts.expStdev(temp, sa(i+1,1));
sa_se(i+1,1)=0;
ycal(i+1,1) = (ycalTs.get(i));
end
else
for i=0:T-1
adj(i+1,1) =adjusted.get(i);
sa(i+1,1) =saTs.get(i);
temp =saTs_se.get(i);
sa_se(i+1,1)=ec.tstoolkit.modelling.arima.LogForecasts.expStdev(temp, sa(i+1,1));
ycal(i+1,1) = (ycalTs.get(i));
end
end
adj2 = exp(sa);
adj2U = adj2 + 2*sa_se;
adj2L = adj2 - 2*sa_se;
% adj2_F = exp(saF);% bug corrected
adj2_F = saF;
adj2U_F = adj2_F + 2*saF_se;
adj2L_F = adj2_F - 2*saF_se;
else % non multiplicative
if replaceByZeros
for i=0:T-1
adj(i+1,1) =adjusted.get(i);
sa(i+1,1) =saTs.get(i);
%temp =saTs_se.get(i);
%sa_se(i+1,1)=ec.tstoolkit.modelling.arima.LogForecasts.expStdev(temp, sa(i+1,1));
sa_se(i+1,1)=0;
ycal(i+1,1) =ycalTs.get(i);
end
else
for i=0:T-1
adj(i+1,1) =adjusted.get(i);
sa(i+1,1) =saTs.get(i);
%temp =saTs_se.get(i);
%sa_se(i+1,1)=ec.tstoolkit.modelling.arima.LogForecasts.expStdev(temp, sa(i+1,1));
sa_se(i+1,1)=saTs_se.get(i);
ycal(i+1,1) =ycalTs.get(i);
end
end
%adj2 = exp(sa);
adj2 = sa;
adj2U = adj2 + 2*sa_se;
adj2L = adj2 - 2*sa_se;
%adj2_F = exp(saF) ;
adj2_F = saF ;
adj2U_F = adj2_F + 2*saF_se;
adj2L_F = adj2_F - 2*saF_se;
end
% raw data
rawData =nan(T+horizon,1);
for i=0:T-1
rawData(i+1,1) =data.get(i);
end
else % x13
% Adjusted data
adjusted = rslts.getData('sa', data.getClass());
ycalTs = rslts.getData('ycal', data.getClass());
T=adjusted.getLength();
adj = nan(T,1); % initialization with NAN
ycal = nan(T,1); % initialization with NAN
for i=0:T-1
adj(i+1,1) =adjusted.get(i);
ycal(i+1,1) =ycalTs.get(i);
end
rawData =nan(T,1);
for i=0:T-1
rawData(i+1,1) =data.get(i);
end
end
%% Graphical analysis and introduciton of outliers as events in timeseries format
for j=1:nout
oType{j}=[' ',description{j}] ;
end
if strcmp(saMethod,'TramoSeats')
% Create timeseries
dominio=data.getDomain();
dominioF= saTsF.getDomain();
T=data.getLength();
H= saTsF.getLength();
for i=0:(T-1)
fechas{i+1}=char(dominio.get(i).toString());
end
for i=T:(T+H-1)
fechas{i+1}=char(dominioF.get(i-T).toString());
end
if frecuencia==4
for i=0:T+H-1
yearF=fechas{i+1}(end-3:end);
quarterF=fechas{i+1}(1:end-4);
if strcmp(quarterF,'I-')
quarter='-Q1';
elseif strcmp(quarterF,'II-')
quarter='-Q2';
elseif strcmp(quarterF,'III-')
quarter='-Q3';
elseif strcmp(quarterF,'IV-')
quarter='-Q4' ;
end
fechasQ{i+1}=[yearF,quarter];
end
timeFormat='YYYY-QQ';
tiempo00=datenum(fechasQ,timeFormat) ;
else
timeFormat='mm-yyyy';
tiempo00= datenum(fechas,timeFormat);
end
ts_adj=timeseries(adj,tiempo00);
for j=1:nout
index = find(dateOutlier(j)==ts_adj.Time);
new_event = tsdata.event(description{j},ts_adj.Time(index));
new_event.Units = 'seconds';
ts_adj = addevent(ts_adj,new_event);
indice(j)=index;
end
% saOutputSE=[ adj2U adj2L adj2U_F adj2L_F];
saOutputSE=[ adj+2*sa_se adj-2*sa_se adj2U_F adj2L_F];
saOutput=[rawData adj adj2 ycal];
ts=timeseries(saOutput,tiempo00);
ts.TimeInfo.Format=timeFormat;
tsCI=timeseries(saOutputSE,tiempo00);
tsCI.TimeInfo.Format=timeFormat;
%figure,plot(ts)
% event https://nl.mathworks.com/help/matlab/ref/timeseries.plot.html
%%
if grafico
% values to add shades in the graph
idx=find(isnan(tsCI.Data(:,1)));
idx1=idx(1)-1;
x=tiempo00(1:idx1)';
y1=tsCI.Data(1:idx1,1)';
y2=tsCI.Data(1:idx1,2)';
idx=find(isnan(tsCI.Data(:,3)));
idx1=idx(end)+1;
xF=tiempo00(idx1:end)';
y1F=tsCI.Data(idx1:end,3)';
y2F=tsCI.Data(idx1:end,4)';
% the graph
figure
plot(ts_adj,'.-b'), datetick('x', timeFormat), xlabel('time');
hold on
plot(tiempo00,ts.Data(:,1),'Color',[0.6 0.6 0.6]) % datenum(fechas,'mm-yyyy'),ts.Data(:,2),'b.-',...
hold on
plot(tiempo00,adj+rawData-ycal ,'m')
legend('Tramo-Seats Seasonal & Calendar adjusted','NSA data','Tramo-Seats SA')
hold on
fill([x fliplr(x)],[y1 fliplr(y2)],[0 1 1],'LineStyle','none')
hold on
fill([xF fliplr(xF)],[y1F fliplr(y2F)],[0 1 1],'LineStyle','none')
hold on
plot(tiempo00,mean(tsCI.Data(:,3:4),2),'b')
hold on
plot(ts_adj,'.-b'), datetick('x', timeFormat)%, xlabel('time'),title(['JD+ adjustment with TRAMO-SEATS-',p.Results.CalendarOption]);
hold on
plot(tiempo00,adj+rawData-ycal ,'m:','LineWidth',1)
hold on
plot(tiempo00,ts.Data(:,1),'Color',[0.6 0.6 0.6])
hold on
if nout>0
text( dateOutlier ,ts_adj.Data(indice,1),oType,'Color','yellow','BackgroundColor','red')
end
hold on
plot(ts_adj,'.-b')
hold on
plot(tiempo00,ts.Data(:,1),'Color',[0.6 0.6 0.6]) % datenum(fechas,'mm-yyyy'),ts.Data(:,2),'b.-',...
hold on
plot(tiempo00,nsaF,'--','Color',[0.6 0.6 0.6])
datetick('x', timeFormat);
title(['JD+ adjustment with TRAMO-SEATS-',p.Results.CalendarOption])
grid minor
% tiempo00,ts.Data(:,3),'c',... % linearised series (remove; confusing for the user)
end
% dateOutlier
% tiempo00(3),ts.Data(3,1),'r-s',...%,'MarkerSize',10,'MarkerEdgeColor','red','MarkerFaceColor',[1 .6 .6],...
% tiempo00(8),ts.Data(8,1),'r-s')%,'MarkerSize',10,'MarkerEdgeColor','red','MarkerFaceColor',[1 .6 .6]),...
else
%% Create timeseries
dominio=data.getDomain();
%->dominioF= saTsF.getDomain();
T=data.getLength();
%->H= saTsF.getLength();
for i=0:(T-1)
fechas{i+1}=char(dominio.get(i).toString());
end
%->for i=T:(T+H-1)
%-> fechas{i+1}=char(dominioF.get(i).toString())
%->end
% tiempo0=datestr(datenum(fechas,'mm-yyyy'))
if frecuencia==4
for i=0:T-1
yearF=fechas{i+1}(end-3:end)
quarterF=fechas{i+1}(1:end-4)
if strcmp(quarterF,'I-')
quarter='-Q1'
elseif strcmp(quarterF,'II-')
quarter='-Q2'
elseif strcmp(quarterF,'III-')
quarter='-Q3'
elseif strcmp(quarterF,'IV-')
quarter='-Q4'
end
fechasQ{i+1}=[yearF,quarter];
end
timeFormat='YYYY-QQ'
tiempo00=datenum(fechasQ,timeFormat);
else
timeFormat='mm-yyyy'
tiempo00= (datenum(fechas,timeFormat));
end
% SA data and events
ts_adj=timeseries(adj,tiempo00)
for j=1:nout
index = find(dateOutlier(j)==ts_adj.Time);
new_event = tsdata.event(description{j},ts_adj.Time(index));
new_event.Units = 'seconds';
ts_adj = addevent(ts_adj,new_event);
indice(j)=index;
end
saOutput=[rawData adj ycal];
ts=timeseries(saOutput,tiempo00);
ts.TimeInfo.Format=timeFormat;
%figure,plot(ts)
%%
if grafico
figure
plot(ts_adj,'.-b'), datetick('x', timeFormat), xlabel('time'),title(['JD+ adjustment with X13-',p.Results.CalendarOption]);
hold on
plot( tiempo00 , rawData ,'Color',[0.6 0.6 0.6]) , datetick('x', timeFormat)
hold on
plot( tiempo00 , adj+rawData-ycal ,'m:') , datetick('x', timeFormat)
if nout>0
hold on %
text( dateOutlier ,ts_adj.Data(indice,1),oType,'Color','yellow','BackgroundColor','red')
end
legend('X13 Seasonal & Calendar adjusted','NSA Data','X13 SA')
datetick('x', timeFormat);
end
end
%% outputs
output.sa=adj;
output.sa_noCal=adj+rawData-ycal;
output.nsa=rawData;
output.saTs=ts_adj;
output.nsaTs=timeseries(rawData,tiempo00);
output.nsaTs.TimeInfo.Format=timeFormat;
if strcmp(saMethod,'TramoSeats')
saLin_with_bounds=[adj2 adj2U adj2L ];
output.saLinTs=timeseries(saLin_with_bounds,tiempo00);
saLin_with_boundsF=[(adj2U_F+adj2L_F)/2 adj2U_F adj2L_F ];
output.saLinFTsF=timeseries(saLin_with_bounds,tiempo00);
output.FnsaTs=timeseries(nsaF,tiempo00);% forecasts of non-adjusted data
output.FnsaTs.TimeInfo.Format=timeFormat;
output.Fnsa=nsaF; % forecasts of non-adjusted data
end