Announcement

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

  • Find max value of a range

    Dear Statalists,

    I am working on a project and hope to figure out how to find the max value of a range of one variable.

    The specific work is as follows. I'm working on panel data. I hope to find a max value of one variable (var1) in a time range (from year1 to year(n+6)). I tried different ways but couldn't find a solution. The one that makes the most sense for me is as below.
    gen maxvar1=max(var1[1]:var1[_n+6])
    OR
    bysort id: egen maxvar1=max(var1[1]:var1[_n+6])

    But it reports "equation [1] not found". I tried other methods, but they don't work either. May I have your advice?

    Many thanks,
    Chenli

  • #2
    No, you can't do it that way. You don't provide example data, so I'll illustrate the technique using the mpg variable in the auto.dta.

    Code:
    sysuse auto, clear
    gen long obs_no = _n
    gen low = 1
    gen high = _n+6
    rangestat (max) mpg, interval(obs_no low high)
    -rangestat- is written by Robert Picard, Nick Cox, and Roberto Ferrer and is available from SSC.

    Comment


    • #3
      Perhaps this does what you want.
      Code:
      bysort id: generate temp = var1 in 1 // last character is the digit 1
      bysort id: replace temp = max(temp,temp[_n-1]) in 2/l  // last character is a lower-case letter l
      bysort id: generate maxvar1 = temp[_n+6]
      Alternatively you may find the community-contributed rangestat command available from SSC (see the output of ssc describe rangestat) helpful in this case and in other similar situations. Something like the following may do what you.
      Code:
      rangestat (max) var1, by(id) interval(year, ., 6)
      All code in this post in untested, however. Consider it indicative rather than definitive. :-)

      Comment


      • #4
        Correction on review: each occurrence of
        Code:
        bysort id:
        should have been
        Code:
        bysort id (year):
        although if your panel data is correctly sorted by id and year before beginning this, the omission should not matter.

        Comment


        • #5
          I think you are asking for estimating the maximum value in a recursive window, and want to include all past and 6 forward observations. If that is the case, you can use asrol with flexible window. Here is an example using the grunfeld dataset, that is a panel dataset with company as panel identifier and year as time variable.

          The new version of asrol can be downloaded from here. It shall be available on SSC in a couple of weeks.

          Code:
          net install asrol, from(http://fintechprofessor.com) replace
          The code window(year -10000 6) uses the -10000 value to include all previous values, and 6 to include 6 leading periods.

          Code:
          webuse grunfeld
          bys company : asrol invest, window(year -10000 6) stat(max) gen(max6)
          list in 1/10
          
               +--------------------------------------------------------------+
               | company   year   invest   mvalue   kstock   time        max6 |
               |--------------------------------------------------------------|
            1. |       1   1935    317.6   3078.5      2.8      1         512 |
            2. |       1   1936    391.8   4661.7     52.6      2         512 |
            3. |       1   1937    410.6   5387.1    156.9      3         512 |
            4. |       1   1938    257.7   2792.2    209.2      4       547.5 |
            5. |       1   1939    330.8   4313.2    203.4      5   561.20001 |
               |--------------------------------------------------------------|
            6. |       1   1940    461.2   4643.9    207.2      6   688.09998 |
            7. |       1   1941      512   4551.2    255.2      7   688.09998 |
            8. |       1   1942      448   3244.1    303.7      8   688.09998 |
            9. |       1   1943    499.6   4053.7    264.1      9   688.09998 |
           10. |       1   1944    547.5   4379.3    201.6     10   688.09998 |
               +--------------------------------------------------------------+
          Regards
          --------------------------------------------------
          Attaullah Shah, PhD.
          Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
          FinTechProfessor.com
          https://asdocx.com
          Check out my asdoc program, which sends outputs to MS Word.
          For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

          Comment


          • #6
            Here is the method of #2 #3 applied to the Grunfeld data.

            Code:
            . webuse grunfeld, clear
            
            . rangestat (max) invest, interval(year . 6) by(company)
            
            . l year invest* if company == 1
            
                 +---------------------------+
                 | year   invest   invest_~x |
                 |---------------------------|
              1. | 1935    317.6         512 |
              2. | 1936    391.8         512 |
              3. | 1937    410.6         512 |
              4. | 1938    257.7       547.5 |
              5. | 1939    330.8   561.20001 |
                 |---------------------------|
              6. | 1940    461.2   688.09998 |
              7. | 1941      512   688.09998 |
              8. | 1942      448   688.09998 |
              9. | 1943    499.6   688.09998 |
             10. | 1944    547.5   688.09998 |
                 |---------------------------|
             11. | 1945    561.2   755.90002 |
             12. | 1946    688.1   891.20001 |
             13. | 1947    568.9      1304.4 |
             14. | 1948    529.2      1486.7 |
             15. | 1949    555.1      1486.7 |
                 |---------------------------|
             16. | 1950    642.9      1486.7 |
             17. | 1951    755.9      1486.7 |
             18. | 1952    891.2      1486.7 |
             19. | 1953   1304.4      1486.7 |
             20. | 1954   1486.7      1486.7 |
                 +---------------------------+

            Comment


            • #7
              Dear All,

              Thanks a lot for your great help. These work very well for my purpose. The issue has been perfectly addressed. Much appreciated!

              Best regards,
              Chenli

              Comment

              Working...
              X