for loop within for loop

For questions regarding programming in the EViews programming language.

Moderators: EViews Gareth, EViews Moderator, EViews Jason, EViews Matt

mjfl
Posts: 49
Joined: Mon Jul 18, 2011 8:18 am

for loop within for loop

Postby mjfl » Thu Aug 04, 2011 7:48 am

hi all,

Code: Select all

series k=djia for !horizon= 58 to 82 smpl 1989q1 1989q1+!horizon k.hpf(lambda=1600) k_hp{!horizon} series k_c{!horizon} = k - k_hp{!horizon} next smpl @all series y = dlhs series x = dpiet equation eq !sc = 9999999 !maxlags = 4 !bestlag = 0 !best2lag = 0 for !horizon= 59 to 82 smpl 1989q1 1989q1+!horizon equation eq{!horizon} for !i=0 to !maxlags for !j=0 to !maxlags eq{!horizon}.ls(cov=hac) y c x( to -!i) k_c{!horizon}( to -!j) if eq{!horizon}.@sc < !sc then !best2lag =!j !bestlag = !i !sc = eq{!horizon}.@sc endif next next eq{!horizon}.ls(cov=hac) y c x( to -!bestlag) k_c{!horizon}( to -!best2lag) smpl 1989q1+!horizon+1 1989q1+!horizon+1 eq{!horizon}.fit(f=na) y_hat{!horizon} y_se{!horizon} series master_series = y_hat{!horizon} series ee= (dlhs - y_hat{!horizon})^2 next smpl @all scalar m = sqr((1/24)*@sum(ee)) show m
i wish to hp filter a sequence recursively and forecast the best in the next. but i keep comming up with error. i tried to do seperately but still to no avail. note i tried to use !horizon for many things here. i wish to first from 1989q1 to 2003q3 get a hp cycle called kc_59. (in the seperated version, this is all correct for kc_59, 60 etc) then i would like to put this series to use and forecast y_hat59 which is for 2004q1. in fact if i dont forecast and seperate and just do the equation prog w/o forecast, all estimation is only 1989q1 to 2003q3 i.e.

Code: Select all

smpl @all series y = dlhs series x = dpiet equation eq !sc = 9999999 !maxlags = 4 !bestlag = 0 !best2lag = 0 for !horizon= 59 to 82 smpl 1989q1 1989q1+!horizon equation eq{!horizon} for !i=0 to !maxlags for !j=0 to !maxlags eq{!horizon}.ls(cov=hac) y c x( to -!i) k_c{!horizon}( to -!j) if eq{!horizon}.@sc < !sc then !best2lag =!j !bestlag = !i !sc = eq{!horizon}.@sc endif next next eq{!horizon}.ls(cov=hac) y c x( to -!bestlag) k_c{!horizo}( to -!best2lag)


thats where it cant 'UNABLE TO FIT DUE TO MISSING DATA EQN59............

i tried to seperate the 2 for loop into one running called eq{!horizon} and then k_c{!hori} but it does not work. can i seek help?

mjfl
Posts: 49
Joined: Mon Jul 18, 2011 8:18 am

Re: for loop within for loop

Postby mjfl » Thu Aug 04, 2011 8:02 am

yet this works well.

Code: Select all

for !horizon= 59 to 82 smpl 1989q1 1989q1+!horizon g3.makepcomp comp{!horizon} next smpl @all 'create a variable series y = dlhs series x = dpiet series z= comp{!horizon} 'create empty equation to be used inside the loop equation eq 'variable to store the minimum AIC. Initialise it to a large number !sc = 9999999 'variable saying how many lags to go up to !maxlags = 4 'Variable to store the "best" number of lags !bestlag = 0 !best2lag = 0 for !horizon= 59 to 82 smpl 1989q1 1989q1+!horizon equation eq{!horizon} for !i=0 to !maxlags for !j=0 to !maxlags eq{!horizon}.ls(cov=hac) y c x( to -!i) z( to -!j) if eq{!horizon}.@sc < !sc then !best2lag =!j !bestlag = !i !sc = eq{!horizon}.@sc endif next
same logic as making a running pca or a running hp_cycle right? pls help.

mjfl
Posts: 49
Joined: Mon Jul 18, 2011 8:18 am

Re: for loop within for loop

Postby mjfl » Thu Aug 04, 2011 8:21 am

and apology, i always have this note that it cant make series 83 even though i asked to only 82 as the 2nd post above so i make an 83(no harm) and then go back to 82 and then it works.
but its weird that same y same x and if i use the prog where z=comp54 and i do myself using estimation the results differ slightly. this happens EVEN when the sample is NOT ADJUSTED.


