Announcement

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

  • Margins and retaining only a subset of pairwise comparisons

    Let's imagine I fit the following model, using a stock Stata dataset.

    Code:
    use http://www.stata-press.com/data/r15/margex
    logistic outcome agegroup##group
    
    ------------------------------------------------------------------------------
         outcome | Odds Ratio   Std. Err.      z    P>|z|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
             sex |
         female  |   .9197162   .1476234    -0.52   0.602     .6714717    1.259737
                 |
           group |
              2  |   .1091104   .0248865    -9.71   0.000     .0697781    .1706132
              3  |   .0470885    .014446    -9.96   0.000     .0258096    .0859111
                 |
       sex#group |
       female#2  |   4.412609   1.182369     5.54   0.000      2.60984    7.460655
       female#3  |   1.463475   1.147196     0.49   0.627     .3148774    6.801886
                 |
           _cons |   .4930556   .0714998    -4.88   0.000     .3710736    .6551363
    ------------------------------------------------------------------------------
    Imagine that for some reason, I want to treat females in group 1 as the reference category. For all other combinations of sex by group, I want to obtain the difference in the predicted probability of the outcome relative to that reference category. That is, I want to know the difference for male + group 1, for female + group 2, for male + group 2, etc, relative to the reference group.

    What is the most concise margins command that I can type? I know that this works, but it produces a lot of extra comparisons:

    Code:
     margins sex#group, pwcompare
    
    Pairwise comparisons of adjusted predictions
    Model VCE    : OIM
    
    Expression   : Pr(outcome), predict()
    
    ---------------------------------------------------------------------------
                              |            Delta-method         Unadjusted
                              |   Contrast   Std. Err.     [95% Conf. Interval]
    --------------------------+------------------------------------------------
                    sex#group |
        (male#2) vs (male#1)  |  -.2791815   .0331885     -.3442299   -.2141332
        (male#3) vs (male#1)  |  -.3075421   .0326294     -.3714947   -.2435896
      (female#1) vs (male#1)  |  -.0182407   .0353112     -.0874494     .050968
      (female#2) vs (male#1)  |   -.151029   .0367989     -.2231535   -.0789045
      (female#3) vs (male#1)  |  -.2999295   .0383922     -.3751769   -.2246821
        (male#3) vs (male#2)  |  -.0283606    .010425     -.0487933    -.007928
      (female#1) vs (male#2)  |   .2609408   .0170553       .227513    .2943686
      (female#2) vs (male#2)  |   .1281525   .0199539      .0890435    .1672615
      (female#3) vs (male#2)  |   -.020748   .0227588     -.0653545    .0238584
      (female#1) vs (male#3)  |   .2893014     .01594      .2580596    .3205433
      (female#2) vs (male#3)  |   .1565131   .0190095      .1192552     .193771
      (female#3) vs (male#3)  |   .0076126   .0219355     -.0353801    .0506053
    (female#2) vs (female#1)  |  -.1327883   .0233144     -.1784838   -.0870929
    (female#3) vs (female#1)  |  -.2816888   .0257559     -.3321695   -.2312082
    (female#3) vs (female#2)  |  -.1489005   .0277605       -.20331    -.094491
    ---------------------------------------------------------------------------
    I think that I should be using -margins, contrast-. But I can't quite figure out how to do this. The closest I can get is:

    Code:
    margins r.sex#r.group
    
    Contrasts of adjusted predictions
    Model VCE    : OIM
    
    Expression   : Pr(outcome), predict()
    
    --------------------------------------------------------------
                               |         df        chi2     P>chi2
    ---------------------------+----------------------------------
                     sex#group |
    (female vs male) (2 vs 1)  |          1       13.03     0.0003
    (female vs male) (3 vs 1)  |          1        0.39     0.5340
                        Joint  |          2       22.28     0.0000
    --------------------------------------------------------------
    
    ----------------------------------------------------------------------------
                               |            Delta-method
                               |   Contrast   Std. Err.     [95% Conf. Interval]
    ---------------------------+------------------------------------------------
                     sex#group |
    (female vs male) (2 vs 1)  |   .1463932   .0405591      .0668988    .2258876
    (female vs male) (3 vs 1)  |   .0258533   .0415698      -.055622    .1073286
    ----------------------------------------------------------------------------
    And that's not quite it. Any thoughts?
    Be aware that it can be very hard to answer a question without sample data. You can use the dataex command for this. Type help dataex at the command line.

    When presenting code or results, please use the code delimiters format them. Use the # button on the formatting toolbar, between the " (double quote) and <> buttons.

  • #2
    Good question. I've wondered about this myself on a few occasions and know no way to do this. But what you can do that at least makes it easier to find the ones you're interested in is:

    Code:
    . margins group#sex, pwcompare
    
    Pairwise comparisons of adjusted predictions
    Model VCE    : OIM
    
    Expression   : Pr(outcome), predict()
    
    ---------------------------------------------------------------------------
                              |            Delta-method         Unadjusted
                              |   Contrast   Std. Err.     [95% Conf. Interval]
    --------------------------+------------------------------------------------
                    group#sex |
      (1#female) vs (1#male)  |  -.0182407   .0353112     -.0874494     .050968
        (2#male) vs (1#male)  |  -.2791815   .0331885     -.3442299   -.2141332
      (2#female) vs (1#male)  |   -.151029   .0367989     -.2231535   -.0789045
        (3#male) vs (1#male)  |  -.3075421   .0326294     -.3714947   -.2435896
      (3#female) vs (1#male)  |  -.2999295   .0383922     -.3751769   -.2246821
      (2#male) vs (1#female)  |  -.2609408   .0170553     -.2943686    -.227513
    (2#female) vs (1#female)  |  -.1327883   .0233144     -.1784838   -.0870929
      (3#male) vs (1#female)  |  -.2893014     .01594     -.3205433   -.2580596
    (3#female) vs (1#female)  |  -.2816888   .0257559     -.3321695   -.2312082
      (2#female) vs (2#male)  |   .1281525   .0199539      .0890435    .1672615
        (3#male) vs (2#male)  |  -.0283606    .010425     -.0487933    -.007928
      (3#female) vs (2#male)  |   -.020748   .0227588     -.0653545    .0238584
      (3#male) vs (2#female)  |  -.1565131   .0190095      -.193771   -.1192552
    (3#female) vs (2#female)  |  -.1489005   .0277605       -.20331    -.094491
      (3#female) vs (3#male)  |   .0076126   .0219355     -.0353801    .0506053
    ---------------------------------------------------------------------------
    It gives the same total output, but the ordering is different and most of the ones you are interested in form a consecutive block of four rows in the output table. But that is, admittedly, not very satisfying.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      Good question. I've wondered about this myself on a few occasions and know no way to do this. But what you can do that at least makes it easier to find the ones you're interested in is:

      Code:
      . margins group#sex, pwcompare
      
      Pairwise comparisons of adjusted predictions
      Model VCE : OIM
      
      Expression : Pr(outcome), predict()
      
      ---------------------------------------------------------------------------
      | Delta-method Unadjusted
      | Contrast Std. Err. [95% Conf. Interval]
      --------------------------+------------------------------------------------
      group#sex |
      (1#female) vs (1#male) | -.0182407 .0353112 -.0874494 .050968
      (2#male) vs (1#male) | -.2791815 .0331885 -.3442299 -.2141332
      (2#female) vs (1#male) | -.151029 .0367989 -.2231535 -.0789045
      (3#male) vs (1#male) | -.3075421 .0326294 -.3714947 -.2435896
      (3#female) vs (1#male) | -.2999295 .0383922 -.3751769 -.2246821
      (2#male) vs (1#female) | -.2609408 .0170553 -.2943686 -.227513
      (2#female) vs (1#female) | -.1327883 .0233144 -.1784838 -.0870929
      (3#male) vs (1#female) | -.2893014 .01594 -.3205433 -.2580596
      (3#female) vs (1#female) | -.2816888 .0257559 -.3321695 -.2312082
      (2#female) vs (2#male) | .1281525 .0199539 .0890435 .1672615
      (3#male) vs (2#male) | -.0283606 .010425 -.0487933 -.007928
      (3#female) vs (2#male) | -.020748 .0227588 -.0653545 .0238584
      (3#male) vs (2#female) | -.1565131 .0190095 -.193771 -.1192552
      (3#female) vs (2#female) | -.1489005 .0277605 -.20331 -.094491
      (3#female) vs (3#male) | .0076126 .0219355 -.0353801 .0506053
      ---------------------------------------------------------------------------
      It gives the same total output, but the ordering is different and most of the ones you are interested in form a consecutive block of four rows in the output table. But that is, admittedly, not very satisfying.
      I should have said earlier, but I was hoping to use -marginsplot- to plot the differences. I know I can use -coefplot- to remove the extraneous comparisons, but I expect that the syntax will be ungainly. A further complication is that in my real data, I intend to use the -atmeans- option and to use -at- to show how the difference varied by year in our sample. Thus, I don't believe I can use the -contrast- command by itself (but I would be glad to be proven wrong). My actual syntax will be something like:

      Code:
      regress summary_qol_score i.nonwhite##i.high_minority_facility##i.year `other_covariates'
      margins somethingorother, at(year = (0(1)4) atmeans)
      marginsplot, xdimension(year)
      So, I want to show how 3 focal groups compare to White residents in low-minority facilities. I am aware that I can actually generate a group variable using

      Code:
      egen group = group(nonwhite high_minority_facility)
      and then use that in my regression model. I can then simply type

      Code:
      margins, dydx(group) ...
      I know this works mechanically, but I don't know if there is a statistical issue with doing this. I was hoping to preserve the original factor variable syntax. This may be the least bad option. If anyone knows that this is an incorrect approach, please say so. If anyone has another solution, I'd welcome that.
      Last edited by Weiwen Ng; 05 Dec 2018, 18:12.
      Be aware that it can be very hard to answer a question without sample data. You can use the dataex command for this. Type help dataex at the command line.

      When presenting code or results, please use the code delimiters format them. Use the # button on the formatting toolbar, between the " (double quote) and <> buttons.

      Comment


      • #4
        margins has an undocumented saving() option; likewise, marginsplot has an undocumented syntax that allows using filename ... I trust you know how to manipulate Stata datasets, e.g., keep certain observations and drop others.

        Best
        Daniel

        Comment

        Working...
        X