Understanding the dofor loop

Use this forum to post questions about syntax problems or general programming issues. Questions on implementing a particular aspect of econometrics should go in "Econometrics Issues" below.
AdamElderfield
Posts: 28
Joined: Fri Nov 20, 2020 2:37 pm

Understanding the dofor loop

Unread post by AdamElderfield »

Hi,

I'm very new to RATS so I apologise for the "newbie" question - hopefully these will become less frequent (or more interesting) as I become more acquainted with the software.

I'm trying to do something rather simple, loop over a variables and detrend them

Code: Select all

dofor y = lgdpus lgdp ltot rus lxgs lgne lrtwi rcash cinfl lausq lusq
   FILTER(REMOVE=TREND) y / y_DT
end dofor y
Unfortunately, this doesn't produce the detrended series. I'm obviously missing some important syntax here, I've looked in the user guide but can't seem to find my answer.

I understand the logic, so my guess is that I am not declaring y properly. But I am not sure how to fix this, I tried

Code: Select all

dofor [string] y = "lgdpus" "lgdp" "ltot" "rus" "lxgs" "lgne" "lrtwi" "rcash" "cinfl" "lausq" "lusq"
   FILTER(REMOVE=TREND) y / y_DT
end dofor y
But this gave me an error:

Code: Select all

## SX22. Expected Type INTEGER, Got STRING Instead
>>>>ring] y = "lgdpus" <<<<
Any pointers would be much appreciated!

Thanks

Adam
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Understanding the dofor loop

Unread post by TomDoan »

The %L function gives the name of a series as a string, and %S can be used to create a new series from a string. So (if I understand what you want), you would do

dofor y = lgdpus lgdp ltot rus lxgs lgne lrtwi rcash cinfl lausq lusq
FILTER(REMOVE=TREND) y / %s(%l(y)+"_DT")
end dofor y

which will give you lgdpus_dt, lgpd_dt, etc. as the detrended series.
AdamElderfield
Posts: 28
Joined: Fri Nov 20, 2020 2:37 pm

Re: Understanding the dofor loop

Unread post by AdamElderfield »

Thanks Tom - that is exactly what I am after.
AdamElderfield
Posts: 28
Joined: Fri Nov 20, 2020 2:37 pm

Re: Understanding the dofor loop

Unread post by AdamElderfield »

Another question - this one seems simple, but I can't get it to work.

I want to loop over some parameters and set their staring values

Code: Select all

dofor y = L_ar1 L_ar12 L_ar13 S_ar1 S_ar12 S_ar13 C_ar1 C_ar12 C_ar13 L_ar1 L_ar12 L_ar13
   compute y = 0.5
end dofor y
I get the following error

## SX22. Expected Type INTEGER, Got REAL Instead
>>>>dofor y = L_ar1 <<<<

I use this kind of loop all the time (in other programs. So would be good to understand what I am doing wrong here.
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Understanding the dofor loop

Unread post by TomDoan »

The DOFOR index is (by default) integer. If you want to loop over some other type of object, you need to declare it. This should work:

declare real y
dofor y = L_ar1 L_ar12 L_ar13 S_ar1 S_ar12 S_ar13 C_ar1 C_ar12 C_ar13 L_ar1 L_ar12 L_ar13
compute y = 0.5
end dofor y
AdamElderfield
Posts: 28
Joined: Fri Nov 20, 2020 2:37 pm

Re: Understanding the dofor loop

Unread post by AdamElderfield »

Great thanks! Kind of obvious now you point that out.

While that got rid of the error, I don't think I am solving my problem of setting starting values of my parameters:

Code: Select all


********************************************
* Declare parameter set
********************************************

nonlin(parmset=NSA_MODEL) lambda L_ar1 L_ar12 L_ar13 S_ar1 S_ar12 S_ar13 C_ar1 C_ar12 C_ar13 L_ar1 L_ar12 L_ar13 sigL sigS sigC cvLS cvLC cvSC sigbe10 sigbie3 sigcie1 sigme1 sigme2 siguie1 siguie2

compute lambda = 0.03

declare real y
dofor y = L_ar1 L_ar12 L_ar13 S_ar1 S_ar12 S_ar13 C_ar1 C_ar12 C_ar13 L_ar1 L_ar12 L_ar13
   compute y = 0.5
end dofor y

dofor y = sigL sigS sigC cvLS cvLC cvSC sigbe10 sigbie3 sigcie1 sigme1 sigme2 siguie1 siguie2
   compute y = 0.1
end dofor y
 
display NSA_MODEL
And the result from the display command:

LAMBDA 0.03
L_AR1 NA
L_AR12 NA
L_AR13 NA
S_AR1 NA
S_AR12 NA
S_AR13 NA
C_AR1 NA
C_AR12 NA
C_AR13 NA
L_AR1 NA
L_AR12 NA
L_AR13 NA
SIGL NA
SIGS NA
SIGC NA
CVLS NA
CVLC NA
CVSC NA
SIGBE10 NA
SIGBIE3 NA
SIGCIE1 NA
SIGME1 NA
SIGME2 NA
SIGUIE1 NA
SIGUIE2 NA


So looks like I am setting lambda with my direct call to compute, but the loops are not working?
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Understanding the dofor loop

Unread post by TomDoan »

The DOFOR uses those as inputs, not outputs, so all the DOFOR loops are doing is setting (and promptly forgetting) a value for Y.

compute L_ar1=L_ar12=L_ar13=S_ar1=S_ar12=S_ar13=C_ar1=C_ar12=C_ar13=L_ar1=L_ar12=L_ar13=.5

will do what intended your first DOFOR loop to do.
Post Reply