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
Related
for simplification, let's assume following script to create a simple regression table:
sysuse auto
eststo clear
qui regress price weight mpg
esttab using "table.rtf", cells(t) mtitles onecell nogap ///
stats(N, labels("Observations")) label ///
compress replace
eststo clear
Output:
(1)
.
t
Weight (lbs.) 2.723238
Mileage (mpg) -.5746808
Constant .541018
Observations 74
Question:
Would it be possible to mark every t-value above 0.5 or below 0.5 with an asterisk? (= greater than absolute value 0.5)
Please note: In the specific application case, I can't work with given p-values, and need a custom solution that works with thresholds of t.
Desired outcome:
(1)
.
t
Weight (lbs.) 2.723238*
Mileage (mpg) -.5746808*
Constant .541018*
Observations 74
Crossposting can be found here:
Thank you for your help!
You cannot do this directly with estout but the following works all the same:
sysuse auto, clear
regress price weight mpg
quietly esttab, mtitles onecell nogap stats(N, labels("Observations")) label ///
compress replace star staraux
matrix A = r(coefs)
matrix A = A[1...,2]
svmat A
generate A2 = "*" if abs(A1) >= 0.5
generate A4 = string(A1) + A2
local names : rownames A
generate A3 = ""
forvalues i = 1 / `: word count `names'' {
replace A3 = `"`: word `i' of `names''"' in `i'
}
list A3 A4 if !missing(A3)
+---------------------+
| A3 A4 |
|---------------------|
1. | weight 2.723238* |
2. | mpg -.5746808* |
3. | _cons .541018* |
+---------------------+
preserve
keep if !missing(A3)
export delimited A3 A4 using table.txt, delimiter(" ") novarnames
restore
You will have to do some more gymnastics to get the variable labels etc.
I tried creating a table using the community-contributed family of commands estout:
esttab est1 est2 est3 using table3.tex, se label nobaselevels ///
star(* 0.10 ** 0.05 *** 0.01) cell((coef(fmt(%9.2f)) sd(fmt(%9.2f)))) ///
drop(_Iprovince* _Iyear*) stats(year province robust r2 N, ///
label("Year Fixed Effects" "Province Fixed Effects" "Robust SE" "R-squared")) ///
replace booktabs
However, Stata produces the following error:
coefficient _Iprovince* not found
These are "fixed effect" dummies that I want to drop them out.
The code works fine when I take out cell().
Finally, how can I also round up the coefficient estimates and standard errors?
Unless you have a very old version of Stata, don't use xi to create your FEs. Use factor variable notation i.province and i.year instead.
The principal problem with your code is that you should have b instead of coef (Stata cannot drop coefficients since they are not included unless you tell Stata that you want them):
sysuse auto
eststo est1: reg price mpg i.rep78
esttab est1, ///
stats(b year province robust r2 N, label("Year Fixed Effects" "Province Fixed Effects" "Robust SE" "R-squared")) ///
replace booktabs drop(*.rep78) se label nobaselevels star(* 0.10 ** 0.05 *** 0.01) cell((b(fmt(%9.2f)) sd(fmt(%9.2f))))
Note the reproducible example on a shared dataset.
The code does not execute because you are using in esttab the suboption sd
instead of se:
sysuse auto, clear
eststo est1: xi: reg price mpg i.rep78
i.rep78 _Irep78_1-5 (naturally coded; _Irep78_1 omitted)
Source | SS df MS Number of obs = 69
-------------+---------------------------------- F(5, 63) = 4.39
Model | 149020603 5 29804120.7 Prob > F = 0.0017
Residual | 427776355 63 6790100.88 R-squared = 0.2584
-------------+---------------------------------- Adj R-squared = 0.1995
Total | 576796959 68 8482308.22 Root MSE = 2605.8
------------------------------------------------------------------------------
price | Coef. Std. Err. t P>|t| [95% Conf. Interval]
-------------+----------------------------------------------------------------
mpg | -280.2615 61.57666 -4.55 0.000 -403.3126 -157.2103
_Irep78_2 | 877.6347 2063.285 0.43 0.672 -3245.51 5000.78
_Irep78_3 | 1425.657 1905.438 0.75 0.457 -2382.057 5233.371
_Irep78_4 | 1693.841 1942.669 0.87 0.387 -2188.274 5575.956
_Irep78_5 | 3131.982 2041.049 1.53 0.130 -946.7282 7210.693
_cons | 10449.99 2251.041 4.64 0.000 5951.646 14948.34
------------------------------------------------------------------------------
esttab est1, cell((coef(fmt(%9.2f)) sd(fmt(%9.2f)))) label nobaselevels ///
star(* 0.10 ** 0.05 *** 0.01) stats(b r2 N) drop(_Irep78*)
coefficient _Irep78* not found
r(111);
If you use the correct suboption se the code runs:
esttab est1, cell((coef(fmt(%9.2f)) se(fmt(%9.2f)))) label nobaselevels ///
star(* 0.10 ** 0.05 *** 0.01) stats(r2 N) drop(_Irep78*)
----------------------------------------------
(1)
Price
coef se
----------------------------------------------
Mileage (mpg) 61.58
Constant 2251.04
----------------------------------------------
r2 0.26
N 69.00
----------------------------------------------
However, the suboption coeflabels (coef in your code) is supposed to only
specify labels for beta coefficients, not include them.
As such, instead you need to use the suboption b as suggested in
#Dimitriy's answer:
esttab est1, cell((b(fmt(%9.2f)) se(fmt(%9.2f)))) label nobaselevels ///
star(* 0.10 ** 0.05 *** 0.01) stats(r2 N) drop(_Irep78*)
----------------------------------------------
(1)
Price
b se
----------------------------------------------
Mileage (mpg) -280.26 61.58
Constant 10449.99 2251.04
----------------------------------------------
r2 0.26
N 69.00
----------------------------------------------
Here's the full output in esttab (wihout dropping anything):
esttab est1, cell((b(fmt(%9.2f)) se(fmt(%9.2f)))) label nobaselevels ///
star(* 0.10 ** 0.05 *** 0.01) stats(r2 N)
----------------------------------------------
(1)
Price
b se
----------------------------------------------
Mileage (mpg) -280.26 61.58
rep78==2 877.63 2063.28
rep78==3 1425.66 1905.44
rep78==4 1693.84 1942.67
rep78==5 3131.98 2041.05
Constant 10449.99 2251.04
----------------------------------------------
r2 0.26
N 69.00
----------------------------------------------
Below you can find my code:
#delimit;
local fixed_effect "Yes";
estout pre_post using output.xls, cells(b(star fmt(4) keep(post
`ctrlVars')) t(par fmt(2) keep(post `ctrlVars')))
legend starlevels( * 0.10 ** 0.05 *** 0.010) stats(`fixed_effect' r2 N
labels("Industry fixed effects" "Adjusted R-squared")) varlabels(_cons
Constant) append;
This produces the following error message:
( invalid name
"Industry fixed effects invalid name
"Adjusted R-squared invalid name
) invalid name
r(7);
What is wrong?
EDIT:
Sorry for not being clear enough. This is what I would like to have:
----------------------------
(1)
Industry FEs
b/t
----------------------------
mpg -174.3133*
(-1.99)
headroom -520.2934
(-1.23)
length 31.3659
(1.30)
Constant 5540.3487
(0.94)
----------------------------
Industry FE Yes
Adjusted R~d 0.2454
N 74
----------------------------
* p<0.10, ** p<0.05, *** p<0.010
I can reproduce your problem using Stata's toy dataset auto as follows:
sysuse auto, clear
regress price mpg headroom length
#delimit;
esttab ., cells(b(star fmt(4)) t(par fmt(2)))
legend starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2 N
label("Industry fixed effects" "Adjusted R-squared")) varlabels(_cons
Constant);
( invalid name
"Industry fixed effects invalid name
"Adjusted R-squared invalid name
) invalid name
r(7);
This error happens because you are using the options of the community-contributed command estout incorrectly: labels() is a sub-option of stats() and thus it has to be separated using a comma. In addition, you need the standalone option mlabels() to specify a custom model name:
esttab ., cells(b(star fmt(4)) t(par fmt(2))) legend ///
starlevels(* 0.10 ** 0.05 *** 0.010) stats(r2 N, labels("Adjusted R-squared")) ///
mlabels("Industry FEs") varlabels(_cons Constant)
----------------------------
(1)
Industry FEs
b/t
----------------------------
mpg -174.3133*
(-1.99)
headroom -520.2934
(-1.23)
length 31.3659
(1.30)
Constant 5540.3487
(0.94)
----------------------------
Adjusted R~d 0.2454
N 74.0000
----------------------------
* p<0.10, ** p<0.05, *** p<0.010
Note that delimit also appears to cause some issues.
EDIT:
You need to use estadd for that:
sysuse auto, clear
regress price mpg headroom length
estadd local fe Yes
esttab ., cells(b(star fmt(4)) t(par fmt(2))) legend ///
starlevels(* 0.10 ** 0.05 *** 0.010) stats(fe r2 N, ///
labels("Industry FE" "Adjusted R-squared")) ///
mlabels("Industry FEs") varlabels(_cons Constant)
----------------------------
(1)
Industry FEs
b/t
----------------------------
mpg -174.3133*
(-1.99)
headroom -520.2934
(-1.23)
length 31.3659
(1.30)
Constant 5540.3487
(0.94)
----------------------------
Industry FE Yes
Adjusted R~d 0.2454
N 74.0000
----------------------------
* p<0.10, ** p<0.05, *** p<0.010
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
// -------------------------
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
----------------------------------------------------------------