Announcement

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

  • How to add reference level(s) for regression interaction in Stata

    Hi,
    I have this data (attached - my_data.csv ) and I want to add a reference levels (if possible) for interaction term to be explicitly visible
    in regression summary table (something like extended coefficients table in eg. SPSS) ?
    I want to use model specified like this in R:
    model3 <- lm(response ~ time * treatment, data = my_data)
    so response is DV and categorical predictors are time and treatment. Time has got two levels:
    Pre and Post, and treatment has two levels as well: Control and Treatment.

    I want to achieve something like this:



    or this:


    Can it be done in Stata ?
    Attached Files

  • #2
    Use option allbaselevels:

    Code:
    sysuse nlsw88, clear
    regress wage i.race##i.married, allbaselevels
    Results:

    Code:
          Source |       SS           df       MS      Number of obs   =     2,246
    -------------+----------------------------------   F(5, 2240)      =      7.22
           Model |   1180.1563         5   236.03126   Prob > F        =    0.0000
        Residual |  73187.8111     2,240    32.67313   R-squared       =    0.0159
    -------------+----------------------------------   Adj R-squared   =    0.0137
           Total |  74367.9674     2,245  33.1260434   Root MSE        =     5.716
    
    --------------------------------------------------------------------------------
              wage | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
    ---------------+----------------------------------------------------------------
              race |
            White  |          0  (base)
            Black  |  -2.194947    .415727    -5.28   0.000    -3.010198   -1.379697
            Other  |  -.4967496   2.037457    -0.24   0.807    -4.492251    3.498752
                   |
           married |
           Single  |          0  (base)
          Married  |  -1.204674    .309034    -3.90   0.000    -1.810697   -.5986511
                   |
      race#married |
     White#Single  |          0  (base)
    White#Married  |          0  (base)
     Black#Single  |          0  (base)
    Black#Married  |   1.439186   .5661142     2.54   0.011     .3290224    2.549349
     Other#Single  |          0  (base)
    Other#Married  |   1.375469   2.448432     0.56   0.574    -3.425963    6.176901
                   |
             _cons |   8.929288   .2590186    34.47   0.000     8.421347     9.43723
    --------------------------------------------------------------------------------

    Comment


    • #3
      Thank you, how to set base level for a specific variable ? I have attached my_data, can you show me how to use it, step-by-step please ?
      Last edited by Andrzej Andrzej; 21 Feb 2024, 05:39.

      Comment


      • #4
        The general practice is that we don't download data through attachment, due to safety and other reasons. (see FAQ section 12) A much more preferred way to share sample data is by posting the results of a command called dataex. More recent versions of Stata has it by default. If yours does not have it, you may be able to download it from ssc.

        Whenever we cannot get the actual data, then we usually use a sample data set in Stata to show the concept, then the original poster needs to adopt the codes themselves.

        To set a particular level as your base group, you'll first need to find out the coding scheme of that variable using codebook.

        Code:
        sysuse nlsw88, clear
        codebook race married
        And the schemes are:

        Code:
        ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        race                                                                                                                                                                                        Race
        ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        
                          Type: Numeric (byte)
                         Label: racelbl
        
                         Range: [1,3]                         Units: 1
                 Unique values: 3                         Missing .: 0/2,246
        
                    Tabulation: Freq.   Numeric  Label
                                1,637         1  White
                                  583         2  Black
                                   26         3  Other
        
        ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        married                                                                                                                                                                                  Married
        ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        
                          Type: Numeric (byte)
                         Label: marlbl
        
                         Range: [0,1]                         Units: 1
                 Unique values: 2                         Missing .: 0/2,246
        
                    Tabulation: Freq.   Numeric  Label
                                  804         0  Single
                                1,442         1  Married
        Stata uses the lowest one by default. If we'd like to use "Black" in race and "Married" in married as base group, note down their code (2 and 1), and revise the code as:

        Code:
        regress wage ib2.race##ib1.married, allbaselevels
        This is the effect:

        Code:
              Source |       SS           df       MS      Number of obs   =     2,246
        -------------+----------------------------------   F(5, 2240)      =      7.22
               Model |   1180.1563         5   236.03126   Prob > F        =    0.0000
            Residual |  73187.8111     2,240    32.67313   R-squared       =    0.0159
        -------------+----------------------------------   Adj R-squared   =    0.0137
               Total |  74367.9674     2,245  33.1260434   Root MSE        =     5.716
        
        --------------------------------------------------------------------------------
                  wage | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
        ---------------+----------------------------------------------------------------
                  race |
                White  |   .7557614   .3842609     1.97   0.049     .0022168    1.509306
                Black  |          0  (base)
                Other  |    1.63448   1.390834     1.18   0.240    -1.092978    4.361938
                       |
               married |
               Single  |  -.2345117    .474324    -0.49   0.621    -1.164672     .695649
              Married  |          0  (base)
                       |
          race#married |
         White#Single  |   1.439186   .5661142     2.54   0.011     .3290224    2.549349
        White#Married  |          0  (base)
         Black#Single  |          0  (base)
        Black#Married  |          0  (base)
         Other#Single  |   .0637171   2.474732     0.03   0.979    -4.789291    4.916725
        Other#Married  |          0  (base)
                       |
                 _cons |   6.968853   .3453187    20.18   0.000     6.291675    7.646031
        --------------------------------------------------------------------------------

        Comment


        • #5
          Thank you very much indeed, I am still beginner in Stata.It helps a lot.
          Is it possible to export output window to ms word somehow ?

          Comment


          • #6
            see help etable
            ---------------------------------
            Maarten L. Buis
            University of Konstanz
            Department of history and sociology
            box 40
            78457 Konstanz
            Germany
            http://www.maartenbuis.nl
            ---------------------------------

            Comment

            Working...
            X