Announcement

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

  • Is it possible to rename margins?

    I have a regression where the outcome is transformed using an inverse hyperbolic sine function. I want to calculate the marginal effect on the original scale, which I am doing it like this:
    Code:
    sysuse auto, clear
    gen asinh_price =asinh(price)
    reg asinh_price mpg, robust
    margins, expression(.5*(exp(predict(xb)) + exp(-1*predict(xb)))*_b[mpg])
    This expression is from p. 6 of this paper.

    Unfortunately, this expression is labeled as a constant by margins, so it does not line up nicely with the other mpg coefficients when I try to build a table of them. I know I can use the rename() in Ben Jann's esttab, but that is not feasible for other reasons that would be a distraction to explain. Some Stata commands, like nlcom, allow you to rename the "coefficients". Is there something like that for margins?

  • #2
    To clarify, you'd like "_cons" to say what instead... "mpg"?

    How are you building your tables?

    Comment


    • #3
      mpg would be perfect. I am actually using a home-brewed routine that stacks some coefficients from different specifications on top of each other in a matrix, and then I am combining these into a table with estout. In other words, each column is different outcome and the rows are the same coefficient from varying specifications.

      Comment


      • #4
        Here's a kludge that really doesn't solve the problem.

        Code:
        sysuse auto, clear
        gen asinh_price =asinh(price)
        gen name = 1
        reg asinh_price i.name mpg, robust nocons
        
        margins name ,expression(.5*(exp(predict(xb)) + exp(-1*predict(xb)))*_b[mpg])
        resulting in:
        Code:
                     |            Delta-method
                     |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
        -------------+----------------------------------------------------------------
              1.name |  -191.5165   47.88523    -4.00   0.000    -285.3699    -97.6632
        ------------------------------------------------------------------------------
        Steve Samuels
        Statistical Consulting
        [email protected]

        Stata 14.2

        Comment


        • #5
          Adding onto Steve's approach (which was very creative, btw), this is the closest I could get to a solution.
          Code:
          sysuse auto, clear
          gen asinh_price =asinh(price)
          
          //Create & label proxy variable "_"
          ge _ = 1
          label define f_ 1 "mpg"
          label values _ f_
          
          reg asinh_price i._ mpg, robust nocons
          margins _ ,expression(.5*(exp(predict(xb)) + exp(-1*predict(xb)))*_b[mpg])
          And the output:
          Code:
          ------------------------------------------------------------------------------
                       |            Delta-method
                       |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
          -------------+----------------------------------------------------------------
                     _ |
                  mpg  |  -191.5165   47.88523    -4.00   0.000    -285.3699    -97.6632
          ------------------------------------------------------------------------------
          Dimitriy, if you're still having issues, perhaps you can tweak your homebrewed program to ignore "_" (or whichever other derp you'd rather ignore).

          Comment


          • #6
            Here's a solution that involves an eclass program which renames the e(b) matrix. This was suggested by Joy Wang of Stata.


            Code:
            cscript
            program mymargins, eclass
            qui margins, expression(.5*(exp(predict(xb)) + ///
            exp(-1*predict(xb)))*_b[mpg]) post
            
            matrix b = e(b)
            matrix V = e(V)
            matrix colnames b = mpg
            matrix rownames V = mpg
            matrix colnames V = mpg
            ereturn post b V
            end
            
            estimates clear
            sysuse auto, clear
            gen asinh_price =asinh(price)
            reg asinh_price mpg, robust
            eststo, title(elasticity): margins, dydx(mpg) post
            reg asinh_price mpg, robust
            eststo:mymargins
            esttab, mtitle

            Comment


            • #7
              If you're willing, why not customize the formatting of your output? It might produce clearer output.


              Here's a quick program that only reports varname ("mpg") and the value of "Margin":

              Program "customtable":
              Code:
              capture drop program customtable
              
              program customtable
                  args consvar 
                  
                  // save "Margin" estimate
                  matrix b = r(b)
                  svmat b
                  
                  // report varname and results
                  di as text "`consvar'", as result b
                  
                  drop b
              end
              Input:
              Code:
              sysuse auto, clear
              gen asinh_price =asinh(price)
              reg asinh_price mpg length, robust
              margins, expression(.5*(exp(predict(xb)) + exp(-1*predict(xb)))*_b[mpg])
              
              customtable mpg
              Output:
              Code:
              mpg -133.95311



              If you'd rather your output resemble a table, write some borders into the program:

              Program "customtable" (with added borders):
              Code:
              capture drop program customtable
              
              program customtable    
                  args consvar 
                  
                  // save "Margin" estimate
                  matrix b = r(b)
                  svmat b
                  
                  // build table, adjusting borders for varname length
                  local l = length("`consvar'")
                  
                  if `l' <= 12 {
                      di as text "{hline 13}" "{c TT}" "{hline 13}"
                      di as text %18s "{c |}" %~14s "Margin"
                      di as text "{hline 13}" "{c +}" "{hline 13}"
                      di as text %12s "`consvar'", "{c |}", as result b // reports varname and results
                      di as text "{hline 13}" "{c BT}" "{hline 13}"
                  }
                  
                  else {
                      local l1 = `l' + 1
                      local l6 = `l' + 6
                      
                      di as text "{hline `l1'}" "{c TT}" "{hline 13}"
                      di as text %`l6's "{c |}" %~14s "Margin"
                      di as text "{hline `l1'}" "{c +}" "{hline 13}"
                      di as text %`l's "`consvar'", "{c |}", as result b // reports varname and results
                      di as text "{hline `l1'}" "{c BT}" "{hline 13}"
                  }
                  
                  drop b
              end
              Input (same as before):
              Code:
              sysuse auto, clear
              gen asinh_price =asinh(price)
              reg asinh_price mpg length, robust
              margins, expression(.5*(exp(predict(xb)) + exp(-1*predict(xb)))*_b[mpg])
              
              customtable mpg
              Output:
              Code:
              ---------------------------
                           |    Margin    
              -------------+-------------
                       mpg | -133.95311
              ---------------------------

              Comment

              Working...
              X