[img]
Dependent Variable: Y
Method: Least Squares
Date: 08/04/11 Time: 23:14
Sample: 1992Q1 2005Q3
Included observations: 55
HAC standard errors & covariance (Bartlett kernel, Newey-West fixed
bandwidth = 4.0000)

Variable Coefficient Std. Error t-Statistic Prob.

C 0.040337 0.373105 0.108113 0.9143
X -0.549216 0.097926 -5.608467 0.0000
X(-1) -0.453251 0.110992 -4.083630 0.0002
X(-2) -0.344894 0.112005 -3.079260 0.0034
Z 0.184841 0.331847 0.557006 0.5800

R-squared 0.357723 Mean dependent var -0.062957
Adjusted R-squared 0.306341 S.D. dependent var 1.619877
S.E. of regression 1.349133 Akaike info criterion 3.523310
Sum squared resid 91.00803 Schwarz criterion 3.705795
Log likelihood -91.89102 Hannan-Quinn criter. 3.593878
F-statistic 6.962019 Durbin-Watson stat 0.692928
Prob(F-statistic) 0.000155

and


Dependent Variable: DLHS
Method: Least Squares
Date: 08/04/11 Time: 23:19
Sample: 1992Q1 2005Q3
Included observations: 55
HAC standard errors & covariance (Bartlett kernel, Newey-West fixed
bandwidth = 4.0000)

Variable Coefficient Std. Error t-Statistic Prob.

DPIET -0.548639 0.097864 -5.606116 0.0000
DPIET(-1) -0.452689 0.111451 -4.061766 0.0002
DPIET(-2) -0.344709 0.112288 -3.069865 0.0035
COMP54 0.072149 0.141307 0.510580 0.6119
C -0.092494 0.282097 -0.327879 0.7444

R-squared 0.356807 Mean dependent var -0.062957
Adjusted R-squared 0.305351 S.D. dependent var 1.619877
S.E. of regression 1.350096 Akaike info criterion 3.524736
Sum squared resid 91.13794 Schwarz criterion 3.707221
Log likelihood -91.93025 Hannan-Quinn criter. 3.595305
F-statistic 6.934278 Durbin-Watson stat 0.693059
Prob(F-statistic) 0.000160

[/img]

im realli trying hard to learn.

mjfl
Posts: 49
Joined: Mon Jul 18, 2011 8:18 am

another loop question

Postby mjfl » Thu Aug 04, 2011 8:53 am

Code: Select all

'create a variable series y = dlhs series x = dpiet series z= d(unemp_sa) 'create empty equation to be used inside the loop equation eq 'variable to store the minimum AIC. Initialise it to a large number !sc = 9999999 'variable saying how many lags to go up to !maxlags = 4 'Variable to store the "best" number of lags !bestlag = 0 !best2lag = 0 for !horizon= 59 to 82 smpl 1989q1 1989q1+!horizon equation eq{!horizon} for !i=0 to !maxlags for !j=0 to !maxlags eq{!horizon}.ls(cov=hac) y c x( to -!i) z( to -!j) if eq{!horizon}.@sc < !sc then !best2lag =!j !bestlag = !i !sc = eq{!horizon}.@sc endif next next eq{!horizon}.ls(cov=hac) y c x( to -!bestlag) z( to -!best2lag) smpl 1989q1+!horizon+1 1989q1+!horizon+1 eq{!horizon}.fit(f=na) y_hat{!horizon} y_se{!horizon} next smpl @all

Code: Select all

for !horizon= 54 to 73 smpl 1992q1 1992q1+!horizon g3.makepcomp comp{!horizon} smpl @all 'create a variable series y = dlhs series x = dpiet series z= comp{!horizon} 'create empty equation to be used inside the loop equation eq 'variable to store the minimum AIC. Initialise it to a large number !sc = 9999999 'variable saying how many lags to go up to !maxlags = 4 'Variable to store the "best" number of lags !bestlag = 0 !best2lag = 0 smpl 1992q1 1992q1+!horizon equation eq{!horizon} for !i=0 to !maxlags for !j=0 to !maxlags eq{!horizon}.ls(cov=hac) y c x( to -!i) z( to -!j) if eq{!horizon}.@sc < !sc then !best2lag =!j !bestlag = !i !sc = eq{!horizon}.@sc endif next next eq{!horizon}.ls(cov=hac) y c x( to -!bestlag) z( to -!best2lag) smpl 1992q1+!horizon+1 1992q1+!horizon+1 eq{!horizon}.fit(f=na) y_hat{!horizon} y_se{!horizon} next smpl @all
hi sorry another question. why does a running z do not allow forecasting in the 2nd loop?
"unable to compute due to missing data in doeq54.fit(f=na) y_hat{!horizon} y_se{!horizon} and how can i overcome this?
pls advice.

