Announcement

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

  • Get cumulative product (multiply in sequence) in Stata

    Dear Stata Users,

    I have a dataset about GDP of several years (1980—2000), the dataset has GDP in current price (curregdp) and GDP growth rate (growth).
    Now I want to generate a variable that represents GDP calculated in 1990 year price, let's name the variable gdpbase1990.
    According to economic theory, gdpbase1990 can be generated by multiplying GDP of 1990 and GDP growth rate of responding years.
    eg. gdpbase1990 of year 1991=gdp1990*growth[1991]
    gdpbase1990 of year 1992=gdp1990*growth[1991]*growth[1992]
    gdpbase1990 of year 1989=gdp1990/growth[1989]
    gdpbase1990 of year 1988=gdp1990/growth[1989]/growth[1988], and so on.

    So it takes the following forms:

    Y[1]=A*X[1]
    Y[2]=A*X[1]*X[2]
    Y[3]=A*X[1]*X[2]*X[3]
    Y[_n]=A*X[1]*X[2]*......*X[_n-1]*X[_n]

    Y[-1]=A*X[-1]
    Y[-2]=A*X[-1]/X[-2]
    Y[-3]=A*X[-1]/X[-2]/X[-3]
    Y[-_n]=A*X[-1]/X[-2]/....../X[-_n-1]/X[-_n]

    However, I couldn't find a fast solution in Stata. The codes I use is so clumsy and error-prone. Can anyone help me providing clever codes? Thank you.
    Addtionaly, the resulting product would approximately equal to variable constgdp in 1990—2000 which represents GDP calculated in constant price. And sorry for the stupid codes that follows.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(year constgdp curregdp growth)
    1980  4567.9  4545.6 107.8
    1981  4807.4  4891.6 105.2
    1982  5242.8  5323.4 109.1
    1983  5811.8  5962.7 110.9
    1984  6693.8  7208.1 115.2
    1985  7595.2    9016 113.5
    1986  8267.1 10275.2 108.8
    1987  9224.7 12058.6 111.6
    1988 10265.3 15042.8 111.3
    1989 10682.4 16992.3 104.1
    1990 18547.9 18667.8 103.8
    1991 20250.4 21781.5 109.2
    1992 23134.2 26923.5 114.2
    1993 26364.7 35333.9   114
    1994 29813.4 48197.9 113.1
    1995 33070.5 60793.7 110.9
    1996 36380.4 71176.6   110
    1997 39762.7   78973 109.3
    1998 42877.4 84402.3 107.8
    1999 46144.6 89677.1 107.6
    2000 50035.2 99214.6 108.4
    end
    label var constgdp "GDP calculated at constant prices (base year are 1980,1990)"
    label var curregdp "GDP calculated at current prices"
    label var growth "Real GDP growth rate"
    Code:
    gen gdpbase1990=curregdp[11]
    /*gdpbase1990: years after 1990 (1991—2000)*/
    replace gdpbase1990=curregdp[11]*(growth[11]/100)*(growth[12]/100) in 12
    replace gdpbase1990=curregdp[11]*(growth[11]/100)*(growth[12]/100)*(growth[13]/100) in 13
    replace gdpbase1990=curregdp[11]*(growth[11]/100)*(growth[12]/100)*(growth[13]/100)*(growth[14]/100) in 14
    replace gdpbase1990=curregdp[11]*(growth[11]/100)*(growth[12]/100)*(growth[13]/100)*(growth[14]/100)*(growth[15]/100) in 15
    replace gdpbase1990=curregdp[11]*(growth[11]/100)*(growth[12]/100)*(growth[13]/100)*(growth[14]/100)*(growth[15]/100)*(growth[16]/100) in 16
    replace gdpbase1990=curregdp[11]*(growth[11]/100)*(growth[12]/100)*(growth[13]/100)*(growth[14]/100)*(growth[15]/100)*(growth[16]/100)*(growth[17]/100) in 17
    replace gdpbase1990=curregdp[11]*(growth[11]/100)*(growth[12]/100)*(growth[13]/100)*(growth[14]/100)*(growth[15]/100)*(growth[16]/100)*(growth[17]/100)*(growth[18]/100) in 18
    replace gdpbase1990=curregdp[11]*(growth[11]/100)*(growth[12]/100)*(growth[13]/100)*(growth[14]/100)*(growth[15]/100)*(growth[16]/100)*(growth[17]/100)*(growth[18]/100)*(growth[19]/100) in 19
    replace gdpbase1990=curregdp[11]*(growth[11]/100)*(growth[12]/100)*(growth[13]/100)*(growth[14]/100)*(growth[15]/100)*(growth[16]/100)*(growth[17]/100)*(growth[18]/100)*(growth[19]/100)*(growth[20]/100) in 20
    replace gdpbase1990=curregdp[11]*(growth[11]/100)*(growth[12]/100)*(growth[13]/100)*(growth[14]/100)*(growth[15]/100)*(growth[16]/100)*(growth[17]/100)*(growth[18]/100)*(growth[19]/100)*(growth[20]/100)*(growth[21]/100) in 21
    /*gdpbase1990: years before 1990 (1980—1989)*/
    replace gdpbase1990=curregdp[11]/(growth[10]/100) in 10
    replace gdpbase1990=curregdp[11]/(growth[10]/100)/(growth[9]/100) in 9
    replace gdpbase1990=curregdp[11]/(growth[10]/100)/(growth[9]/100)/(growth[8]/100) in 8
    replace gdpbase1990=curregdp[11]/(growth[10]/100)/(growth[9]/100)/(growth[8]/100)/(growth[7]/100) in 7
    replace gdpbase1990=curregdp[11]/(growth[10]/100)/(growth[9]/100)/(growth[8]/100)/(growth[7]/100)/(growth[6]/100) in 6
    replace gdpbase1990=curregdp[11]/(growth[10]/100)/(growth[9]/100)/(growth[8]/100)/(growth[7]/100)/(growth[6]/100)/(growth[5]/100) in 5
    replace gdpbase1990=curregdp[11]/(growth[10]/100)/(growth[9]/100)/(growth[8]/100)/(growth[7]/100)/(growth[6]/100)/(growth[5]/100)/(growth[4]/100) in 4
    replace gdpbase1990=curregdp[11]/(growth[10]/100)/(growth[9]/100)/(growth[8]/100)/(growth[7]/100)/(growth[6]/100)/(growth[5]/100)/(growth[4]/100)/(growth[3]/100) in 3
    replace gdpbase1990=curregdp[11]/(growth[10]/100)/(growth[9]/100)/(growth[8]/100)/(growth[7]/100)/(growth[6]/100)/(growth[5]/100)/(growth[4]/100)/(growth[3]/100)/(growth[2]/100) in 2
    replace gdpbase1990=curregdp[11]/(growth[10]/100)/(growth[9]/100)/(growth[8]/100)/(growth[7]/100)/(growth[6]/100)/(growth[5]/100)/(growth[4]/100)/(growth[3]/100)/(growth[2]/100)/(growth[1]/100) in 1
    Last edited by Chen Samulsion; 26 Feb 2018, 05:11.

  • #2
    Your calcuations could be made easier by doing:

    Code:
    sort year
    gen index1980=100 if year==1980
    replace index1980=(growth/100)*(index1980[_n-1]) if year!=1980
    
    gen index1990=100 if year==1990
    replace index1990=(growth/100)*(index1990[_n-1]) if year>1990
    This gives you the values you ask for.
    This is not, however, a way to convert between real and current gdp values. For that, you need to replace gdp growth with consumer price index (CPI) inflation numbers in your calculation.
    See http://www.inflation.eu/inflation-ra...ed-states.aspx
    or
    https://data.bls.gov/cgi-bin/cpicalc.pl

    Comment


    • #3
      Dear Jorrit Gosens, thank you very much! Your code for year>1990 works perfect well. But for year<1990 (which in fact should use division "/"), it seems to have some trouble.
      I agree with you that real GDP that based in selected year's price should consider adjusting to CPI. But the Real GDP growth rate in my dataset is calculated based on GDP in constant price (constgdp & growth of 1980—1990 is base on 1980's price, constgdp & growth of 1990—2000 is based on 1990's price, and there are two values for year 1990 which were deleted for neat), an expert worked in National Bureau of Statistics suggests the multiplying method that I deploy above.
      Last edited by Chen Samulsion; 26 Feb 2018, 06:35.

      Comment


      • #4
        Add the last few lines from here:
        Code:
        sort year
        gen index1980=100 if year==1980
        replace index1980=(growth/100)*(index1980[_n-1]) if year!=1980
        
        gen index1990=100 if year==1990
        replace index1990=(growth/100)*(index1990[_n-1]) if year>1990
        
        gsort -year
        replace index1990=(index1990[_n-1])/(growth/100) if year<1990
        sort year
        
        gen growth_real_vs_curr= curregdp/constgdp
        gen index1990_factor=index1990/100
        Again, note that gdp growth and inflation are not the same thing.

        Compare your output:
        Code:
             +---------------------------------------------------------------------------------+
             | year   constgdp   curregdp   growth   ind~1980   ind~1990   growth~r   index1~r |
             |---------------------------------------------------------------------------------|
          1. | 1980     4567.9     4545.6    107.8        100   39.62068   .9951181   .3962068 |
          2. | 1981     4807.4     4891.6    105.2      105.2   42.71109   1.017515   .4271109 |
          3. | 1982     5242.8     5323.4    109.1   114.7732   44.93206   1.015373   .4493206 |
          4. | 1983     5811.8     5962.7    110.9   127.2835   49.02088   1.025964   .4902088 |
          5. | 1984     6693.8     7208.1    115.2   146.6306   54.36416   1.076832   .5436416 |
             |---------------------------------------------------------------------------------|
          6. | 1985     7595.2       9016    113.5   166.4257   62.62751   1.187065   .6262751 |
          7. | 1986     8267.1    10275.2    108.8   181.0711   71.08222   1.242903   .7108222 |
          8. | 1987     9224.7    12058.6    111.6   202.0754   77.33746   1.307208   .7733746 |
          9. | 1988    10265.3    15042.8    111.3   224.9099    86.3086   1.465403    .863086 |
         10. | 1989    10682.4    16992.3    104.1   234.1312   96.06148   1.590682   .9606148 |
             |---------------------------------------------------------------------------------|
         11. | 1990    18547.9    18667.8    103.8   243.0282        100   1.006464          1 |
         12. | 1991    20250.4    21781.5    109.2   265.3868      109.2   1.075608      1.092 |
         13. | 1992    23134.2    26923.5    114.2   303.0717   124.7064   1.163797   1.247064 |
         14. | 1993    26364.7    35333.9      114   345.5017   142.1653   1.340197   1.421653 |
         15. | 1994    29813.4    48197.9    113.1   390.7624   160.7889   1.616652   1.607889 |
             |---------------------------------------------------------------------------------|
         16. | 1995    33070.5    60793.7    110.9   433.3555   178.3149   1.838306   1.783149 |
         17. | 1996    36380.4    71176.6      110   476.6911   196.1464   1.956455   1.961464 |
         18. | 1997    39762.7      78973    109.3   521.0234   214.3881   1.986108   2.143881 |
         19. | 1998    42877.4    84402.3    107.8   561.6632   231.1103   1.968457   2.311103 |
         20. | 1999    46144.6    89677.1    107.6   604.3496   248.6747   1.943393   2.486747 |
             |---------------------------------------------------------------------------------|
         21. | 2000    50035.2    99214.6    108.4    655.115   269.5634   1.982896   2.695634 |
             +---------------------------------------------------------------------------------+
        
        .

        Comment


        • #5
          Yes, the multiplying method is NOT complete. Maybe the expert takes it as a convenient process.

          Code:
          labgen gdpbase1990= curregdp[11]*index1990/100
          list year constgdp curregdp gdpbase1990 if year>=1990, noobs sep(0)
          Code:
           
            +---------------------------------------+
            | year   constgdp   curregdp   gdp~1990 |
            |---------------------------------------|
            | 1990    18547.9    18667.8    18667.8 |
            | 1991    20250.4    21781.5   20385.24 |
            | 1992    23134.2    26923.5   23279.94 |
            | 1993    26364.7    35333.9   26539.13 |
            | 1994    29813.4    48197.9   30015.76 |
            | 1995    33070.5    60793.7   33287.48 |
            | 1996    36380.4    71176.6   36616.23 |
            | 1997    39762.7      78973   40021.54 |
            | 1998    42877.4    84402.3   43143.22 |
            | 1999    46144.6    89677.1    46422.1 |
            | 2000    50035.2    99214.6   50321.55 |
            +---------------------------------------+

          Comment


          • #6
            I would use double variables here routinely.

            Comment


            • #7
              Dear Nick Cox, the original variables are actually stored in double, and I recast them as float for neatly display here (#1).

              Comment


              • #8
                OK, good, but how are we supposed to know that? Also, display is better fixed by assigning a display format that is suitable (and the code for that can be added to dataex output).

                Comment


                • #9
                  In pursuing a "good" display, I separately used round(), format, and eventually decided to use recast. Now I realized that do harm to calculate using those variables. I'll learn this lesson. Thank you Nick.

                  Comment


                  • #10
                    A side-issue, and not a fault here, but as you've mentioned round() I will underline that I would (almost) never use round() unless with integer second arguments. I wouldn't go so far as to make that illegal, as for example round(x, 0.5) or round(x, 0.25) may make sense, but I have seen far too many cases in which users thought e.g. that round(x, 0.1) or round(x, 0.01) obliged Stata to change what it is storing to a different decimal, thereby forgetting the first lesson about computers, that they work in binary (*).

                    (*) This was at least in my first lesson about computers circa 1964 when a mathematics teacher digressed from the syllabus. I didn't get even to see a computer until 1970 or to program one until 1973 (**).

                    (**) These dates are CE/AD not BCE/BC.

                    Comment


                    • #11
                      That you underlied in bold is indeed what I want to achieve when using round(). And when I found the round() cannot achieve that goal (what a wrong goal it is!), I turned to another command.
                      I am glad this thread let you memorize those days long (*) ago. Nick, You are so humor!
                      (*) Maybe I should recast it to double.

                      Comment


                      • #12
                        Daniel Pailañir Vargas wrote -grvar- command in 2020, thank you.
                        Code:
                        . ssc describe grvar
                        
                        ------------------------------------------------------------------------------------------------
                        package grvar from http://fmwww.bc.edu/repec/bocode/g
                        ------------------------------------------------------------------------------------------------
                        
                        TITLE
                              'GRVAR': module to apply a non-constant growth rate to a variable
                        
                        DESCRIPTION/AUTHOR(S)
                              
                               grvar Calculate the future values of a variable from the initial
                              value and a non-constant growth/decrease rate    (e.g.
                              inflation).  It is useful when we want to fit a variable with a
                              growth rate in a time serie or panel   data.
                              
                              KW: data management
                              KW: growth rate
                              KW: time series
                              KW: panel
                              
                              Requires: Stata version 13
                              
                              Distribution-Date: 20200628
                              
                              Author: Daniel Pailañir Vargas
                              Support: email [email protected]
                              
                        
                        INSTALLATION FILES                                (type net install grvar)
                              grvar.ado
                              grvar.sthlp
                        ------------------------------------------------------------------------------------------------
                        (type ssc install grvar to install)

                        Comment


                        • #13
                          Originally posted by Nick Cox View Post
                          A side-issue, and not a fault here, but as you've mentioned round() I will underline that I would (almost) never use round() unless with integer second arguments. I wouldn't go so far as to make that illegal, as for example round(x, 0.5) or round(x, 0.25) may make sense, but I have seen far too many cases in which users thought e.g. that round(x, 0.1) or round(x, 0.01) obliged Stata to change what it is storing to a different decimal, thereby forgetting the first lesson about computers, that they work in binary (*).

                          (*) This was at least in my first lesson about computers circa 1964 when a mathematics teacher digressed from the syllabus. I didn't get even to see a computer until 1970 or to program one until 1973 (**).

                          (**) These dates are CE/AD not BCE/BC.
                          Provide double type variables as is.

                          Code:
                          * Example generated by -dataex-. For more info, type help dataex
                          clear
                          input float(year constgdp curregdp) double(growthR growthR100 gdp198090 group)
                          1980  4567.9  4545.6  7.800000000000001                .078  4545.6 1
                          1981  4807.4  4891.6                5.2 .052000000000000005       . 1
                          1982  5242.8  5323.4                9.1                .091       . 1
                          1983  5811.8  5962.7               10.9                .109       . 1
                          1984  6693.8  7208.1 15.200000000000001                .152       . 1
                          1985  7595.2    9016               13.5                .135       . 1
                          1986  8267.1 10275.2                8.8                .088       . 1
                          1987  9224.7 12058.6 11.600000000000001                .116       . 1
                          1988 10265.3 15042.8               11.3                .113       . 1
                          1989 10682.4 16992.3 4.1000000000000005                .041       . 1
                          1990 18547.9 18667.8 3.8000000000000003                .038 18547.9 2
                          1991 20250.4 21781.5  9.200000000000001                .092       . 2
                          1992 23134.2 26923.5 14.200000000000001  .14200000000000002       . 2
                          1993 26364.7 35333.9                 14                 .14       . 2
                          1994 29813.4 48197.9 13.100000000000001                .131       . 2
                          1995 33070.5 60793.7               10.9                .109       . 2
                          1996 36380.4 71176.6                 10                  .1       . 2
                          1997 39762.7   78973                9.3                .093       . 2
                          1998 42877.4 84402.3  7.800000000000001                .078       . 2
                          1999 46144.6 89677.1 7.6000000000000005                .076       . 2
                          2000 50035.2 99214.6                8.4                .084       . 2
                          end
                          
                          label variable group "easy to understand but difficult to explain"
                          Code:
                          xtset group year
                          grvar growthR100 gdp198090, gen(gdp198090B)

                          Comment

                          Working...
                          X