Announcement

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

  • Calculating marginal effects at different values for a squared term in a logit

    I'm having trouble calculating marginal effects at different values of a variable var1 that I have included as a continuous independent variable in a logit regression along with its square as c.var1##c.var1.

    For example, I've written my command as:
    xi: logit depvar c.var1##c.var1 var2 var3 i.year, vce(bootstrap)

    When I attempt to have STATA calculate marginal effects of var1 over a range of values of var1, STATA tells me it is inestimable. However, STATA will calculate marginal effects for var2 and var3, indicating that it is having an issue because of the squared term. The command I have used for calculating marginal effects is:
    margins, dydx(var1) at(var1==(0(10)100))

    Can you tell me why STATA is unable to do marginal effects for the squared term and how I might get it to work?

    Many thanks.

  • #2
    Don't use the xi: prefix. It isn't necessary and it confuses margins.

    For more on margins, and margins for continuous variables, see

    http://www3.nd.edu/~rwilliam/stats3/Margins01.pdf

    http://www3.nd.edu/~rwilliam/stats3/Margins03.pdf

    Also, for a good discussion of why you don't get marginal effects for interactions and squared terms,

    http://www.stata.com/statalist/archi.../msg00263.html
    -------------------------------------------
    Richard Williams, Notre Dame Dept of Sociology
    StataNow Version: 19.5 MP (2 processor)

    EMAIL: [email protected]
    WWW: https://www3.nd.edu/~rwilliam

    Comment


    • #3
      I removed the xi:, and unfortunately I'm having the same problem (inestimable). STATA will calculate marginal effects for variables not included in an interaction but not for the variable that is interacted.

      I may have misspoken before. I'm not trying to calculate the marginal effect for the interaction term (c.var1##c.var1). Rather, I need it for var1 itself. Is that possible in STATA, or do I have to do it manually?

      Thanks for the help.

      Comment


      • #4
        Can you show us the output?

        It is not the squared term per se that is causing the problems, as this little example shows

        Code:
        . sysuse nlsw88
        (NLSW, 1988 extract)
        
        . logit union c.age##c.age c.wage ,vce(boot)
        (running logit on estimation sample)
        
        Bootstrap replications (50)
        ----+--- 1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5 
        ..................................................    50
        
        Logistic regression                             Number of obs      =      1878
                                                        Replications       =        50
                                                        Wald chi2(3)       =     46.87
                                                        Prob > chi2        =    0.0000
        Log likelihood = -1026.2595                     Pseudo R2          =    0.0195
        
        ------------------------------------------------------------------------------
                     |   Observed   Bootstrap                         Normal-based
               union |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
        -------------+----------------------------------------------------------------
                 age |   .0830317   .5094544     0.16   0.871    -.9154805    1.081544
                     |
         c.age#c.age |  -.0009451   .0064221    -0.15   0.883    -.0135322     .011642
                     |
                wage |   .0779428   .0117173     6.65   0.000     .0549773    .1009083
               _cons |  -3.530711   10.04165    -0.35   0.725    -23.21198    16.15056
        ------------------------------------------------------------------------------
        
        . su age
        
            Variable |       Obs        Mean    Std. Dev.       Min        Max
        -------------+--------------------------------------------------------
                 age |      2246    39.15316    3.060002         34         46
        
        . sysuse nlsw88
        (NLSW, 1988 extract)
        
        . logit union c.age##c.age c.wage ,vce(boot)
        (running logit on estimation sample)
        
        Bootstrap replications (50)
        ----+--- 1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5 
        ..................................................    50
        
        Logistic regression                             Number of obs      =      1878
                                                        Replications       =        50
                                                        Wald chi2(3)       =     71.17
                                                        Prob > chi2        =    0.0000
        Log likelihood = -1026.2595                     Pseudo R2          =    0.0195
        
        ------------------------------------------------------------------------------
                     |   Observed   Bootstrap                         Normal-based
               union |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
        -------------+----------------------------------------------------------------
                 age |   .0830317   .5751146     0.14   0.885    -1.044172    1.210236
                     |
         c.age#c.age |  -.0009451   .0072629    -0.13   0.896    -.0151801    .0132899
                     |
                wage |   .0779428   .0096715     8.06   0.000      .058987    .0968986
               _cons |  -3.530711   11.31371    -0.31   0.755    -25.70518    18.64376
        ------------------------------------------------------------------------------
        
        . margins ,dydx(age) at(age = (30(5)50))
        
        Average marginal effects                          Number of obs   =       1878
        Model VCE    : Bootstrap
        
        Expression   : Pr(union), predict()
        dy/dx w.r.t. : age
        
        1._at        : age             =          30
        
        2._at        : age             =          35
        
        3._at        : age             =          40
        
        4._at        : age             =          45
        
        5._at        : age             =          50
        
        ------------------------------------------------------------------------------
                     |            Delta-method
                     |      dy/dx   Std. Err.      z    P>|z|     [95% Conf. Interval]
        -------------+----------------------------------------------------------------
        age          |
                 _at |
                  1  |   .0043974   .0219566     0.20   0.841    -.0386366    .0474315
                  2  |   .0029835   .0119592     0.25   0.803     -.020456    .0264231
                  3  |   .0013528   .0031051     0.44   0.663    -.0047332    .0074388
                  4  |  -.0003719   .0147131    -0.03   0.980    -.0292091    .0284653
                  5  |   -.002071    .026725    -0.08   0.938    -.0544511     .050309
        ------------------------------------------------------------------------------
        
        .
        Best
        Danie


        Please read the FAQs, especially 6. and 18. And please use the code environment to post code and results - this makes it much easier to read.

        Comment


        • #5
          I think we need to see your exact code and output. This example seems similar to me, and it runs fine:

          Code:
          webuse nhanes2f, clear
          logit diabetes weight i.female c.age##c.age, vce(bootstrap)
          margins, dydx(age) at(age=(20 (5) 70)) vsquish
          When entering the code and output, please do it like I just did. When typing the message, click on the underlined A on the upper right, then click #. Copy and paste your code and output between the two code tags.

          Also, does it work if you don't specify a range?
          -------------------------------------------
          Richard Williams, Notre Dame Dept of Sociology
          StataNow Version: 19.5 MP (2 processor)

          EMAIL: [email protected]
          WWW: https://www3.nd.edu/~rwilliam

          Comment


          • #6
            I appreciate your help very much. Here is the code and the output.

            Courtney Yarbrough


            Code:
            use "`datadir'/DTCfinal.dta", clear;
            
            ****rx7 IS REFERENCE****;
            
            
            ********INSTRUMENTS*******;
            global instr osteoporosis_iv osteoporosis_iv2 offpatent mark_share unitprice drugcount combsales
            enroll rank unemp rx1 rx2 rx3 rx4 rx5 rx6 rx8 rx9 rx10 rx11 rx12 ; 
            
            ********INDEPEDNENT VARS*******;
            global indep offpatent mark_share unitprice 
            drugcount combsales enroll rank unemp rx1 rx2 rx3 rx4 rx5 rx6 rx8 rx9 rx10 rx11 rx12;
            
            
            ****** RUN FIRST STAGE OF 2SRI MODEL ******;
            ****** RUN FIRST STAGE OF 2SRI MODEL ******;
            ****** RUN FIRST STAGE OF 2SRI MODEL ******;
            xi: reg dtctv $instr i.year i.state_id;
            predict ehat1, r;
            est store iv_coef;
            label var ehat1 "IV Residual for Own DTC";
            est save "`studydir'/iv_coef.ster", replace;
            
            xi: reg pct_dtctv $instr  i.year i.state_id;
            predict ehat2, r;
            est store iv_coef2;
            label var ehat2 "IV Residual for Percent Own DTC";
            est save "`studydir'/iv_coef2.ster", replace;
            
            
            ****** RUN SECOND STAGE OF 2SRI MODEL ******;
            ****** RUN SECOND STAGE OF 2SRI MODEL ******;
            ****** RUN SECOND STAGE OF 2SRI MODEL ******;
            
            
            ********YEAR AND STATE FIXED EFFECTS*******;
            logit tier3 c.dtctv##c.dtctv c.comp_dtctv##c.comp_dtctv pct_dtctv 
            $indep ehat1 ehat2 i.year i.state_id, vce(bootstrap);
            est store tier3_coef_endog;
            est save "`studydir'/tier3_coef_endog.ster", replace;
            
            margins, dydx(dtctv comp_dtctv pct_dtctv offpatent mark_share unitprice drugcount enroll rank unemp) post;
            est store tier3_me_endog;
            est save "`studydir'/tier3_me_endog.ster", replace;
            
            
            
            estimates restore tier3_coef_endog;
            margins, dydx(dtctv) at(dtctv==(0(5)65));
            marginsplot, 
            title("Marginal Effect of Lagged TV DTC") 
            subtitle("by DTC spending level")
            ytitle("Change in the Pr(Drug is on Tier 3)", size (medsmall) )
            xtitle("Lagged Statin TV DTC - in millions of dollars", size(medsmall))
            ylabel(, labsize(small))
            xlabel(, labsize(small))
            yline(0)
            saving("`resultsdir'/ME for pre-DTC across levels.gph", replace);


            Code:
            .
            
            ********YEAR AND STATE FIXED EFFECTS*******;
            . logit tier3 c.dtctv##c.dtctv c.comp_dtctv##c.comp_dtctv pct_dtctv 
            > $indep ehat1 ehat2 i.year i.state_id, vce(bootstrap);
            note: 2009.year omitted because of collinearity
            (running logit on estimation sample)
            
            Bootstrap replications (50)
            ----+--- 1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5 
            ..................................................    50
            
            Logistic regression                             Number of obs      =     92294
                                                            Replications       =        50
                                                            Wald chi2(49)      =         .
                                                            Prob > chi2        =         .
            Log likelihood = -46236.562                     Pseudo R2          =    0.2772
            
            -------------------------------------------------------------------------------
                          |   Observed   Bootstrap                         Normal-based
                    tier3 |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
            --------------+----------------------------------------------------------------
                    dtctv |  -.3240426   .0302156   -10.72   0.000     -.383264   -.2648211
                          |
                  c.dtctv#|
                  c.dtctv |   .0006589    .000126     5.23   0.000      .000412    .0009057
                          |
               comp_dtctv |   .1688027   .0296938     5.68   0.000     .1106039    .2270016
                          |
             c.comp_dtctv#|
             c.comp_dtctv |  -.0004517   .0001133    -3.99   0.000    -.0006736   -.0002297
                          |
                pct_dtctv |     .39543   .0343691    11.51   0.000     .3280679    .4627921
                offpatent |    2.34081   .2140492    10.94   0.000     1.921281    2.760338
               mark_share |  -.0323753   .0083993    -3.85   0.000    -.0488375    -.015913
                unitprice |   .2216178    .034591     6.41   0.000     .1538207    .2894149
                drugcount |  -.2445509   .0126496   -19.33   0.000    -.2693436   -.2197581
                combsales |   .0001148   .0000275     4.17   0.000     .0000609    .0001687
                   enroll |   .0004406    .000313     1.41   0.159    -.0001728     .001054
                     rank |   .0046341   .0002837    16.33   0.000      .004078    .0051902
                    unemp |  -.0779873   .0223815    -3.48   0.000    -.1218542   -.0341204
                      rx1 |  -7.439726   1.171668    -6.35   0.000    -9.736152     -5.1433
                      rx2 |   -13.5757   1.493084    -9.09   0.000    -16.50209   -10.64931
                      rx3 |  -1.371345   .5427068    -2.53   0.012    -2.435031   -.3076594
                      rx4 |  -9.003761   .8499758   -10.59   0.000    -10.66968   -7.337839
                      rx5 |  -7.123702   1.146536    -6.21   0.000     -9.37087   -4.876534
                      rx6 |  -6.406982   1.186145    -5.40   0.000    -8.731783    -4.08218
                      rx8 |  -11.28036    .837652   -13.47   0.000    -12.92213   -9.638591
                      rx9 |  -11.09249   1.342224    -8.26   0.000     -13.7232   -8.461778
                     rx10 |  -9.611291   1.115936    -8.61   0.000    -11.79848   -7.424097
                     rx11 |  -3.011886   .5797944    -5.19   0.000    -4.148262    -1.87551
                     rx12 |  -6.901231   .8364846    -8.25   0.000    -8.540711   -5.261751
                    ehat1 |   .2676613    .011839    22.61   0.000     .2444574    .2908653
                    ehat2 |  -.3624523   .0208636   -17.37   0.000    -.4033442   -.3215604
                          |
                     year |
                    2007  |   1.074457   .1153425     9.32   0.000     .8483896    1.300524
                    2008  |  -.8511964   .1590708    -5.35   0.000    -1.162969   -.5394234
                    2009  |          0  (omitted)
                          |
                 state_id |
                       2  |   .3057444   .1131611     2.70   0.007     .0839526    .5275362
                       4  |   .4211974   .0828566     5.08   0.000     .2588015    .5835933
                       5  |   .5328415   .0726966     7.33   0.000     .3903587    .6753242
                       6  |   .2025526   .0798412     2.54   0.011     .0460667    .3590386
                       7  |   .1261486   .0808892     1.56   0.119    -.0323915    .2846886
                       8  |   .2577842    .087231     2.96   0.003     .0868145    .4287538
                       9  |   .1392999   .0621256     2.24   0.025      .017536    .2610637
                      10  |   .4041623   .0758896     5.33   0.000     .2554215    .5529031
                      11  |  -.1107206   .0970095    -1.14   0.254    -.3008557    .0794146
                      12  |  -.0016339   .0880818    -0.02   0.985    -.1742711    .1710033
                      13  |   .1913869   .0846261     2.26   0.024     .0255228     .357251
                      14  |   .0842027   .0764746     1.10   0.271    -.0656848    .2340903
                      15  |   .1456644    .072709     2.00   0.045     .0031573    .2881715
                      16  |   .2565955    .085889     2.99   0.003     .0882563    .4249348
                      17  |    .099478   .1090289     0.91   0.362    -.1142147    .3131707
                      18  |   .2209883   .0777587     2.84   0.004      .068584    .3733926
                      19  |   .2094426   .0779416     2.69   0.007     .0566798    .3622054
                      20  |    .303627   .0828313     3.67   0.000     .1412807    .4659733
                      21  |   .6070566   .0840972     7.22   0.000      .442229    .7718841
                      22  |   .1800938   .1156869     1.56   0.120    -.0466485     .406836
                      23  |   .3034836   .0822912     3.69   0.000     .1421957    .4647714
                      24  |   .2504132   .1102354     2.27   0.023     .0343558    .4664707
                      25  |   .2131395   .0902913     2.36   0.018     .0361718    .3901071
                      26  |   .1462796   .0853793     1.71   0.087    -.0210609      .31362
                      27  |   .0648055   .0845458     0.77   0.443    -.1009012    .2305122
                      28  |   .2753373   .0847793     3.25   0.001     .1091729    .4415017
                      29  |   .0438266   .1061926     0.41   0.680     -.164307    .2519603
                      30  |     .54294   .0750654     7.23   0.000     .3958146    .6900654
                      31  |   .4416992   .0830936     5.32   0.000     .2788388    .6045597
                      32  |   .0908294   .0678199     1.34   0.180    -.0420951    .2237539
                      33  |  -.1810277    .090979    -1.99   0.047    -.3593433   -.0027122
                      34  |   .1498803   .0993012     1.51   0.131    -.0447464    .3445071
                      35  |   .4267741   .0822993     5.19   0.000     .2654705    .5880778
                      36  |   .1402126   .0840615     1.67   0.095    -.0245449      .30497
                      37  |   .2565708   .0955218     2.69   0.007     .0693515      .44379
                      38  |   .4930436   .0755505     6.53   0.000     .3449673    .6411199
                      39  |   .4555804   .1057377     4.31   0.000     .2483384    .6628224
                      40  |   .4016865   .0900511     4.46   0.000     .2251896    .5781833
                      41  |   .0551261   .0947821     0.58   0.561    -.1306434    .2408957
                      42  |   .2570303    .086772     2.96   0.003     .0869604    .4271003
                      43  |   .1267611   .0698514     1.81   0.070    -.0101451    .2636673
                      44  |   .0760076   .0744966     1.02   0.308    -.0700031    .2220182
                      45  |   .0502561   .1019381     0.49   0.622    -.1495389    .2500511
                      46  |   .0289844   .0818703     0.35   0.723    -.1314785    .1894473
                      47  |   .2415181   .0815765     2.96   0.003      .081631    .4014052
                      49  |   .4311747   .0803313     5.37   0.000     .2737282    .5886211
                      50  |   .3378018   .0836816     4.04   0.000     .1737888    .5018148
                      51  |   .0639106    .092097     0.69   0.488    -.1165962    .2444175
                          |
                    _cons |  -1.818922   1.460472    -1.25   0.213    -4.681395     1.04355
            -------------------------------------------------------------------------------
            
            . est store tier3_coef_endog;
            
            . est save "`studydir'/tier3_coef_endog.ster", replace;
            file /Users/moxiemoo/Dropbox/DTC and Formularies/Analysis/Results/tier3_coef_endo
            > g.ster saved
            
            . margins, dydx(dtctv comp_dtctv pct_dtctv offpatent mark_share unitprice drugcou
            > nt enroll rank unemp) post;
            
            Average marginal effects                          Number of obs   =      92294
            Model VCE    : Bootstrap
            
            Expression   : Pr(tier3), predict()
            dy/dx w.r.t. : dtctv comp_dtctv pct_dtctv offpatent mark_share unitprice
                           drugcount enroll rank unemp
            
            ------------------------------------------------------------------------------
                         |            Delta-method
                         |      dy/dx   Std. Err.      z    P>|z|     [95% Conf. Interval]
            -------------+----------------------------------------------------------------
                   dtctv |  -.0502009   .0044055   -11.40   0.000    -.0588356   -.0415663
              comp_dtctv |   .0098243   .0005216    18.84   0.000      .008802    .0108466
               pct_dtctv |   .0653218   .0056396    11.58   0.000     .0542685    .0763751
               offpatent |   .3866826   .0351711    10.99   0.000     .3177484    .4556167
              mark_share |  -.0053481   .0013875    -3.85   0.000    -.0080676   -.0026287
               unitprice |   .0366094   .0056954     6.43   0.000     .0254466    .0477723
               drugcount |  -.0403978   .0020393   -19.81   0.000    -.0443947   -.0364009
                  enroll |   .0000728   .0000517     1.41   0.159    -.0000285    .0001741
                    rank |   .0007655   .0000459    16.67   0.000     .0006755    .0008555
                   unemp |  -.0128829   .0037012    -3.48   0.001    -.0201371   -.0056287
            ------------------------------------------------------------------------------
            
            . est store tier3_me_endog;
            
            . est save "`studydir'/tier3_me_endog.ster", replace;
            file /Users/moxiemoo/Dropbox/DTC and Formularies/Analysis/Results/tier3_me_endog.
            > ster saved
            
            . estimates restore tier3_coef_endog;
            (results tier3_coef_endog are active now)
            
            . margins, dydx(dtctv) at(dtctv==(0(5)65));
            
            Average marginal effects                          Number of obs   =      92294
            Model VCE    : Bootstrap
            
            Expression   : Pr(tier3), predict()
            dy/dx w.r.t. : dtctv
            
            1._at        : dtctv           =           0
            
            2._at        : dtctv           =           5
            
            3._at        : dtctv           =          10
            
            4._at        : dtctv           =          15
            
            5._at        : dtctv           =          20
            
            6._at        : dtctv           =          25
            
            7._at        : dtctv           =          30
            
            8._at        : dtctv           =          35
            
            9._at        : dtctv           =          40
            
            10._at       : dtctv           =          45
            
            11._at       : dtctv           =          50
            
            12._at       : dtctv           =          55
            
            13._at       : dtctv           =          60
            
            14._at       : dtctv           =          65
            
            ------------------------------------------------------------------------------
                         |            Delta-method
                         |      dy/dx   Std. Err.      z    P>|z|     [95% Conf. Interval]
            -------------+----------------------------------------------------------------
            dtctv        |
                     _at |
                      1  |          .  (not estimable)
                      2  |          .  (not estimable)
                      3  |          .  (not estimable)
                      4  |          .  (not estimable)
                      5  |          .  (not estimable)
                      6  |          .  (not estimable)
                      7  |          .  (not estimable)
                      8  |          .  (not estimable)
                      9  |          .  (not estimable)
                     10  |          .  (not estimable)
                     11  |          .  (not estimable)
                     12  |          .  (not estimable)
                     13  |          .  (not estimable)
                     14  |          .  (not estimable)
            ------------------------------------------------------------------------------
            
            . marginsplot, 
            > title("Marginal Effect of Lagged TV DTC") 
            > subtitle("by DTC spending level")
            > ytitle("Change in the Pr(Drug is on Tier 3)", size (medsmall) )
            > xtitle("Lagged Statin TV DTC - in millions of dollars", size(medsmall))
            > ylabel(, labsize(small))
            > xlabel(, labsize(small))
            > yline(0)
            > saving("`resultsdir'/ME for pre-DTC across levels.gph", replace);

            Comment


            • #7
              You still have a bunch of unnecessary xi: commands in there. They may not be causing the main problem but they aren't helping either.

              This is an incredibly complicated problem, much more complicated than what you first presented. My general advice in a situation like this is to start with a minimal number of variables, and then gradually build up. i.e.start with something like you first presented.
              -------------------------------------------
              Richard Williams, Notre Dame Dept of Sociology
              StataNow Version: 19.5 MP (2 processor)

              EMAIL: [email protected]
              WWW: https://www3.nd.edu/~rwilliam

              Comment

              Working...
              X