Plotting same coefficient over time
Asked Answered
N

1

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.

Nilotic answered 11/10, 2015 at 18:14 Comment(1)
I have inserted if in your regress commands; they could not possibly work otherwise.Portuna
A
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).

Autobus answered 11/10, 2015 at 21:17 Comment(7)
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"?Nilotic
Those are not valid names for estimates store, but you can use the bylabels() option. I edited my answer.Autobus
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);Nilotic
You can accept and upvote the answer if it solves your problem.Autobus
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?Supererogate
@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.Autobus
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?Simmer

© 2022 - 2024 — McMap. All rights reserved.