Announcement

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

  • rangerun for byable egen std

    Dear All, The egen (std) is not byable. I tried to use rangerun (to make it byable) but probably made some mistakes. Can anyone have any suggestion?
    Code:
    webuse grunfeld, clear
    
    egen std1 = std(invest) if company == 1
    list 
    egen std2 = std(invest) if company == 2
    list 
    
    capture program drop myprog
    program myprog
      egen std = std(invest)
    end
    
    rangerun myprog, interval(year 0 0) use(invest) by(company)
    The list is
    Code:
    . list
    
         +--------------------------------------------------------------------------------+
         | company   year   invest   mvalue   kstock   time        std1        std2   std |
         |--------------------------------------------------------------------------------|
      1. |       1   1935    317.6   3078.5      2.8      1    -.938126           .     . |
      2. |       1   1936    391.8   4661.7     52.6      2   -.6984423           .     . |
      3. |       1   1937    410.6   5387.1    156.9      3   -.6377137           .     . |
      4. |       1   1938    257.7   2792.2    209.2      4   -1.131617           .     . |
      5. |       1   1939    330.8   4313.2    203.4      5   -.8954869           .     . |
         |--------------------------------------------------------------------------------|
      6. |       1   1940    461.2   4643.9    207.2      6   -.4742636           .     . |
      7. |       1   1941      512   4551.2    255.2      7   -.3101676           .     . |
      8. |       1   1942      448   3244.1    303.7      8   -.5169029           .     . |
      9. |       1   1943    499.6   4053.7    264.1      9   -.3502225           .     . |
     10. |       1   1944    547.5   4379.3    201.6     10   -.1954941           .     . |
         |--------------------------------------------------------------------------------|
     11. |       1   1945    561.2   4840.9      265     11   -.1512398           .     . |
     12. |       1   1946    688.1   4900.9    402.2     12    .2586775           .     . |
     13. |       1   1947    568.9   3526.5    761.5     13   -.1263669           .     . |
     14. |       1   1948    529.2   3254.7    922.4     14   -.2546074           .     . |
     15. |       1   1949    555.1   3700.2   1020.1     15   -.1709443           .     . |
         |--------------------------------------------------------------------------------|
     16. |       1   1950    642.9   3755.6     1099     16    .1126708           .     . |
     17. |       1   1951    755.9     4833   1207.7     17    .4776878           .     . |
     18. |       1   1952    891.2   4924.9   1430.5     18    .9147391           .     . |
     19. |       1   1953   1304.4   6241.7   1777.3     19    2.249474           .     . |
     20. |       1   1954   1486.7   5593.6   2226.3     20    2.838346           .     . |
         |--------------------------------------------------------------------------------|
     21. |       2   1935    209.9   1362.4     53.8      1           .   -1.599489     . |
     22. |       2   1936    355.3   1807.1     50.5      2           .   -.4399941     . |
     23. |       2   1937    469.9   2676.3    118.1      3           .    .4738857     . |
     24. |       2   1938    262.3   1801.9    260.2      4           .   -1.181624     . |
     25. |       2   1939    230.4   1957.3    312.7      5           .   -1.436011     . |
         |--------------------------------------------------------------------------------|
     26. |       2   1940    361.6   2202.9    254.2      6           .   -.3897545     . |
     27. |       2   1941    472.8   2380.5    261.4      7           .    .4970118     . |
     28. |       2   1942    445.6   2168.6    298.7      8           .     .280105     . |
     29. |       2   1943    361.6   1985.1    301.8      9           .   -.3897545     . |
     30. |       2   1944    288.2   1813.9    279.1     10           .   -.9750841     . |
         |--------------------------------------------------------------------------------|
     31. |       2   1945    258.7   1850.2    213.8     11           .   -1.210332     . |
     32. |       2   1946    420.3   2067.7    132.6     12           .    .0783496     . |
     33. |       2   1947    420.5   1796.7    264.8     13           .    .0799446     . |
     34. |       2   1948    494.5   1625.8    306.9     14           .    .6700589     . |
     35. |       2   1949    405.1     1667    351.1     15           .    -.042863     . |
         |--------------------------------------------------------------------------------|
     36. |       2   1950    418.8   1677.4    357.8     16           .    .0663878     . |
     37. |       2   1951    588.2   2289.5    342.1     17           .    1.417271     . |
     38. |       2   1952    645.5   2159.4    444.2     18           .    1.874211     . |
     39. |       2   1953      641   2031.3    623.6     19           .    1.838326     . |
     40. |       2   1954    459.3   2115.5    669.7     20           .    .3893557     . |
         |--------------------------------------------------------------------------------|
     41. |       3   1935     33.1   1170.6     97.8      1           .           .     . |
     42. |       3   1936       45   2015.8    104.4      2           .           .     . |
     43. |       3   1937     77.2   2803.3      118      3           .           .     . |
     44. |       3   1938     44.6   2039.7    156.2      4           .           .     . |
     45. |       3   1939     48.1   2256.2    172.6      5           .           .     . |
         |--------------------------------------------------------------------------------|
     46. |       3   1940     74.4   2132.2    186.6      6           .           .     . |
     47. |       3   1941      113   1834.1    220.9      7           .           .     . |
     48. |       3   1942     91.9     1588    287.8      8           .           .     . |
     49. |       3   1943     61.3   1749.4    319.9      9           .           .     . |
     50. |       3   1944     56.8   1687.2    321.3     10           .           .     . |
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    I now find that the following code seems to work!
    Code:
    rangestat (mean) invest (sd) invest, interval(year . .) by(company)
    gen invest_std = (invest-invest_mean)/invest_sd
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

    Comment


    • #3
      I think this is better done with -runby-, and you can avoid the overhead associated with -egen, std()-.

      Code:
      webuse grunfeld, clear
      
      capture program drop one_std
      program define one_std
          summ invest
          gen std = r(sd)
          gen mean = r(mean)
          gen invest_std = (invest - mean)/std
          exit
      end
      
      runby one_std, by(company)
      -runby-, written by Robert Picard and me, is available from SSC. You are trying to do, in effect, -by company: egen std = std(invest)-, but this particular -egen- function doesn't support -by:-. The code shown above is, I believe, the simplest (and fastest) way to do this. In fact, the best way to think of -runby- is as a -by:- for programs.

      Added: Your approach with -rangestat- will also work, but is more complicated than is necessary. You really want to -by- over company, and the -interval(year . .)- option is just a way to trick -rangestat- into including all years.
      Last edited by Clyde Schechter; 02 Jul 2018, 20:28.

      Comment


      • #4
        Dear Clyde, Many thanks for this helpful suggestion.
        Ho-Chuan (River) Huang
        Stata 19.0, MP(4)

        Comment


        • #5
          Dear Clyde, So, basically, everything is byable by using your suggested `runby' command.
          Code:
          webuse grunfeld, clear
          
          capture program drop one_std
          program define one_std    
            egen invest_std = std(invest)
            exit
          end
          
          runby one_std, by(company)
          Ho-Chuan (River) Huang
          Stata 19.0, MP(4)

          Comment


          • #6
            Yes, with -runby- everything is byable: just wrap it in a program, and write the program to handle a single by-group.

            Comment


            • #7
              Dear Clyde, I see, and thanks.
              Ho-Chuan (River) Huang
              Stata 19.0, MP(4)

              Comment


              • #8
                Dear Clyde, Is the `exit' necessary in the program? What is its function? Thanks.
                Ho-Chuan (River) Huang
                Stata 19.0, MP(4)

                Comment


                • #9
                  No, it's not necessary. Ending programs/subroutines with an explicit exit command is a habit I developed long ago in other computer languages, and I just do it routinely without thinking about it. It can be useful in that, in Stata, if the program ends abnormally, you can specify an error code to return as an argument to -exit-. It is also useful if you want to terminate a program someplace other than at the physical end of the code. But bare -exit- as the last line in the program without a return code actually does nothing.

                  Comment


                  • #10
                    Dear Clyde, Thanks for the explanation. So, it doesn't hurt to remove the -exit- command.

                    Ho-Chuan (River) Huang
                    Stata 19.0, MP(4)

                    Comment

                    Working...
                    X