Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • realise a(n)=a(n-1)*b(n) in stata

    Dear all:

    Sorry for bothering you. I have a problem when I use stata, could you give me some adive? So, I am using time series data, tr and afd are two variables, i is one of all the periods, and I want to replace the tr(i) by tr(i-1)*afd(i) in a loop for i from 1 to the whole periods, but I can't make it after several tries. The code is as follows.
    Code:
     set trace on
    local j =_N
    forvalues i=1(1)`j'{
        local k = `i'-1
          replace tr = tr[`k']*afd[`i']
    }
    set trace off
    Best wishes,
    Xinkai Mao

  • #2
    This may be what you want; no loop is needed.
    Code:
    replace tr = L.tr*afd if tr>1
    This will
    • leave tr[1] unchanged
    • replace tr[2] with tr[1]*afd[2]
    • replace tr[3] with (tr[2])*afd[3] = (tr[1]*afd[2])*afd[3]
    • and so forth
    which is perhaps not what you want, since none of the original values of tr come into play except for tr[1]. But it is close to what you described. Perhaps what you want is
    Code:
    generate tr_new = L.tr*afd
    This will
    • create tr_new[1] as a Stata missing value
    • create tr_new[2] = tr[1]*aft[2]
    • create tr_new[3] = tr[2]*aft[3]
    • and so on

    Comment


    • #3
      Originally posted by William Lisowski View Post
      This may be what you want; no loop is needed.
      Code:
      replace tr = L.tr*afd if tr>1
      This will
      • leave tr[1] unchanged
      • replace tr[2] with tr[1]*afd[2]
      • replace tr[3] with (tr[2])*afd[3] = (tr[1]*afd[2])*afd[3]
      • and so forth
      which is perhaps not what you want, since none of the original values of tr come into play except for tr[1]. But it is close to what you described. Perhaps what you want is
      Code:
      generate tr_new = L.tr*afd
      This will
      • create tr_new[1] as a Stata missing value
      • create tr_new[2] = tr[1]*aft[2]
      • create tr_new[3] = tr[2]*aft[3]
      • and so on
      Thanks, anyway but what I want is for example:
      Code:
      make c(n) from b(n) and a(n)
      b(n)       a(n)       c(n)  
      1           1           1
      1           1           1
      0.9         1           0.9
      1           1           0.9
      1           1           0.9
      0.1         1           0.09
      1           1           0.09
      1           1           0.09  
      
      or just make c(n) from b(n)
      b(n)              c(n)  
      1                   1
      1                   1
      0.9                 0.9
      1                   0.9
      1                   0.9
      0.1                 0.09
      1                   0.09
      1                   0.09
      Finally, I made it through the following code: I omitted the in `i'.
      Code:
      set trace on
      local j =_N
      forvalues i=2(1)`j'{
          local k = `i'-1
          replace tr = tr[`k']*afd[`i'] in `i' 
      }
      set trace off
      Last edited by Xinkai Mao; 30 Jul 2022, 02:27. Reason: solved!

      Comment


      • #4
        Thank you for providing a clear example of your inputs and expected outputs.

        There is no need to write a loop, and if you write Stata code that loops over observations, you will generally produce code that will run far slower than it has to, since Stata commands do their own looping over observations.
        Code:
        // read your example data
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input float b byte a
         1 1
         1 1
        .9 1
         1 1
         1 1
        .1 1
         1 1
         1 1
        end
        // this does the work that you want
        generate c = b*a in 1
        replace c = c[_n-1]*b*a in 2/L
        list, clean noobs
        Code:
        . // this does the work that you want
        . generate c = b*a in 1
        (7 missing values generated)
        
        . replace c = c[_n-1]*b*a in 2/L
        (7 real changes made)
        
        . list, clean noobs
        
             b   a     c  
             1   1     1  
             1   1     1  
            .9   1    .9  
             1   1    .9  
             1   1    .9  
            .1   1   .09  
             1   1   .09  
             1   1   .09  
        
        .
        If you have actual time series data with a variable indicating time, you can use Stata's time series notation.
        Code:
        // read your example data
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input float(year b) byte a
        2001  1 1
        2002  1 1
        2003 .9 1
        2004  1 1
        2005  1 1
        2006 .1 1
        2007  1 1
        2008  1 1
        end
        // this does the work that you want
        tsset year
        generate c = b*a in 1
        replace c = L.c*b*a in 2/L
        list, clean noobs
        Code:
        . // this does the work that you want
        . tsset year
        
        Time variable: year, 2001 to 2008
                Delta: 1 unit
        
        . generate c = b*a in 1
        (7 missing values generated)
        
        . replace c = L.c*b*a in 2/L
        (7 real changes made)
        
        . list, clean noobs
        
            year    b   a     c  
            2001    1   1     1  
            2002    1   1     1  
            2003   .9   1    .9  
            2004    1   1    .9  
            2005    1   1    .9  
            2006   .1   1   .09  
            2007    1   1   .09  
            2008    1   1   .09  
        
        .

        Comment


        • #5
          Originally posted by William Lisowski View Post
          Thank you for providing a clear example of your inputs and expected outputs.

          There is no need to write a loop, and if you write Stata code that loops over observations, you will generally produce code that will run far slower than it has to, since Stata commands do their own looping over observations.
          Code:
          // read your example data
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input float b byte a
          1 1
          1 1
          .9 1
          1 1
          1 1
          .1 1
          1 1
          1 1
          end
          // this does the work that you want
          generate c = b*a in 1
          replace c = c[_n-1]*b*a in 2/L
          list, clean noobs
          Code:
          . // this does the work that you want
          . generate c = b*a in 1
          (7 missing values generated)
          
          . replace c = c[_n-1]*b*a in 2/L
          (7 real changes made)
          
          . list, clean noobs
          
          b a c
          1 1 1
          1 1 1
          .9 1 .9
          1 1 .9
          1 1 .9
          .1 1 .09
          1 1 .09
          1 1 .09
          
          .
          If you have actual time series data with a variable indicating time, you can use Stata's time series notation.
          Code:
          // read your example data
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input float(year b) byte a
          2001 1 1
          2002 1 1
          2003 .9 1
          2004 1 1
          2005 1 1
          2006 .1 1
          2007 1 1
          2008 1 1
          end
          // this does the work that you want
          tsset year
          generate c = b*a in 1
          replace c = L.c*b*a in 2/L
          list, clean noobs
          Code:
          . // this does the work that you want
          . tsset year
          
          Time variable: year, 2001 to 2008
          Delta: 1 unit
          
          . generate c = b*a in 1
          (7 missing values generated)
          
          . replace c = L.c*b*a in 2/L
          (7 real changes made)
          
          . list, clean noobs
          
          year b a c
          2001 1 1 1
          2002 1 1 1
          2003 .9 1 .9
          2004 1 1 .9
          2005 1 1 .9
          2006 .1 1 .09
          2007 1 1 .09
          2008 1 1 .09
          
          .
          Thank you very much, William! This is more concise and faster! I didn't know the use of "in 2/L" before, so I tried a loop method, now I know it! Thank you!

          Comment

          Working...
          X