Announcement

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

  • margins, dydx(x) at only certain values of x

    Dear Statalisters,

    I am running a logit

    Code:
    svy, subpop(if marital==1&age>17&age<36&(evunion==2)&jbstat!=7&ivfioall==1): logit event1 ib1.skills##i.wave5  i.agesm i.livingpar i.ukborn i.sex i.educ2 i.biosonsn2 i.london i.religion i.nssecpar , or level(95)
    and, then, computing the marginal effects for skills (which is categorical and has 4 levels) this way

    Code:
    margins , level(95)  post atmeans    dydx(ib1.skills) force at(wave5=(1 2 3)) subpop(if marital==1&age>17&age<36&(evunion==2)&jbstat!=7&ivfioall==1)
    I am interesting in obtaining the marginal effects only for specific values of i.skills but I am not sure how to do it. If I specify at(skills=(1(1)3), I still get the contrast 4 vs 1.

    The reason I am asking it is that I would like to plot the results with marginsplot later and, if I have the contrast 4 vs.1, marginplot is going to plot that one too.

    Could you please help me?

    Thank you and best,
    Lydia

    Last edited by Lydia Palumbo; 21 Apr 2021, 02:49.

  • #2
    Marginal effects of a factor variable are the differences of the predictive margins at each level of the factor compared to the base level.
    This is also known as a contrast with the reference (base) category.
    margins supports contrast operators in the marginslist specification, and you can select which levels to contrast there to.

    Here is an example using data from the logit help file.
    Code:
    webuse nhanes2d
    * survey data, already setup
    svyset
    * fit our model
    svy: logit highbp height weight age female i.region
    * compute the marginal effect of our factor variable
    margins, atmeans dydx(region)
    * compute the same using contrast syntax
    margins r.region, atmeans contrast(nowald effects)
    * get the levels of interest
    describe region
    label list region
    * choose contrasts of levels 4 and 3 versus level 1
    margins r(4 3 1).region, atmeans contrast(nowald effects)
    Here is the log from Stata 17, but should be essentially the same in earlier versions of Stata too:
    Code:
    . webuse nhanes2d
    
    . * survey data, already setup
    . svyset
    
    Sampling weights: finalwgt
                 VCE: linearized
         Single unit: missing
            Strata 1: strata
     Sampling unit 1: psu
               FPC 1: <zero>
    
    . * fit our model
    . svy: logit highbp height weight age female i.region
    (running logit on estimation sample)
    
    Survey: Logistic regression
    
    Number of strata = 31                            Number of obs   =      10,351
    Number of PSUs   = 62                            Population size = 117,157,513
                                                     Design df       =          31
                                                     F(7, 25)        =      237.00
                                                     Prob > F        =      0.0000
    
    ------------------------------------------------------------------------------
                 |             Linearized
          highbp | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
    -------------+----------------------------------------------------------------
          height |  -.0346967   .0052669    -6.59   0.000    -.0454387   -.0239548
          weight |   .0518302   .0024187    21.43   0.000     .0468972    .0567632
             age |   .0488588   .0019091    25.59   0.000     .0449651    .0527525
          female |  -.4596187   .0571295    -8.05   0.000    -.5761351   -.3431024
                 |
          region |
             MW  |  -.1695395   .2367601    -0.72   0.479    -.6524149    .3133359
              S  |  -.1423818    .233099    -0.61   0.546    -.6177903    .3330268
              W  |  -.0362159   .2140672    -0.17   0.867     -.472809    .4003771
                 |
           _cons |  -.2934606   .8631403    -0.34   0.736    -2.053847    1.466926
    ------------------------------------------------------------------------------
    
    . * compute the marginal effect of our factor variable
    . margins, atmeans dydx(region)
    
    Conditional marginal effects
    
    Number of strata = 31                            Number of obs   =      10,351
    Number of PSUs   = 62                            Population size = 117,157,513
    Model VCE: Linearized                            Design df       =          31
    
    Expression: Pr(highbp), predict()
    dy/dx wrt:  2.region 3.region 4.region
    At: height   = 168.4599 (mean)
        weight   = 71.90064 (mean)
        age      = 42.25264 (mean)
        female   = .5206498 (mean)
        1.region =  .206883 (mean)
        2.region = .2489281 (mean)
        3.region = .2652914 (mean)
        4.region = .2788975 (mean)
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |      dy/dx   std. err.      t    P>|t|     [95% conf. interval]
    -------------+----------------------------------------------------------------
          region |
             MW  |  -.0379287   .0530809    -0.71   0.480    -.1461879    .0703305
              S  |  -.0319958   .0525213    -0.61   0.547    -.1391136    .0751221
              W  |  -.0082737   .0489926    -0.17   0.867    -.1081948    .0916473
    ------------------------------------------------------------------------------
    Note: dy/dx for factor levels is the discrete change from the base level.
    
    . * compute the same using contrast syntax
    . margins r.region, atmeans contrast(nowald effects)
    
    Contrasts of adjusted predictions
    
    Number of strata = 31                            Number of obs   =      10,351
    Number of PSUs   = 62                            Population size = 117,157,513
    Model VCE: Linearized                            Design df       =          31
    
    Expression: Pr(highbp), predict()
    At: height   = 168.4599 (mean)
        weight   = 71.90064 (mean)
        age      = 42.25264 (mean)
        female   = .5206498 (mean)
        1.region =  .206883 (mean)
        2.region = .2489281 (mean)
        3.region = .2652914 (mean)
        4.region = .2788975 (mean)
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |   Contrast   std. err.      t    P>|t|     [95% conf. interval]
    -------------+----------------------------------------------------------------
          region |
     (MW vs NE)  |  -.0379287   .0530809    -0.71   0.480    -.1461879    .0703305
      (S vs NE)  |  -.0319958   .0525213    -0.61   0.547    -.1391136    .0751221
      (W vs NE)  |  -.0082737   .0489926    -0.17   0.867    -.1081948    .0916473
    ------------------------------------------------------------------------------
    
    . * get the levels of interest
    . describe region
    
    Variable      Storage   Display    Value
        name         type    format    label      Variable label
    -------------------------------------------------------------------------------
    region          byte    %9.0g      region     Region
    
    . label list region
    region:
               1 NE
               2 MW
               3 S
               4 W
    
    . * choose contrasts of levels 4 and 3 versus level 1
    . margins r(4 3 1).region, atmeans contrast(nowald effects)
    
    Contrasts of adjusted predictions
    
    Number of strata = 31                            Number of obs   =      10,351
    Number of PSUs   = 62                            Population size = 117,157,513
    Model VCE: Linearized                            Design df       =          31
    
    Expression: Pr(highbp), predict()
    At: height   = 168.4599 (mean)
        weight   = 71.90064 (mean)
        age      = 42.25264 (mean)
        female   = .5206498 (mean)
        1.region =  .206883 (mean)
        2.region = .2489281 (mean)
        3.region = .2652914 (mean)
        4.region = .2788975 (mean)
    
    ------------------------------------------------------------------------------
                 |            Delta-method
                 |   Contrast   std. err.      t    P>|t|     [95% conf. interval]
    -------------+----------------------------------------------------------------
          region |
      (S vs NE)  |  -.0319958   .0525213    -0.61   0.547    -.1391136    .0751221
      (W vs NE)  |  -.0082737   .0489926    -0.17   0.867    -.1081948    .0916473
    ------------------------------------------------------------------------------

    Comment


    • #3
      That works perfectly.
      Thank you very much.

      Comment

      Working...
      X