Announcement

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

  • Accessing function output that's displayed but not stored

    Hi,
    I would like to store some of the output of the adjrr command in macros. The output is displayed in the Results window but not stored in the return list of the command. Specifically, I would like to access the limits of the 95% CI (e.g. 0.5325 and 2.1780) of the ARR in the example and associated output below. These CI values are not stored in the return list of adjrr and cannot be directly recreated from the standard errors that are in the return list.

    Thanks, Tillmann


    Code:
    set seed 123
    sysuse auto, clear
    
    gen rand=runiform()*2
    replace rand=int(rand)
    
    logit foreign rand, or
    adjrr rand
    Click image for larger version

Name:	adjrr.png
Views:	1
Size:	7.1 KB
ID:	1665095

    Last edited by Tillmann von Carnap; 17 May 2022, 14:34.

  • #2
    Since the program -adjrr.ado- is community contributed, you could and should ask the authors directly. Their 2013 Stata Journal article with which they published the code for -adjrr- is here:
    https://www.stata-journal.com/articl...article=st0306

    But one of the advantages of Stata is that most code is open-source. So you might want to make your own hacked version of the program -adjrr-:

    Begin by making your own copy of the program. The safest way to do this is to locate your PERSONAL ado folder and place your hacked ado program in that folder with its own distinctive name.

    First take note of your current working directory by typing -pwd- at the Stata prompt. (See -help pwd- for more on that command.) On a Windows computer, the result might be something like:

    Code:
    . pwd
    c:\Users\My_name\Documents\this_project
    To locate your PERSONAL ado folder, type the command:
    Code:
    adopath
    Stata should respond by showing you the folders in which it expects all ado programs to be stored. The result looks something like this:
    Code:
    . adopath
      [1]              "."
      [2]  (BASE)      "C:\ProgramFiles\Stata17\ado\base/"
      [3]  (SITE)      "C:/ado/site/"
      [4]  (PERSONAL)  "C:/ado/personal"
      [5]  (PLUS)      "C:/ado/stbplus/"
    Of the named folders, Stata alone should handle the BASE, SITE and PLUS folders. It's extremely unwise for the user to even open those folders. But the folder named PERSONAL is the place to store your own ado programs. (Create it if it does not exist.) Navigate to that folder from inside Stata with the command:

    Code:
    cd  "C:/ado/personal"
    Once there (as confirmed by typing -pwd- at the Stata prompt), create your own copy of -adjrr- like this:
    Code:
    copysource adjrr.ado 
    shell rename adjrr.ado  my_adjrr.ado
    Having created your own copy of the program -adjrr-, you can hack it to deliver the lower and upper bounds of the ARR confidence interval as part of its returned results.

    Use the Stata editor to open your version of the program.

    Code:
    doedit  my_adjrr.ado
    First, use Find/Replace to change every occurrence of the character string "adjrr" to the string "my_adjrr". Save the program -my_adjrr.ado- in the PERSONAL folder. Then find line 58 of the program that reads -scalar `lnarr_se' = _se[lnARR]- . Insert the following four lines of code after line 58:

    Code:
    return scalar lnarr = `lnarr'        //  Added in my_adjrr
    return scalar lnarr_se = `lnarr_se'     //  Added in my_adjrr
    return scalar ARR_lb = exp(`lnarr' - invnorm(0.975)*`lnarr_se')  //  Added in my_adjrr
    return scalar ARR_ub = exp(`lnarr' + invnorm(0.975)*`lnarr_se')  //  Added in my_adjrr
    Again save the file -my_adjrr.ado- with these changes.

    Now return to your working directory with the commands:

    Code:
    cd  "c:\Users\My_name\Documents\this_project"
    pwd
    Check that Stata can find your bersion of the program by typing:
    Code:
    which my_adjrr
    Now run your example using your version of -my_adjrr- with this code:

    Code:
    capture prog drop my_adjrr
    set seed 123
    sysuse auto, clear
    
    gen rand=runiform()*2
    replace rand=int(rand)
    
    logit foreign rand, or
    
    my_adjrr rand
    return list
    
    di as txt "Estimated ARR is: " as res `r(ARR)'
    di as txt "Lower bound of 95% CI of estimated ARR is: " as res `r(ARR_lb)'
    di as txt "Upper bound of 95% CI of estimated ARR is: " as res `r(ARR_ub)'
    The results are:

    Code:
    . 
    . my_adjrr rand
    
    R1  = 0.3077 (0.0739)    95% CI  (0.1628, 0.4525)
    R0  = 0.2857 (0.0764)    95% CI  (0.1361, 0.4354)
    ARR = 1.0769 (0.3870)    95% CI  (0.5325, 2.1780)
    ARD = 0.0220 (0.1063)    95% CI  (-0.1863, 0.2303)
    p-value (R0 = R1):  0.8362
    p-value (ln(R1/R0) = 0):  0.8366
    
    . return list
    
    scalars:
                    r(ARD) =  .0219780135055854
                 r(ARD_se) =  .1062680466375581
                 r(ARR_ub) =  2.1779618162591
                 r(ARR_lb) =  .5324993443597063
               r(lnarr_se) =  .359333925662632
                  r(lnarr) =  .0741079400734589
                    r(ARR) =  1.076923042375102
                 r(ARR_se) =  .3869749844759268
                r(pvalue2) =  .8366058105881841
                 r(pvalue) =  .8361529635372051
                      r(N) =  74
                     r(R0) =  .2857143038936682
                  r(R0_se) =  .0763603562897145
                     r(R1) =  .3076923173992536
                  r(R1_se) =  .0739053024038421
    
    . 
    . di as txt "Estimated ARR is: " as res `r(ARR)'
    Estimated ARR is: 1.076923
    
    . di as txt "Lower bound of 95% CI of estimated ARR is: " as res `r(ARR_lb)'
    Lower bound of 95% CI of estimated ARR is: .53249934
    
    . di as txt "Upper bound of 95% CI of estimated ARR is: " as res `r(ARR_ub)'
    Upper bound of 95% CI of estimated ARR is: 2.1779618
    Using a similar approach, you can return any other intermediate results as part of -my_adjrr.ado-'s returned results.


    Comment


    • #3
      Thank you very much, works like a charm!

      Comment

      Working...
      X