Announcement

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

  • Changing color of reference coefficient in coefplot

    Hello,
    I am using the user-created command coefplot (http://repec.sowi.unibe.ch/stata/coe...g-started.html) to graph coefficient plots, and I am using a baseline year as a comparison. I'd like this baseline's dot to have a different color so that it's clear that it's not an actual estimate.


    Code:
    reghdfe lhsvar  ib0.1##ib2003.year ///
                control1 control2 control3, ///
                absorb(country) vce(cluster country#year)
        
        coefplot, keep(1.1#*year) baselevel  ///
                vertical xlabel(, angle(90)) ylabel(, angle(90)) yline(0) omittedlevels(99 95 90) ///
                ciopts(color(red green blue)) ///
                xtitle("year") ytitle("coefficient")  ///
                xline(5, lpattern(solid) lcolor(black)) graphregion(fcolor(white)) ///
                recast(connected) lpattern(dash) xlabel(, angle(90))
    The baselevel option is the one that shows the coefficient for the reference year (2003). I've tried something like (baselevel, mcolor(red)), where I get "invalid 'mcolor'". Another way I could think of doing it is running the original regression, storing those estimates, and then running a regression that only graphs the baseline and then laying them on top of each other. But I think in this case, the graphs would end up next to each other rather than on top each other (like in the one from this example: https://www.statalist.org/forums/for...ious-estimates)

    What's the best way to change the color of just the coefficient for the reference category?

    Thank you in advance!

  • #2
    As usual, note our preference for reproducible examples (FAQ Advice #12).

    Code:
    sysuse auto, clear
    regress mpg weight displacement i.foreign, nocons
    estimates store m1
    coefplot (m1, baselevels) (m1, baselevels keep(0*foreign) offset(0)), nokey
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	26.1 KB
ID:	1654025

    Comment


    • #3
      Dear Andrew,
      thank you for your help. I've included a -dataex- below. It doesn't replicate the full dataset completely since it's only 100 observations, but it does the trick more or less.

      With the code below, the red and the green coefficients overlap. Also, how do I keep just the coefficients on the interaction (ref3 = 1 # year)? Thank you in advance!
      Code:
      xtset year firmid
       
       reghdfe lhsvar ib1.ref3##ib2013.year ///
               control1 control2 control3, ///
               absorb(country) vce(cluster country#year)
      
       
       estimates store m1 
      
       coefplot (m1, baselevels mcolor(green)) (m1, baselevels keep(1.ref3#*year offset(0))), nokey
      [
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float year long firmid float(lhsvar ref3 control1 control2) double control3 byte country
      2019 207853 2293.1643 0  192296640  15.40637                .065  1
      2016 274973 37248.168 0  115216256  14.91139                .065  5
      2020 229459 22173.137 0  125973840 14.924246                .065  5
      2013 419759  57445.14 0 1825303680 17.459753  .08839999884366989  6
      2013 254976  64213.86 0 1825303680 17.459753  .08839999884366989  6
      2013 200075 23097.373 0 1825303680 17.459753  .08839999884366989  6
      2013 200897  95419.54 0 1825303680 17.459753  .08839999884366989  6
      2014 200877  60437.93 0 1911013632 17.468418  .08839999884366989  6
      2016 199351 141311.33 0 2098095360 17.482891               .0884  6
      2016 515064  45727.72 0 2098095360 17.482891               .0884  6
      2017 273402  120658.9 0 2152038912 17.487696               .0884  6
      2019 557635  29782.64 0 2265766656  17.49023               .0884  6
      2021 419881  53490.51 0          .         .               .0884  6
      2013  15250  20699.28 0  245227328 15.477688 .046300001442432404  8
      2020  16558  47731.73 0  325954720 15.574698               .0463  8
      2018 184208  76184.26 1   46511932 13.781939                .087 10
      2015 443209  19826.25 0  878631424  16.82214                .055 12
      2016 495764  8388.759 0  902306688 16.842123                .055 12
      2018 252916         0 0  985515456 16.872099                .055 12
      2018 447330  72738.73 0  985515456 16.872099                .055 12
      2020 541480  52217.23 0 1064827904 16.894358              .04458 12
      2012  10501 13121.197 0  371462368 16.108406                 .06 13
      2015 290113  83190.77 0  408290624 16.136265                 .06 13
      2020 252614  22303.38 0  488032928  16.18669               .0575 13
      2013 358571  15391.04 0  599305728  16.37241   .0949999988079071 17
      2018  11937  20107.55 0  660249792 16.359055                .095 17
      2020 441905   16253.2 0  697099840 16.348217                .095 17
      2021 229376 126185.45 0          .         .                .095 17
      2017 221811  47070.89 0  279969312  15.71194               .0625 18
      2018 445195  15147.29 0  287156992  15.71739                 .06 18
      2021 511944 11222.512 0          .         .               .0525 18
      2021 289988  111321.8 0          .         .               .0525 18
      2018 230768         0 0  141476384  14.96288                 .12 19
      2015 229695 28890.193 0  132140056  14.88391                 .07 20
      2018 258264   4906.78 0  135025488 14.884607                 .07 20
      2016 526386 146857.17 0  167577152 15.306233                 .06 21
      2017 453833  2460.513 0  169364240  15.30967                 .06 21
      2013   2806         0 0  188208320 15.347212  .07999999821186066 22
      2014   2815 145382.72 0  193243440 15.351503  .07999999821186066 22
      2017 551257 13905.473 0  191386512 15.357456                 .08 22
      2021 541067         0 0          .         .               .0825 24
      2012 259744  40714.61 0  379273536  15.71227                 .08 25
      2018 496121  81795.38 0  440734432  15.74496                 .08 25
      2015 206973  98169.34 0  414261792 16.111523                 .06 26
      2017   2601   34886.2 0  282592448 15.532777                .098 27
      2018   3489 249545.95 0  289728544  15.53984                .098 27
      2019   9458  15466.17 0  293457280 15.545404                .098 27
      2012 219974 36909.555 0  100378720 14.908976                 .05 28
      2016 225745 21356.814 0  101667088 14.910983                 .05 28
      2019 206223   49791.5 0  103602792 14.906838                 .05 28
      2014 440142 11813.968 0  245365328 15.617077               .0625 29
      2019 609120  88891.06 0  267650992 15.630413               .0625 29
      2020 275412  42113.64 0  279865088 15.632215                 .04 29
      2021 423310 14839.133 0          .         .                 .04 29
      2016 454525  87598.69 0   89692592  14.46077               .0781 31
      2020 442293  14229.14 0   98162528 14.476936               .0781 31
      2015 290242  39143.37 1  121422864  14.86931                   0 32
      2016 495239  157149.7 1  124893536 14.886942                   0 32
      2018 357671 31664.275 0   75383904  14.11936                .082 33
      2012   4340 168273.53 0  487534976  15.99544                 .09 34
      2020 228889  39709.41 0  574216000  15.99958                .105 34
      2014  41973  30960.41 0   75230320 14.552788   .0729999989271164 35
      2012 215267 11537.173 0 1049173952 16.789732                .071 36
      2013 183246 221191.67 0 1043893376  16.79239  .07100000232458115 36
      2014 183760 107021.33 0 1066979136 16.793762  .07100000232458115 36
      2018 436697  98742.94 0 1193327232 16.788183                .065 36
      2019 596257  19799.58 0 1212458496 16.784033                .065 36
      2021 236970  76884.47 0          .         .                .065 36
      2014 225812  34852.41 0  384086144 16.111805  .05999999865889549 37
      2016 259356  36467.93 0  409154080 16.134146                 .04 37
      2019   7643  46014.68 0  445717120 16.167017                .025 37
      2013   4047 10580.112 0  470860448 16.264763                   0 39
      2016 211800 26239.695 0  500364992 16.269962                   0 39
      2017 211891  78898.85 0  508876768 16.272163                   0 39
      2019 229474 34962.074 0  522910304 16.274801                   0 39
      2019 356682 30120.156 0  522910304 16.274801                   0 39
      2020 364662  20367.32 0  551979904 16.274519                   0 39
      2018   2665  31027.02 0  165500064 15.187576                 .06 40
      2021 271145  57856.65 0          .         .                .076 41
      2012 264584  29821.45 0  587674816 16.362541               .0999 42
      2017 215293 167692.66 0  630786304  16.36454               .0999 42
      2018 229773 32019.615 0  649345152 16.365667               .0999 42
      2017 357191 69741.766 0  197348144 15.430354                 .05 45
      2019 181724  6178.631 1   43234212 13.695744                   0 46
      2012 430946 18343.275 0  254474448 15.680482                .065 47
      2014 557353 11264.997 0  257880000 15.694154  .06499999761581421 47
      2016 477834  14429.23 0  274492896  15.71032                .065 47
      2015 257292  91685.41 0 1242254592 17.128551                   0 48
      2016 361178  5595.027 0 1226140288 17.144642                   0 48
      2017 452098 19082.668 0 1288102656 17.158054                   0 48
      2018 494881 17195.031 0 1344420992 17.169775                   0 48
      2019 589948 131627.64 0 1375026560  17.18235                   0 48
      2019   7591  74930.81 0 1375026560  17.18235                   0 48
      2020 558539 10093.898 0 1424441088  17.19517                   0 48
      2016 493354  6204.087 0  425424576 15.945842                 .06 51
      2021 212292  167069.3 0          .         .                 .06 51
      2014  36088 66453.445 0  347029280 15.769606                   0 53
      2016 195679  94588.69 0  379976032  15.80338                   0 53
      2020 251424  44581.24 0  454481632   15.8559                   0 53
      2017 277587 21287.014 0  264033904 15.572186                .079 55
      end

      Comment


      • #4
        The -offset- is a separate option and should be outside -keep-. I think your code does keep the wanted coefficient.

        Code:
        coefplot (m1, baselevels mcolor(green)) (m1, baselevels keep(1.ref3#*year) offset(0) ), nokey

        Comment


        • #5
          Hi Andrew, thank you. Yes, the code keeps the wanted coefficient, but it also keeps all the other ones, and I only want to keep ref 3 =1 & year == 20.. So when I run it on the full dataset (it's a bit harder to replicate with the dataex since there are fewer year observations, but it should nonetheless be reproduceable), the graph looks like the image below. I'd like the coefficient on ref3 = 1 # year = 2015 to be green (highlighted in yellow for reference), and to be between ref3 = 1 # year = 2014 and ref3 = 1 # year = 2016. I'd also like these years to be on the x-axis, but when I put - vertical - into one of the parentheses, I get error "option vertical not allowed"

          Am I missing something? Is there a better way to do this? I saw -marginsplot dydx - recommended in other places if you think that would be a better option. Thank you in advance!

          Code:
           coefplot (m1, baselevels mcolor(green) vertical) ///
                         (m1, baselevel keep(1.th3#*year) ///
                                 xlabel(, angle(90)) yline(0) omitted   ///
                                 xtitle("year") ytitle("Title") name(name, replace) ///
                                 xline(5, lpattern(solid) lcolor(black)) graphregion(fcolor(white)) recast(connected) lpattern(dash) xlabel(, angle(90)) offset(0)), nokey
          Click image for larger version

Name:	statalist screenshot .png
Views:	1
Size:	110.6 KB
ID:	1654085

          Comment


          • #6
            For the sake on anyone who is following this thread, note that the OP's code uses reghdfe from SSC. The -vertical- option should be global to the plots (not specific to a plot). Your wanted graph implies two sets of restrictions, one for the coefficients and one for the base coefficient. A slight modification of your data results in the creation of the full set of interactions. To order coefficients, see the -order()- option within -help coefplot-.

            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input float year long firmid float(lhsvar ref3 control1 control2) double control3 byte country
            2019 207853 2293.1643 0  192296640  15.40637                .065  1
            2016 274973 37248.168 0  115216256  14.91139                .065  5
            2020 229459 22173.137 0  125973840 14.924246                .065  5
            2013 419759  57445.14 0 1825303680 17.459753  .08839999884366989  6
            2013 254976  64213.86 0 1825303680 17.459753  .08839999884366989  6
            2013 200075 23097.373 0 1825303680 17.459753  .08839999884366989  6
            2013 200897  95419.54 0 1825303680 17.459753  .08839999884366989  6
            2014 200877  60437.93 0 1911013632 17.468418  .08839999884366989  6
            2016 199351 141311.33 0 2098095360 17.482891               .0884  6
            2016 515064  45727.72 0 2098095360 17.482891               .0884  6
            2017 273402  120658.9 0 2152038912 17.487696               .0884  6
            2019 557635  29782.64 0 2265766656  17.49023               .0884  6
            2021 419881  53490.51 0          .         .               .0884  6
            2013  15250  20699.28 0  245227328 15.477688 .046300001442432404  8
            2020  16558  47731.73 0  325954720 15.574698               .0463  8
            2018 184208  76184.26 1   46511932 13.781939                .087 10
            2015 443209  19826.25 0  878631424  16.82214                .055 12
            2016 495764  8388.759 0  902306688 16.842123                .055 12
            2018 252916         0 0  985515456 16.872099                .055 12
            2018 447330  72738.73 0  985515456 16.872099                .055 12
            2020 541480  52217.23 0 1064827904 16.894358              .04458 12
            2012  10501 13121.197 0  371462368 16.108406                 .06 13
            2015 290113  83190.77 0  408290624 16.136265                 .06 13
            2020 252614  22303.38 0  488032928  16.18669               .0575 13
            2013 358571  15391.04 0  599305728  16.37241   .0949999988079071 17
            2018  11937  20107.55 0  660249792 16.359055                .095 17
            2020 441905   16253.2 0  697099840 16.348217                .095 17
            2021 229376 126185.45 0          .         .                .095 17
            2017 221811  47070.89 0  279969312  15.71194               .0625 18
            2018 445195  15147.29 0  287156992  15.71739                 .06 18
            2021 511944 11222.512 0          .         .               .0525 18
            2021 289988  111321.8 0          .         .               .0525 18
            2018 230768         0 0  141476384  14.96288                 .12 19
            2015 229695 28890.193 0  132140056  14.88391                 .07 20
            2018 258264   4906.78 0  135025488 14.884607                 .07 20
            2016 526386 146857.17 0  167577152 15.306233                 .06 21
            2017 453833  2460.513 0  169364240  15.30967                 .06 21
            2013   2806         0 0  188208320 15.347212  .07999999821186066 22
            2014   2815 145382.72 0  193243440 15.351503  .07999999821186066 22
            2017 551257 13905.473 0  191386512 15.357456                 .08 22
            2021 541067         0 0          .         .               .0825 24
            2012 259744  40714.61 0  379273536  15.71227                 .08 25
            2018 496121  81795.38 0  440734432  15.74496                 .08 25
            2015 206973  98169.34 0  414261792 16.111523                 .06 26
            2017   2601   34886.2 0  282592448 15.532777                .098 27
            2018   3489 249545.95 0  289728544  15.53984                .098 27
            2019   9458  15466.17 0  293457280 15.545404                .098 27
            2012 219974 36909.555 0  100378720 14.908976                 .05 28
            2016 225745 21356.814 0  101667088 14.910983                 .05 28
            2019 206223   49791.5 0  103602792 14.906838                 .05 28
            2014 440142 11813.968 0  245365328 15.617077               .0625 29
            2019 609120  88891.06 0  267650992 15.630413               .0625 29
            2020 275412  42113.64 0  279865088 15.632215                 .04 29
            2021 423310 14839.133 0          .         .                 .04 29
            2016 454525  87598.69 0   89692592  14.46077               .0781 31
            2020 442293  14229.14 0   98162528 14.476936               .0781 31
            2015 290242  39143.37 1  121422864  14.86931                   0 32
            2016 495239  157149.7 1  124893536 14.886942                   0 32
            2018 357671 31664.275 0   75383904  14.11936                .082 33
            2012   4340 168273.53 0  487534976  15.99544                 .09 34
            2020 228889  39709.41 0  574216000  15.99958                .105 34
            2014  41973  30960.41 0   75230320 14.552788   .0729999989271164 35
            2012 215267 11537.173 0 1049173952 16.789732                .071 36
            2013 183246 221191.67 0 1043893376  16.79239  .07100000232458115 36
            2014 183760 107021.33 0 1066979136 16.793762  .07100000232458115 36
            2018 436697  98742.94 0 1193327232 16.788183                .065 36
            2019 596257  19799.58 0 1212458496 16.784033                .065 36
            2021 236970  76884.47 0          .         .                .065 36
            2014 225812  34852.41 0  384086144 16.111805  .05999999865889549 37
            2016 259356  36467.93 0  409154080 16.134146                 .04 37
            2019   7643  46014.68 0  445717120 16.167017                .025 37
            2013   4047 10580.112 0  470860448 16.264763                   0 39
            2016 211800 26239.695 0  500364992 16.269962                   0 39
            2017 211891  78898.85 0  508876768 16.272163                   0 39
            2019 229474 34962.074 0  522910304 16.274801                   0 39
            2019 356682 30120.156 0  522910304 16.274801                   0 39
            2020 364662  20367.32 0  551979904 16.274519                   0 39
            2018   2665  31027.02 0  165500064 15.187576                 .06 40
            2021 271145  57856.65 0          .         .                .076 41
            2012 264584  29821.45 0  587674816 16.362541               .0999 42
            2017 215293 167692.66 0  630786304  16.36454               .0999 42
            2018 229773 32019.615 0  649345152 16.365667               .0999 42
            2017 357191 69741.766 0  197348144 15.430354                 .05 45
            2019 181724  6178.631 1   43234212 13.695744                   0 46
            2012 430946 18343.275 0  254474448 15.680482                .065 47
            2014 557353 11264.997 0  257880000 15.694154  .06499999761581421 47
            2016 477834  14429.23 0  274492896  15.71032                .065 47
            2015 257292  91685.41 0 1242254592 17.128551                   0 48
            2016 361178  5595.027 0 1226140288 17.144642                   0 48
            2017 452098 19082.668 0 1288102656 17.158054                   0 48
            2018 494881 17195.031 0 1344420992 17.169775                   0 48
            2019 589948 131627.64 0 1375026560  17.18235                   0 48
            2019   7591  74930.81 0 1375026560  17.18235                   0 48
            2020 558539 10093.898 0 1424441088  17.19517                   0 48
            2016 493354  6204.087 0  425424576 15.945842                 .06 51
            2021 212292  167069.3 0          .         .                 .06 51
            2014  36088 66453.445 0  347029280 15.769606                   0 53
            2016 195679  94588.69 0  379976032  15.80338                   0 53
            2020 251424  44581.24 0  454481632   15.8559                   0 53
            2017 277587 21287.014 0  264033904 15.572186                .079 55
            end
            
            set seed 03132022
            replace ref3= runiformint(0,1)
            recode ref3 (0=1) (1=0)
            xtset year firmid
             
             reghdfe lhsvar ib0.ref3##ib2015.year ///
                     control1 control2 control3, ///
                     absorb(country) vce(cluster country#year)
            
             estimates store m1
             
            coefplot (m1, baselevels mcolor(red) ciopts(lc(red)) keep(1.ref3*20*))  ///
                           (m1, baselevel  keep(1.ref3*2015*) mcolor(green) ciopts(lc(green)) ///
                                   xlabel(, angle(90)) yline(0) omitted   ///
                                   xtitle("year") ytitle("Title") name(name, replace) ///
                                   graphregion(fcolor(white)) recast(connected) lpattern(dash) ///
                                   offset(0)), vertical nokey xline(4, lp(solid) lc(black)) ///
                                   order(*2012* *2013* *2014* *2015* *2016* *2017* *2018* *2019* *2020*) ///
                                   xlab(1 "2012" 2 "2013" 3 "2014" 4 "2015" 5 "2016" 6 "2017" 7 "2018" ///
                                   8 "2019" 9 "2020") title("Some description that these are interactions")
            Click image for larger version

Name:	Graph.png
Views:	1
Size:	36.8 KB
ID:	1654104



            Last edited by Andrew Musau; 13 Mar 2022, 08:51.

            Comment

            Working...
            X