Announcement

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

  • how to drop variables from the dummy interaction variable with zero values using outreg2 command?

    Dear Stata Experts,

    I encountered a simple command to export my table using outreg but I am lost on how to specify in the dummy variable I need to drop.

    Let me show you the codes and stata result.

    [CODE]

    xtreg InpatientNo i.Agegroup_new i.Country_new Agegroup_new##Country_new, re robust

    Random-effects GLS regression Number of obs = 19,352
    Group variable: panelid Number of groups = 7,068

    R-squared: Obs per group:
    Within = 0.0000 min = 1
    Between = 0.0192 avg = 2.7
    Overall = 0.0175 max = 3

    Wald chi2(7) = 354.73
    corr(u_i, X) = 0 (assumed) Prob > chi2 = 0.0000

    (Std. err. adjusted for 7,068 clusters in panelid)
    ------------------------------------------------------------------------------------------
    | Robust
    InpatientNo | Coefficient std. err. z P>|z| [95% conf. interval]
    -------------------------+----------------------------------------------------------------
    Agegroup_new |
    15-39 | 102.9816 52.41481 1.96 0.049 .2504866 205.7128
    40-69 | 706.6887 78.92718 8.95 0.000 551.9943 861.3832
    70+ | 302.6225 48.34639 6.26 0.000 207.8653 397.3797

    Country_new |
    Switzerland | 81.39452 67.38679 1.21 0.227 -50.68117 213.4702

    Agegroup_new#Country_new |
    15-39#Switzerland | 29.37796 91.83564 0.32 0.749 -150.6166 209.3725
    40-69#Switzerland | -354.0323 107.3944 -3.30 0.001 -564.5213 -143.5432
    70+#Switzerland | 41.87351 87.90335 0.48 0.634 -130.4139 214.1609

    _cons | 62.89575 13.22812 4.75 0.000 36.96911 88.82239
    -------------------------+----------------------------------------------------------------
    sigma_u | 1410.2979
    sigma_e | 312.15199
    rho | .95329762 (fraction of variance due to u_i)
    ------------------------------------------------------------------------------------------

    [CODE]

    outreg2 using myreg.doc, title(Panel Data Regression Random Effects) drop(Agegroup_new##Country_newFinland) addstat ("Prob > chi2",e(p))

    Panel Data Regression Random Effects
    (1)
    VARIABLES InpatientNo
    2.Agegroup_new 103.0**
    (52.41)
    3.Agegroup_new 706.7***
    (78.93)
    4.Agegroup_new 302.6***
    (48.35)
    2.Country_new 81.39
    (67.39)
    1b.Agegroup_new#1b.Country_new 0
    (0)
    1b.Agegroup_new#2o.Country_new 0
    (0)
    2o.Agegroup_new#1b.Country_new 0
    (0)
    2.Agegroup_new#2.Country_new 29.38
    (91.84)
    3o.Agegroup_new#1b.Country_new 0
    (0)
    3.Agegroup_new#2.Country_new -354.0***
    (107.4)
    4o.Agegroup_new#1b.Country_new 0
    (0)
    4.Agegroup_new#2.Country_new 41.87
    (87.90)
    Constant 62.90***
    (13.23)
    Observations 19,352
    Number of panelid 7,068
    Prob > chi2 0

    Robust standard errors in parentheses
    *** p<0.01, ** p<0.05, * p<0.1



    In this case, The base country for the interaction dummy variable Agegroup_new##Country_new is Finland with 0 coefficients. The variables 1b.Agegroup_new#1b.Country_new, 1b.Agegroup_new#2o.Country_new, 2o.Agegroup_new#1b.Country_new, 3o.Agegroup_new#1b.Country_new, 4o.Agegroup_new#1b.Country_new have to be drop using outreg2 command but I couldn`t find the right way to drop these. I also use the words shown on the table to be dropped but no effect. Can someone help me?

    Kind Regards,
    Rose
    Last edited by Rose Strebel; 14 Mar 2023, 06:58.

  • #2
    outreg2 is from SSC (FAQ Advice #12). This command is old and not updated. I have found it difficult to reference the interaction terms when using it in the past. As you need to keep some interaction terms, neither the option -keep()- nor -drop()- will work without knowing how to reference the interaction terms. As the command uses the coefficients' matrix and VCE from the estimation command, you can deal with this at the level of the estimation command by specifying exactly the interactions to keep instead of the using the double hash syntax. Here is a reproducible example:

    Code:
    webuse nhanes2.dta, clear
    reg  bpsystol i.sex##i.race  age height weight
    outreg2 using myfile, replace seeout
    Res.:
    Click image for larger version

Name:	Capture1.PNG
Views:	1
Size:	23.5 KB
ID:	1705706




    And now specifying only relevant interactions in the command:

    Code:
    webuse nhanes2.dta, clear
    reg  bpsystol i.sex i.race  2.sex#2.race 2.sex#3.race age height weight
    outreg2 using myfile, replace seeout
    Click image for larger version

Name:	Capture1.PNG
Views:	1
Size:	17.8 KB
ID:	1705707

    Last edited by Andrew Musau; 14 Mar 2023, 14:02.

    Comment


    • #3
      Wow! Thank you so much, Andrew. It worked! Thank you for saving my life with this. I wish you a great day.

      Comment


      • #4
        Following https://www.statalist.org/forums/for...=1752939918363, I will add an easier way to select interactions to keep using fvexpand based on the example in #2.

        Code:
        webuse nhanes2.dta, clear
        reg bpsystol i.sex##i.race age height weight
        
        *SHOW COEFFICIENTS' MATRIX WITH BASELEVELS COEFFICIENTS
        mat list e(b)
        
        *GENERATE VARIABLES OF INTERACTIONS USING FVEXPAND
        fvexpand i.sex#i.race
        
        *SELECT ONLY NON-BASELEVELS INTERACTIONS VARIABLES
        local nonbase= ustrregexra(" " + "`r(varlist)'" + " ",  "\b\S*b\.\S*\b", "")
        
        *RUN REGRESSION WITH NON-BASELEVELS INTERACTIONS VARIABLES
        reg bpsystol i.sex i.race `nonbase' age height weight
        
        *RESULTING COEFFICIENTS' MATRIX
        mat list e(b)
        Res.:

        Code:
        . reg bpsystol i.sex##i.race age height weight
        
              Source |       SS           df       MS      Number of obs   =    10,351
        -------------+----------------------------------   F(8, 10342)     =    589.09
               Model |   1763880.4         8   220485.05   Prob > F        =    0.0000
            Residual |  3870789.63    10,342  374.278633   R-squared       =    0.3130
        -------------+----------------------------------   Adj R-squared   =    0.3125
               Total |  5634670.03    10,350  544.412563   Root MSE        =    19.346
        
        -------------------------------------------------------------------------------
             bpsystol | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
        --------------+----------------------------------------------------------------
                  sex |
              Female  |  -3.133777   .5672512    -5.52   0.000    -4.245699   -2.021855
                      |
                 race |
               Black  |    2.27656   .9142492     2.49   0.013      .484455    4.068665
               Other  |   2.653246   1.944347     1.36   0.172    -1.158049    6.464542
                      |
             sex#race |
        Female#Black  |   1.005509     1.2519     0.80   0.422    -1.448457    3.459475
        Female#Other  |  -.6156061    2.76858    -0.22   0.824    -6.042559    4.811347
                      |
                  age |   .6007905   .0116708    51.48   0.000     .5779133    .6236676
               height |  -.3291986   .0308365   -10.68   0.000    -.3896441   -.2687532
               weight |   .4669846   .0144179    32.39   0.000     .4387228    .4952465
                _cons |   125.2161   5.318721    23.54   0.000     114.7904    135.6419
        -------------------------------------------------------------------------------
        
        .
        .
        .
        . *SHOW COEFFICIENTS' MATRIX WITH BASELEVELS COEFFICIENTS
        
        .
        . mat list e(b)
        
        e(b)[1,15]
                    1b.          2.         1b.          2.          3.     1b.sex#     1b.sex#     1b.sex#     2o.sex#      2.sex#      2.sex#
                   sex         sex        race        race        race     1b.race     2o.race     3o.race     1b.race      2.race      3.race
        y1           0  -3.1337772           0   2.2765602   2.6532464           0           0           0           0   1.0055091  -.61560607
        
                                                          
                   age      height      weight       _cons
        y1   .60079047  -.32919865   .46698461   125.21614
        
        .
        .
        .
        . *GENERATE VARIABLES OF INTERACTIONS USING FVEXPAND
        
        .
        . fvexpand i.sex#i.race
        
        .
        .
        .
        . *SELECT ONLY NON-BASELEVELS INTERACTIONS VARIABLES
        
        .
        . local nonbase= ustrregexra(" " + "`r(varlist)'" + " ",  "\b\S*b\.\S*\b", "")
        
        .
        .
        .
        . *RUN REGRESSION WITH NON-BASELEVELS INTERACTIONS VARIABLES
        
        .
        . reg bpsystol i.sex i.race `nonbase' age height weight
        
              Source |       SS           df       MS      Number of obs   =    10,351
        -------------+----------------------------------   F(8, 10342)     =    589.09
               Model |   1763880.4         8   220485.05   Prob > F        =    0.0000
            Residual |  3870789.63    10,342  374.278633   R-squared       =    0.3130
        -------------+----------------------------------   Adj R-squared   =    0.3125
               Total |  5634670.03    10,350  544.412563   Root MSE        =    19.346
        
        -------------------------------------------------------------------------------
             bpsystol | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
        --------------+----------------------------------------------------------------
                  sex |
              Female  |  -3.133777   .5672512    -5.52   0.000    -4.245699   -2.021855
                      |
                 race |
               Black  |    2.27656   .9142492     2.49   0.013      .484455    4.068665
               Other  |   2.653246   1.944347     1.36   0.172    -1.158049    6.464542
                      |
             sex#race |
        Female#Black  |   1.005509     1.2519     0.80   0.422    -1.448457    3.459475
        Female#Other  |  -.6156061    2.76858    -0.22   0.824    -6.042559    4.811347
                      |
                  age |   .6007905   .0116708    51.48   0.000     .5779133    .6236676
               height |  -.3291986   .0308365   -10.68   0.000    -.3896441   -.2687532
               weight |   .4669846   .0144179    32.39   0.000     .4387228    .4952465
                _cons |   125.2161   5.318721    23.54   0.000     114.7904    135.6419
        -------------------------------------------------------------------------------
        
        .
        .
        .
        . *RESULTING COEFFICIENTS' MATRIX
        
        .
        . mat list e(b)
        
        e(b)[1,11]
                    1b.          2.         1b.          2.          3.      2.sex#      2.sex#                                                
                   sex         sex        race        race        race      2.race      3.race         age      height      weight       _cons
        y1           0  -3.1337772           0   2.2765602   2.6532464   1.0055091  -.61560607   .60079047  -.32919865   .46698461   125.21614
        
        .
        Last edited by Andrew Musau; 19 Jul 2025, 10:33.

        Comment

        Working...
        X