How can I stack equations using estout? - stata

I am creating a summary statistics table using the community-contributed command estout.
The code looks like this:
sysuse auto, clear
eststo clear
eststo: estpost ttest price mpg weight headroom trunk if rep78 ==3, by(foreign)
eststo: estpost ttest price mpg weight headroom trunk if rep78 ==4, by(foreign)
estout, cells("mu_1 mu_2 b(star)")
The result looks as follows:
--------------------------------------------------------------------------------------------
est1 est2
mu_1 mu_2 b mu_1 mu_2 b
--------------------------------------------------------------------------------------------
price 6607.074 4828.667 1778.407 5881.556 6261.444 -379.8889
mpg 19 23.33333 -4.333333 18.44444 24.88889 -6.444444**
weight 3442.222 2010 1432.222*** 3532.222 2207.778 1324.444***
headroom 3.222222 2.666667 .5555556 3.444444 2.5 .9444444*
trunk 15.59259 12.33333 3.259259 16.66667 10.33333 6.333333**
--------------------------------------------------------------------------------------------
I would like to know how I could stack est1 and est2 on top of each other.

The command estout cannot automatically stack results from stored estimates. Consequently, the use of eststo is redundant. In this case, the easiest way to obtain the desired output is to simply create two matrices with the results and stack one on top of the other.
For example:
sysuse auto, clear
matrix A = J(5, 3, .)
local i 0
foreach var of varlist price mpg weight headroom trunk {
local ++i
ttest `var' if rep78 == 3, by(foreign)
matrix A[`i', 1] = r(mu_1)
matrix A[`i', 2] = r(mu_2)
matrix A[`i', 3] = r(mu_1) - r(mu_2)
local matnamesA `matnamesA' "rep78==3:`var'"
}
matrix rownames A = `matnamesA'
matrix B = J(5, 3, .)
local i 0
foreach var of varlist price mpg weight headroom trunk {
local ++i
ttest `var' if rep78 == 4, by(foreign)
matrix B[`i', 1] = r(mu_1)
matrix B[`i', 2] = r(mu_2)
matrix B[`i', 3] = r(mu_1) - r(mu_2)
local matnamesB `matnamesB' "rep78==4:`var'"
}
matrix rownames B = `matnamesB'
matrix C = A \ B
esttab matrix(C), nomtitles collabels("mu_1" "mu_2" "diff")
---------------------------------------------------
mu_1 mu_2 diff
---------------------------------------------------
rep78==3
price 6607.074 4828.667 1778.407
mpg 19 23.33333 -4.333333
weight 3442.222 2010 1432.222
headroom 3.222222 2.666667 .5555556
trunk 15.59259 12.33333 3.259259
---------------------------------------------------
rep78==4
price 5881.556 6261.444 -379.8889
mpg 18.44444 24.88889 -6.444444
weight 3532.222 2207.778 1324.444
headroom 3.444444 2.5 .9444444
trunk 16.66667 10.33333 6.333333
---------------------------------------------------

Related

Export tabulation in Excel

Consider the following toy example:
sysuse auto, clear
tab foreign, sum(price)
| Summary of Price
Car type | Mean Std. Dev. Freq.
------------+------------------------------------
Domestic | 6,072.423 3,097.104 52
Foreign | 6,384.682 2,621.915 22
------------+------------------------------------
Total | 6,165.257 2,949.496 74
How can I save the results in an Excel file?
Using the community-contributed command esttab, the following works for me:
sysuse auto, clear
egen m_total = mean(price)
egen s_total = sd(price)
scalar mtotal = m_total
scalar stotal = s_total
scalar N = _N
collapse (mean) Mean=price (sd) StdDev=price (count) Freq = price, by(foreign)
set obs 3
replace Mean = mtotal in 3
replace StdDev = stotal in 3
replace Freq = N in 3
mkmat Mean StdDev Freq, matrix(A)
esttab matrix(A) using myfilename.xls, varlabels(r1 Domestic r2 Foreign r3 Total) ///
title(" Summary of Price") mlabels(none)
Summary of Price
---------------------------------------------------
Mean StdDev Freq
---------------------------------------------------
Domestic 6072.423 3097.104 52
Foreign 6384.682 2621.915 22
Total 6165.257 2949.496 74
---------------------------------------------------

Combining output from linear and non-linear regressions in esttab

I display the results of two regressions using the community-contributed command esttab:
sysuse auto, clear
quietly reg price weight
est store ols
quietly nl (price = {b0} + {b1} * weight)
est store nls
esttab *
--------------------------------------------
(1) (2)
price price
--------------------------------------------
main
weight 2.044***
(5.42)
_cons -6.707 -6.707
(-0.01) (-0.01)
--------------------------------------------
b1
_cons 2.044***
(5.42)
--------------------------------------------
N 74 74
--------------------------------------------
t statistics in parentheses
* p<0.05, ** p<0.01, *** p<0.001
How can I make the b1 coefficient from the nl command to appear in the weight row?
The easiest way of doing this is the following:
sysuse auto, clear
estimates clear
regress price weight
estimates store ols
nl (price = {b0} + {b1} * weight)
matrix b = e(b)
matrix V = e(V)
matrix coleq b = " "
matrix coleq V = " "
matrix colnames b = _cons weight
matrix colnames V = _cons weight
erepost b = b V = V, rename
estimates store nls
Results:
esttab ols nls
--------------------------------------------
(1) (2)
price price
--------------------------------------------
weight 2.044*** 2.044***
(5.42) (5.42)
_cons -6.707 -6.707
(-0.01) (-0.01)
--------------------------------------------
N 74 74
--------------------------------------------
t statistics in parentheses
* p<0.05, ** p<0.01, *** p<0.001
Note that erepost is a community-contributed command, which you can download from SSC:
ssc install erepost

Add marginal effect t-statistics to an estout table

I would like to add marginal effect t-statistics to an estout table (ssc install estout).
I can add marginal effect coefficients and standard errors with estadd margins.
However, margins does not add t-statistics.
I thought that I could add a t-statistic with estadd matrix but this code fails to calculate margins_t:
webuse grunfeld
eststo clear
regress mvalue c.kstock##c.invest
eststo
estadd margins, dydx(kstock)
estadd matrix margins_t = margins_b :/ margins_se
I want to report marginal effects for only kstock (i.e., only one of the interaction variables):
esttab, cells("b margins_b" "t(par) margins_se(par)")
--------------------------------------
(1)
mvalue
b/t margins_b/~e
--------------------------------------
kstock -.0229636 -.2073908
(-.0947377) (.2213873)
invest 6.672997
(19.13787)
c.kstock#c~t -.0012636
(-4.351608)
_cons 219.3425
(2.903506)
--------------------------------------
N 200
--------------------------------------
The standard errors version works but I would prefer t-statistics.
The following works for me:
webuse grunfeld
eststo clear
eststo m1: regress mvalue c.kstock##c.invest
eststo m2: margins, dydx(kstock) post
esttab m1 m2 using output, replace
type output.txt
--------------------------------------------
(1) (2)
mvalue
--------------------------------------------
kstock -0.0230 -0.207
(-0.09) (-0.94)
invest 6.673***
(19.14)
c.kstock#c~t -0.00126***
(-4.35)
_cons 219.3**
(2.90)
--------------------------------------------
N 200 200
--------------------------------------------
t statistics in parentheses
* p<0.05, ** p<0.01, *** p<0.001
A revision addressing the concerns in OP's comment:
eststo clear
eststo m1: regress mvalue c.kstock##c.invest
estadd local Obs = e(N)
eststo m2: margins, dydx(kstock) post
esttab m1 m2, s(Obs) mtitles("(1)" "") nonumbers noobs
--------------------------------------------
(1)
--------------------------------------------
kstock -0.0230 -0.207
(-0.09) (-0.94)
invest 6.673***
(19.14)
c.kstock#c~t -0.00126***
(-4.35)
_cons 219.3**
(2.90)
--------------------------------------------
Obs 200
--------------------------------------------
t statistics in parentheses
* p<0.05, ** p<0.01, *** p<0.001
There are (at least) two problems with the code in my question.
:/ is elementwise division in Mata. Use matewd for elementwise division in Stata. matewd is from a Stata technical bulletin, so use findit matewd.
Elementwise division would not retain column names, which estout uses to align coefficient estimates and other statistics properly.
The code below solves the original question but requires a handful of extra steps. It may be easier to manually modify the table layout than taking this handful of additional steps.
webuse grunfeld
eststo clear
regress mvalue c.kstock##c.invest
eststo
estadd margins, dydx(kstock)
matrix margins_b = e(margins_b)
matrix margins_se = e(margins_se)
matewd margins_b margins_se margins_t
local colnames : colnames margins_b
matrix colnames margins_t = "`colnames'"
estadd matrix margins_t
esttab, cells("b margins_b" "t(par) margins_t(par)")
// --------------------------------------
// (1)
// mvalue
// b/t margins_b/~t
// --------------------------------------
// kstock -.0229636 -.2073908
// (-.0947377) (-.9367782)
// invest 6.672997
// (19.13787)
// c.kstock#c~t -.0012636
// (-4.351608)
// _cons 219.3425
// (2.903506)
// --------------------------------------
// N 200
// --------------------------------------
esttab, cells(b t(par) margins_b margins_t(par))
// -------------------------
// (1)
// mvalue
// b/t/margin~t
// -------------------------
// kstock -.0229636
// (-.0947377)
// -.2073908
// (-.9367782)
// invest 6.672997
// (19.13787)
// c.kstock#c~t -.0012636
// (-4.351608)
// _cons 219.3425
// (2.903506)
// -------------------------
// N 200
// -------------------------

Exporting results from regressions in Excel

I want to store results from ordinary least squares (OLS) regressions in Stata within a double loop.
Here is the structure of my code:
foreach i2 of numlist 1 2 3{
foreach i3 of numlist 1 2 3 4{
quiet: eststo: reg dep covariates, robust
}
}
The end goal is to have a table in Excel composed by twelve rows (one for each model) and seven columns (number of observations, estimated constant, five estimated coefficients).
Any suggestion on how can I do this?
Such a table can be created simply by using the community-contributed command esttab:
sysuse auto, clear
eststo clear
eststo m1: quietly regress price weight
eststo m2: quietly regress price weight mpg
quietly esttab
matrix A = r(coefs)'
matrix C = r(stats)'
tokenize "`: rownames A'"
forvalues i = 1 / `=rowsof(A)' {
if strmatch("``i''", "*b*") matrix B = nullmat(B) \ A[`i', 1...]
}
matrix C = B , C
matrix rownames C = "Model 1" "Model 2"
Result:
esttab matrix(C) using table.csv, eqlabels(none) mlabels(none) varlabels("Model 1" "Model 2")
----------------------------------------------------------------
weight mpg _cons N
----------------------------------------------------------------
Model 1 2.044063 -6.707353 74
Model 2 1.746559 -49.51222 1946.069 74
----------------------------------------------------------------

use xtile by year using weights

I have data with income variable, with weight, and I want to calculate the 5% quantiles by year.
Is there a way to do that?
For the weight I can use regular xtile:
xtile quan = salary [aw=weight], n(20)
And for the years I can use xtile from egenmore:
egen quan = xtile(salary), by(year) nq(20)
But how can I do it for weights and by year together?
There is a weights() option, as stated in help egenmore:
clear
set more off
sysuse auto
keep mpg foreign weight
// egenmore
egen mpg4 = xtile(mpg), by(foreign) nq(4) weights(weight)
// compare with xtile
xtile mpg4_1 = mpg [aweight=weight] if foreign, nq(4)
xtile mpg4_2= mpg [aweight=weight] if !foreign, nq(4)
egen mpg42 = rowtotal(mpg4_1 mpg4_2)
assert mpg4 == mpg42
sort foreign mpg weight
list, sepby(foreign)
In the ado-file for egen's xtile function, you can check how weights are set:
if "`weights'" ~= "" {
local weight "[aw = `weights']"
}
See viewsource _gxtile.ado.