Announcement

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

  • Creating a graph using coefplot

    Hi all,

    I'm trying to use the user-written coefplot command from SSC to plot a graph. I'm running regressions to estimate the effect of unemployment on job security satisfaction by mental health and job and demographic characteristics. I therefore have several subsamples. I want to plot the estimates of unemployment for all mental health and job-related subsamples in one column, and mental health and demographic subsamples in another.

    What I have done so far is to estimate and store separate regressions and then try to combine them all together. Below is the code for four subsamples on mental health and sector of employment. I similarly have 2 more sets of four regressions for type of job contract and tenure and then another set of 12 regressions for mental health and gender, age (young vs old), and education (high vs low).

    Code:
    xtreg job_sec_sat unemp_av6 i.sa4 i.year if entry3==0&mh8_qn1==0&private==1, fe i(id) cluster(id) 
    est store e1
    
    xtreg job_sec_sat unemp_av6 i.sa4 i.year if entry3==0&mh8_qn1==1&private==1, fe i(id) cluster(id) 
    est store e2
    
    xtreg job_sec_sat unemp_av6 i.sa4 i.year if entry3==0&mh8_qn1==0&private==0, fe i(id) cluster(id) 
    est store e3
    
    xtreg job_sec_sat unemp_av6 i.sa4 i.year if entry3==0&mh8_qn1==1&private==0, fe i(id) cluster(id) 
    est store e4
    After running all 24 regressions, I used the following command to plot them on a graph:

    Code:
    coefplot e1 e2 e3 e4 p1 p2 p3 p4 t1 t2 t3 t4, bylabel(Job characteristics) ///
        || m1 m2 f1 f2 y1 y2 o1 o2 a1 a2 b1 b2, bylabel(Demographics) /// 
        || , keep(unemp_av6)
    This gives me the following:
    Click image for larger version

Name:	coefplot.png
Views:	1
Size:	25.6 KB
ID:	1754356




    What I would ideally like to produce is something like the graph below, where instead of the legend in my graph with the letters, I would have the relevant labels appearing on the y-axis. The legend would alternatively distinguish between better and poor mental health in blue and red colour, respectively. For example e1 and e2 would have the common axis title of private sector, with e1 appearing in blue to denote better mental health and e2 appearing in red to denote poor mental health. Likewise, e2 and e3 would have the title of public sector and so on. Given that the axis titles for the two groups (job characteristics and demographics) would be different in my case unlike in the example below, I'm ok to have two separate graphs as well.


    Click image for larger version

