Search code examples
stata

How to add shaded region and line segments in coefplot?


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!


Solution

  • 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