Announcement

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

  • Clear results saved in r()

    Dear All,

    I am trying to clear the r()-saved results left by the previous command before going to the next one.
    According to the documentation return clear should clear the r-saved results.
    However the results saved in r() seem to survive this command.

    Click image for larger version

Name:	return_clear.png
Views:	1
Size:	42.1 KB
ID:	1597288


    I have found an earlier discussion of this in this forum thread (dating back to 2015), in which Daniel Klein agrees that this behavior is a bug. If this is not fixed until now, is it some sort of desirable/intended behavior? And if so:
    1. what does return clear actually clear?
    2. how to reliably clear the r()-saved results, perhaps with a different command?
    The relevant documentation paragraph seems to be using two terms, r() and return() to describe the placement of the saved results, such as, quote:
    • return list lists results stored in r().
    • return clear clears return().
    Is there any difference in meaning of r() and return()? Which one should be used in which case?

    Thank you, Sergiy Radyakin



  • #2
    That is interesting. Apparently, Stata stores the returned results temporarily in the functions return() and only transfers them into r() once executing the rclass program is completed. return clear indeed just clears the temporary results in return() but not those in r().

    Code:
    . sysuse auto
    (1978 Automobile Data)
    
    . program myreturn, rclass
      1.         version 16.1
      2.         sum `0', mean
      3.         return scalar mymean = r(mean)
      4.         disp "return(mymean) = " return(mymean)
      5.         disp "r(mymean) = " r(mymean)
      6. end
    
    . program myrclear, rclass
      1.         version 16.1
      2. end
    
    . myreturn price
    return(mymean) = 6165.2568
    r(mymean) = .
    
    . return list
    
    scalars:
                 r(mymean) =  6165.256756756757
    
    . myrclear
    
    . return list
    To be fair, this is documented in the return help file. However, that behavior is at odds with ereturn or sreturn, which is quite confusing.

    To really clear the results in r(), one apparently would have to write an rclass program that returns nothing (and thus overwrites the existing r() results with nothing).
    Last edited by Sebastian Kripfganz; 11 Mar 2021, 04:44.
    https://www.kripfganz.de/stata/

    Comment


    • #3
      Hi Segiy,
      That is a very odd result. I actually check the program "clear.ado" to see how it should "clear" the information. Apparently, there are differences between return() and r()
      It seems that this is the best approach:
      Code:
      program clearreturn, rclass
              exit
      end
      So just execute that command, and r()'s will be clear (that is what the official procedure is).
      HTH
      Fernando

      Comment


      • #4
        Originally posted by Sergiy Radyakin View Post
        I have found an earlier discussion of this in this forum thread (dating back to 2015), in which Daniel Klein agrees that this behavior is a bug.
        Looking at this again, I was wrong. This is not a bug. Sebastian points to the relevant documentation. He also notes that the behavior is inconsistent with both ereturn clear and sreturn clear; but then, the equivalent command to those would arguably have to becalled rreturn clear.


        Originally posted by Sergiy Radyakin View Post
        If this is not fixed until now, is it some sort of desirable/intended behavior? And if so:
        1. what does return clear actually clear?
        2. how to reliably clear the r()-saved results, perhaps with a different command?
          [...]
        Is there any difference in meaning of r() and return()? Which one should be used in which case?

        Again, Sebastian has answered the first question and provided one possible answer to the second. I will add that

        Code:
        mata : st_rclear()
        is another way of clearing r() results from Stata.

        As for the last question, you want to use return() inside of r-class programs to access the contents that this program has stored in r():

        Code:
        . mata : st_rclear()
        
        . program my_rclass , rclass
          1.         return scalar foo = 42
          2.         display "r(foo) is " r(foo)
          3.         display "but return(foo) is " return(foo)
          4. end
        
        . my_rclass
        r(foo) is .
        but return(foo) is 42

        Comment


        • #5
          Dear Colleagues,

          thank you very much for your insights on the issue and the examples you've provided. For clearing the r-results I will be using the mata command that Daniel Klein has pointed to (with the advantage that it does not require any additional ado file, the other suggestion to use a blank r-class command is also good).

          The difference between the r() and return() is also clear.

          Sincerely, Sergiy

          Comment

          Working...
          X