# Re: st: looping with augmented dickey fuller test

 From Ernest Berkhout <[email protected]> To [email protected], <[email protected]> Subject Re: st: looping with augmented dickey fuller test Date Wed, 15 Oct 2003 13:59:20 +0200

At 13:19 15-10-2003, Dev Vencappa wrote:
I have a data set that looks like the following

company year var1 var2 var3 var4 optlagvar1 optlagvar2 optlagvar3 optlagvar4
1 1980 1
1 1981 1
1 1982 1
.. ..
1 2002 1
2 1980 3
2 1981 3
2 1982 3
.. ..
2 2002
... ...
... ...
20 1980 6
20 1981 6
20 1982 6
.. ..
20 2002

The variable optlagvar1 for instance is the optimal lag value from an Akaike Information criteriaon exercise for each company. My problem is I now want to implement the DF test for each company and each variable, writing something like:

local z "var1 var2 var3 var4"
foreach k of local z{
forvalues x=1/20{
dfuller `k', lags(optlag`k')
}
}

My problem is I cannot get stata to read the optimal lags value for the option lags, as this has to be specified as a numeric variable. Is there a way I can ask stata to do this loop and correctly specify the values in the lags option for the dfuller test?
You need to parrallel loops, one over company and one over optlag`k', and that block within a loop over your k variables. So your first two lines are OK, although they might be telescoped into one line, check if there are no other variables starting with "var": (foreach k of varlist var*) or (foreach k of varlist var1-var4) or (foreach k of varlist var?).

But then, you want to loop over companies (forvalues x=1/20) as well as over the accompanying optlag`x'. The problem howver is that specifying "dfuller `k', lags(optlag`k')" does not work, as the number of lags cannot be specified as a varname but should be an integer value. So you only have to create a routine that translates the respective values of optlag`k' to a scalar.

Note that this variable optlag`x' is a constant within the variable company.
Following your approach, I would probably come up with something like:

foreach var of varlist var1-var4 {
forvalues x=1/20 {
summ optlag`var' if company==`x' , mean
dfuller `var', lags(`r(mean)') if company==`x'
}
}

It might even be worth the effort to look at the command dfgls. If that produces the optimal number of lags for you, you can acces that number directly as well, in `r(optlag)' or `r(maiclag)' for instance.

Ernest Berkhout
SEO Amsterdam Economics
University of Amsterdam

Room 3.08
1018 WB Amsterdam
The Netherlands

tel.:+ 31 20 525 1657
fax:+ 31 20 525 1686
http://www.seo.nl
===========================
A statistician: someone who insists