Announcement

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

  • Testing interactions in xtreg

    I am fairly new to xtreg and what to sure I am doing the right thing:


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(panelid visit) double sbp float condition byte sex
    1  1 133 3 1
    1  3 115 3 1
    1  6 122 3 1
    1  9 131 3 1
    1 12   . 3 1
    1 15   . 3 1
    1 18   . 3 1
    1 21 128 3 1
    1 24 139 3 1
    1 27 131 3 1
    1 30 108 3 1
    1 33 138 3 1
    1 36 136 3 1
    1 39   . 3 1
    1 42 146 3 1
    1 45 127 3 1
    1 48 159 3 1
    1 51   . 3 1
    1 54   . 3 1
    2  1 151 6 2
    end
    Code:
    xtset panelid visit
    xtreg sbp i.condition i.sex i.condition#i.sex, re vce(robust)
    margins i.condition#i.sex
    marginsplot, noci

  • #2
    Well, without knowing what you hope to learn from the data, it is impossible to say whether this code is the right thing or not. Here is what we can say:

    1. The code contains no syntax errors.
    2. With appropriate data, this code will estimate a random effects regression of SBP on condition, with sex as an effect modifier.
    3. With appropriate data, the -margins- command will then show you the expected values of sbp for each combination of condition and sex.
    4. And -marginsplot- will make a graph of those results.

    5. The example data shown is not appropriate for this approach because it is always the case that condition = 2 * sex. Consequently of the four anticipated combinations of sex an condition, only two are actually instantiated. This may just be a fluke of the example you showed, and not be a problem in your full data set. Only you can tell. Run -tab condition sex- to be sure all the combinations actually occur in the data.

    If your full data set is appropriate and if 1 through 4 describe what you are trying to do, then your code is "the right thing."

    Comment


    • #3
      The full dataset has 900 unique individuals, and condition (4 levels) X sex (2 levels) combinations occur resulting in 8 cells. Yes, I want to estimate a random-effects regression for SBP on a condition with sex as a modifier. Thank you, Clyde.

      Comment


      • #4
        In SPSS, I can get a single p-value for the interaction term between condition X sex. After this, I use a follow-up code to get the expected values of sbp for each combination of condition and sex. Will it be appropriate to run the following code in Stata? Condition and sex are both categorical variables, so I don't think it is appropriate to treat them as continuous. However, this is the only way to get a single p-value for the interaction term. Any suggestions?

        Code:
         
         xtreg sbp c.condition c.sex c.condition#c.sex, re vce(robust)

        Comment


        • #5
          Al.
          your Stata code can be amended (since -sex- and -condition- are categoricl variable, the right prefix is -i-., not -c.-, as the latter tells Stata that the variabe is continuous) and slimmed down:
          Code:
          xtreg sbp i.condition##i.sex, re vce(robust)
          Testing interaction for different combination o9f the variables included in the interaction you can use -margins-:
          Code:
          . use "https://www.stata-press.com/data/r16/auto.dta"
          (1978 Automobile Data)
          
          . regress price i.foreign##i.rep78
          note: 1.foreign#1b.rep78 identifies no observations in the sample
          note: 1.foreign#2.rep78 identifies no observations in the sample
          note: 1.foreign#5.rep78 omitted because of collinearity
          
                Source |       SS           df       MS      Number of obs   =        69
          -------------+----------------------------------   F(7, 61)        =      0.39
                 Model |    24684607         7  3526372.43   Prob > F        =    0.9049
              Residual |   552112352        61  9051022.16   R-squared       =    0.0428
          -------------+----------------------------------   Adj R-squared   =   -0.0670
                 Total |   576796959        68  8482308.22   Root MSE        =    3008.5
          
          -------------------------------------------------------------------------------
                  price |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
          --------------+----------------------------------------------------------------
                foreign |
               Foreign  |   2088.167   2351.846     0.89   0.378     -2614.64    6790.974
                        |
                  rep78 |
                     2  |   1403.125   2378.422     0.59   0.557    -3352.823    6159.073
                     3  |   2042.574   2204.707     0.93   0.358    -2366.011    6451.159
                     4  |   1317.056   2351.846     0.56   0.578    -3385.751    6019.863
                     5  |       -360   3008.492    -0.12   0.905    -6375.851    5655.851
                        |
          foreign#rep78 |
             Foreign#1  |          0  (empty)
             Foreign#2  |          0  (empty)
             Foreign#3  |  -3866.574   2980.505    -1.30   0.199    -9826.462    2093.314
             Foreign#4  |  -1708.278   2746.365    -0.62   0.536    -7199.973    3783.418
             Foreign#5  |          0  (omitted)
                        |
                  _cons |     4564.5   2127.325     2.15   0.036      310.651    8818.349
          -------------------------------------------------------------------------------
          
          
          . margins r.foreign@rep78, plot
          
          Contrasts of adjusted predictions               Number of obs     =         69
          Model VCE    : OLS
          
          Expression   : Linear prediction, predict()
          
          ------------------------------------------------------------
                                   |         df           F        P>F
          -------------------------+----------------------------------
                     foreign@rep78 |
          (Foreign vs Domestic) 1  |  (not testable)
          (Foreign vs Domestic) 2  |  (not testable)
          (Foreign vs Domestic) 3  |          1        0.94     0.3352
          (Foreign vs Domestic) 4  |          1        0.07     0.7897
          (Foreign vs Domestic) 5  |          1        0.79     0.3781
                            Joint  |  (not testable)
                                   |
                       Denominator |         61
          ------------------------------------------------------------
          
          --------------------------------------------------------------------------
                                   |            Delta-method
                                   |   Contrast   Std. Err.     [95% Conf. Interval]
          -------------------------+------------------------------------------------
                     foreign@rep78 |
          (Foreign vs Domestic) 1  |          .  (not estimable)
          (Foreign vs Domestic) 2  |          .  (not estimable)
          (Foreign vs Domestic) 3  |  -1778.407    1830.91     -5439.538    1882.723
          (Foreign vs Domestic) 4  |   379.8889   1418.217      -2456.01    3215.788
          (Foreign vs Domestic) 5  |   2088.167   2351.846      -2614.64    6790.974
          --------------------------------------------------------------------------
          
            Variables that uniquely identify margins: foreign rep78
          
          .
          Kind regards,
          Carlo
          (Stata 19.0)

          Comment


          • #6
            Thanks, Carlo, this is very helpful. Would you interpret the results in the contrast table the same way as regression coefficients?


            Comment


            • #7
              Al:
              yes.
              Kind regards,
              Carlo
              (Stata 19.0)

              Comment


              • #8
                Going back to my post at #4, running this code will provide an ANOVA-style overall p-value for the interaction term, and the last line will provide the cell contrasts.

                Code:
                webuse systolic
                reg systolic i.drug##i.disease
                contrast drug#disease
                contrast r.drug@disease

                Comment


                • #9
                  Re #4: it makes no sense to treat a categorical variable as continuous in order to get a single overall significance test for the interaction. You're mutilating the data, and that significance test you are getting that way is meaningless.

                  If you run -xtreg outcome i.disease##i.treatment...- and you want a single overall test for interaction effects, follow that regression up with:

                  Code:
                  testparm i.disease#i.treatment // NOTE #, NOT ## HERE
                  and you will get it.

                  Comment


                  • #10
                    Al:
                    correct.
                    -contrast- conveniently elaborates on -lincom-:
                    Code:
                    . webuse systolic
                    (Systolic Blood Pressure Data)
                    
                    . reg systolic i.drug##i.disease
                    
                          Source |       SS           df       MS      Number of obs   =        58
                    -------------+----------------------------------   F(11, 46)       =      3.51
                           Model |  4259.33851        11  387.212591   Prob > F        =    0.0013
                        Residual |  5080.81667        46  110.452536   R-squared       =    0.4560
                    -------------+----------------------------------   Adj R-squared   =    0.3259
                           Total |  9340.15517        57  163.862371   Root MSE        =     10.51
                    
                    ------------------------------------------------------------------------------
                        systolic |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                    -------------+----------------------------------------------------------------
                            drug |
                              2  |  -1.333333   6.363903    -0.21   0.835    -14.14321    11.47654
                              3  |        -13   7.431438    -1.75   0.087    -27.95871    1.958708
                              4  |  -15.73333   6.363903    -2.47   0.017    -28.54321   -2.923461
                                 |
                         disease |
                              2  |  -1.083333   6.783944    -0.16   0.874     -14.7387    12.57204
                              3  |  -8.933333   6.363903    -1.40   0.167    -21.74321    3.876539
                                 |
                    drug#disease |
                            2 2  |   6.583333   9.783943     0.67   0.504    -13.11072    26.27739
                            2 3  |        -.9   8.999918    -0.10   0.921     -19.0159     17.2159
                            3 2  |     -10.85   10.24353    -1.06   0.295    -31.46916    9.769157
                            3 3  |        1.1   10.24353     0.11   0.915    -19.51916    21.71916
                            4 2  |   .3166667   9.301675     0.03   0.973    -18.40663    19.03997
                            4 3  |   9.533333   9.202189     1.04   0.306    -8.989712    28.05638
                                 |
                           _cons |   29.33333   4.290543     6.84   0.000     20.69692    37.96975
                    ------------------------------------------------------------------------------
                    
                    . lincom 1b.drug-2.drug
                    
                     ( 1)  1b.drug - 2.drug = 0
                    
                    ------------------------------------------------------------------------------
                        systolic |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                    -------------+----------------------------------------------------------------
                             (1) |   1.333333   6.363903     0.21   0.835    -11.47654    14.14321
                    ------------------------------------------------------------------------------
                    
                    . lincom 2.drug-1b.drug
                    
                     ( 1)  - 1b.drug + 2.drug = 0
                    
                    ------------------------------------------------------------------------------
                        systolic |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                    -------------+----------------------------------------------------------------
                             (1) |  -1.333333   6.363903    -0.21   0.835    -14.14321    11.47654
                    ------------------------------------------------------------------------------
                    
                    .. contrast drug#disease
                    
                    Contrasts of marginal linear predictions
                    
                    Margins      : asbalanced
                    
                    ------------------------------------------------
                                 |         df           F        P>F
                    -------------+----------------------------------
                    drug#disease |          6        1.07     0.3958
                                 |
                     Denominator |         46
                    ------------------------------------------------
                    
                    .
                    . contrast r.drug@disease
                    
                    Contrasts of marginal linear predictions
                    
                    Margins      : asbalanced
                    
                    ------------------------------------------------
                                 |         df           F        P>F
                    -------------+----------------------------------
                    drug@disease |
                     (2 vs 1) 1  |          1        0.04     0.8350
                     (2 vs 1) 2  |          1        0.50     0.4835
                     (2 vs 1) 3  |          1        0.12     0.7272
                     (3 vs 1) 1  |          1        3.06     0.0869
                     (3 vs 1) 2  |          1       11.44     0.0015
                     (3 vs 1) 3  |          1        2.85     0.0982
                     (4 vs 1) 1  |          1        6.11     0.0172
                     (4 vs 1) 2  |          1        5.16     0.0278
                     (4 vs 1) 3  |          1        0.87     0.3558
                          Joint  |          9        3.79     0.0012
                                 |
                     Denominator |         46
                    ------------------------------------------------
                    
                    --------------------------------------------------------------
                                 |   Contrast   Std. Err.     [95% Conf. Interval]
                    -------------+------------------------------------------------
                    drug@disease |
                     (2 vs 1) 1  |  -1.333333   6.363903     -14.14321    11.47654
                     (2 vs 1) 2  |       5.25   7.431438     -9.708708    20.20871
                     (2 vs 1) 3  |  -2.233333   6.363903     -15.04321    10.57654
                     (3 vs 1) 1  |        -13   7.431438     -27.95871    1.958708
                     (3 vs 1) 2  |     -23.85   7.050081     -38.04108   -9.658923
                     (3 vs 1) 3  |      -11.9   7.050081     -26.09108    2.291077
                     (4 vs 1) 1  |  -15.73333   6.363903     -28.54321   -2.923461
                     (4 vs 1) 2  |  -15.41667   6.783944     -29.07204   -1.761297
                     (4 vs 1) 3  |       -6.2    6.64688     -19.57948    7.179476
                    --------------------------------------------------------------
                    Kind regards,
                    Carlo
                    (Stata 19.0)

                    Comment

                    Working...
                    X