mjfl
Posts: 49
Joined: Mon Jul 18, 2011 8:18 am

Re: for loop within for loop

Postby mjfl » Thu Aug 04, 2011 7:44 pm

hi i think i finally know whats the problem after many many hr of dianostics.

Code: Select all

series k=ka for !horizon= 27 to 29 smpl 1997q1 1997q1+!horizon k.hpf(lambda=1600) k_hp{!horizon} series k_c{!horizon} = k - k_hp{!horizon} next smpl @all 'create a variable series y = dlhs series x = dpiet series z= k_c{!horizon} 'create empty equation to be used inside the loop equation eq 'variable to store the minimum AIC. Initialise it to a large number !sc = 9999999 'variable saying how many lags to go up to !maxlags = 4 'Variable to store the "best" number of lags !bestlag = 0 !best2lag = 0 for !horizon= 27 to 29 smpl 1997q1 1997q1+!horizon equation eq{!horizon} for !i=0 to !maxlags for !j=0 to !maxlags eq{!horizon}.ls(cov=hac) y c x( to -!i) z( to -!) if eq{!horizon}.@sc < !sc then !best2lag =!j !bestlag = !i !sc = eq{!horizon}.@sc endif next next eq{!horizon}.ls(cov=hac) y c x( to -!bestlag) z( to -!best2lag) smpl 1997q1+!horizon+1 1997q1+!horizon+1 eq{!horizon}.fit(f=na) y_hat{!horizon} y_se{!horizon} series master_series = y_hat{!horizon} series ee= (dlhs - y_hat{!horizon})^2 next smpl @all scalar r = sqr((1/24)*@sum(ee)) show r
this code should make equation29 using dlhs,dpiet and k_c29 but in fact uses k_c30 thats why it prompts for k_c30 which after feeding when i return to run only up to eqn29 it works.
may i know is there any way to change z=kc{!horizon} so that it uses kc_29 in eqn29? i tried z=kc{!horizon-1) and z=kc{!horizon}-1 to no avail. tk u.zzz

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13604
Joined: Tue Sep 16, 2008 5:38 pm

Re: for loop within for loop

Postby EViews Gareth » Thu Aug 04, 2011 9:33 pm

Code: Select all

!temp = !horizon-1 z=kc{!temp}

mjfl
Posts: 49
Joined: Mon Jul 18, 2011 8:18 am

Re: for loop within for loop

Postby mjfl » Thu Aug 04, 2011 9:51 pm

hi gareth, i think i got it all wrong again. sigh.
the loops are wrong somehow because eqn 27 uses kc_30 and eqn 28 also uses kc_30 but in my latest post i didnt insert any sampling on kc_30.
i learnt ur creation of !temp. thk u for teaching.
but how can i use eqn 27 using kc_27 and eqn 28 using kc_28?
pls advice.

mjfl
Posts: 49
Joined: Mon Jul 18, 2011 8:18 am

Re: for loop within for loop

Postby mjfl » Sun Aug 07, 2011 5:00 pm

Code: Select all

series k=sti for !horizon= 27 to 29 smpl 1997q1 1997q1+!horizon k.hpf(lambda=1600) k_hp{!horizon} series k_c{!horizon} = k - k_hp{!horizon} series y = dlhs series x = dpiet series z= k_c{!horizon} ' equation eq !sc = 9999999 !maxlags = 4 !bestlag = 0 !best2lag = 0 smpl 1997q1 1997q1+!horizon equation eq{!horizon} for !i=0 to !maxlags for !j=0 to !maxlags eq{!horizon}.ls(cov=hac) y c x( to -!i) z( to -!) if eq{!horizon}.@sc < !sc then !best2lag =!j !bestlag = !i !sc = eq{!horizon}.@sc endif next next eq{!horizon}.ls(cov=hac) y c x( to -!bestlag) z( to -!best2lag) smpl 1997q1+!horizon+1 1997q1+!horizon+1 eq{!horizon}.fit(f=na) y_hat{!horizon} y_se{!horizon}
hi gareth could u pls help? i have twisted the code abit taking away a for and a next but now it doesn't choose the best sic anymore. where is it wrong? thk u.

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13604
Joined: Tue Sep 16, 2008 5:38 pm

Re: for loop within for loop

Postby EViews Gareth » Sun Aug 07, 2011 5:28 pm

I'm afraid I don't understand about 80% of your posts in this thread.

You're going to have to break the question/code up into more understandable parts, plus give considerably more information than "it doesn't work".

mjfl
Posts: 49
Joined: Mon Jul 18, 2011 8:18 am

Re: for loop within for loop

Postby mjfl » Sun Aug 07, 2011 6:07 pm

hi gareth, thk u for the reply. i will have another try to make it in parts. my fault. hope this works.

