7

I am using the coefplot package in Stata to plot how a coefficient changes depending on the model employed. In particular, I want to see how the coefficient of interest changes over time. I am plotting it vertically, so the x-axis could show the year to which each coefficient refers to. However, I am not able to label the x-axis accordingly (instead of showing the name of the variable I am interested, x1, it should state 1, 2 and 3. I would also like to omit the legend by using the option legend(off) but that does not work.

This is the code I am using:

reg y x1 x2 if year==1;
estimates store t1;

reg y x1 x2 if year==2;
estimates store t2;

reg y x1 x2 if year==3;
estimates store t3;

coefplot t1 t2 t3, drop(x2) vertical yline(0);

Any suggestion would be greatly appreciated.

Nick Cox
  • 35,529
  • 6
  • 31
  • 47
Fran
  • 73
  • 1
  • 3

1 Answers1

7

A nonsensical example that uses two categories (and not time) can be easily adapted:

clear
set more off

sysuse auto

reg price weight rep78 if foreign
estimates store foreign

reg price weight rep78 if !foreign
estimates store not_foreign

matrix at = (1 / 2)

coefplot foreign || not_foreign, drop(rep78 _cons) vertical bycoefs

You can build the syntax within a loop, using a local. Then feed it to coefplot. To be precise, I mean something like the exemplary syntax:

year1 || year2 || ... || yearn

The final command would look something like:

coefplot `allyears', drop(<some_stuff>) vertical bycoefs

A complete example that does involves time:

clear
set more off

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

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

// check
display "`allyears'"
display `"`labels'"'

coefplot `allyears', keep(grade) vertical bycoefs bylabels(`labels')

If coefplot doesn't turn out to be flexible enough, you can always try with statsby and graph commands (help graph).

Roberto Ferrer
  • 11,024
  • 1
  • 21
  • 23
  • Following on that, instead of "year70", "year71" and so on, would it be possible that the x-axis shows only "70", "71", "72" and "73"? – Fran Oct 12 '15 at 16:52
  • Those are not valid names for `estimates store`, but you can use the `bylabels()` option. I edited my answer. – Roberto Ferrer Oct 12 '15 at 17:17
  • Brilliant!! This is the final code in case other users may find it useful: reg y x1 x2 if year==1; estimates store t1; reg y x1 x2 if year==2; estimates store t2; reg y x1 x2 if year==3; estimates store t3; coefplot t1 || t2 || t3, drop(x2) vertical bycoefs bylabels(1 2 3) yline(0); – Fran Oct 12 '15 at 19:14
  • 1
    You can **accept** and **upvote** the answer if it solves your problem. – Roberto Ferrer Oct 12 '15 at 19:20
  • I have tried the exact same thing, but I have 200 years worth of data. In this event, I have replicated your code exactly, but for some reason, the local variable allyears does not store all the different year values. If I restrict the sample to just 10 years for instance, it works perfectly. Do you have any idea why this may be the case? – ChinG Jan 17 '19 at 20:16
  • @ChinG, It works just fine if you susbstitute for `i = 1800/2018` in the `forvalues` loop (run only the loop without the `regress` and `estimates` commands and check using the `display` commands). If not, than you should post a new question with the code you're running in order for someone to help. – Roberto Ferrer Jan 18 '19 at 00:56
  • If I try running the suggested code but without the " | | " separator in coefplot (to have all coefficients in the same plot), the solution no longer works. Is there a way to achieve the same result for this case? – cyberalcito Aug 27 '22 at 15:54