Bookmark and Share

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: Looping Enquiry


From   Robert Picard <[email protected]>
To   [email protected]
Subject   Re: st: Looping Enquiry
Date   Thu, 29 Sep 2011 12:39:48 -0400

It looks like George is stuck on calculating a running product,
similar to the sum() function. This can easily be done without a loop:

gen rp = _n
replace rp = rp * rp[_n-1] if _n > 1

Since George is trying to replicate the RESULT variable and he's not
using the time and group variables, I suspect that Richard's solution
is not what George is looking for. While I can't quite figure out
everything, here's an attempt that get close:

*----------- begin example -------------
clear
input    time   group   var  RESULT
          30       1        0  -0.958
          57       0        0  -0.916
          58       0        0  -0.834
          67       0        0  -0.874
          74       0        0  -0.792
          79       0        0  -0.750
          79       1        1   0.125
          82       1        1   0.125
          89       0        0  -0.706
          95       1        0  -0.662
          98       0        0  -0.678
         101       0        0  -0.574
         104       0        0  -0.532
         110       0        0  -0.448
         118       0        0  -0.444
end

* setup, per OP
egen ssize=seq() if var==0,from(47) to(1)
replace ssize=ssize[_n-1] if ssize==.
gen  product= (ssize/(ssize+1))

* runing product that does not change when var == 1
gen double rprod = cond(var,1,product)
replace rprod = rprod * rprod[_n-1] if _n > 1

* replicate RESULT
gen score = 1 - cond(var,1,2) * rprod
format %9.3g score
list, clean noobs
*------------ end example --------------


On Thu, Sep 29, 2011 at 10:24 AM, Nick Cox <[email protected]> wrote:
> This looks a neat solution, assuming that Richard has correctly understood the question.
>
> I'd add a thought that no harm would be done by putting the result of -generate- into a -double-. These numbers don't look problematic, but a little worry about loss of precision would do no harm.
>
> Nick
> [email protected]
>
> Richard Herron
>
> If I understand the question, I think you can do this without a loop.
> If you sort on group and time, then you can create a sequential time
> index, use -tsset-, and use lag operators to generate your product.
> Here's my attempt, please let me know if I got your question wrong.
>
> * begin code
> clear
> input    time   group   var  RESULT
>          30       1        0  -0.958
>          57       0        0  -0.916
>          58       0        0  -0.834
>          67       0        0  -0.874
>          74       0        0  -0.792
>          79       0        0  -0.750
>          79       1        1   0.125
>          82       1        1   0.125
>          89       0        0  -0.706
>          95       1        0  -0.662
>          98       0        0  -0.678
>         101       0        0  -0.574
>         104       0        0  -0.532
>         110       0        0  -0.448
>         118       0        0  -0.444
> end
>
> bysort group (time): generate time_seq = _n
> tsset group time_seq
> by group: generate observ = RESULT * l.RESULT * l2.RESULT * l3.RESULT
> * end code
>
> which produces
>
> . list, clean
>
>       time   group   var   RESULT   time_seq     observ
>  1.     57       0     0    -.916          1          .
>  2.     58       0     0    -.834          2          .
>  3.     67       0     0    -.874          3          .
>  4.     74       0     0    -.792          4   .5288082
>  5.     79       0     0     -.75          5   .4329761
>  6.     89       0     0    -.706          6   .3665241
>  7.     98       0     0    -.678          7   .2843288
>  8.    101       0     0    -.574          8   .2060666
>  9.    104       0     0    -.532          9   .1461699
>  10.    110       0     0    -.448         10   .0927537
>  11.    118       0     0    -.444         11   .0607414
>  12.     30       1     0    -.958          1          .
>  13.     79       1     1     .125          2          .
>  14.     82       1     1     .125          3          .
>  15.     95       1     0    -.662          4   .0099093
>
> .
>
> On Thu, Sep 29, 2011 at 09:22, George Bouliotis <[email protected]> wrote:
>
>> Although an old Stata user, currently I am doing my first steps in programming.
>>
>> One of the parts in my programme tries (unsuccessfully) to replicate the column RESULT below. The difficulty is in how to loop a sequential product as, for instance: observ4= obs4 X obs3 (lag1) X obs2 (lag2)  X obs1 (lag1).
>>
>> I tried some loops with "forvalue" but none was successful. I would appreciate any help with this.
>
> [...]
>
>>
>> #####################################
>> set more off
>> clear
>> input    time   group   var  RESULT
>>           30       1        0  -0.958
>>           57       0        0  -0.916
>>           58       0        0  -0.834
>>           67       0        0  -0.874
>>           74       0        0  -0.792
>>           79       0        0  -0.750
>>           79       1        1   0.125
>>           82       1        1   0.125
>>           89       0        0  -0.706
>>           95       1        0  -0.662
>>           98       0        0  -0.678
>>          101       0        0  -0.574
>>          104       0        0  -0.532
>>          110       0        0  -0.448
>>          118       0        0  -0.444
>> end
>>
>>
>> list , clean
>>
>> //Generating Ssize variable
>> egen ssize=seq() if var==0,from(47) to(1)
>> replace ssize=ssize[_n-1] if ssize==.
>> list, noobs clean
>>
>>
>> //Generating product variable
>> gen  product= (ssize/(ssize+1))
>>
>>
>> //Generating Score variable (PRODUCT)
>> gen  score= 1-(2*product) in 1/1 if var==0
>> // for the first observation only
>>
>> //**REPLACEMENT A: when var==0
>> replace  score= 1-(2*(product*product[_n-1]))  if var==0 & score==.
>> // fine for the second obs only  (correct formula for when var=0)
>>
>> //**REPLACEMENT B: when var==1
>> replace  i1= 1-1*(score*score[_n-1])  if var==1
>> // fine for the second observ only (correct formula for when var=1)
>> // but instead of [_n-1] I need a loop for [_n-`n(lagged)'] with "forvalue" command?
>>
>> list, clean noobs
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/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/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index