Announcement

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

  • How to find the index of the observation that has the maximum value of a variable

    Hello, I have two questions:
    - Is there easy way to retrieve the index of the observation that has the maximum value for a variable?
    - How can I retrieve the value of a variable at the i-th observation point?

    Thank you,

  • #2
    Maybe this is not the most elegant solution, but I gather it can do the trick:

    Code:
    . sysuse auto
     
    
    . su mpg
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
             mpg |         74     21.2973    5.785503         12         41
    
    . list mpg if mpg == r(max)
    
         +-----+
         | mpg |
         |-----|
     71. |  41 |
         +-----+
    Hopefully that helps.
    Last edited by Marcos Almeida; 29 Oct 2019, 06:49.
    Best regards,

    Marcos

    Comment


    • #3


      Watch out for ties. In many variables and many datasets, several observations will have values equal to the maximum.

      Code:
      . sysuse auto
      (1978 Automobile Data)
      
      . gen long obsno = _n
      
      . su mpg
      
          Variable |        Obs        Mean    Std. Dev.       Min        Max
      -------------+---------------------------------------------------------
               mpg |         74     21.2973    5.785503         12         41
      
      . l obsno if mpg == r(max)
      
           +-------+
           | obsno |
           |-------|
       71. |    71 |
           +-------+
      
      . di mpg[71]
      41

      Comment


      • #4
        Hello,

        Thank you so much for your replies. What I am trying to do is, I have generated the variables AUC and Alpha by executing the code below:

        Code:
        generate Alpha = .
        generate AUC = .
        local i = 1
        local j = 1
        
        foreach a1 of local alphaGrid {
        
        replace Alpha = `a1' in `i'
        local i = `++i'
        
        }
        
        foreach auc of local AUC_macro {
        
        replace AUC = `auc' in `j'
        local j = `++j'
        
        }
        Please note that both variables AUC and Alpha contains some number of missing values at the end.
        I am trying to locate the index number with the maximum AUC (let's call this index as i), and then try to retrieve the best value of alpha by retrieving the value of Alpha at that index i, but I am still not sure how I can retrieve the best value for alpha and then store the resulting value under a local macro 'bestAlpha'?

        Thank you,
        Last edited by Dominique Bourget; 29 Oct 2019, 07:47.

        Comment


        • #5
          Originally posted by Nick Cox View Post

          Watch out for ties. In many variables and many datasets, several observations will have values equal to the maximum.

          Code:
          . sysuse auto
          (1978 Automobile Data)
          
          . gen long obsno = _n
          
          . su mpg
          
          Variable | Obs Mean Std. Dev. Min Max
          -------------+---------------------------------------------------------
          mpg | 74 21.2973 5.785503 12 41
          
          . l obsno if mpg == r(max)
          
          +-------+
          | obsno |
          |-------|
          71. | 71 |
          +-------+
          
          . di mpg[71]
          41
          Hello,
          How can I store the value 41 (that results from -di mpg[71]- ) under a local macro? Thank you,

          Comment


          • #6
            Code:
            summ mpg
            return list // for demo only; see -help return list-
            local mpgmax = r(max)
            Note that in order to store the maximum value in a local, there's no need to have the observation number of the observation holding the maximum value.

            Comment


            • #7
              The reply given in #2 was related to the expression "how to find", clearly stated in the first message. In #5, however, it became clear that "how to store" is what fits in Dominique's needs.
              Best regards,

              Marcos

              Comment

              Working...
              X