'This program spins through the TS trend series contained in forecasting_series::TS_Seriesin:
'	1. The number of series and the specific series to be treated as constant series (random walks around a mean) need to be adjusted
'	 according to the series being forecast and the outcomes of the Tramoseats long term trend decomposition.
'	2. Change the autoarma specification, for both the with and without constant versions for specific results:
'		a. Most general: ser{!i}.autoarma(tform=auto, diff=2, maxar=4, maxma=4, maxsar=1, maxsma=1, forclen=20, etable) ser{!i}_F +/- c
'		b. AR only: ser{!i}.autoarma(tform=auto, diff=2, maxar=4, maxma=0, maxsar=1, maxsma=0, forclen=20, etable) ser{!i}_F +/- c
'		c. MA only: ser{!i}.autoarma(tform=auto, diff=2, maxar=0, maxma=4, maxsar=0, maxsma=1, forclen=20, etable) ser{!i}_F +/- c
'	3. Forecast length ( forclen=..) in the spec may need to be adjusted according to the circumstances.

'make forecasts for each each constant series, most are 'flat zero' trend equal 0.1 throughout
subroutine flatZERO
	series ser{!i}_F
' to the last observation if producing in sample forecasts, to the end point of the forecast period if out of sample forecasts
	For !j=1 to 110
	ser{!i}_F(!j,1)=@mean(ser{!i})
	ARIMA.add ser{!i}_F
	ARIMA_models({!i}+1,1) = {!i}
	ARIMA_models({!i}+1,2) = "Flat"
	next
endsub 

mode quiet
logmode logmsg
cd "\\Emp\50030303\OSAS\12 Projections\2017 Projections - five years to November 2021\02 ANZSIC\02 Creating Forecasts\Eviews"

wfopen forecasting_series
wfselect forecasting_series::TS_Seriesin

'for in sample forecasts use
'pagestruct(end=11/01/2016)
'for out of sample forecasts use
pagestruct(end=11/01/2021)

'set the number of series to be modelled
!lastseries = 417

group ARIMA
'group ARIMA for real forecasts, ARIMA1 for in sample forecasts

'set up table of model components
table (@str({!lastseries })+1,6) ARIMA_models
	ARIMA_models(1,1) = "Series"
	ARIMA_models(1,2) = "Model"
	ARIMA_models(1,3) = "Dependent Variable"
	ARIMA_models(1,4) = "Indep Vars"
	ARIMA_models(1,5) = "Convergence"
	ARIMA_models(1,6) = "Warning"

for !i= 1 to @str({!lastseries})
	wfselect forecasting_series::TS_Seriesin
	statusline Up to: ser{!i}

'series set equal to 0.1 - possibly include: or !i=415 or !i=416
if !i = 11 or !i= 22 or !i= 68 or !i= 76 or !i=126 or !i=189 or !i=221 or !i=222 or !i=239 or !i=245  or !i=257  or !i=259  or !i=271 or !i=275 or !i=278 or !i=279 or !i=300 or !i=302 or !i=309 or !i=310 or !i=328 or !i=333 or !i=336 or !i=337 or !i=349 or !i=356 or !i=386  or !i=402  or !i=407  then
	call flatZERO

'series set equal to their means over a specified range - possibly include: if !i = 3 or !i= 13 or !i= 65 or !i= 90 or !i= 92 or !i=94 or !i=113 or !i=129 or !i=133 or !i=137 or !i=166  or !i=179  or !i=185  or !i=186 or !i=195 or !i=199 or !i=208 or !i=210 or !i=225 or !i=235 or !i=251 or !i=266 or !i=267 or !i=282 or !i=288 or !i=293 or !i=296 or !i=297 or !i=306 or !i=313  or !i=314  or !i=324 or !i=325 or !i=340 or !i=352 or !i=353 or !i=362 or !i=382 or !i=383  or !i=392  or !i=398 or !i=399 or !i=415 or !i=416 
		else
if !i= 39 or !i= 57 or !i=217 then
	call flatZERO

		else

'the following code lets autoarma choose the best arima model and determines whether or not there should be a constant then saves required outputs
	smpl @FIRST @LAST-20
