Announcement

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

  • Unable to refer to sreturned varlist outside of program

    I wrote an sclass program which returns two variable lists in separate macros:

    Code:
    capture program drop model_select
    program define model_select, sclass
    
        syntax, cenyear(integer) filename(string)
    
        ** Read
        use "${DATA}/dta/`filename'", clear
        
        ** FE ID's
        egen state_year = group(state_code_`cenyear' year)
        egen distid = group(c_code_`cenyear')
        
        ** Controls
        local weather temperature_mean rainfall_mean
        
        ** Residualize
        reghdfe capgw_dist_cum capgw_state_cum_pred, ///
            a(c_code_`cenyear'_num month state_code_`cenyear'_num#year) ///
            vce(cl state_code_`cenyear'_num#year) residuals(resid_capgw_dist_cum)
                
        ** Standard LASSO (K-Fold CV)
        lassoregress resid_capgw_dist_cum psc_*
        sreturn local lasso_ivs "`e(varlist_nonzero)'"
    
        ** RLASSO (Heteroskedasticity-Robust)
        rlasso capgw_dist_cum psc_* ///
            i.distid i.month i.state_year capgw_state_cum_pred `weather', robust ///
            partial(i.distid i.month i.state_year capgw_state_cum_pred) ///
            pnotpen(`weather') cluster(state_year) supscore
        sreturn local rlasso_ivs "`e(selected)'"
    
    end
    Running sreturn list after the program ends confirms that s(lasso_ivs) and s(rlasso_ivs) hold the correct variables. However, later on I want to run two IV regressions with all variables in each s() as instruments.

    Code:
    foreach model in lasso_ivs rlasso_ivs  {
        reghdfe so2_ln capgw_state_cum_pred ///
            (capgw_dist_cum =  " `s(`model')' "), ///
            a(c_code_2011_num month state_code_2011_num#year) vce(cl state_code_2011_num#year old
    I get an error varlist required r(100). I believe the issue is with the quotations -- the varlist saved in the s() from the program is not being inserted. I've tried several variations of quotes including "s(`model')", `s(`model')', but nothing seems to work. Any ideas? Thanks




  • #2
    Hi Raahil,

    There should not be any quotation marks within the s() reference, only outside it. So, e.g. the following are OK, depending on the exact context:
    Code:
    s(model)
    `s(model)'
    "`s(model)'"
    ...but the following will never work:
    Code:
    s(`model')
    `s(`model')'
    "`s(`model')'"
    "s(`model')"
    Hope that helps,

    David.

    Comment


    • #3
      Thanks David. Still having a bit of trouble. I've replicated the important parts of the analysis on the auto dataset for easier troubleshooting. I found that sclass was not returning both macros specified after sreturn, however rclass was able to do this.

      Code:
          sysuse auto.dta, clear
          
          capture program drop model_select
              program define model_select, rclass
                  
              lassoregress mpg rep78 headroom trunk weight length turn displacement gear_ratio foreign
              return local lasso_ivs "`e(varlist_nonzero)'"
              
              rlasso price mpg rep78 headroom trunk weight length turn displacement gear_ratio foreign
              return local rlasso_ivs "`e(selected)'"
          end
          
          model_select
          return list
          di "`r(lasso_ivs)'"
          di "`r(rlasso_ivs)'"
      This returns:

      macros:
      r(rlasso_ivs) : "weight displacement"
      r(lasso_ivs) : "rep78 trunk weight foreign"

      Which is great. I want to then run two IV regressions with the varlist stored in each r() as instruments. This works using the first varlist r(lasso_ivs) but gives me a varlist required error when I use the varlist r(rlasso_ivs). Upon further scrutiny, if I display the contents of r(lasso_ivs) and r(rlasso_ivs) after the first regression, they both come back empty.

      Code:
          reghdfe price (mpg = "`r(lasso_ivs)'"), a(foreign) old // This works fine
      
          di "`r(lasso_ivs)'"  // nothing displayed
          di "`r(rlasso_ivs)'" // nothing displayed
          
          reghdfe price (mpg = "`r(rlasso_ivs)'"), a(foreign) old // varlist required error
      This is where the error comes from because the varlist of instruments in the second regression is empty. Why does stata erase the contents of r() after the first regression? How can I prevent this so that I can keep the contents of r(lasso_ivs) and r(rlasso_ivs) throughout my do-file? Thanks

      Comment


      • #4
        Why does stata erase the contents of r() after the first regression?
        The regression program e(class) run r(class) sub programs, programs or mata programs returning in r(). -return list- will show what is left in r().

        To keep the macros make copies of macros after running your program model_select
        Code:
            model_select
            
            local lasso_ivs `r(lasso_ivs)'  
            local rlasso_ivs `r(rlasso_ivs)'
            
            reghdfe price (mpg = "`lasso_ivs'" ), a(foreign) old  
            reghdfe price (mpg = "`rlasso_ivs'"), a(foreign) old
        Last edited by Bjarte Aagnes; 05 Feb 2019, 15:00.

        Comment


        • #5
          To follow on from Bjarte Aagnes 's response:

          Of the three classes of Stata programs/returned results, r() e() and s() ... r() is the most "flaky", and is very easily overwritten. For instance, as Bjarte says, e() class programs might call subroutines which use r(), and therefore will erase any previous r() results which may have been of interest to you. Hence, it is important to store any important r() results in user-defined macros immediately after the program that created them. e() and s() class results typically stick around for a little longer, although it may still be wise to store them under a user-defined name as soon as possible if you wish to refer to them later in your work.

          For more information, see help return, help stored results, and [U] 18.10 Storing results in the manuals.

          David.

          Comment

          Working...
          X