0

I plan to run the following cross-sectional regression for 10 years and plot the coefficient estimate for variable x in one graph.

Thanks to this post, I wrote the following and it works:

forvalues i=1/10 {
    reg y x   if year==1
    estimates store year`i'
    local     allyears  `allyears'  year`i' ||
    local     labels   `labels'     `i'
}
coefplot `allyears', keep(grade) vertical bycoefs bylabels(`labels') 

I want to add the following to the same graph but don't know how:

  1. A horizontal line segment x=5 for year 1 to year 5, and another horizontal line segment x=4 for year 6 to year 10.

  2. A shaded area ranging from x=4 to x=6 for year 1 to year 5, and another shaded area ranging from x=2 to 4 for year 6 to year 10.

(Note that my horizontal axis is year, and my vertical axis is coefficient for x.)

Any help is greatly appreciated!

SXS
  • 193
  • 1
  • 3
  • 16

1 Answers1

3

Here's an example based on the nlswork toy dataset:

clear
use http://www.stata-press.com/data/r12/nlswork.dta

for values i = 70 / 73 {
    regress ln_w grade if year==`i'
    estimates store year`i'
    local allyears `allyears'year`i' ||
    local labels `labels' `i'
}

coefplot `allyears', keep(grade) vertical bycoefs bylabels(`labels') ///
addplot(scatteri 0.08 1 0.08 3, recast(connected) || ///
        scatteri 0.09  1 0.09 3, recast(connected) || ///
        scatteri 0.065 2 0.065 3 0.075 3 0.075 2, recast(area) lwidth(none))

enter image description here

  • Hi @PearlySpencer, I have a follow-up question: if I move the shaded region up, it will overlay the coefficient plot. Point estimates and CI can no longer be fully seen. Is it possible to move the shaded region to the bottom layer? – SXS Mar 14 '19 at 21:14
  • If you have Stata 15 you can adjust the [transparency](https://www.stata.com/new-in-stata/transparency-in-graphs/). Otherwise you need to modify the source code of `coefplot` to draw the recasted areas first. –  Mar 14 '19 at 21:16