Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting same coefficient over time

Tags:

stata

coefplot

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.

like image 799
Fran Avatar asked Oct 11 '15 18:10

Fran


1 Answers

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).

like image 102
Roberto Ferrer Avatar answered Sep 20 '22 21:09

Roberto Ferrer