Name:	graph.png
Views:	1
Size:	69.2 KB
ID:	1754359



    I have also included an example of my dataset if that would help:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str7 xwaveid float year int sa4 byte job_sec_sat float(unemp_av6 entry3 mh8_qn1 private permanent tenure_qn1 sex_avg1 young bach_above1)
    "0100003" 2001 405 10  8.533333 1 0 . . 0 1 0 0
    "0100003" 2002 405  9  6.816667 1 0 1 . 0 1 0 .
    "0100003" 2003 405  2  7.516666 1 0 1 . 0 1 0 .
    "0100003" 2004 405  4  5.366667 0 0 1 . 0 1 0 .
    "0100003" 2005 405  2       3.2 0 0 1 . 0 1 0 .
    "0100003" 2006 405  . 4.6666665 0 0 . . 0 1 0 .
    "0100003" 2008 405  .  5.383333 0 0 . . 0 1 0 .
    "0100003" 2009 405  8       2.5 0 0 1 0 0 1 0 .
    "0100003" 2011 405  .  3.383333 0 0 . . 0 1 0 .
    "0100003" 2012 405  .         4 0 0 . . 0 1 0 .
    "0100003" 2013 405  .  6.016667 0 0 . . 0 1 0 .
    "0100003" 2014 403  .  7.266667 0 0 . . 0 1 0 .
    "0100003" 2015 403  .       7.8 0 0 . . 0 1 0 .
    "0100003" 2016 403  .  6.933333 0 0 . . 0 1 0 .
    "0100003" 2017 403  .       6.8 0 0 . . 0 1 0 .
    "0100003" 2018 403  .  6.416667 0 0 . . 0 1 0 .
    "0100010" 2002 207  3       4.5 1 0 1 . 0 0 1 1
    "0100010" 2003 207  2  5.116667 1 0 1 . 0 0 1 .
    "0100010" 2004 207  1  3.466667 1 0 1 . 0 0 1 .
    "0100010" 2005 207  2 4.2166667 0 0 1 . 0 0 1 .
    "0100010" 2006 207  .       4.4 0 0 . . 0 0 0 .
    "0100010" 2007 207  .  3.533333 0 0 . . 0 0 0 .
    "0100010" 2008 206  .      4.75 0 0 . . 0 0 0 .
    "0100010" 2009 206  .  5.733334 0 0 . . 0 0 0 .
    "0100010" 2010 206  1 4.7166667 0 0 1 . 0 0 0 .
    "0100010" 2011 206  1  3.516667 0 0 1 . 0 0 0 .
    "0100010" 2012 208  5 4.4833336 0 0 1 . 0 0 0 .
    "0100010" 2013 208  1  4.766667 0 0 1 . 0 0 0 .
    "0100010" 2014 208  1      5.05 0 0 1 . 0 0 0 .
    "0100010" 2015 208  3  3.583333 0 0 1 . 0 0 0 .
    "0100010" 2016 208  .      3.65 0 0 . . 0 0 0 .
    "0100010" 2017 208  2 4.2833333 0 0 1 . 0 0 0 .
    "0100010" 2018 208  4       3.2 0 0 1 . 0 0 0 .
    "0100010" 2019 208  5 4.0666666 0 0 1 . 0 0 0 .
    "0100014" 2001 106  7  9.333334 1 1 1 1 0 1 0 0
    "0100014" 2002 106  7  9.666666 1 1 1 1 0 1 0 .
    "0100014" 2003 106  9       5.8 1 1 1 1 0 1 0 .
    "0100014" 2004 106  7  5.716667 0 1 1 1 0 1 0 .
    "0100014" 2005 106  7  5.183333 0 1 1 1 0 1 0 .
    "0100014" 2006 106  8  3.866667 0 1 1 1 0 1 0 .
    "0100014" 2007 106  6 3.8166666 0 1 1 1 0 1 0 .
    "0100014" 2008 106  7 3.9333334 0 1 1 1 0 1 0 .
    "0100014" 2009 106  0       6.8 0 1 1 0 0 1 0 .
    "0100014" 2010 106  . 4.7333336 0 1 . . 0 1 0 .
    "0100014" 2011 106  .  3.766667 0 1 . . 0 1 0 .
    "0100014" 2012 106  .  2.866667 0 1 . . 0 1 0 .
    "0100014" 2013 106  .  5.316667 0 1 . . 0 1 0 .
    "0100014" 2014 106  .  7.566667 0 1 . . 0 1 0 .
    "0100014" 2015 106  . 11.416667 0 1 . . 0 1 0 .
    "0100014" 2016 106  .  5.066667 0 1 . . 0 1 0 .
    "0100014" 2018 106  .       6.1 0 1 . . 0 1 0 .
    "0100014" 2019 106  .  5.666667 0 1 . . 0 1 0 .
    "0100015" 2001 106  7  9.333334 1 0 1 1 0 0 0 0
    "0100015" 2002 106  2  9.666666 1 0 1 1 0 0 0 .
    "0100015" 2003 106  8       5.8 1 0 1 1 0 0 0 .
    "0100015" 2004 106  9  5.716667 0 0 1 1 0 0 0 .
    "0100015" 2005 106 10  5.183333 0 0 1 1 0 0 0 .
    "0100015" 2006 106  9  3.866667 0 0 1 1 0 0 0 .
    "0100015" 2007 106  9 3.8166666 0 0 1 1 0 0 0 .
    "0100015" 2009 106 10       6.8 0 0 1 1 0 0 0 .
    "0100015" 2010 106 10 4.7333336 0 0 1 1 0 0 0 .
    "0100015" 2011 106 10  3.766667 0 0 1 1 0 0 0 .
    "0100015" 2012 106  9  2.866667 0 0 1 1 0 0 0 .
    "0100015" 2013 106  8  5.316667 0 0 1 1 0 0 0 .
    "0100015" 2015 106  9 11.416667 0 0 1 1 0 0 0 .
    "0100015" 2016 106 10  5.066667 0 0 1 1 0 0 0 .
    "0100015" 2017 106  8       4.8 0 0 1 1 0 0 0 .
    "0100015" 2018 106  8  6.166667 0 0 1 1 0 0 0 .
    "0100015" 2019 106  8  5.383333 0 0 1 1 0 0 0 .
    "0100016" 2007 111 10      4.85 1 0 1 1 1 1 1 1
    "0100016" 2008 111  9 4.7166667 1 0 1 1 1 1 1 .
    "0100016" 2009 111  9 4.7166667 1 0 1 1 1 1 1 .
    "0100016" 2010 111  9       4.5 0 0 1 1 1 1 1 .
    "0100016" 2011 111  9  4.116667 0 0 1 1 1 1 1 .
    "0100016" 2012 111  9      5.85 0 0 1 1 1 1 1 .
    "0100016" 2013 111  9  6.333333 0 0 1 1 1 1 1 .
    "0100016" 2014 111  9       6.7 0 0 1 1 1 1 1 .
    "0100016" 2015 111  9  6.766666 0 0 1 1 1 1 1 .
    "0100016" 2016 111  9      5.85 0 0 1 1 1 1 1 .
    "0100016" 2017 111 10  5.633333 0 0 1 1 1 1 1 .
    "0100016" 2018 111 10  6.033333 0 0 1 1 1 1 1 .
    "0100016" 2019 111 10  4.366667 0 0 1 1 1 1 1 .
    "0100018" 2001 507  .  7.133333 1 1 . . . 0 1 0
    "0100018" 2002 507  .  5.516666 1 1 . . . 0 0 .
    "0100018" 2003 507  .  6.766666 1 1 . . . 0 0 .
    "0100018" 2004 507  .       5.3 0 1 . . . 0 0 .
    "0100018" 2005 507  .      4.85 0 1 . . . 0 0 .
    "0100018" 2006 507  2 2.8166666 0 1 0 0 . 0 0 .
    "0100018" 2007 507  2  2.966667 0 1 0 0 . 0 0 .
    "0100018" 2008 507  3 3.6666665 0 1 0 0 . 0 0 .
    "0100018" 2009 507  4      5.45 0 1 1 0 . 0 0 .
    "0100018" 2010 507  5  5.433333 0 1 0 0 . 0 0 .
    "0100018" 2011 507  6 4.4333334 0 1 0 0 . 0 0 .
    "0100018" 2012 507  8  4.133333 0 1 0 1 . 0 0 .
    "0100018" 2013 507  7  5.266666 0 1 0 1 . 0 0 .
    "0100018" 2014 507  8  5.516666 0 1 0 1 . 0 0 .
    "0100018" 2015 507  8       5.7 0 1 0 1 . 0 0 .
    "0100018" 2016 507  7  6.316667 0 1 0 1 . 0 0 .
    "0100018" 2018 507  7  6.883333 0 1 0 1 . 0 0 .
    "0100018" 2019 507  9  7.016666 0 1 0 1 . 0 0 .
    end
    label values job_sec_sat AJBMS
    label def AJBMS 0 "[0] Totally dissatisfied", modify
    label def AJBMS 10 "[10] Totally satisfied", modify
    Thank you very much.

    Attached Files

  • #2
    You have enclosed a data example, but it generates no results when combined with your codes. We don't need all 24 regressions, 2 for each group should be sufficient. You can constrain the sample period, but make sure that the combination of your data and codes produces a reproducible example.
    Last edited by Andrew Musau; 25 May 2024, 05:38.

    Comment


    • #3
      Thanks Andrew, I'm unfortunately unable to include a sample size large enough for there to be enough observations to run each regression, as it exceeds the character limit. The following lines of code was missing earlier which would have prevented running, but even with this it gives an error msg of insufficient observations. Do you have any other suggestions on how I can show you the data and code that will run smoothly?

      Code:
      destring xwaveid, gen(id)
      sort xwaveid year
      xtset id year

      Comment


      • #4
        Alternatively, would you know what I need to change in the following code, so that the graph I produce shows the current legend names (private sector, public sector etc.) on the y-axis alongside the relevant estimate instead of in the legend? And that all estimates appear in blue instead of multiple colours?

        Code:
          coefplot (e1, label(Private sector)) (e2, label(Public sector)) (p1, label(Permanent contract)) (p2, label(Fixed or casual contract)) (t1, label(High tenure)) (t2, label(Low tenure)), bylabel(Better MH) ///
          || (e3, label(Private sector)) (e4, label(Public sector)) (p3, label(Permanent contract)) (p4, label(Fixed or casual contract)) (t3, label(High tenure)) (t4, label(Low tenure)), bylabel(Poor MH) ///
          || , keep(unemp_av6)
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	25.9 KB
ID:	1754809

        Comment


        • #5
          It is not difficult to provide a reproducible example. In fact, you can go regress and keep only the outcome and "unemp_av6". Then 4 regressions are sufficient. Sorry, I am too busy to do this for you.

          Code:
          keep job_sec unemp_av6 in 1/100
          eststo e1: regress job_sec unemp_av6 in 1/25
          eststo e2: regress job_sec unemp_av6 in 26/50
          ...
          coefplot ...

          Comment

          Working...
          X