Announcement

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

  • Exponentiate values in marginsplot

    Hi everyone,

    I am running a regression where the DV has been log transformed. It looks like this:
    reg lnone_mint_priceUSD $controls c.months_from_first_buy##c.max_rare_within

    Then I run margins and marginsplot:
    qui margins, at(c.months_from_first_buy =(6 36) c.max_rare_within =(1.48e-08 .0000487)) atmeans
    marginsplot, ytitle(““) xtitle("Months from first NFT purchase by NFT buyer") title (““)legend (size(vsmall))

    It all works well, and I get a graph. As expected the values on the Y axis are log transformed (see attached). I was wondering whether there is a way to automatically exponentiate the y-values so that I get the real (non log transformed values)? For example, in the attached example, I would like the value of 4.8 to be 121.5 (exp of 4.8), and the same for the other values on the y-axis.

    Thank you in advance,

    Cristiano

    fig1.gph

  • #2
    You would rather fit the model with poisson, see https://blog.stata.com/2011/08/22/us...tell-a-friend/. To answer your question, you could ask margins to exponentiate the predicted margins.

    Code:
    sysuse auto, clear
    gen lnprice= ln(price)
    reg lnprice turn c.mpg##c.weight
    margins,  at(weight =(1760 (10) 1780) mpg =(12/14)) atmeans
    margins,  at(weight =(1760 (10) 1780) mpg =(12/14)) atmeans expression(exp(predict(xb)))
    Res.:

    Code:
    . margins,  at(weight =(1760 (10) 1780) mpg =(12/14)) atmeans
    
    Adjusted predictions                            Number of obs     =         74
    Model VCE    : OLS
    
    Expression   : Linear prediction, predict()
    
    1._at        : turn            =    39.64865 (mean)
                   mpg             =          12
                   weight          =        1760
    
    2._at        : turn            =    39.64865 (mean)
                   mpg             =          12
                   weight          =        1770
    
    3._at        : turn            =    39.64865 (mean)
                   mpg             =          12
                   weight          =        1780
    
    4._at        : turn            =    39.64865 (mean)
                   mpg             =          13
                   weight          =        1760
    
    5._at        : turn            =    39.64865 (mean)
                   mpg             =          13
                   weight          =        1770
    
    6._at        : turn            =    39.64865 (mean)
                   mpg             =          13
                   weight          =        1780
    
    7._at        : turn            =    39.64865 (mean)
                   mpg             =          14
                   weight          =        1760
    
    8._at        : turn            =    39.64865 (mean)
                   mpg             =          14
                   weight          =        1770
    
    9._at        : turn            =    39.64865 (mean)
                   mpg             =          14
                   weight          =        1780
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
             _at |
              1  |   8.062888   .2003254    40.25   0.000      7.66325    8.462526
              2  |   8.069167   .1993311    40.48   0.000     7.671512    8.466821
              3  |   8.075446   .1983382    40.72   0.000     7.679772     8.47112
              4  |   8.065004   .1914181    42.13   0.000     7.683135    8.446873
              5  |   8.070997   .1904352    42.38   0.000     7.691089    8.450904
              6  |   8.076989   .1894536    42.63   0.000     7.699039    8.454939
              7  |    8.06712   .1827516    44.14   0.000     7.702541    8.431699
              8  |   8.072826   .1817781    44.41   0.000     7.710189    8.435464
              9  |   8.078533   .1808057    44.68   0.000     7.717835     8.43923
    ------------------------------------------------------------------------------
    
    . 
    . margins,  at(weight =(1760 (10) 1780) mpg =(12/14)) atmeans expression(exp(predict(xb)))
    
    Adjusted predictions                            Number of obs     =         74
    Model VCE    : OLS
    
    Expression   : exp(predict(xb))
    
    1._at        : turn            =    39.64865 (mean)
                   mpg             =          12
                   weight          =        1760
    
    2._at        : turn            =    39.64865 (mean)
                   mpg             =          12
                   weight          =        1770
    
    3._at        : turn            =    39.64865 (mean)
                   mpg             =          12
                   weight          =        1780
    
    4._at        : turn            =    39.64865 (mean)
                   mpg             =          13
                   weight          =        1760
    
    5._at        : turn            =    39.64865 (mean)
                   mpg             =          13
                   weight          =        1770
    
    6._at        : turn            =    39.64865 (mean)
                   mpg             =          13
                   weight          =        1780
    
    7._at        : turn            =    39.64865 (mean)
                   mpg             =          14
                   weight          =        1760
    
    8._at        : turn            =    39.64865 (mean)
                   mpg             =          14
                   weight          =        1770
    
    9._at        : turn            =    39.64865 (mean)
                   mpg             =          14
                   weight          =        1780
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
             _at |
              1  |   3174.444    635.922     4.99   0.000      1928.06    4420.829
              2  |   3194.439   636.7509     5.02   0.000      1946.43    4442.448
              3  |    3214.56     637.57     5.04   0.000     1964.945    4464.174
              4  |   3181.169   608.9332     5.22   0.000     1987.682    4374.656
              5  |   3200.289   609.4478     5.25   0.000     2005.794    4394.785
              6  |   3219.525   609.9507     5.28   0.000     2024.043    4415.006
              7  |   3187.907    582.595     5.47   0.000     2046.042    4329.773
              8  |    3206.15   582.8079     5.50   0.000     2063.868    4348.433
              9  |   3224.498   583.0076     5.53   0.000     2081.824    4367.172
    ------------------------------------------------------------------------------

    Comment


    • #3
      Originally posted by Andrew Musau View Post
      You would rather fit the model with poisson, see https://blog.stata.com/2011/08/22/us...tell-a-friend/. To answer your question, you could ask margins to exponentiate the predicted margins.

      Code:
      sysuse auto, clear
      gen lnprice= ln(price)
      reg lnprice turn c.mpg##c.weight
      margins, at(weight =(1760 (10) 1780) mpg =(12/14)) atmeans
      margins, at(weight =(1760 (10) 1780) mpg =(12/14)) atmeans expression(exp(predict(xb)))
      Res.:

      Code:
      . margins, at(weight =(1760 (10) 1780) mpg =(12/14)) atmeans
      
      Adjusted predictions Number of obs = 74
      Model VCE : OLS
      
      Expression : Linear prediction, predict()
      
      1._at : turn = 39.64865 (mean)
      mpg = 12
      weight = 1760
      
      2._at : turn = 39.64865 (mean)
      mpg = 12
      weight = 1770
      
      3._at : turn = 39.64865 (mean)
      mpg = 12
      weight = 1780
      
      4._at : turn = 39.64865 (mean)
      mpg = 13
      weight = 1760
      
      5._at : turn = 39.64865 (mean)
      mpg = 13
      weight = 1770
      
      6._at : turn = 39.64865 (mean)
      mpg = 13
      weight = 1780
      
      7._at : turn = 39.64865 (mean)
      mpg = 14
      weight = 1760
      
      8._at : turn = 39.64865 (mean)
      mpg = 14
      weight = 1770
      
      9._at : turn = 39.64865 (mean)
      mpg = 14
      weight = 1780
      
      ------------------------------------------------------------------------------
      | Delta-method
      | Margin Std. Err. t P>|t| [95% Conf. Interval]
      -------------+----------------------------------------------------------------
      _at |
      1 | 8.062888 .2003254 40.25 0.000 7.66325 8.462526
      2 | 8.069167 .1993311 40.48 0.000 7.671512 8.466821
      3 | 8.075446 .1983382 40.72 0.000 7.679772 8.47112
      4 | 8.065004 .1914181 42.13 0.000 7.683135 8.446873
      5 | 8.070997 .1904352 42.38 0.000 7.691089 8.450904
      6 | 8.076989 .1894536 42.63 0.000 7.699039 8.454939
      7 | 8.06712 .1827516 44.14 0.000 7.702541 8.431699
      8 | 8.072826 .1817781 44.41 0.000 7.710189 8.435464
      9 | 8.078533 .1808057 44.68 0.000 7.717835 8.43923
      ------------------------------------------------------------------------------
      
      .
      . margins, at(weight =(1760 (10) 1780) mpg =(12/14)) atmeans expression(exp(predict(xb)))
      
      Adjusted predictions Number of obs = 74
      Model VCE : OLS
      
      Expression : exp(predict(xb))
      
      1._at : turn = 39.64865 (mean)
      mpg = 12
      weight = 1760
      
      2._at : turn = 39.64865 (mean)
      mpg = 12
      weight = 1770
      
      3._at : turn = 39.64865 (mean)
      mpg = 12
      weight = 1780
      
      4._at : turn = 39.64865 (mean)
      mpg = 13
      weight = 1760
      
      5._at : turn = 39.64865 (mean)
      mpg = 13
      weight = 1770
      
      6._at : turn = 39.64865 (mean)
      mpg = 13
      weight = 1780
      
      7._at : turn = 39.64865 (mean)
      mpg = 14
      weight = 1760
      
      8._at : turn = 39.64865 (mean)
      mpg = 14
      weight = 1770
      
      9._at : turn = 39.64865 (mean)
      mpg = 14
      weight = 1780
      
      ------------------------------------------------------------------------------
      | Delta-method
      | Margin Std. Err. z P>|z| [95% Conf. Interval]
      -------------+----------------------------------------------------------------
      _at |
      1 | 3174.444 635.922 4.99 0.000 1928.06 4420.829
      2 | 3194.439 636.7509 5.02 0.000 1946.43 4442.448
      3 | 3214.56 637.57 5.04 0.000 1964.945 4464.174
      4 | 3181.169 608.9332 5.22 0.000 1987.682 4374.656
      5 | 3200.289 609.4478 5.25 0.000 2005.794 4394.785
      6 | 3219.525 609.9507 5.28 0.000 2024.043 4415.006
      7 | 3187.907 582.595 5.47 0.000 2046.042 4329.773
      8 | 3206.15 582.8079 5.50 0.000 2063.868 4348.433
      9 | 3224.498 583.0076 5.53 0.000 2081.824 4367.172
      ------------------------------------------------------------------------------
      That works perfectly, thank you!

      Comment

      Working...
      X