!errcount = @errorcount
freeze(Output) ser{!i}.autoarma(tform=auto, diff=2, maxar=4, maxma=4, maxsar=1, maxsma=1, forclen=20, etable) ser{!i}_F c
'alternative specs:
'		b. AR only: 
'freeze(Output) ser{!i}.autoarma(tform=auto, diff=2, maxar=4, maxma=0, maxsar=1, maxsma=0, forclen=20, etable) ser{!i}_F c
'		c. MA only: 
'freeze(Output) ser{!i}.autoarma(tform=auto, diff=2, maxar=0, maxma=4, maxsar=0, maxsma=1, forclen=20, etable) ser{!i}_F c
	Output.extract(tab{!i}) eqoutput
	scalar constant_pvalue = @val(tab{!i}(11,5))	
if constant_pvalue > 0.05 then
	delete  Output
	delete tab{!i}
freeze(Output) ser{!i}.autoarma(tform=auto, diff=2, maxar=4, maxma=4, maxsar=1, maxsma=1, forclen=20, etable) ser{!i}_F
'		b. AR only: 
'freeze(Output) ser{!i}.autoarma(tform=auto, diff=2, maxar=4, maxma=0, maxsar=1, maxsma=0, forclen=20, etable) ser{!i}_F
'		c. MA only: 
'freeze(Output) ser{!i}.autoarma(tform=auto, diff=2, maxar=0, maxma=4, maxsar=0, maxsma=1, forclen=20, etable) ser{!i}_F
	Output.extract(tab{!i}) eqoutput
	endif
!errcount2 = @errorcount
if !errcount2>!errcount then
  	logmsg !i
	copy errormsg forecasting_series::Errors\errmsg{!i}
endif
	output.extract(sum{!i}) summary
	copy Output forecasting_series::Models\arima{!i}
	delete output

'wfselect forecasting_series::TS_Seriesin

'save forecast series to group for exporting to excel
	smpl @FIRST @LAST
	ARIMA.add ser{!i}_F

'populate model table
	ARIMA_models({!i}+1,1) = {!i}
	ARIMA_models({!i}+1,2) = @mid(sum{!i}(10,1),22)
	ARIMA_models({!i}+1,3) = @mid(tab{!i}(1,1),21)
	ARIMA_models({!i}+1,5) = tab{!i}(6,1)

scalar start=99
string variable = tab{!i}(7,1)
if @trim(variable)="Variable" then
	scalar start=9
else  
string variable = tab{!i}(8,1)
if @trim(variable)="Variable" then
	scalar start=10
else  
string variable = tab{!i}(9,1)
if @trim(variable)="Variable" then
	scalar start=11
else  
string variable = tab{!i}(10,1)
if @trim(variable)="Variable" then
	scalar start=12
	ARIMA_models({!i}+1,6) = tab{!i}(8,1)
endif
endif
endif
endif
if start=99 then
	ARIMA_models({!i}+1,4) = "NA"
else
	string ind_vars = tab{!i}({start},1)
	for !j=1 to 20
		string a = tab{!i}({start}+!j)
		if a = "SIGMASQ" or a = "R-squared" then
			!j =21
		else
		ind_vars = ind_vars + " " + tab{!i}({start}+!j)
		endif
	next
	ARIMA_models({!i}+1,4) =ind_vars
	ARIMA_models({!i}+1,7) = start
endif	

close SUM{!i}
close TAB{!i}
endif
endif
next

pagesave(type=excelxml, mode=update) EviewsARIMAForecastsv1.xlsm byrow range="ARIMA!A1" @keep ARIMA
'pagesave(type=excelxml, mode=update) EviewsARIMAForecastsv1.xlsm byrow range="AR_only!A1" @keep ARIMA
'pagesave(type=excelxml, mode=update) EviewsARIMAForecastsv1.xlsm byrow range="MA_only!A1" @keep ARIMA


'output models, model parameters, etc to csv which needs to be converted to an excel file - can't pagesave a table
	ARIMA_models.save(t=csv) ARIMA_models


