Announcement

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

  • How does Stata -predict- after logit?

    Hi Statalist,

    After running logit, how does stata predict the probability of outcome? More importantly and specifically, how do I reproduce the results manually? Here is an example using -predict- and using my attempt at manual calculation (which is somehow wrong?) produces 2 different results. I tried manual calculation after a linear regression (eg. substituting -reg- for -logit- here) and the results of -predict- and manual calculation are the same. What am I doing wrong with my manual calculation after -logit-?

    Code:
    **Load example dataset
    sysuse auto, clear
    
    **Regression
    qui: logit foreign price
    
    **Stata predicted probability
    predict yhat
    
    **Calc predicted probability manually
    gen yhat2 = (price*_b[price] + _b[_cons])
    
    sum yhat yhat2
    Ultimately, I'm trying to calculate an elasticity manually and the results after running -logit- are not the same compared with using -margins, eyex- Again, the code below works for a linear regression but not logit. What am I missing? Do I need to somehow take into account the logit link? If so how?


    Code:
    ///First by automated method (eyex)
    **Load example dataset
    sysuse auto, clear
    
    **Regression
    qui: logit foreign price
    
    **Predict elasticities
    margins, eyex(price) nose
    
    ///Second by manual method
    
    **Load example dataset
    sysuse auto, clear
    
    **Regression
    qui: logit foreign price
    
    **Calc eyex
    *Denominator
    gen denom = (price*_b[price] + _b[_cons])
    
    *Numerator
    gen numer = (price*_b[price])
    
    
    *Eyex
    gen price_eyex = (numer/denom)
    
    sum price_eyex
    Thanks!
    Last edited by Karl Yesler; 15 Jul 2015, 17:57.
    I am using Stata SE x64 ver 13.1 with Win 7 x64 and with 8 GB of ram.

  • #2
    Yes; you need to take account of the link. Otherwise logit would just be regression and predictions outside [0,1] would be possible. Documented, e.g. at http://www.stata.com/manuals14/rlogit.pdf

    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . logit foreign price
    
    Iteration 0:   log likelihood =  -45.03321
    Iteration 1:   log likelihood = -44.947363
    Iteration 2:   log likelihood =  -44.94724
    
    Logistic regression                               Number of obs   =         74
                                                      LR chi2(1)      =       0.17
                                                      Prob > chi2     =     0.6784
    Log likelihood =  -44.94724                       Pseudo R2       =     0.0019
    
    ------------------------------------------------------------------------------
         foreign |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
           price |   .0000353   .0000844     0.42   0.676    -.0001301    .0002006
           _cons |  -1.079792    .587834    -1.84   0.066    -2.231926    .0723411
    ------------------------------------------------------------------------------
    
    . predict yhat
    (option pr assumed; Pr(foreign))
    
    . gen yhat2 = invlogit(_b[_cons] + _b[price] * price)
    
    . su yhat*
    
        Variable |       Obs        Mean    Std. Dev.       Min        Max
    -------------+--------------------------------------------------------
            yhat |        74    .2972973    .0224245   .2761411   .3731745
           yhat2 |        74    .2972973    .0224245   .2761411   .3731745
    .
    Last edited by Nick Cox; 15 Jul 2015, 18:04.

    Comment


    • #3
      Nick, thanks for your helpful comment. 2 follow up questions to get me where I am ultimately trying to go:

      1) To calculate the -margins, eyex(price)- manually, the following code does not seem to produce the same results, even after accounting for the link. Again, the code works for a linear regression but not for the logit. There must be something I am misunderstanding?

      Code:
      ///First by automated method (eyex)
      **Load example dataset
      sysuse auto, clear
      
      **Regression
      qui: logit foreign price 
      
      **Predict elasticities
      margins, eyex(price) nose
      
      ///Second by manual method
      
      **Load example dataset
      sysuse auto, clear
      
      **Regression
      qui: logit foreign price 
      
      **Calc eyex
      *Denominator
      gen denom = invlogit(price*_b[price] + _b[_cons]) 
      
      *Numerator
      gen numer = invlogit(price*_b[price])
      
      
      *Eyex
      gen price_eyex = (numer/denom)
      
      sum price_eyex
      Secondly, aside from http://www.stata.com/manuals13/dfunctions.pdf, is there a resource which would even more clearly describe how I would take the link into account in the predictions using other link and distributions? For example log poisson, or probit?

      Code:
      ///First by automated method (eyex)
      sysuse auto, clear
      
      glm rep78 price, family(poisson) link(log)
      
      **Predict elasticities
      margins, eyex(price) nose
      
      
      ///Second by manual method
      sysuse auto, clear
      
      glm rep78 price, family(poisson) link(log)
      
      
      *Denominator
      gen denom = ?????(price*_b[price] + _b[_cons]) 
      
      *Numerator
      gen numer = ?????(price*_b[price])
      
      
      *Eyex
      gen price_eyex = (numer/denom)
      
      sum price_eyex
      Many Thanks!
      I am using Stata SE x64 ver 13.1 with Win 7 x64 and with 8 GB of ram.

      Comment

      Working...
      X