Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: st: FORVAL loop incomplete
From
"Francis, Richard N" <[email protected]>
To
"[email protected]" <[email protected]>
Subject
RE: st: FORVAL loop incomplete
Date
Sat, 9 Nov 2013 18:00:28 +0000
Thanks Sergiy!!!
Rick Francis
Associate Professor
Department of Accounting
College of Business Administration
University of Texas at El Paso
500 W. University Avenue
El Paso, TX 79968
Office: 915-747-7953
FAX: 915-747-8618
Email: [email protected]
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Sergiy Radyakin
Sent: Saturday, November 09, 2013 10:58 AM
To: [email protected]
Subject: Re: st: FORVAL loop incomplete
Have a look at svmat, which saves a matrix into the variables:
http://www.stata.com/help.cgi?svmat
You can also do your original:
g Z14 = .
g Z24 = .
g Z34 = .
g Z4t = .
g Z3t = .
g Z2t = .
g Z1t = .
su id, meanonly
local limit=r(max)
forval i = 1/`limit' {
display "Iteration `i' of `limit'"
sroot fcf if id==`i'
return list
ereturn list
local p=`i'
replace Z14 = r(Z14) in `p'
replace Z24 = r(Z24) in `p'
replace Z34 = r(Z34) in `p'
replace Z4t = r(Z4t) in `p'
replace Z3t = r(Z3t) in `p'
replace Z2t = r(Z2t) in `p'
replace Z1t = r(Z1t) in `p'
}
Note I introduced `p'. Your `i' index is the group index. My understanding is that it will take values of 1,2, and 3 for 3 of your groups. Within each of these 3 groups you are conducting the seasonal unit root test and get some 7 scalars. You now have to decide where do you want to save the results (3 vectors) in your dataset (180 observations), although I would still recommend a matrix for this. Now the candidates are:
1) to the right of the first observation of the group;
2) to the right of each observation of each group (3x60duplicates);
3) to observations 1 2 and 3 (fixed indices, regardless from fact that they belong to the same group)
4) to the right of 3 new observations that are appended to the dataset;
The loop as shown above implements #3.
To implement #1, add before loop
generate pos=_n
and in the loop
sum pos if id==`i'
local p=r(min)
To implement #2 add to the above:
local pp=r(max)
and change <in `p'> to <in `p'/`pp'>
To implement #4 I would add before the loop:
expand 1+`limit' in _N
and in the loop:
local p=_N-`limit'+`i'
Best, Sergiy Radyakin
On Sat, Nov 9, 2013 at 9:34 AM, Francis, Richard N <[email protected]> wrote:
> Sergiy,
>
> APPEND is probably used inappropriately.
>
> Goal is to create new variables, instead of new observations.
>
> Think APPEND creates new obs. So, better rethink that idea.
>
> Thanks again!!!!
>
> Rick Francis
>
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf Of Sergiy
> Radyakin
> Sent: Saturday, November 09, 2013 10:24 AM
> To: [email protected]
> Subject: Re: st: FORVAL loop incomplete
>
> upps, sorry I overlooked in #2 above that fcf is a variable name in
> your case, not a command, but sroot also supports an if modifier, see
> here:
> http://www.stata-journal.com/sjpdf.html?articlenum=st0172
>
> Try the following loop:
>
> local limit=r(max)
> forval i = 1/`limit' {
> display "Iteration `i' of `limit'"
> sroot fcf if id==`i'
> matrix Zi=r(Z14),r(Z24),r(Z34),r(Z4t),r(Z3t),r(Z2t),r(Z1t)
> matrix Z=nullmat(Z)\Zi
> }
>
> matrix list Z
>
> -----------------
> I still don't understand what -append- was supposed to do in the
> original program :(
>
> Sergiy Radyakin
>
>
> On Sat, Nov 9, 2013 at 9:12 AM, Sergiy Radyakin <[email protected]> wrote:
>> Rick
>>
>> 1) your original email mentions that the loop only runs once. Now we
>> know that the r(max) is equal to 3 so we expect the loop to have
>> three iterations. The only way it can do 1 iteration is if it breaks
>> with an error. To avoid any confusion do the following
>> adjustment:
>>
>> ...
>> local limit=r(max)
>> forval i=1/`limit' {
>> display "This is iteration `i' of `limit'"
>> ...
>>
>> 2) Do you use fcf from here?
>> https://www.uni-goettingen.de/en/199539.html
>> If yes, note that it supports if condition. Instead of your "keep if
>> id==.." you can write "fcf if id==.."
>>
>> 3) Your i need to have a meaning. It is either ID or observation
>> number. This is not necessarily the same. It is definitely not the
>> same if you have multiple observations of the same subjects, which I
>> assume you do, since you have time series. This is hugely
>> incompatible with the -keep- statement, since it also affects the
>> observation number and hence the working of -in- modifier.
>>
>> 4) Whatever results you save with -replace- will be lost after you -restore-.
>>
>> 5) Make sure you understand how -append- works. It appends data to
>> memory from file, not the other way around. If you intend to save
>> results to file at each iteration look at postfile.
>>
>> Sergiy
>>
>>
>> On Sat, Nov 9, 2013 at 8:41 AM, Francis, Richard N <[email protected]> wrote:
>>> Hi Sergiy,
>>>
>>> Thank you for taking time out of your day for such a novice issue.
>>>
>>> The entire pgm looks like this:
>>>
>>>
>>> destring gvkey, replace
>>>
>>> egen id = group(gvkey), label
>>>
>>> gen date = yq(year, fqtr)
>>>
>>> tsset id date, quarterly
>>>
>>> su id, meanonly
>>>
>>> g Z14 = .
>>> g Z24 = .
>>> g Z34 = .
>>> g Z4t = .
>>> g Z3t = .
>>> g Z2t = .
>>> g Z1t = .
>>>
>>> forval i = 1/`r(max)' {
>>> preserve
>>> keep if id == `i'
>>> sroot fcf
>>> return list
>>> ereturn list
>>> replace Z14 = r(Z14) in `i'
>>> replace Z24 = r(Z24) in `i'
>>> replace Z34 = r(Z34) in `i'
>>> replace Z4t = r(Z4t) in `i'
>>> replace Z3t = r(Z3t) in `i'
>>> replace Z2t = r(Z2t) in `i'
>>> replace Z1t = r(Z1t) in `i'
>>> append using new
>>> restore
>>> }
>>>
>>> R(max) immediately prior to the FORVAL loop is three (3), but I'm not sure this is what you are looking for in your first comment.
>>>
>>> Does this help at all?
>>>
>>> Again, thank you!
>>>
>>> Rick Francis
>>>
>>> -----Original Message-----
>>> From: [email protected]
>>> [mailto:[email protected]] On Behalf Of Sergiy
>>> Radyakin
>>> Sent: Saturday, November 09, 2013 9:28 AM
>>> To: [email protected]
>>> Subject: Re: st: FORVAL loop incomplete
>>>
>>> Rick,
>>> 1) N may be 180, but what is r(max)? In Stata r(max) is the result of the previous command [of r-class]. You don't show us this command.
>>> Please do.
>>> 2) The combination of "append using new" and "restore" does not make sense, Whatever you append you immediately lose.
>>> 3) If append is meant to add one observation where you are saving
>>> the results (dataset new is a blank one observation of the same
>>> layout as the current dataset), consider using: expand in _N
>>> 4) If you want to accumulate the results of your tests, consider using a matrix to hold the results, and avoid modifying the data.
>>> Best, Sergiy
>>>
>>> On Sat, Nov 9, 2013 at 8:14 AM, Francis, Richard N <[email protected]> wrote:
>>>> I'm sorry, N = 180 obs, currently cycles through the first 60 (3 groups of 60 =180 total obs).
>>>>
>>>> Thank you!!
>>>>
>>>> Rick Francis
>>>>
>>>> -----Original Message-----
>>>> From: [email protected]
>>>> [mailto:[email protected]] On Behalf Of Nick Cox
>>>> Sent: Saturday, November 09, 2013 9:08 AM
>>>> To: [email protected]
>>>> Subject: Re: st: FORVAL loop incomplete
>>>>
>>>> Tell us what r(max) is.
>>>> Nick
>>>> [email protected]
>>>>
>>>>
>>>> On 9 November 2013 15:53, Francis, Richard N <[email protected]> wrote:
>>>>> Hello Statalist,
>>>>>
>>>>> Have a simple FORVAL loop which should result in multiple cycles.
>>>>>
>>>>> However, she only cycles once. I'm sure the answer is obvious to experienced Stata users (which I am not).
>>>>>
>>>>> The loop is as follows:
>>>>>
>>>>>
>>>>> forval i = 1/`r(max)' {
>>>>> preserve
>>>>> keep if id == `i'
>>>>> sroot fcf
>>>>> return list
>>>>> ereturn list
>>>>> replace Z14 = r(Z14) in `i'
>>>>> replace Z24 = r(Z24) in `i'
>>>>> replace Z34 = r(Z34) in `i'
>>>>> replace Z4t = r(Z4t) in `i'
>>>>> replace Z3t = r(Z3t) in `i'
>>>>> replace Z2t = r(Z2t) in `i'
>>>>> replace Z1t = r(Z1t) in `i'
>>>>> append using new
>>>>> restore
>>>>> }
>>>>>
>>>>> I am grateful for any thoughts anyone may have.
>>>>>
>>>>> Thank you!!
>>>>>
>>>>> Rick Francis
>>>>>
>>>>> *
>>>>> * For searches and help try:
>>>>> * http://www.stata.com/help.cgi?search
>>>>> * http://www.stata.com/support/faqs/resources/statalist-faq/
>>>>> * http://www.ats.ucla.edu/stat/stata/
>>>> *
>>>> * For searches and help try:
>>>> * http://www.stata.com/help.cgi?search
>>>> * http://www.stata.com/support/faqs/resources/statalist-faq/
>>>> * http://www.ats.ucla.edu/stat/stata/
>>>>
>>>> *
>>>> * For searches and help try:
>>>> * http://www.stata.com/help.cgi?search
>>>> * http://www.stata.com/support/faqs/resources/statalist-faq/
>>>> * http://www.ats.ucla.edu/stat/stata/
>>> *
>>> * For searches and help try:
>>> * http://www.stata.com/help.cgi?search
>>> * http://www.stata.com/support/faqs/resources/statalist-faq/
>>> * http://www.ats.ucla.edu/stat/stata/
>>>
>>> *
>>> * For searches and help try:
>>> * http://www.stata.com/help.cgi?search
>>> * http://www.stata.com/support/faqs/resources/statalist-faq/
>>> * http://www.ats.ucla.edu/stat/stata/
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/faqs/resources/statalist-faq/
> * http://www.ats.ucla.edu/stat/stata/
>
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/faqs/resources/statalist-faq/
> * http://www.ats.ucla.edu/stat/stata/
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/