Suppose I have a model like this:
webuse nlswork
poisson hours i.union##c.tenure, robust
margins union, dydx(tenure)
margins rb1.union, dydx(tenure)
I would like to stack the two AMEs on top of the differences of the AMEs using Ben Jann's -estout-. Unfortunately, you need to post the margins results for estout, which interferes with the second margins command.
Is there any way around this?
Cross-posted at the Statalist forum for some time without an answer.
I've never used -estout-, but perhaps this will give you a start.
webuse nlswork
poisson hours i.union##c.tenure, robust
estimates store m0
margins union, dydx(tenure) post
estimates store m1
estimates restore m0
margins rb1.union, dydx(tenure) post
estimates store m2
Why this works: margins needs access to the results of the original command, poisson in this example. As margins does not itself leave estimation results behind, the original results remain available if you run margins without post, and you can have several margins commands in a row without problems. However, if you add the post option to the first margins command, the new posted results displace those in memory. In that case, the second margins will complain that
margins cannot work with its own posted results
The solution, therefore, is to present the second margins with the original estimation results, just what estimates restore is designed to do.
Update
r(table) contains all the results from margins, and the columns are named. Here's a version of Roberto's stacking solution that takes advantage of these properties:
use nlswork, clear,
poisson hours i.union##c.tenure, robust
margins union, dydx(tenure)
matrix list r(table)
matrix m1 = r(table)
matrix m11 = m1["b".."se", 1...]'
matrix m12 = m1["ll".."ul",1...]'
matrix first = m11,m12
margins rb1.union, dydx(tenure)
matrix m2 = r(table)
matrix m21 = m2["b".."se", 1...]'
matrix m22 = m2["ll".."ul",1...]'
matrix second = m21,m22
matrix rownames second = tenure:diff
matrix RESULTS = first \ second
estout matrix(RESULTS)
estout takes matrices, so maybe you can try with that:
webuse nlswork, clear
poisson hours i.union##c.tenure, robust
margins union, dydx(tenure)
matrix first = r(b)
matrix list first
margins rb1.union, dydx(tenure)
matrix second = r(b)
matrix list second
*-----
matrix b = first[1,1] , first[1,2] \ second[1,1] , .
estout matrix(b)
You would need to polish the results, of course.
Update
There's a thread on Statalist from 2007, where Ben Jann (the author of estout) clarifies that stacking multiple stored results into one column is not possible with estout alone. His solution involves a program that merges results manipulating matrices and column/row names.
For the example you have provided, something like the following works:
webuse nlswork, clear
poisson hours i.union##c.tenure, robust
// first margin
margins union, dydx(tenure)
matrix first = r(b)
// second margin
margins rb1.union, dydx(tenure)
matrix second = r(b)
matrix rownames second = tenure:diff
// put together
matrix c = first' \ second
estout matrix(c)
(The thread is a bit old so I'm not sure if estout has been updated to do this at present.)
Related
I am trying to create a graph with two y axes (below code), but the title of the right-side y-axis does not appear. Can anyone please help me with that?
sysuse auto, clear
generate kpl=mpg*0.425144
twoway (scatter mpg weight, color(navy) yaxis(1)) (scatter kpl weight, color(navy) yaxis(2) ylabel(4.25 8.5 12.75 17, axis(2)) ytitle(Kilometres per Litre, axis(2))), by(foreign, legend(off) note(Graphs by Car origin))
enter image description here
I think I understand what you want but I would approach it quite differently.
If you want a second scale of km per litre to compare with miles per gallon, that is just the same data points explained differently, just as you could show Celsius and Fahrenheit temperatures on different axes or calculate proportions and show percents, or vice versa.
Another variable holding km per litre makes this more difficult, not easier, as the values differ by the corresponding conversion factor.
Here I use mylabels from SSC, which must be installed before you can use it.
Naturally you don't need to show zero, but the identity 0 miles per gallon = 0 km per litre may make the point easier to follow.
sysuse auto, clear
set scheme s1color
mylabels 0(4)16, myscale(#/0.425144) local(yla)
scatter mpg weight, yaxis(1 2) yla(`yla', axis(2) ang(h)) yla(0(10)40, axis(1) ang(h)) ytitle(km per litre, axis(2)) ms(Oh)
I am using the coefplot command in Stata to plot coefficients and confidence intervals from multiple regression models. I am plotting the same coefficient (X) from 4 different model specifications.
There is one model specification (alternative standard errors) that I cannot figure out how to estimate in Stata, but am able to estimate using R. That means that for one model specification, I have the standard error in R, but not in Stata.
Is there an easy way to manually alter the standard errors in coefplot?
My code is:
coefplot A B C D, drop(_cons) xline(0) keep(X)
How can I add to this code that the standard errors for coefficient X in model D should be Z?
You can manually edit the e(V) (variance-covariance matrix) and e(b) vectors. For this, define a program:
est restore estimates1
capture program drop changeeV
program changeeV, eclass
tempname b V
matrix `b' = e(b)
matrix `V' = e(V)
matrix `V'[1,1] = 1.1 // Add values of new variance-covariance matrix
matrix `b'[1,1] = 10 // Add new coefficient vector
ereturn post `b' `V' // Repost new vectors
ereturn local cmd "reg outcome treatment covariates"
// Repost initial command (required)
end
changeeV // Execute program
est store eaX // Store new generated estimtes
Note that, to reach the covariance matrix, you need to take the square of the standard errors from your output in R. Good luck!
I have used the by option of the histogram command to group my two categorical variables:
sysuse auto, clear
hist price, percent by(foreign rep78)
I would like to graph 8 histograms together on the same plot, with the following layout:
XXXX
XXXX
However, as the above graph shows Stata places the histograms by default as follows:
XXX
XXX
XX
How can I achieve a 2x4 orientation of histograms?
I have played around with the aspectratio() option, but this changes the aspect ratio of the individual graphs, not of the whole plot.
You need to specify the cols() sub-option in by():
sysuse auto, clear
histogram price, percent by(foreign rep78, cols(4))
I am running a regression with two fixed effects categories (country and year, is economic macro data). Since I am using xtreg, one is autohid, but the other is a variable:
xtreg fiveyearyg taxratio i.year if taxratiocut == 1, i(wbcode1) fe cluster(wbcode1)
estimates store yi
I am running a number of these and I want to graph the coefficients for taxratio from each. But when I store the data, it stores both the taxratio coefficient, and the 50+ coefficients for the year fixed effects.
After a lot of searching, I cannot find any way to store (or recall) just part of the regression output, the one coefficient (with SEs) that I care about. Does anyone know a way to do that?
Here is how you can do that:
webuse grunfeld,clear
qui xtreg mvalue invest i.year,fe cluster(company)
//e(b) stores coefficient matrix and e(V) stores variance-covariance matrix. For details type: ereturn list after running the model
//let's say you want to extract only the coefficient on invest
mat coef_matrix=e(b)
scalar coef_invest=coef_matrix[1,1]
dis coef_invest
1.7178414
//to extract se of the the coefficient on invest
mat var_matrix=e(V)
mat diag_var_matrix=vecdiag(var_matrix) //diagonal elements are variances and the standard errors are square roots of these variances
matmap diag_var_matrix se_matrix , m(sqrt(#))) //you need to install matmap using ssc install matmap, you will get error if variance is negative
scalar se_invest=se_matrix[1,1]
dis se_invest
.14082153
Accessing coefficients is as easy as calling _b[varname]; analogously the corresponding standard errors: _se[varname].
An example:
webuse grunfeld, clear
qui xtreg mvalue invest i.year,fe cluster(company)
// coef for invest
display _b[invest]
// std error for invest
display _se[invest]
// displayed results in matrix
matrix list r(table)
For multiple-equation models use [eqno]_b[varname] where the preceding bracket contains an equation number.
More detail can be found in [U] 13.5 Accessing coefficients and standard errors.
Starting Stata 12, estimation commands also store results in r() [and not just e()]. Notice I listed r(table), which contains most results displayed by the estimation command xtreg.
You show interest in plotting coefficients, so you should read on the user-written command coefplot. Run ssc install coefplot to download and help coefplot to get started. It has many options.
Edit
A complete example that plots only coefficients for invest (leaving out those for year), using coefplot, and based on conditional regressions is:
clear
set more off
webuse grunfeld
xtreg mvalue invest i.year if time <= 10,fe cluster(company)
estimates store before10
xtreg mvalue invest i.year if time > 10,fe cluster(company)
estimates store after10
coefplot before10 after10, keep(invest)
After running glm I can type matrix list r(table) and see a table of all of my results. If I wish, I can write slopes and SEs to variables, e.g., gen B=_b[x1] or gen se=_se[x1]. However, this does not work with the confidence limits, ll and ul. How can I access them in a similar manner?
I am not sure if the _b[] and _se[] results are associated with r(table)--I have thought they are products of e(b) and e(V).
Anyway, since you have r(table), you can just save the results into another matrix, and then use the regular matrix operations to put the lower bounds and upper bounds into new matrices. If for some reason transformation into variables is desired (for example, plotting), there's always -svmat-.
sysuse auto,clear
glm price mpg foreign, f(gaussian)
mat r=r(table)
matrix ll=r["ll",....]' // see -help matrix extraction-; transposed for svmat
svmat ll,names(ll) // lower bounds are in variable ll1