Announcement

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

  • Replicate margins results in a coefplot graph

    Dear all,

    I am a new user of Stata, my back up is R, but now I need make some coefplot (SSC) graph. I wana replicate margins value after a logit regression. I did read that i can use post in a margins command but for others consideration I wanna use transform in a coefplot (SSC):

    Code:
    sysuse nlsw88, clear
    
    logit union grade i.south
    margins i.south
    
    
    Predictive margins                                       Number of obs = 1,876
    Model VCE: OIM
    
    Expression: Pr(union), predict()
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |     Margin   std. err.      z    P>|z|     [95% conf. interval]
    -------------+----------------------------------------------------------------
           south |
      Not south  |    .297394   .0138465    21.48   0.000     .2702553    .3245327
          South  |   .1732535   .0134751    12.86   0.000     .1468428    .1996643
    ------------------------------------------------------------------------------
    I did read that I should use :

    margins i.south, post

    But I would like use some like:

    Code:
    coefplot (., transform(* = (exp(@)/( 1+exp(@))))), drop(_cons) mlabel baselevels
    
    coefplot (., transform(* = invlogit(@))), drop(_cons) mlabel citype(logit) baselevels
    Values not coincide with margins:

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	35.8 KB
ID:	1784958

    Click image for larger version

Name:	Graph2.png
Views:	1
Size:	31.7 KB
ID:	1784959

    Thanks
    Attached Files

  • #2
    In general, there is no way to recover predictive margins using only the coefficient vector without the data (unless the model contains no other covariates, or you are willing to evaluate at arbitrary fixed values such as means). This is precisely why the margins command exists: it uses the estimation sample to generate predictions for each observation and then averages them appropriately (and computes standard errors via the delta method). For details, see the PDF manual entry of margins.

    Comment


    • #3
      Thanks Andrew Musau, My idea is not just a whim, here I can explain why i post it:

      My idea is use (, if(@ll>0 | @ul<0)) to get only probabilities significant.

      But if I use margins I can not select coefficients significants, because in margins table all coefficients are significants (except age==46), but in logit table only age==36 and 40 are significants.

      This is the reason to my idea to transforms coefficient to get some coefficients.

      Code:
      
      ------------------------------------------------------------------------------
             union | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]
      -------------+----------------------------------------------------------------
               age |
               34  |          0  (base)
               35  |  -.6802438   .3717162    -1.83   0.067    -1.408794    .0483066
               36  |  -.8699093   .3781728    -2.30   0.021    -1.611114   -.1287042
               37  |  -.2789024   .3680752    -0.76   0.449    -1.000317    .4425117
               38  |  -.4297407   .3715141    -1.16   0.247    -1.157895    .2984135
               39  |  -.2578905   .3670603    -0.70   0.482    -.9773154    .4615344
               40  |  -.7810485   .3801542    -2.05   0.040    -1.526137     -.03596
               41  |  -.5280255   .3713805    -1.42   0.155    -1.255918    .1998669
               42  |  -.4597705    .386396    -1.19   0.234    -1.217093    .2975518
               43  |  -.3713595   .3808608    -0.98   0.330    -1.117833    .3751139
               44  |  -.5572821   .3867026    -1.44   0.150    -1.315205    .2006411
               45  |   -.501552   .4453043    -1.13   0.260    -1.374332    .3712283
               46  |   .6190392   1.452546     0.43   0.670    -2.227899    3.465977
                   |
             _cons |  -.6190392   .3314968    -1.87   0.062    -1.268761    .0306826
      ------------------------------------------------------------------------------


      Code:
      sysuse nlsw88, clear
      
      logit union i.age
      
      margins i.age, post
      
      
      Adjusted predictions                                     Number of obs = 1,878
      Model VCE: OIM
      
      Expression: Pr(union), predict()
      
      ------------------------------------------------------------------------------
                   |            Delta-method
                   |     Margin   std. err.      z    P>|z|     [95% conf. interval]
      -------------+----------------------------------------------------------------
               age |
               34  |        .35   .0754155     4.64   0.000     .2021883    .4978117
               35  |   .2142857   .0283152     7.57   0.000      .158789    .2697824
               36  |   .1840796   .0273356     6.73   0.000     .1305028    .2376564
               37  |   .2894737   .0329017     8.80   0.000     .2249876    .3539598
               38  |   .2594595   .0322273     8.05   0.000     .1962952    .3226237
               39  |   .2938144   .0327036     8.98   0.000     .2297166    .3579122
               40  |   .1978022   .0295271     6.70   0.000     .1399302    .2556742
               41  |   .2410256   .0306287     7.87   0.000     .1809946    .3010567
               42  |   .2537313   .0375909     6.75   0.000     .1800546    .3274081
               43  |   .2708333   .0370325     7.31   0.000     .1982509    .3434157
               44  |   .2357143   .0358721     6.57   0.000     .1654063    .3060223
               45  |   .2459016   .0551353     4.46   0.000     .1378384    .3539649
               46  |         .5   .3535534     1.41   0.157    -.1929519    1.192952
      ------------------------------------------------------------------------------
      
      
      coefplot (, if(@ll>0 | @ul<0)), drop(_cons) mlabel citype(logit) baselevels
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	82.4 KB
ID:	1784966

      Last edited by Federico Requena; 18 Feb 2026, 15:04.

      Comment


      • #4
        The margins table is not directly testing whether each coefficient is significant. It's testing whether the predicted probability is significantly different from 0. If your goal is to highlight significant logit coefficients, you need to work with the logit output, not margins. If you want to highlight significant predicted probabilities, margins is fine - but then it is not testing coefficient significance, it's testing predicted probabilities. So I cannot follow exactly what you are after. From a coding perspective, if I want to focus on interpreting marginal effects of statistically significant coefficients (at the 5% level), I'd filter them out as follows:

        Code:
        sysuse nlsw88, clear
        logit union i.age
        
        local list
        levelsof age if e(sample), local(ages)
        foreach a of local ages{
            if abs(`=_b[`a'.age]/_se[`a'.age]')>=1.96 & `=_b[`a'.age]/_se[`a'.age]'<. {
                local list `list' `a'.age
            }
        }
        margins `list'
        Res.:

        Code:
        . logit union i.age
        
        Iteration 0:  Log likelihood = -1046.6242  
        Iteration 1:  Log likelihood = -1038.8365  
        Iteration 2:  Log likelihood =  -1038.771  
        Iteration 3:  Log likelihood =  -1038.771  
        
        Logistic regression                                     Number of obs =  1,878
                                                                LR chi2(12)   =  15.71
                                                                Prob > chi2   = 0.2051
        Log likelihood = -1038.771                              Pseudo R2     = 0.0075
        
        ------------------------------------------------------------------------------
               union | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]
        -------------+----------------------------------------------------------------
                 age |
                 35  |  -.6802438   .3717162    -1.83   0.067    -1.408794    .0483066
                 36  |  -.8699093   .3781728    -2.30   0.021    -1.611114   -.1287042
                 37  |  -.2789024   .3680752    -0.76   0.449    -1.000317    .4425117
                 38  |  -.4297407   .3715141    -1.16   0.247    -1.157895    .2984135
                 39  |  -.2578905   .3670603    -0.70   0.482    -.9773154    .4615344
                 40  |  -.7810485   .3801542    -2.05   0.040    -1.526137     -.03596
                 41  |  -.5280255   .3713805    -1.42   0.155    -1.255918    .1998669
                 42  |  -.4597705    .386396    -1.19   0.234    -1.217093    .2975518
                 43  |  -.3713595   .3808608    -0.98   0.330    -1.117833    .3751139
                 44  |  -.5572821   .3867026    -1.44   0.150    -1.315205    .2006411
                 45  |   -.501552   .4453043    -1.13   0.260    -1.374332    .3712283
                 46  |   .6190392   1.452546     0.43   0.670    -2.227899    3.465977
                     |
               _cons |  -.6190392   .3314968    -1.87   0.062    -1.268761    .0306826
        ------------------------------------------------------------------------------
        
        .
        .
        .
        . local list
        
        .
        . levelsof age if e(sample), local(ages)
        34 35 36 37 38 39 40 41 42 43 44 45 46
        
        .
        . foreach a of local ages{
          2.
        .     if abs(`=_b[`a'.age]/_se[`a'.age]')>=1.96 & `=_b[`a'.age]/_se[`a'.age]'<. {
          3.
        .         local list `list' `a'.age
          4.
        .     }
          5.
        . }
        
        .
        . margins `list'
        
        Adjusted predictions                                     Number of obs = 1,878
        Model VCE: OIM
        
        Expression: Pr(union), predict()
        
        ------------------------------------------------------------------------------
                     |            Delta-method
                     |     Margin   std. err.      z    P>|z|     [95% conf. interval]
        -------------+----------------------------------------------------------------
                 age |
                 36  |   .1840796   .0273356     6.73   0.000     .1305028    .2376564
                 40  |   .1978022   .0295271     6.70   0.000     .1399302    .2556742
        ------------------------------------------------------------------------------
        Last edited by Andrew Musau; 18 Feb 2026, 15:34.

        Comment


        • #5
          Good! Thanks Andrew Musau !

          Comment

          Working...
          X