Announcement

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

  • interactions and reference catagory in Margins

    I have a question about interactions and the reference categories when using the margins command. Specifically, I want to compare all categories to the same base reference category. So, for example, I might have an interaction of dummy variables, age and gender like this:

    reg income age##female
    margins, over(r.age) at(female=(0 1))

    This will give me the difference between age categories for males, and the difference between age categories for females.

    But what I want to do is compare all groups to young males. Specifically, how can I get a "diagonal" difference that compares young males to older females?


    Thanks in advance for advice.

  • #2
    For contrasts of linear predictions, you can just specify the contrasts you want directly in marginlist.
    You do not need to use over() or at().

    Code:
    margins r.age#r.female
    In fact, you do not need margins, you can just use contrast directly.

    Code:
    contrast r.age#r.female
    Here is an example using a dataset from the margins documentation.

    Code:
    . webuse margex 
    (Artificial data for margins)
    
    . regress y sex##group
    
          Source |       SS       df       MS              Number of obs =    3000
    -------------+------------------------------           F(  5,  2994) =   92.91
           Model |  186898.199     5  37379.6398           Prob > F      =  0.0000
        Residual |  1204534.81  2994  402.316235           R-squared     =  0.1343
    -------------+------------------------------           Adj R-squared =  0.1329
           Total |  1391433.01  2999  463.965657           Root MSE      =  20.058
    
    ------------------------------------------------------------------------------
               y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
             sex |
         female  |   21.62507   1.509999    14.32   0.000     18.66433    24.58581
                 |
           group |
              2  |   11.37444   1.573314     7.23   0.000     8.289552    14.45932
              3  |    21.6188   1.588487    13.61   0.000     18.50416    24.73344
                 |
       sex#group |
       female#2  |  -4.851582   1.942744    -2.50   0.013     -8.66083   -1.042333
       female#3  |  -6.084875   3.004638    -2.03   0.043    -11.97624   -.1935115
                 |
           _cons |    50.6107   1.367932    37.00   0.000     47.92852    53.29288
    ------------------------------------------------------------------------------
    
    . margins r.sex#r.group
    
    Contrasts of adjusted predictions
    Model VCE    : OLS
    
    Expression   : Linear prediction, predict()
    
    --------------------------------------------------------------
                               |         df           F        P>F
    ---------------------------+----------------------------------
                     sex#group |
    (female vs male) (2 vs 1)  |          1        6.24     0.0126
    (female vs male) (3 vs 1)  |          1        4.10     0.0429
                        Joint  |          2        3.77     0.0232
                               |
                   Denominator |       2994
    --------------------------------------------------------------
    
    ----------------------------------------------------------------------------
                               |            Delta-method
                               |   Contrast   Std. Err.     [95% Conf. Interval]
    ---------------------------+------------------------------------------------
                     sex#group |
    (female vs male) (2 vs 1)  |  -4.851582   1.942744      -8.66083   -1.042333
    (female vs male) (3 vs 1)  |  -6.084875   3.004638     -11.97624   -.1935115
    ----------------------------------------------------------------------------
    
    . contrast r.sex#r.group
    
    Contrasts of marginal linear predictions
    
    Margins      : asbalanced
    
    --------------------------------------------------------------
                               |         df           F        P>F
    ---------------------------+----------------------------------
                     sex#group |
    (female vs male) (2 vs 1)  |          1        6.24     0.0126
    (female vs male) (3 vs 1)  |          1        4.10     0.0429
                        Joint  |          2        3.77     0.0232
                               |
                   Denominator |       2994
    --------------------------------------------------------------
    
    ----------------------------------------------------------------------------
                               |   Contrast   Std. Err.     [95% Conf. Interval]
    ---------------------------+------------------------------------------------
                     sex#group |
    (female vs male) (2 vs 1)  |  -4.851582   1.942744      -8.66083   -1.042333
    (female vs male) (3 vs 1)  |  -6.084875   3.004638     -11.97624   -.1935115
    ----------------------------------------------------------------------------

    Comment


    • #3
      Thanks - but I that syntax just gives me a second difference - it compares the difference between female/male in group one, to female/male in group 2.

      I need to compare male in group 1, to female in group 2. That is, compare all combinations to the same base reference category (male/group 1)

      Looking at the documentation, I think I have to use user-defined contrasts. Although I haven't been able to figure them out yet.

      Paul

      Comment


      • #4
        Well, one fairly simple approach would be to -egen group_gender = group(sex group)- and then use group_gender in place of group, sex, and their interaction in the regression. (To make it as simple as possible, set male in group 1 as the base category for group_gender.) Then run -margins group_gender-.

        Comment


        • #5
          As Paul indicated, this is possible with contrast with a custom contrast. In my example there
          are 2 levels in sex (0 for male, 1 for female) and 3 levels in group (numbered 1 2 3).
          We want the difference between the margins for 0.sex#1.group and 1.sex#2.group.

          The interaction is organized as follows
          Code:
          i  sex  group
          -----------------
          1    0      1
          2    0      2
          3    0      3
          4    1      1
          5    1      2
          6    1      3
          The i above is an index, or position, that identifies which two margins Paul is interested in. Namely
          1 for 0.sex#1.group and 5 for 1.sex#2.group. This tells us which position to put non-zero
          values in our custom contrast. contrast allows us to leave off all trailing zeroes.

          Code:
          . contrast {sex#group 1 0 0  0 -1}
          
          Contrasts of marginal linear predictions
          
          Margins      : asbalanced
          
          ------------------------------------------------
                       |         df           F        P>F
          -------------+----------------------------------
             sex#group |          1      286.93     0.0000
                       |
           Denominator |       2994
          ------------------------------------------------
          
          --------------------------------------------------------------
                       |   Contrast   Std. Err.     [95% Conf. Interval]
          -------------+------------------------------------------------
             sex#group |
              (1) (1)  |  -28.14793   1.661722     -31.40616    -24.8897
          --------------------------------------------------------------
          As I just illustrated, contrast allows custom contrasts, but they can be difficult to develop and
          the labeling can be quite cryptic.

          An easier alternative for this case is pwcompare, which will produce a table of all
          pairwise comparisons of the specified marginal linear predictions.

          In the following, I believe Paul is interested in the row titled (female#2) vs (male#1).
          Code:
          . pwcompare sex#group
          
          Pairwise comparisons of marginal linear predictions
          
          Margins      : asbalanced
          
          ---------------------------------------------------------------------------
                                    |                                 Unadjusted
                                    |   Contrast   Std. Err.     [95% Conf. Interval]
          --------------------------+------------------------------------------------
                          sex#group |
              (male#2) vs (male#1)  |   11.37444   1.573314      8.289552    14.45932
              (male#3) vs (male#1)  |    21.6188   1.588487      18.50416    24.73344
            (female#1) vs (male#1)  |   21.62507   1.509999      18.66433    24.58581
            (female#2) vs (male#1)  |   28.14793   1.661722       24.8897    31.40616
            (female#3) vs (male#1)  |     37.159   2.822577      31.62461    42.69339
              (male#3) vs (male#2)  |   10.24436   1.120772        8.0468    12.44192
            (female#1) vs (male#2)  |   10.25064   1.006447      8.277239    12.22404
            (female#2) vs (male#2)  |   16.77349   1.222358      14.37675    19.17024
            (female#3) vs (male#2)  |   25.78456   2.588393      20.70935    30.85977
            (female#1) vs (male#3)  |   .0062748   1.030005     -2.013314    2.025864
            (female#2) vs (male#3)  |   6.529131   1.241826      4.094212     8.96405
            (female#3) vs (male#3)  |    15.5402   2.597644      10.44685    20.63355
          (female#2) vs (female#1)  |   6.522856    1.13971      4.288163    8.757549
          (female#3) vs (female#1)  |   15.53392   2.550404       10.5332    20.53465
          (female#3) vs (female#2)  |   9.011069   2.643063      3.828666    14.19347
          ---------------------------------------------------------------------------
          We can cut down on the volume of output by specifying levels for the factor variables.
          Code:
          . pwcompare i(0 1).sex#i(1 2).group
          
          Pairwise comparisons of marginal linear predictions
          
          Margins      : asbalanced
          
          ---------------------------------------------------------------------------
                                    |                                 Unadjusted
                                    |   Contrast   Std. Err.     [95% Conf. Interval]
          --------------------------+------------------------------------------------
                          sex#group |
              (male#2) vs (male#1)  |   11.37444   1.573314      8.289552    14.45932
            (female#1) vs (male#1)  |   21.62507   1.509999      18.66433    24.58581
            (female#2) vs (male#1)  |   28.14793   1.661722       24.8897    31.40616
            (female#1) vs (male#2)  |   10.25064   1.006447      8.277239    12.22404
            (female#2) vs (male#2)  |   16.77349   1.222358      14.37675    19.17024
          (female#2) vs (female#1)  |   6.522856    1.13971      4.288163    8.757549
          ---------------------------------------------------------------------------

          Comment


          • #6
            For future readers, pwcompare is exactly what I needed, and much easier to use than contrast. Many thanks to Jeff Pitblado.

            Comment

            Working...
            X