regsave
and coefplot
are two commands that help plot coefficients from a regression.
Check out this worksheet that TA’s created as well!
Coefplot
First, install the command and browse the help file to understand the syntax of the command
ssc install coefplot, replace
help coefplot
Now, let’s open some data and play with it
sysuse auto
* this is a dataset on cars
* let's run a regression of mpg on price
reg mpg price
Source | SS df MS Number of obs = 74
-------------+---------------------------------- F(1, 72) = 20.26
Model | 536.541807 1 536.541807 Prob > F = 0.0000
Residual | 1906.91765 72 26.4849674 R-squared = 0.2196
-------------+---------------------------------- Adj R-squared = 0.2087
Total | 2443.45946 73 33.4720474 Root MSE = 5.1464
------------------------------------------------------------------------------
mpg | Coefficient Std. err. t P>|t| [95% conf. interval]
-------------+----------------------------------------------------------------
price | -.0009192 .0002042 -4.50 0.000 -.0013263 -.0005121
_cons | 26.96417 1.393952 19.34 0.000 24.18538 29.74297
------------------------------------------------------------------------------
* Great, so it's a simple regression, and seems that the higher the price, the lower the miles per gallon.
* Now let's plot these betas on a graph
coefplot
So, the command coefplot should have produce the graph above. Notice what’s happening, it’s plotting the betas (those are the dots) and the confidence interval, notice that the coefficient on price looks so small and close to zero, (1) because it is really small, but (2) because relative to the constant, it’s very small, so having the constant there changes the scale of the graph. Let’s drop the constant or let’s keep the price coefficient either should work
coefplot, keep(price)
coefplot, drop(_cons)
Now, we can notice the coefficient and the confidence intervals, and noticed that it is not statistically significant (presumably also not economically significant, this is not surprising because this represent a dollar increase, if we want to understand a more real change we would want to multiply this coefficient by say $5,000, which would imply a decrease of 4.6 mpg.
In any case, now let’s say we want to change the orientation of this coefficient, we would use the option vertical
coefplot, keep(price) vertical
Great, now let’s change the y-label so that it includes a 0, I personally like symmetry around 0 for graphs, so let’s do that
coefplot, keep(price) vertical ylabel(-0.002(0.001)0.002)
Great, now let’s add a line around 0 so that is more visible that this coefficient is statistically significant
coefplot, keep(price) vertical ylabel(-0.002(0.001)0.002) yline(0)
Awesome! Now we’ve used this graph to plot a coefficient from the regression. Now imagine the regression has more than one coefficient
reg mpg price foreign weight
coefplot, drop(_cons) vertical yline(0)
So in this manner we can plot all the coefficients (minus the constant), remember you can either drop a variable/s or keep a number of variables. For an event-study, we’ll want to keep only the coefficients on the interaction.
Regsave
regsave works a bit different from coefplot, what coefplot was doing is plotting the coefficients straight up. What regsave does, is saves the results from the regression in a whole new dta. Why is this useful? Well because then, we can open that dataset where the coefficients have been save and graph them using “twoway” which we’ve learned before.
net install regsave, from("https://raw.githubusercontent.com/reifjulian/regsave/master") replace
sysuse auto, clear
* We run the reg
reg mpg price foreign weight
* And now save it using regsave
regsave using "$hw/test.dta", replace
* Now open the data
use "$hw/test.dta", clear
* Now browse the data and see what this command save
br
* So we can plot the coefficients by just using two way
encode var, gen(variables)
* this createa a new variable called variables which is not string, and we can use it in twoway
twoway (scatter coef variables, sort)
Ok, great! But we need to drop the constant, in my case the constant takes the value of 1. The other thing is the labels! so let’s make both changes
twoway (scatter coef variables, sort) if variables>1, xlabel(, valuelabel)
- Don’t love the”2.5” and the “3.5” so will change the ticks for the x-axis
- Don’t also love the title of “variable” so i’m going to change that to blank.
twoway (scatter coef variables, sort) if variables>1, xtitle(" ") xlabel(2(1)4, valuelabel)
So the only thing left, is the confidence interval, so let’s add that into the dta, but for that we need to run the regression again, and add the option “ci” for regsave part
sysuse auto, clear
* We run the reg
reg mpg price foreign weight
* And now save it using regsave
regsave using "$hw/test.dta", replace ci
* Now open the data
use "$hw/test.dta", clear
encode var, gen(variables)
twoway (scatter coef variables, sort) if variables>1, xtitle(" ") xlabel(2(1)4, valuelabel)
Ok so this creates the same graph as above, but not let’s add the CIs
twoway (scatter coef variables, sort) ///
(rcap ci_lower ci_upper variables, sort) ///
if variables>1, xtitle(" ") xlabel(2(2)1, valuelabel)
ok the legend is too big! let’s turn that off
twoway (scatter coef variables, sort) (rcap ci_lower ci_upper variables, sort) if variables>1, xtitle(" ") xlabel(2(1)4, valuelabel) legend(off)
Great, so that we have a similar graph! For the event studies instead of using scatter you may want to use connected or line.