Announcement

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

  • get matrix of all possible contrasts for a single factor variable after reg

    I'd like to get a matrix showing all possible contrasts for a given factor variable after a regression. For example, let's say I have a factor variable with 4 levels.

    Code:
    sysuse census.data, clear
    tab region
    
         Census |
         region |      Freq.     Percent        Cum.
    ------------+-----------------------------------
             NE |          9       18.00       18.00
        N Cntrl |         12       24.00       42.00
          South |         16       32.00       74.00
           West |         13       26.00      100.00
    ------------+-----------------------------------
          Total |         50      100.00
    
    reg medage i.region
    
          Source |       SS       df       MS              Number of obs =      50
    -------------+------------------------------           F(  3,    46) =    7.56
           Model |  46.3961903     3  15.4653968           Prob > F      =  0.0003
        Residual |  94.1237947    46  2.04616945           R-squared     =  0.3302
    -------------+------------------------------           Adj R-squared =  0.2865
           Total |  140.519985    49   2.8677548           Root MSE      =  1.4304
    
    ------------------------------------------------------------------------------
          medage |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
    -------------+----------------------------------------------------------------
          region |
        N Cntrl  |  -1.708333   .6307664    -2.71   0.009       -2.978   -.4386663
          South  |  -1.614583   .5960182    -2.71   0.009    -2.814306   -.4148606
           West  |  -2.948718    .620282    -4.75   0.000    -4.197281   -1.700155
                 |
           _cons |   31.23333   .4768146    65.50   0.000     30.27356    32.19311
    ------------------------------------------------------------------------------
    I know I can use contrasts r.region, contrast a.region, or contrast ar.region, to get the default, adjacent, or reverse adjacent contrasts, respectively. The r(table) matrix that's accessible after doing any one of these is half way to what I need.

    Code:
    contrast ar.region
    
    Contrasts of marginal linear predictions
    
    Margins      : asbalanced
    
    -------------------------------------------------------
                        |         df           F        P>F
    --------------------+----------------------------------
                 region |
       (N Cntrl vs NE)  |          1        7.34     0.0095
    (South vs N Cntrl)  |          1        0.03     0.8645
       (West vs South)  |          1        6.24     0.0161
                 Joint  |          3        7.56     0.0003
                        |
            Denominator |         46
    -------------------------------------------------------
    
    ---------------------------------------------------------------------
                        |   Contrast   Std. Err.     [95% Conf. Interval]
    --------------------+------------------------------------------------
                 region |
       (N Cntrl vs NE)  |  -1.708333   .6307664        -2.978   -.4386663
    (South vs N Cntrl)  |   .0937502   .5462597     -1.005814    1.193314
       (West vs South)  |  -1.334135   .5341191     -2.409261   -.2590085
    ---------------------------------------------------------------------
    
    matrix list r(table)
    
    r(table)[9,3]
                ar2vs1.     ar3vs2.     ar4vs3.
                region      region      region
         b  -1.7083332   .09375016  -1.3341345
        se   .63076642   .54625975   .53411913
         t  -2.7083452   .17162194   -2.497822
    pvalue   .00946336   .86448754   .01613433
        ll  -2.9780002  -1.0058137  -2.4092605
        ul  -.43866627    1.193314  -.25900847
        df          46          46          46
      crit   2.0128956   2.0128956   2.0128956
     eform           0           0           0
    I'd like a single matrix like this one but with all possible contrasts, regardless of whether they are shown in the original results. That would be either 3 additional columns if 2vs1 and 1vs2, etc., are regarded as redundant (just sign differences, ultimately), or 9 if they are not. I would have a slight preference for the latter, but either would be great. How might I get such a matrix? Thank you.
    Last edited by Colin Peterson; 11 Apr 2019, 22:15.

  • #2
    Dear Colin,
    Possibly there is a more smart way of coding, but here is my blunt solution. You have to run seperate regressions looping through each base level of region:
    Code:
    forvalue i = 1(1)4 {
    qui reg medage b`i'.region
    qui contrast r.region
    matrix C`i'=r(table)
    mat list C`i'
    matrix C`i't = C`i''
    }
    matrix drop C
    matrix C = (C1t \ C2t \ C3t \ C4t)
    mat list C
    The matrix has the result that you are looking for:
    Code:
    C[12,9]
                           b          se           t      pvalue          ll          ul          df        crit       eform
    r2vs1.region  -1.7083332   .63076642  -2.7083452   .00946336  -2.9780002  -.43866627          46   2.0128956           0
    r3vs1.region  -1.6145831   .59601825  -2.7089491   .00944857  -2.8143056  -.41486056          46   2.0128956           0
    r4vs1.region  -2.9487176   .62028204  -4.7538335   .00001998  -4.1972806  -1.7001546          46   2.0128956           0
    r1vs2.region   1.7083332   .63076642   2.7083452   .00946336   .43866627   2.9780002          46   2.0128956           0
    r3vs2.region   .09375016   .54625975   .17162194   .86448754  -1.0058137    1.193314          46   2.0128956           0
    r4vs2.region  -1.2403843   .57263581  -2.1660964   .03552209  -2.3930404  -.08772825          46   2.0128956           0
    r1vs3.region   1.6145831   .59601825   2.7089491   .00944857   .41486056   2.8143056          46   2.0128956           0
    r2vs3.region  -.09375016   .54625975  -.17162194   .86448754   -1.193314   1.0058137          46   2.0128956           0
    r4vs3.region  -1.3341345   .53411913   -2.497822   .01613433  -2.4092605  -.25900847          46   2.0128956           0
    r1vs4.region   2.9487176   .62028204   4.7538335   .00001998   1.7001546   4.1972806          46   2.0128956           0
    r2vs4.region   1.2403843   .57263581   2.1660964   .03552209   .08772825   2.3930404          46   2.0128956           0
    r3vs4.region   1.3341345   .53411913    2.497822   .01613433   .25900847   2.4092605          46   2.0128956           0
    but in the process the correct region strings to indicate the contrast get lost.
    The only solution to this, which I know (I assume they can be retrieved from somewhere more easy), is hard coding, like:
    Code:
    matrix rownames C = NCvsNE SOvsNE WEvsNE NEvsNC SOvsNC WEvsNC NEvsSO NCvsSO WEvsSO NEvsWE NCvsWE SOvsWE
    mat list C
    which will result in:
    Code:
    C[12,9]
                     b          se           t      pvalue          ll          ul          df        crit       eform
    NCvsNE  -1.7083332   .63076642  -2.7083452   .00946336  -2.9780002  -.43866627          46   2.0128956           0
    SOvsNE  -1.6145831   .59601825  -2.7089491   .00944857  -2.8143056  -.41486056          46   2.0128956           0
    WEvsNE  -2.9487176   .62028204  -4.7538335   .00001998  -4.1972806  -1.7001546          46   2.0128956           0
    NEvsNC   1.7083332   .63076642   2.7083452   .00946336   .43866627   2.9780002          46   2.0128956           0
    SOvsNC   .09375016   .54625975   .17162194   .86448754  -1.0058137    1.193314          46   2.0128956           0
    WEvsNC  -1.2403843   .57263581  -2.1660964   .03552209  -2.3930404  -.08772825          46   2.0128956           0
    NEvsSO   1.6145831   .59601825   2.7089491   .00944857   .41486056   2.8143056          46   2.0128956           0
    NCvsSO  -.09375016   .54625975  -.17162194   .86448754   -1.193314   1.0058137          46   2.0128956           0
    WEvsSO  -1.3341345   .53411913   -2.497822   .01613433  -2.4092605  -.25900847          46   2.0128956           0
    NEvsWE   2.9487176   .62028204   4.7538335   .00001998   1.7001546   4.1972806          46   2.0128956           0
    NCvsWE   1.2403843   .57263581   2.1660964   .03552209   .08772825   2.3930404          46   2.0128956           0
    SOvsWE   1.3341345   .53411913    2.497822   .01613433   .25900847   2.4092605          46   2.0128956           0
    I hope that this is helpfull.
    Eric
    http://publicationslist.org/eric.melse

    Comment


    • #3
      Are you looking for all pairwise contrasts? If so, then
      Code:
      sysuse auto
      regress gear_ratio i.rep78
      margins rep78, pwcompare // <= here
      matrix list r(table_vs)

      Comment


      • #4
        Joseph, yes, that's exactly what I need. Thank you!

        Comment

        Working...
        X