Code: Select all

series y = dlhs series x = dpiet series z= d(unemp_sa) equation eq !sc = 9999999 !maxlags = 4 !bestlag = 0 !best2lag = 0 for !horizon= 59 to 82 smpl 1989q1 1989q1+!horizon equation eq{!horizon} for !i=0 to !maxlags for !j=0 to !maxlags eq{!horizon}.ls(cov=hac) y c x( to -!i) z( to -!j) if eq{!horizon}.@sc < !sc then !best2lag =!j !bestlag = !i !sc = eq{!horizon}.@sc endif next next eq{!horizon}.ls(cov=hac) y c x( to -!bestlag) z( to -!best2lag) smpl 1989q1+!horizon+1 1989q1+!horizon+1 eq{!horizon}.fit(f=na) y_hat{!horizon} y_se{!horizon} series master_series = y_hat{!horizon} series ee= (dlhs - y_hat{!horizon})^2 next smpl @all scalar k = sqr((1/24)*@sum(ee)) show k
this code works perfectly fine. it chooses x y z, appends it to a certain time fram, forms the best equation in the timeframe, recurively creates a forecast one at a time and gives it in a whole series to get a RMSFE.

i am trying to do the same except now z is USED after hp filter i.e. recursively hp filtered to obtain a cycle to be used.

Code: Select all

series k=ka for !horizon= 27 to 29 smpl 1997q1 1997q1+!horizon k.hpf(lambda=1600) k_hp{!horizon} series k_c{!horizon} = k - k_hp{!horizon} next smpl @all series y = dlhs series x = dpiet series z= k_c{!horizon} equation eq !sc = 9999999 !maxlags = 4 !bestlag = 0 !best2lag = 0 for !horizon= 27 to 29 smpl 1997q1 1997q1+!horizon equation eq{!horizon} for !i=0 to !maxlags for !j=0 to !maxlags eq{!horizon}.ls(cov=hac) y c x( to -!i) z( to -!) if eq{!horizon}.@sc < !sc then !best2lag =!j !bestlag = !i !sc = eq{!horizon}.@sc endif next next eq{!horizon}.ls(cov=hac) y c x( to -!bestlag) z( to -!best2lag) smpl 1997q1+!horizon+1 1997q1+!horizon+1 eq{!horizon}.fit(f=na) y_hat{!horizon} y_se{!horizon} series master_series = y_hat{!horizon} series ee= (dlhs - y_hat{!horizon})^2 next smpl @all scalar r = sqr((1/24)*@sum(ee)) show r
it prompts me. "k_c30 is not defined in series z=k_c30. afterwhich i add the period from 27 to 30 to create a k_c 30. then i run 27 to 29 again.by this method eqn 27 keeps use kc_30 and eqn28 also uses kc_30. but eqn 27 shld use kc_27 while eqn28 should use kc_28. am i missing something?

here it is slightly different. i took away the a set of 'for' and 'next' and lump together as one. this time the issue is the eqn formed do not have the best SIC. and hence even with forecast, they do not come from the best est. and i have beeen asking myself why for many days.

Code: Select all

series k=sti for !horizon= 27 to 29 smpl 1997q1 1997q1+!horizon k.hpf(lambda=1600) k_hp{!horizon} series k_c{!horizon} = k - k_hp{!horizon} series y = dlhs series x = dpiet series z= k_c{!horizon} ' equation eq !sc = 9999999 !maxlags = 4 !bestlag = 0 !best2lag = 0 smpl 1997q1 1997q1+!horizon equation eq{!horizon} for !i=0 to !maxlags for !j=0 to !maxlags eq{!horizon}.ls(cov=hac) y c x( to -!i) z( to -!) if eq{!horizon}.@sc < !sc then !best2lag =!j !bestlag = !i !sc = eq{!horizon}.@sc endif next next eq{!horizon}.ls(cov=hac) y c x( to -!bestlag) z( to -!best2lag) smpl 1997q1+!horizon+1 1997q1+!horizon+1 eq{!horizon}.fit(f=na) y_hat{!horizon} y_se{!horizon}
should i attach the workfile? pls advice.

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13604
Joined: Tue Sep 16, 2008 5:38 pm

Re: for loop within for loop

Postby EViews Gareth » Mon Aug 08, 2011 10:04 am

Again your question is a bit of a mess.

Try removing all the pieces of code that aren't required to solve the problem. Make the smallest program possible that exhibits the problem you want to solve.

mjfl
Posts: 49
Joined: Mon Jul 18, 2011 8:18 am

Re: for loop within for loop

Postby mjfl » Mon Aug 15, 2011 8:05 am

hi gareth i broke it up and it seems to work. :0


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 2 guests