Announcement

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

  • Visual illustration of coefficients from an interaction model

    Hi all,

    I need some assistance in graphically representing selected coefficients from an interaction model, using either coefplot or margins. In particular, I'm estimating the following regression to test for differential effects of selected explanatory variables on perceived job security for different samples.

    Code:
    xtreg jobsec gender2##(c.unemp male1 c.age c.tenure contract c.fire1 c.hire1 i.j1), fe i(id) cluster(id)
    xtreg jobsec gender2##(c.unemp male1 c.age c.tenure contract c.fire1 c.hire1 i.j1) if mh9_q1==0, fe i(id) cluster(id) 
    xtreg jobsec gender2##(c.unemp male1 c.age c.tenure contract c.fire1 c.hire1 i.j1) if mh9_q1==1, fe i(id) cluster(id)
    For all of these, I want to plot only the interaction between gender2 and unemp (difference in unemployment effect on job security by gender), which would numerically be obtained by the code:
    Code:
    lincom 0b.gender2#co.unemp-1.gender2#c.unemp
    .

    How would I illustrate this value graphically, such that I can combine all three coefficients from the three regression estimations in one graph?

    Thank you very much.

  • #2
    You don't supply example data, so I'll show you the code that would do something like this in a similar data set with similar regressions. In this case, the focused interaction is between the variables collgrad and age:
    Code:
    clear*
    webuse nlswork
    
    local cond1
    local cond2 if union == 0
    local cond3 if union == 1
    
    frame create interactions int model float coefficient
    
    forvalues i = 1/3 {
        xtreg ln_wage i.collgrad##(c.age i.msp c.grade) `cond`i'', fe cluster(idcode)
        frame post interactions (`i') (_b[1.collgrad#c.age])
    }
    
    frame change interactions
    label define model    1    "All"    ///
                        2    "Non-Union"    ///
                        3    "Union"
    label values model model
    
    graph bar coefficient, over(model)
    In the future, when asking for help with code, please use the -dataex- command and show example data. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.


    Comment


    • #3
      Hi Clyde,

      Thank you very much for the code. I followed that but the resulting graph I generated does not seem correct; I'm not sure what the mistake I made is. I have attached it here for your reference.

      Click image for larger version

Name:	Graph.png
Views:	1
Size:	30.0 KB
ID:	1747266



      Following your instructions, I used the -dataex- command to show example data below:
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte(jobsec unemp) float male1 byte age double tenure byte contract float(fire1 hire1 j1 mh9_q1)
      7 -2 1 36  3 1  2  0  5 1
      7  0 1 47 10 0 17  0  2 1
      9 -2 1 60 10 1  0 16  8 1
      7  6 1 25  1 1  0  0  7 1
      7  6 1 63  1 0  0  0 11 1
      5 -2 0 64  1 0  8  0  1 1
      5  0 0 41  3 0 13  0  4 1
      8  4 0 57 .5 1  0  0  6 1
      6 -2 1 48  1 1  5  0  6 0
      6  2 1 41  1 0  0 18  2 0
      5  4 1 37 .5 1  0  0  7 0
      8 -1 1 44 10 1  0  0  3 0
      3  0 0 61  1 0 14  0 12 0
      7  6 0 27  3 0  0  6  5 0
      5  4 0 30  3 0  0  0  9 0
      5  4 0 45  1 0  0  0 11 0
      4  4 1 38 .5 0  2  0 10 0
      8  4 1 37  3 0  0 17  1 0
      8 -1 1 44 10 1  0  0  7 0
      4  4 0 36  1 0 10  0 12 0
      end
      Can you please let me know if you can figure out what is wrong. Thank you.

      Comment


      • #4
        I'd like to help, but your example data set does not contain the variable id, nor gender2, both of which are needed to run the regressions. Please post back.

        Also please show the exact actual code you ran. It is possible that you have subtly modified its logic when translating the variables from my example to your data set.

        Comment


        • #5
          Apologies for the oversight. Here is the dataset with the variables id and gender2.

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input float(id gender2) byte(jobsec unemp) float male1 byte age double tenure byte contract float(fire1 hire1 j1 mh9_q1)
          1 0 7 -2 1 36  3 1  2  0  5 1
          1 0 7  0 1 47 10 0 17  0  2 1
          1 0 9 -2 1 60 10 1  0 16  8 1
          1 0 7  6 1 25  1 1  0  0  7 1
          1 0 7  6 1 63  1 0  0  0 11 1
          1 0 5 -2 0 64  1 0  8  0  1 1
          1 0 5  0 0 41  3 0 13  0  4 1
          1 0 8  4 0 57 .5 1  0  0  6 1
          2 0 6 -2 1 48  1 1  5  0  6 0
          2 0 6  2 1 41  1 0  0 18  2 0
          2 0 5  4 1 37 .5 1  0  0  7 0
          2 0 8 -1 1 44 10 1  0  0  3 0
          2 0 3  0 0 61  1 0 14  0 12 0
          2 0 7  6 0 27  3 0  0  6  5 0
          2 0 5  4 0 30  3 0  0  0  9 0
          2 0 5  4 0 45  1 0  0  0 11 0
          3 1 4  4 1 38 .5 0  2  0 10 0
          3 1 8  4 1 37  3 0  0 17  1 0
          3 1 8 -1 1 44 10 1  0  0  7 0
          3 1 4  4 0 36  1 0 10  0 12 0
          end
          Below is the code I ran to generate the graph:
          Code:
          local cond1
          local cond2 if mh9_q1 == 0
          local cond3 if mh9_q1 == 1
          
          frame create interactions int model float coefficient
          
          forvalues i = 1/3 {
              xtreg jobsec gender2##(c.unemp male1 c.age c.tenure contract c.fire1 c.hire1 i.j1) `cond`i'', fe cluster(id)
              frame post interactions (`i') (_b[1.gender2#c.unemp])
          }
          
          frame change interactions
          label define model    1    "All"    ///
                              2    "Better mental health"    ///
                              3    "Poor mental health"
          label values model model
          
          graph bar coefficient, over(model)

          Comment


          • #6
            When I try to run this code on this example, the regressions largely "collapse." That is, most of the variables end up being omitted due to colinearity (I presume, and have checked in a few cases, with the fixed effect id. Even the interaction term of interest, i.gender2#c.unemp is omitted in all three models, so there is nothing to work with here. I do not see any obvious problems with the porting of the code from my example to yours. And the example worked well on my setup. I can't see what's happening when you run the code with your real data set--you don't mention any of those problems, so presumably in the full data set there is both more data and more variation within id's.

            Reflecting back on #1, you show your graph, but you don't say what you think is actually wrong with it. I suppose you are concerned that all three coefficients appear to be exactly 0. Did you look at the regression results that were printed out along the way. Maybe that's really what happened. I suspect, in fact, that it is. So in that case it means one of three things:
            1. There is something wrong with your data.
            2. There is something wrong with your regression model.
            3. There is nothing wrong: these are the actual correct results. Only your expectations are wrong.
            Now, I doubt very much #3 is the issue, since it is so rare to get exactly 0 coefficients from a well-thought out regression applied to a well-managed data set.

            I think you have to scrutinize all of the regression outputs. Among other things, make sure to check the coefficient you get for 1.gender2#c.unemp in each one: maybe they really are all zero. Then look for signs of badly behaved regressions/data: lots of variables omitted due to colinearity, or ridiculous regression coefficients. Then consider whether your data set is defective, and if it isn't, consider whether your regression model is ill-conceived. I'm happy to assist with this process if you show the regression outputs themselves, and a larger sample of the data.

            Comment


            • #7
              Hi Clyde,

              Thank you very much for the feedback. Yes the gender variable is omitted from the regressions due to collinearity, since gender is not time-invariant for individuals - would this be an issue in trying to estimate the differential impact of unemployment on job security by gender, which is what I want to do? I presume the 20 observation dataset I shared doesn't have enough variation, causing other variables to be omitted. I have now attached a dataset of 200 observations below:

              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input float(id gender2) byte(jobsec unemp) float male1 byte age double tenure byte contract float(fire1 hire1 j1 mh9_q1)
               1 0  7 -2 1 36  3 1  2  0  5 1
               1 0  7  0 1 47 10 0 17  0  2 1
               1 0  9 -2 1 60 10 1  0 16  8 1
               1 0  7  6 1 25  1 1  0  0  7 1
               1 0  7  6 1 63  1 0  0  0 11 1
               1 0  5 -2 0 64  1 0  8  0  1 1
               1 0  5  0 0 41  3 0 13  0  4 1
               1 0  8  4 0 57 .5 1  0  0  6 1
               2 0  6 -2 1 48  1 1  5  0  6 0
               2 0  6  2 1 41  1 0  0 18  2 0
               2 0  5  4 1 37 .5 1  0  0  7 0
               2 0  8 -1 1 44 10 1  0  0  3 0
               2 0  3  0 0 61  1 0 14  0 12 0
               2 0  7  6 0 27  3 0  0  6  5 0
               2 0  5  4 0 30  3 0  0  0  9 0
               2 0  5  4 0 45  1 0  0  0 11 0
               3 1  4  4 1 38 .5 0  2  0 10 0
               3 1  8  4 1 37  3 0  0 17  1 0
               3 1  8 -1 1 44 10 1  0  0  7 0
               3 1  4  4 0 36  1 0 10  0 12 0
               3 1  5  2 0 63  3 0 14  0  6 0
               3 1  8  2 0 26  1 1  0 14  8 0
               3 1  8  2 0 40 10 1  0 12  3 0
               3 1  7 -2 0 47  3 0  0  0  9 0
               4 0  1  4 1 27  1 1 10  0  3 1
               4 0  1  6 1 41 10 0 18  0 12 1
               4 0  7 -1 1 54  1 1  0 15  1 1
               4 0  6  0 1 54  3 0  0  0  9 1
               4 0  4  6 0 40  1 0  3  0  6 1
               4 0  7 -1 0 56  3 1  0  8  2 1
               4 0  6 -2 0 36  3 1  0  0  8 1
               4 0  5  6 0 55  3 1  0  0 11 1
               5 0  5  0 1 61  1 1 17  0 11 0
               5 0  5 -2 1 27 .5 0  0 15  6 0
               5 0  5  2 1 61  1 1  0  0  3 0
               5 0  5  0 1 50  3 0  0  0 10 0
               5 0  5  0 0 46  3 1  0  6  9 0
               5 0  5  0 0 46 10 0  0 20 12 0
               5 0  5 -1 0 50 .5 0  0  0  1 0
               5 0  5  0 0 48 10 0  0  0  5 0
               6 1  4  0 1 40  1 0 12  0 11 0
               6 1  9  4 1 41 10 1  0  9  6 0
               6 1  8  0 1 41  1 1  0  0  4 0
               6 1  6  4 1 62  3 1  0  0 10 0
               6 1  2  6 0 64 10 1 18  0  5 0
               6 1  8  6 0 62 .5 1  0 14  8 0
               6 1  8  2 0 46  3 1  0  3  1 0
               6 1  7  0 0 41  1 0  0  0  9 0
               7 1  3  4 1 43  3 1  5  0  2 0
               7 1  8  4 1 28  1 0  0  9  7 0
               7 1 10  0 1 51  3 1  0  9 10 0
               7 1  6  0 1 29 .5 0  0  0 12 0
               7 1  0 -1 0 64 .5 0 19  0  3 0
               7 1 10  4 0 62 10 0  0 15  4 0
               7 1 10 -2 0 51  1 1  0  0  5 0
               7 1  9 -2 0 38  1 1  0  0 11 0
               8 0  7  2 1 33  3 1  0 13  8 1
               8 0  4  4 1 59  1 1  0 17 10 1
               8 0  7  4 1 52 .5 0  0  0  3 1
               8 0  4 -1 1 62 10 1  0  0  6 1
               8 0  6  0 0 42 10 1 11  0  5 1
               8 0  7  4 0 47  1 0  0 11  4 1
               8 0  7 -1 0 51 10 0  0 14 12 1
               8 0  4  2 0 64  1 1  0  0  9 1
               9 1  7  2 1 38 .5 1 16  0  7 0
               9 1  6  0 1 59  3 1  8  0  3 0
               9 1  6  0 1 36  3 1  0 19  1 0
               9 1  7  6 0 37 .5 0  4  0 11 0
               9 1  6  0 0 53 .5 0  0 14 12 0
               9 1  5  4 0 62  1 1  0 11 10 0
               9 1  8  0 0 44 .5 0  0  0  2 0
               9 1  7  2 0 35  1 0  0  0  9 0
              10 0  5  6 1 47 10 0 15  0 10 0
              10 0  9 -1 1 39  1 1  0 16  6 0
              10 0  7 -1 1 64  1 1  0  0  4 0
              10 0  5  6 1 65 10 0  0  0  8 0
              10 0  6  0 0 53  1 1 15  0  9 0
              10 0  6 -1 0 41  3 1 17  0  5 0
              10 0  5 -2 0 65 .5 0  0 16  2 0
              10 0  5  0 0 65  3 0  0 10  7 0
              11 0  3 -2 1 54  1 1 16  0  4 1
              11 0  7 -2 1 47  3 1  0  6 11 1
              11 0  6  0 1 34  1 1  0  0  8 1
              11 0  4  0 0 49  1 1 20  0  7 1
              11 0  3  6 0 47  3 0  9  0 10 1
              11 0  5  4 0 62 .5 1  0 20  9 1
              11 0  6  0 0 63  1 1  0 10  5 1
              11 0  5 -2 0 44  1 0  0  0  2 1
              12 0  3 -1 1 64  1 0 10  0  8 1
              12 0  9 -1 1 35 .5 1  0  3 11 1
              12 0  5 -2 1 44 10 0  0 20 12 1
              12 0  1  4 1 65 .5 0  0  0  9 1
              12 0  4 -2 0 55 10 0 10  0  5 1
              12 0  3  6 0 28  1 1  0 20  4 1
              12 0  6 -1 0 52  3 0  0  0  1 1
              12 0  3  0 0 62  3 1  0  0  3 1
              13 1  6  6 1 47 10 1  4  0  8 1
              13 1  6  6 1 55  1 1  0  6  6 1
              13 1  4 -2 1 44  3 0  0 16  9 1
              13 1  4 -2 1 60  1 0  0  0  2 1
              13 1  6 -2 0 47  1 1 15  0  7 1
              13 1  4  4 0 44  3 1  4  0 11 1
              13 1  5  2 0 63  3 1  0 18  4 1
              13 1  5  0 0 37 .5 0  0  0  3 1
              14 0  1  6 1 57  3 0 14  0  5 1
              14 0  7  0 1 58  3 1  0  3  6 1
              14 0  4 -2 1 65  1 0  0  0  9 1
              14 0  5 -2 1 53  3 0  0  0 10 1
              14 0  4  2 0 52  3 1  3  0 12 1
              14 0  6  6 0 52  1 0  0 18  7 1
              14 0  6  2 0 35  1 0  0  5  4 1
              14 0  7 -1 0 47 10 1  0  0  2 1
              15 0  6 -1 1 53  3 0 12  0  3 0
              15 0  8 -2 1 35  3 0  0 12  1 0
              15 0  6  2 1 64  3 0  0 18  4 0
              15 0  7 -1 1 47  1 0  0  0  6 0
              15 0  5 -1 1 35  1 1  0  0 12 0
              15 0  6 -1 0 48  3 0 10  0  5 0
              15 0  8  0 0 55  1 0  0 14  7 0
              15 0  7  4 0 45  1 1  0  0 10 0
              16 0  9  6 1 25  1 1  5  0  4 0
              16 0  5 -2 1 51  1 1  0  7 10 0
              16 0  6  0 1 35  3 0  0 17  5 0
              16 0  5  4 1 65 .5 1  0  0  3 0
              16 0  5 -2 0 54  1 1  0 15 12 0
              16 0  5  2 0 56 10 0  0 16  1 0
              16 0  8  2 0 45  3 1  0  0 11 0
              16 0  7  4 0 55  3 0  0  0  9 0
              17 0  6 -1 1 48  3 1 19  0 12 0
              17 0  7  0 1 64 10 1 19  0  7 0
              17 0  6  4 1 65  3 0  0 18  9 0
              17 0  7  0 1 51  3 1  0  0  1 0
              17 0  3 -2 0 53  1 1 12  0  2 0
              17 0  6  4 0 61 10 0  2  0  6 0
              17 0  8 -2 0 49  3 1  0 18  3 0
              17 0  6  6 0 60  3 1  0  0  8 0
              18 1  2  4 1 26  3 0  5  0 12 0
              18 1  5  0 1 36 10 0  4  0  2 0
              18 1  8  6 1 43 .5 1  0 10  1 0
              18 1  8  0 1 58  3 0  0 12  3 0
              18 1  8  2 0 59 .5 1  0 13 11 0
              18 1  8  6 0 48 10 1  0  8 10 0
              18 1  8 -1 0 56 .5 1  0  0  4 0
              18 1  8  4 0 53  3 1  0  0  9 0
              19 0  6  0 1 44  1 0  0  6  3 0
              19 0  7  0 1 59  1 0  0 15 12 0
              19 0  7  4 1 26 .5 0  0  0 11 0
              19 0  8 -1 0 63 10 1 15  0  5 0
              19 0  7 -2 0 55  3 0  0 15 10 0
              19 0  6  4 0 61  1 0  0 20  9 0
              19 0 10 -2 0 65  3 1  0  0  4 0
              19 0  7  6 0 45 10 0  0  0  8 0
              20 0  3  6 1 60 .5 1 18  0  8 0
              20 0  2  6 1 52 10 0 10  0 11 0
              20 0  2  6 1 40  1 0  0 15  1 0
              20 0  9  6 1 33  1 0  0  0  2 0
              20 0 10 -1 1 39 10 0  0  0  4 0
              20 0  7  6 0 65 10 1  6  0  5 0
              20 0 10  4 0 51  3 0  0  8 10 0
              20 0 10 -2 0 28  3 1  0  0  6 0
              21 0  3  2 1 44  1 0 10  0  7 0
              21 0  3 -2 1 50  3 1  8  0  2 0
              21 0  5  0 1 55  1 0  0  6 12 0
              21 0  9  2 1 37  1 0  0  0  9 0
              21 0  3 -1 0 33 .5 0 10  0  6 0
              21 0  2  0 0 57  3 0  9  0  1 0
              21 0  7 -2 0 52  3 0  0  9  3 0
              21 0  1  6 0 42  1 1  0  0  8 0
              22 1  3  6 1 28 .5 0 18  0  8 1
              22 1  8 -2 1 42 .5 0  0  0  9 1
              22 1  8  0 1 50  1 1  0  0  7 1
              22 1  7  4 0 54  3 1  8  0 11 1
              22 1  3  6 0 50 10 0 18  0  5 1
              22 1 10  0 0 32  1 1  0 13  1 1
              22 1  5 -2 0 57 10 0  0  6 10 1
              22 1  8  0 0 33  3 0  0  0  6 1
              23 0  6 -1 1 57  3 0 20  0  7 0
              23 0  9  2 1 37  3 0  2  0  6 0
              23 0  7 -2 1 41  1 0  0  3  2 0
              23 0  8 -1 1 35 .5 0  0  0 11 0
              23 0  9  4 1 52  1 0  0  0  4 0
              23 0  8  4 0 40 .5 1 12  0  5 0
              23 0  9  6 0 59 10 0  0  3 10 0
              23 0 10 -1 0 47  3 1  0  0  1 0
              24 1  8 -2 1 33  3 1  3  0  1 0
              24 1  8  6 1 36 10 1  6  0 10 0
              24 1  7  6 1 39 .5 1  0  0  6 0
              24 1  3  6 1 58  1 0  0  0  7 0
              24 1  5 -1 0 55  3 0  8  0  8 0
              24 1  4  4 0 52  3 0 18  0  5 0
              24 1  6  0 0 57  3 0  0 17 12 0
              24 1  5 -1 0 62  1 0  0  0  2 0
              25 1  2  2 1 60 10 0 20  0  4 0
              25 1  3  0 1 26  3 0  0  6  8 0
              25 1  4 -2 1 38 10 0  0 16  6 0
              25 1  7 -2 1 54 .5 0  0  0 10 0
              25 1  8  0 1 43 10 0  0  0  3 0
              25 1  7  2 0 60  1 0 14  0  9 0
              25 1  4  6 0 42  1 1  0  9  2 0
              25 1  6  2 0 54 10 0  0  6  5 0
              end

              Yes, it can't be the case that all three interacted coefficients are 0, since when I estimate them via the regression model, they take the values of 0.031, 0.027, and 0.039, respectively; it is these three values that I want to illustrate graphically. I have shown the code and output for the first case below:

              Code:
              xtreg jobsec gender2##(c.unemp male1 c.age c.tenure contract c.fire1 c.hire1 i.j1), fe i(id) cluster(id) 
              
              note: 1.gender2 omitted because of collinearity.
              
              Fixed-effects (within) regression               Number of obs     =     47,880
              Group variable: id                              Number of groups  =      5,985
              
              R-squared:                                      Obs per group:
                   Within  = 0.3702                                         min =          8
                   Between = 0.0465                                         avg =        8.0
                   Overall = 0.2704                                         max =          8
              
                                                              F(36,5984)        =     389.16
              corr(u_i, Xb) = -0.0235                         Prob > F          =     0.0000
              
                                                   (Std. err. adjusted for 5,985 clusters in id)
              ----------------------------------------------------------------------------------
                               |               Robust
                        jobsec | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
              -----------------+----------------------------------------------------------------
                     1.gender2 |          0  (omitted)
                         unemp |  -.1023746   .0046161   -22.18   0.000    -.1114238   -.0933254
                       1.male1 |  -.0001076   .0201411    -0.01   0.996    -.0395914    .0393761
                           age |  -.0160586   .0011262   -14.26   0.000    -.0182664   -.0138509
                        tenure |   .1113531   .0038233    29.12   0.000     .1038581    .1188482
                    1.contract |   1.187352   .0285679    41.56   0.000     1.131349    1.243356
                         fire1 |  -.1587199   .0025217   -62.94   0.000    -.1636634   -.1537764
                         hire1 |   .0186235   .0022109     8.42   0.000     .0142894    .0229577
                               |
                            j1 |
                            2  |  -.0554579   .0525083    -1.06   0.291     -.158393    .0474773
                            3  |  -.0449652   .0523322    -0.86   0.390    -.1475552    .0576247
                            4  |  -.0828984   .0531823    -1.56   0.119    -.1871548    .0213581
                            5  |  -.0135002   .0540783    -0.25   0.803    -.1195132    .0925128
                            6  |  -.0503385   .0543059    -0.93   0.354    -.1567976    .0561206
                            7  |  -.0074377    .053241    -0.14   0.889    -.1118092    .0969338
                            8  |  -.0210827   .0532973    -0.40   0.692    -.1255646    .0833993
                            9  |  -.0621711   .0540401    -1.15   0.250    -.1681091    .0437669
                           10  |  -.0471009   .0532178    -0.89   0.376    -.1514269    .0572251
                           11  |   -.060644   .0527362    -1.15   0.250     -.164026    .0427381
                           12  |  -.0629593   .0534376    -1.18   0.239    -.1677162    .0417977
                               |
               gender2#c.unemp |
                            1  |  -.0307022   .0065976    -4.65   0.000    -.0436359   -.0177684
                               |
                 gender2#male1 |
                          1 1  |   .0031291   .0287068     0.11   0.913    -.0531465    .0594047
                               |
                 gender2#c.age |
                            1  |  -.0031811   .0016352    -1.95   0.052    -.0063866    .0000244
                               |
              gender2#c.tenure |
                            1  |  -.0151036   .0053881    -2.80   0.005    -.0256662    -.004541
                               |
              gender2#contract |
                          1 1  |  -.0938002   .0412976    -2.27   0.023    -.1747584   -.0128421
                               |
               gender2#c.fire1 |
                            1  |   .0179759   .0034911     5.15   0.000      .011132    .0248197
                               |
               gender2#c.hire1 |
                            1  |   .0064455   .0031552     2.04   0.041     .0002601    .0126309
                               |
                    gender2#j1 |
                         1  2  |   .0297077   .0749497     0.40   0.692    -.1172207    .1766361
                         1  3  |  -.0102923   .0758313    -0.14   0.892     -.158949    .1383645
                         1  4  |   .0419288   .0754773     0.56   0.579     -.106034    .1898915
                         1  5  |  -.0010652   .0769991    -0.01   0.989    -.1520112    .1498807
                         1  6  |   .0729612   .0756965     0.96   0.335    -.0754313    .2213536
                         1  7  |   .0883193   .0755891     1.17   0.243    -.0598626    .2365012
                         1  8  |   .0388935   .0752892     0.52   0.605    -.1087005    .1864875
                         1  9  |   .0360328   .0765765     0.47   0.638    -.1140848    .1861504
                         1 10  |   .0650403   .0749509     0.87   0.386    -.0818904    .2119711
                         1 11  |   .0239979   .0742969     0.32   0.747    -.1216509    .1696467
                         1 12  |   .0368708   .0763806     0.48   0.629    -.1128627    .1866043
                               |
                         _cons |   6.504353     .04972   130.82   0.000     6.406884    6.601822
              -----------------+----------------------------------------------------------------
                       sigma_u |  1.2459752
                       sigma_e |  1.6365448
                           rho |  .36694687   (fraction of variance due to u_i)
              ----------------------------------------------------------------------------------
              
              
              lincom 0b.gender2#co.unemp-1.gender2#c.unemp
              
              ------------------------------------------------------------------------------
                    jobsec | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
              -------------+----------------------------------------------------------------
                       (1) |   .0307022   .0065976     4.65   0.000     .0177684    .0436359
              ------------------------------------------------------------------------------

              Comment


              • #8
                Yes the gender variable is omitted from the regressions due to collinearity, since gender is not time-invariant for individuals - would this be an issue in trying to estimate the differential impact of unemployment on job security by gender, which is what I want to do?
                No, it's not an issue at all for this.

                Well, with the new example data, the three regressions run well, and the code correctly captures the values of the interaction coefficient and graphs them:
                Code:
                . xtset id
                
                Panel variable: id (balanced)
                
                .
                . local cond1
                
                . local cond2 if mh9_q1 == 0
                
                . local cond3 if mh9_q1 == 1
                
                .
                . frame create interactions int model float coefficient
                
                .
                . forvalues i = 1/3 {
                  2.     xtreg jobsec gender2##(c.unemp male1 c.age c.tenure contract c.fire1 c.hire1 i.j1) `cond`i'', fe cluster(id)
                  3.     frame post interactions (`i') (_b[1.gender2#c.unemp])
                  4. }
                note: 1.gender2 omitted because of collinearity.
                
                Fixed-effects (within) regression               Number of obs     =        200
                Group variable: id                              Number of groups  =         25
                
                R-squared:                                      Obs per group:
                     Within  = 0.4532                                         min =          8
                     Between = 0.0043                                         avg =        8.0
                     Overall = 0.3062                                         max =          8
                
                                                                F(23, 24)         =          .
                corr(u_i, Xb) = -0.1437                         Prob > F          =          .
                
                                                        (Std. err. adjusted for 25 clusters in id)
                ----------------------------------------------------------------------------------
                                 |               Robust
                          jobsec | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
                -----------------+----------------------------------------------------------------
                       1.gender2 |          0  (omitted)
                           unemp |  -.1974204   .0751206    -2.63   0.015    -.3524617    -.042379
                         1.male1 |  -.1668501   .2843268    -0.59   0.563    -.7536717    .4199715
                             age |   -.030092    .018259    -1.65   0.112    -.0677768    .0075928
                          tenure |   .1051585   .0464169     2.27   0.033     .0093586    .2009583
                      1.contract |    .527914   .3959473     1.33   0.195     -.289281    1.345109
                           fire1 |  -.1428992   .0341886    -4.18   0.000    -.2134611   -.0723373
                           hire1 |  -.0021789   .0263626    -0.08   0.935    -.0565886    .0522307
                                 |
                              j1 |
                              2  |   .1796385   1.048012     0.17   0.865    -1.983351    2.342628
                              3  |   .4880681   1.034718     0.47   0.641    -1.647486    2.623622
                              4  |   1.334948   .8793256     1.52   0.142    -.4798906    3.149787
                              5  |   .7481604   .8267904     0.90   0.375     -.958251    2.454572
                              6  |   .6277552   .7301675     0.86   0.398    -.8792364    2.134747
                              7  |   1.030857   .8994551     1.15   0.263    -.8255269    2.887241
                              8  |   .0934605    .697554     0.13   0.895     -1.34622    1.533141
                              9  |   .7737149   .9486677     0.82   0.423    -1.184239    2.731669
                             10  |   .6728367   1.045831     0.64   0.526    -1.485651    2.831325
                             11  |   1.095008   .5153379     2.12   0.044     .0314032    2.158613
                             12  |  -.1356303   .9086978    -0.15   0.883     -2.01109     1.73983
                                 |
                 gender2#c.unemp |
                              1  |   .1138604   .0980842     1.16   0.257    -.0885755    .3162963
                                 |
                   gender2#male1 |
                            1 1  |  -.4395601   .3572452    -1.23   0.230    -1.176878    .2977577
                                 |
                   gender2#c.age |
                              1  |   .0206878    .034787     0.59   0.558     -.051109    .0924846
                                 |
                gender2#c.tenure |
                              1  |  -.0602824   .0647848    -0.93   0.361    -.1939917    .0734269
                                 |
                gender2#contract |
                            1 1  |    .614458   .5986663     1.03   0.315    -.6211285    1.850044
                                 |
                 gender2#c.fire1 |
                              1  |  -.0448777   .0697753    -0.64   0.526    -.1888869    .0991316
                                 |
                 gender2#c.hire1 |
                              1  |  -.0192378   .0613791    -0.31   0.757     -.145918    .1074425
                                 |
                      gender2#j1 |
                           1  2  |  -2.599875   1.626369    -1.60   0.123    -5.956536    .7567854
                           1  3  |  -1.448218   1.413015    -1.02   0.316    -4.364538    1.468102
                           1  4  |  -1.630617   1.607676    -1.01   0.321    -4.948696    1.687463
                           1  5  |  -1.776717   1.583283    -1.12   0.273    -5.044453    1.491019
                           1  6  |  -1.003861   1.291702    -0.78   0.445    -3.669802    1.662079
                           1  7  |  -1.222709   1.424812    -0.86   0.399    -4.163377    1.717959
                           1  8  |  -.9855387   1.280821    -0.77   0.449    -3.629024    1.657947
                           1  9  |  -.9388671   1.246642    -0.75   0.459     -3.51181    1.634076
                           1 10  |  -1.790179   1.493282    -1.20   0.242    -4.872162    1.291803
                           1 11  |  -1.804615   1.164432    -1.55   0.134    -4.207883     .598654
                           1 12  |  -2.029553     1.2776    -1.59   0.125     -4.66639    .6072836
                                 |
                           _cons |   7.344312   .7040006    10.43   0.000     5.891326    8.797298
                -----------------+----------------------------------------------------------------
                         sigma_u |  1.1321627
                         sigma_e |  1.6662508
                             rho |  .31585367   (fraction of variance due to u_i)
                ----------------------------------------------------------------------------------
                note: 1.gender2 omitted because of collinearity.
                
                Fixed-effects (within) regression               Number of obs     =        136
                Group variable: id                              Number of groups  =         17
                
                R-squared:                                      Obs per group:
                     Within  = 0.4904                                         min =          8
                     Between = 0.0512                                         avg =        8.0
                     Overall = 0.3999                                         max =          8
                
                                                                F(15, 16)         =          .
                corr(u_i, Xb) = -0.0614                         Prob > F          =          .
                
                                                        (Std. err. adjusted for 17 clusters in id)
                ----------------------------------------------------------------------------------
                                 |               Robust
                          jobsec | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
                -----------------+----------------------------------------------------------------
                       1.gender2 |          0  (omitted)
                           unemp |  -.1271447   .0884843    -1.44   0.170    -.3147231    .0604337
                         1.male1 |  -.1784932   .5605546    -0.32   0.754    -1.366816    1.009829
                             age |  -.0381762   .0164784    -2.32   0.034     -.073109   -.0032435
                          tenure |   .1004407   .0616774     1.63   0.123    -.0303096     .231191
                      1.contract |   .5806602   .4181608     1.39   0.184    -.3058011    1.467121
                           fire1 |  -.1161876   .0426842    -2.72   0.015    -.2066741   -.0257011
                           hire1 |  -.0252806   .0417727    -0.61   0.554    -.1138348    .0632737
                                 |
                              j1 |
                              2  |   .1211781   1.520652     0.08   0.937    -3.102461    3.344817
                              3  |   1.268232   .9785518     1.30   0.213    -.8062052    3.342669
                              4  |   2.373729   .9845006     2.41   0.028     .2866813    4.460778
                              5  |   1.198996   1.095867     1.09   0.290    -1.124138     3.52213
                              6  |   1.053064   .8393653     1.25   0.228    -.7263113    2.832439
                              7  |   1.051004   1.064427     0.99   0.338     -1.20548    3.307487
                              8  |  -.6067198   1.089419    -0.56   0.585    -2.916185    1.702745
                              9  |   1.632477   1.312905     1.24   0.232    -1.150757    4.415711
                             10  |    1.26822     1.3681     0.93   0.368    -1.632022    4.168463
                             11  |   .4819143   .6360878     0.76   0.460    -.8665315     1.83036
                             12  |   .3946263   1.203353     0.33   0.747    -2.156368     2.94562
                                 |
                 gender2#c.unemp |
                              1  |   .0302309   .1118724     0.27   0.790     -.206928    .2673898
                                 |
                   gender2#male1 |
                            1 1  |  -.4813819   .7531422    -0.64   0.532    -2.077972    1.115208
                                 |
                   gender2#c.age |
                              1  |   .0253417   .0397376     0.64   0.533    -.0588983    .1095817
                                 |
                gender2#c.tenure |
                              1  |  -.0343424   .0734676    -0.47   0.646    -.1900868    .1214019
                                 |
                gender2#contract |
                            1 1  |   .3629237   .5496672     0.66   0.518    -.8023188    1.528166
                                 |
                 gender2#c.fire1 |
                              1  |  -.0801691   .0860524    -0.93   0.365    -.2625921    .1022539
                                 |
                 gender2#c.hire1 |
                              1  |   .0369698   .0725468     0.51   0.617    -.1168226    .1907622
                                 |
                      gender2#j1 |
                           1  2  |  -2.257061   1.933165    -1.17   0.260    -6.355188    1.841066
                           1  3  |    -1.7859   1.381879    -1.29   0.215    -4.715353    1.143554
                           1  4  |  -1.865982   1.631588    -1.14   0.270    -5.324795     1.59283
                           1  5  |  -1.653999   1.994369    -0.83   0.419    -5.881872    2.573875
                           1  6  |  -1.362762   1.592058    -0.86   0.405    -4.737773     2.01225
                           1  7  |  -1.142377   1.915474    -0.60   0.559    -5.203001    2.918247
                           1  8  |  -.4505476    1.62293    -0.28   0.785    -3.891006    2.989911
                           1  9  |  -1.175961    1.66016    -0.71   0.489    -4.695342     2.34342
                           1 10  |  -1.617564   1.735553    -0.93   0.365    -5.296772    2.061645
                           1 11  |  -.4256857   .9942409    -0.43   0.674    -2.533382    1.682011
                           1 12  |  -2.333675   1.467259    -1.59   0.131    -5.444125    .7767756
                                 |
                           _cons |   7.580005   .7404155    10.24   0.000     6.010394    9.149616
                -----------------+----------------------------------------------------------------
                         sigma_u |  .94848757
                         sigma_e |  1.7504987
                             rho |  .22695691   (fraction of variance due to u_i)
                ----------------------------------------------------------------------------------
                note: 1.gender2 omitted because of collinearity.
                note: 1.gender2#9.j1 omitted because of collinearity.
                note: 1.gender2#10.j1 omitted because of collinearity.
                note: 1.gender2#11.j1 omitted because of collinearity.
                note: 1.gender2#12.j1 identifies no observations in the sample.
                
                Fixed-effects (within) regression               Number of obs     =         64
                Group variable: id                              Number of groups  =          8
                
                R-squared:                                      Obs per group:
                     Within  = 0.7658                                         min =          8
                     Between = 0.0556                                         avg =        8.0
                     Overall = 0.1473                                         max =          8
                
                                                                F(5, 7)           =          .
                corr(u_i, Xb) = -0.9085                         Prob > F          =          .
                
                                                         (Std. err. adjusted for 8 clusters in id)
                ----------------------------------------------------------------------------------
                                 |               Robust
                          jobsec | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
                -----------------+----------------------------------------------------------------
                       1.gender2 |          0  (omitted)
                           unemp |  -.2539245   .1126917    -2.25   0.059    -.5203982    .0125491
                         1.male1 |  -.3534825   .4699877    -0.75   0.477    -1.464827    .7578619
                             age |  -.0325311   .0297775    -1.09   0.311    -.1029437    .0378815
                          tenure |   .0148282   .0955631     0.16   0.881    -.2111425     .240799
                      1.contract |  -.3397587   .7646022    -0.44   0.670    -2.147756    1.468238
                           fire1 |    -.12923   .0524097    -2.47   0.043    -.2531592   -.0053008
                           hire1 |   .0669733   .0184466     3.63   0.008      .023354    .1105926
                                 |
                              j1 |
                              2  |   .4310003   1.650413     0.26   0.801    -3.471606    4.333606
                              3  |  -.6904653   2.042976    -0.34   0.745    -5.521336    4.140405
                              4  |  -.8075251   1.301556    -0.62   0.555    -3.885215    2.270165
                              5  |  -.2671416   .9559217    -0.28   0.788    -2.527537    1.993254
                              6  |   .2707916   1.670954     0.16   0.876    -3.680387     4.22197
                              7  |   .6512984   .9381808     0.69   0.510    -1.567147    2.869744
                              8  |   .0740656    1.15043     0.06   0.950    -2.646268    2.794399
                              9  |  -1.014989   .9582792    -1.06   0.325    -3.280959    1.250982
                             10  |  -1.175742   1.106446    -1.06   0.323    -3.792071    1.440586
                             11  |   1.886611   1.273716     1.48   0.182    -1.125248     4.89847
                             12  |  -1.168747   .9186392    -1.27   0.244    -3.340984    1.003489
                                 |
                 gender2#c.unemp |
                              1  |  -1.216709   .2506102    -4.85   0.002    -1.809308   -.6241104
                                 |
                   gender2#male1 |
                            1 1  |  -.9900292   5.788788    -0.17   0.869    -14.67834    12.69828
                                 |
                   gender2#c.age |
                              1  |   .2130157   .3266438     0.65   0.535    -.5593742    .9854056
                                 |
                gender2#c.tenure |
                              1  |  -.7227888   1.026989    -0.70   0.504    -3.151233    1.705655
                                 |
                gender2#contract |
                            1 1  |   5.437556   2.244435     2.42   0.046     .1303098     10.7448
                                 |
                 gender2#c.fire1 |
                              1  |  -.1101474   .4705735    -0.23   0.822    -1.222877    1.002582
                                 |
                 gender2#c.hire1 |
                              1  |  -.0943735   .0526565    -1.79   0.116    -.2188863    .0301392
                                 |
                      gender2#j1 |
                           1  2  |  -6.188068   6.903887    -0.90   0.400    -22.51317    10.13703
                           1  3  |   1.328319   6.374495     0.21   0.841    -13.74497    16.40161
                           1  4  |  -3.140644   7.584613    -0.41   0.691    -21.07541    14.79412
                           1  5  |   14.26425   12.19495     1.17   0.280    -14.57222    43.10073
                           1  6  |   3.706239    1.07226     3.46   0.011     1.170748     6.24173
                           1  7  |  -4.912713   1.588807    -3.09   0.018    -8.669645   -1.155781
                           1  8  |    12.5116   14.29913     0.87   0.411    -21.30047    46.32366
                           1  9  |          0  (omitted)
                           1 10  |          0  (omitted)
                           1 11  |          0  (omitted)
                           1 12  |          0  (empty)
                                 |
                           _cons |   5.636312   1.342618     4.20   0.004     2.461524      8.8111
                -----------------+----------------------------------------------------------------
                         sigma_u |  4.1541169
                         sigma_e |  1.4198758
                             rho |  .89539382   (fraction of variance due to u_i)
                ----------------------------------------------------------------------------------
                
                .
                . frame change interactions
                
                . label define model    1    "All"    ///
                >                     2    "Better mental health"    ///
                >                     3    "Poor mental health"
                
                . label values model model
                
                .
                . graph bar coefficient, over(model)
                Click image for larger version

Name:	bargraph.png
Views:	1
Size:	83.5 KB
ID:	1747385

                In short, I can't reproduce the problem you are having. I wonder if when you originally ran this you had mistakenly used only a subset of your entire data set, one which led to the regressions essentially failing.

                Comment


                • #9
                  Thanks Clyde, I ran it again and it first produced the same graph it did earlier. I then exited Stata, reopened and ran the same code again and it worked! So I'm really not sure what the issue was, but it's finally working.

                  Thank you so much for all your help, very much appreciated.

                  Comment

                  Working...
                  X