Fe and first difference in a regression table - regression

I am doing an analysis in Stata and I have a lot of different panel regressions (within, first-difference and random trend) and to see the results properly, I am using eststo and esttab.
My problem now is that to get the difference for first difference and the double difference for the random trend, I use d.varname and d.d.varname.
Stata then thinks that the differences are new variables and puts them in their own rows which becomes very difficult to read.
Has anyone an idea how I can get a regression table in which Stata sees varname, d.varname and d.d.varname as the same variable?
My regression looks like this:
foreach v in a aa aaa aaaa{
qui eststo: xtreg `v' b b1 b2 b3 b4 b5 i.year, fe cluster(xy)
qui eststo: xtreg `v' b b1 b2 b3 b4 b5 i.year if c>d, fe cluster(xy)
qui eststo: reg d.`v' d.b d.b1 d.b2 d.b3 d.b4 d.b5 i.year, cluster(xy)
qui eststo: reg d.`v' d.b d.b1 d.b2 d.b3 d.b4 d.b5 i.year if c>d, cluster(xy)
qui eststo: reg d.d.`v' d.d.b d.d.b1 d.d.b2 d.d.b3 d.d.b4 d.d.b5 i.year, cluster(xy)
qui eststo: reg d.d.`v' d.d.b d.d.b1 d.d.b2 d.d.b3 d.d.b4 d.d.b5 i.year if c>d, cluster(xy)
esttab using output.tex, wide
}
In my table I then get my estimates for
b
b1
b2
b3
b4
b5
d.b1
d.d.b1
d.b2
d.d.b2
and so on..

This is a bit hacked together--ultimately it doesn't do anything fancy but just automates the changing of variable names across specifications. It seems a bit too much code for such a simple question, but I don't know of an easier way to do this.
***create dummy data
set seed 99
webuse xtsetxmpl, clear
foreach i in "" 1 2 3 4 5 {
g b`i' = uniform()
}
foreach i in a aa aaa aaaa c d {
g `i'= y*uniform()
}
*next two lines just so the differencing works
g xy = pid
replace tod = (tod-1609570800000)/(36*100000)
xtset pid tod
***end of data creation
cap program drop diff
program define diff
syntax anything
cap drop *_adj *adjDV
if "`anything'" == "orig" {
foreach i in "" 1 2 3 4 5 {
g b`i'_adj = b`i'
}
foreach i in a aa aaa aaaa {
g `i'_adjDV = `i'
}
}
else {
foreach i in "" 1 2 3 4 5 {
g b`i'_adj = `anything'b`i'
}
foreach i in a aa aaa aaaa {
g `i'_adjDV = `anything'`i'
}
}
end
**************************************
*run original regression (excluding year term not necessary to example)
**************************************
eststo clear
foreach v in a aa aaa aaaa {
diff orig
eststo: xtreg `v'_adjDV *adj , fe cluster(xy)
eststo: xtreg `v'_adjDV *adj if c>d, fe cluster(xy)
diff d.
eststo: reg `v'_adjDV *adj , cluster(xy)
eststo: reg `v'_adjDV *adj if c>d, cluster(xy)
diff d.d.
eststo: reg `v'_adjDV *adj , cluster(xy)
eststo: reg `v'_adjDV *adj if c>d, cluster(xy)
esttab _all, wide
}
You are new here, so just for the future, try to post a MWE (minimal working example)---it makes things a bit quicker on this end. You can see that I have given an example of how to do this in the first section of the code.

It actually works just using the esttab command. One example:
esttab ,compress nogaps booktabs drop( _cons) ///
indicate("State specific effects = var_dummy_*") ///
rename( D.b b D2.b b D.b2 b2 D2.b b2 ) ///
The rename gets the job done. The rest is included to show more possibilities.

Related

How to deduce left-hand side matrix from vector?

Suppose I have the following script, which constructs a symbolic array, A_known, and a symbolic vector x, and performs a matrix multiplication.
clc; clearvars
try
pkg load symbolic
catch
error('Symbolic package not available!');
end
syms V_l k s0 s_mean
N = 3;
% Generate left-hand-side square matrix
A_known = sym(zeros(N));
for hI = 1:N
A_known(hI, 1:hI) = exp(-(hI:-1:1)*k);
end
A_known = A_known./V_l;
% Generate x vector
x = sym('x', [N 1]);
x(1) = x(1) + s0*V_l;
% Matrix multiplication to give b vector
b = A_known*x
Suppose A_known was actually unknown. Is there a way to deduce it from b and x? If so, how?
Til now, I only had the case where x was unknown, which normally can be solved via x = b \ A.
Mathematically, it is possible to get a solution, but it actually has infinite solutions.
Example
A = magic(5);
x = (1:5)';
b = A*x;
A_sol = b*pinv(x);
which has
>> A
A =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
but solves A as A_sol like
>> A_sol
A_sol =
3.1818 6.3636 9.5455 12.7273 15.9091
3.4545 6.9091 10.3636 13.8182 17.2727
4.4545 8.9091 13.3636 17.8182 22.2727
3.4545 6.9091 10.3636 13.8182 17.2727
3.1818 6.3636 9.5455 12.7273 15.9091

Change ordering of esttab output

The solution below involving the community-contributed command esttab (based on code from estout's help file), provides a way to show coefficients from different regressions in the same row.
However, the ordering is unexpected. How can I fix this?
First, I define a program appendmodels:
capt prog drop appendmodels
program appendmodels, eclass
// using first equation of model
syntax namelist
tempname b V tmp
foreach name of local namelist {
qui est restore `name'
mat `tmp' = e(b)
local eq1: coleq `tmp'
gettoken eq1 : eq1
mat `tmp' = `tmp'[1,"`eq1':"]
local cons = colnumb(`tmp',"_cons")
if `cons'<. & `cons'>1 {
mat `tmp' = `tmp'[1,1..`cons'-1]
}
mat `b' = nullmat(`b') , `tmp'
mat `tmp' = e(V)
mat `tmp' = `tmp'["`eq1':","`eq1':"]
if `cons'<. & `cons'>1 {
mat `tmp' = `tmp'[1..`cons'-1,1..`cons'-1]
}
capt confirm matrix `V'
if _rc {
mat `V' = `tmp'
}
else {
mat `V' = ///
( `V' , J(rowsof(`V'),colsof(`tmp'),0) ) \ ///
( J(rowsof(`tmp'),colsof(`V'),0) , `tmp' )
}
}
local names: colfullnames `b'
mat coln `V' = `names'
mat rown `V' = `names'
eret post `b' `V'
eret local cmd "whatever"
end
Then I run regressions and use the program to combine the results of these regressions into one:
eststo clear
sysuse auto, clear
eststo b1: regress price weight
eststo b2: regress price mpg
eststo b3: regress price foreign
eststo bivar: appendmodels b1 b2 b3
Then I transpose them:
esttab b1 b2 b3 bivar, se nostar noconstant
matrix C = r(coefs)
eststo clear
local rnames : rownames C
local models : coleq C
local models : list uniq models
local i 0
foreach name of local rnames {
local ++i
local j 0
capture matrix drop b
capture matrix drop se
foreach model of local models {
local ++j
matrix tmp = C[`i', 2*`j'-1]
if tmp[1,1]<. {
matrix colnames tmp = `model'
matrix b = nullmat(b), tmp
matrix tmp[1,1] = C[`i', 2*`j']
matrix se = nullmat(se), tmp
}
}
ereturn post b
quietly estadd matrix se
eststo `name'
}
esttab, se mtitle noobs
Result:
------------------------------------------------------------
(1) (2) (3)
weight mpg foreign
------------------------------------------------------------
b1 2.044***
(0.377)
bivar 2.044*** -238.9*** 312.3
(0.377) (53.08) (754.4)
b2 -238.9***
(53.08)
b3 312.3
(754.4)
------------------------------------------------------------
Clearly, the ordering has changed: instead of b1, b2, b3, bivar, it is b1, bivar, b2, b3.
How can I change the ordering to be b1, b2, b3, bivar?
Just use the order() option of esttab:
esttab, se mtitle noobs order(b?)
------------------------------------------------------------
(1) (2) (3)
weight mpg foreign
------------------------------------------------------------
b1 2.044***
(0.377)
b2 -238.9***
(53.08)
b3 312.3
(754.4)
bivar 2.044*** -238.9*** 312.3
(0.377) (53.08) (754.4)
------------------------------------------------------------
Standard errors in parentheses
* p<0.05, ** p<0.01, *** p<0.001

Storing coefficients from a Regression in Stata

I am trying to store the coefficients from a simulated regression in a variable b1 and b2 in the code below, but I'm not quite sure how to go about this. I've tried using return scalar b1 = _b[x1] and return scalar b2 = _b[x2], from the rclass() function, but that didn't work. Then I tried using scalar b1 = e(x1) and scalar b2 = e(x2), from the eclass() function and also wasn't successful.
The goal is to use these stored coefficients to estimate some value (say rhat) and test the standard error of rhat.
Here's my code below:
program montecarlo2, eclass
clear
version 11
drop _all
set obs 20
gen x1 = rchi2(4) - 4
gen x2 = (runiform(1,2) + 3.5)^2
gen u = 0.3*rnormal(0,25) + 0.7*rnormal(0,5)
gen y = 1.3*x1 + 0.7*x2 + 0.5*u
* OLS Model
regress y x1 x2
scalar b1 = e(x1)
scalar b2 = e(x2)
end
I want to do something like,
rhat = b1 + b2, and then test the standard error of rhat.
Let's hack a bit at your program:
Version 1
program montecarlo2
clear
version 11
set obs 20
gen x1 = rchi2(4) - 4
gen x2 = (runiform(1,2) + 3.5)^2
gen u = 0.3*rnormal(0,25) + 0.7*rnormal(0,5)
gen y = 1.3*x1 + 0.7*x2 + 0.5*u
* OLS Model
regress y x1 x2
end
I cut drop _all as unnecessary given the clear. I cut the eclass. One reason for doing that is the regress will leave e-class results in its wake any way. Also, you can if you wish add
scalar b1 = _b[x1]
scalar b2 = _b[x2]
scalar r = b1 + b2
either within the program after the regress or immediately after the program runs.
Version 2
program montecarlo2, eclass
clear
version 11
set obs 20
gen x1 = rchi2(4) - 4
gen x2 = (runiform(1,2) + 3.5)^2
gen u = 0.3*rnormal(0,25) + 0.7*rnormal(0,5)
gen y = 1.3*x1 + 0.7*x2 + 0.5*u
* OLS Model
regress y x1 x2
* stuff to add
end
Again, I cut drop _all as unnecessary given the clear. Now the declaration eclass is double-edged. It gives the programmer scope for their program to save e-class results, but you have to say what they will be. That's the stuff to add indicated by a comment above.
Warning: I've tested none of this. I am not addressing the wider context. #Dimitriy V. Masterov's suggestion of lincom is likely to be a really good idea for whatever your problem is.

Regression by year and companyID to save coefficients

I am trying to run regressions by companyID and year, and save the coefficients for each firm-year model as new variables in a new column right besides the other columns. There is an additional wrinkle‹ I have panel data for 1990-2010 and want to run each regression using t to t-4 only (I.e., for 2001, use only 1998-2001 years of data and i.e. for 1990 then only the data of 1990 and so on). I am new to using foreach loops and I found some prior coding on the web. I have tried to adapt it to my situation but two issues: anything.....
the output is staying blank
I have not figured out how to use the rolling four year data periods.
Here is the code I tried. Any suggestions would be much appreciated.
use paneldata.dta // the dataset I am working in
generate coeff . //empty variable for coefficient
foreach x of local levels {
forval z = 1990/2010
{
capture reg excess_returns excess_market
replace coeff = _b[fyear] & _b[CompanyID] if e(sample) }
}
So below is a short snapshot of what the data looks like;
CompanyID Re_Rf Rm-Rf Year
10 2 2 1990 
10 3 2 1991 
15 3 2 1991 
15 4 2 1992
15 5 2 1993 
21 4 2 1990 
21 4 2 1991 
34 3 1 1990 
34 3 1 1991
34 4 1 1992
34 2 1 1993  
34 3 1 1994
34 4 1 1995
34 2 1 1996   
 
Re_Rf = excess_returns 
Rm_Rf = excess_market 
I want to run the following regression: ​​​​​​​
reg excess_returns excess_market
There is a good discussion on Statalist, but I think this answer may be helpful for your learning about loops and how Stata syntax work.
the code I would use is as follows:
generate coeff = . //empty variable for coefficient
// put the values of gvkey into a local macro called levels
qui levelsof CompanyID, local(levels)
foreach co of local levels {
forval yr = 1994/2010 {
// run the regression with the condition that year is between yr
// and yr-3 (which is what you write in your example)
// and the CompanyID is the same as in the regression
qui reg Re_Rf Rm_Rf if fyear <= `yr' & fyear >= `yr'-3 & CompanyID== `co'
// now replace coeff equal to the coefficient on Rm_Rf with the same
// condiditions as above, but only for year yr
replace coeff = _b[Rm_Rf] if fyear == `yr' & CompanyID == `co'
}
}
This is a potentially dangerous thing to do if you do not have a balanced panel. If you are worried about this, there may be a way to deal with it using capture or changing the fyear loop to include something like:
levelsof fyear if CompanyID == `co', local(yr_level)
foreach yr of `yr_level' { ...

Trying to find index of minimum value in a matrix fails in Octave

So I have this matrix:
E1 = [54 5 2 4;4 5 19 29;31 4 2 9; 1 3 99 34]
lets say I want to find the location of the value closest to 18.9. let A = 18.9
I would do
[r,c] = find(E1==min(min(abs(E1-A))))
This doesn't work. It returns r = "[](0x1)" and c = "[](0x1)"
however,
if I first do:
F = abs(E1-A) and then do
[r,c] = find(F==min(min(F)))
this gives r = 2 and c = 3 which is correct. 19 is the closest value and 19 lives in row 2 column 3.
Why doesnt this work then? F is simply abs(E1-A) so why can I not put abs(E1-A) in place of F in the find formula?
min(min(abs(E1-A)))
ans = 0.10000
This gives you the min over the absolute difference. Then you compare it to E1 which has absolute values. This is complete different from your second formular
[r,c] = find(F==min(min(F)))
where you comapre the minimum difference with the matrix containing the absolute of differences between E1 and A. If you replace in your second formula F with abs(E1-A) you would get
[r,c] = find(abs(E1-A)==min(min(abs(E1-A))))
Which would also work. Nevertheless I would suggest another approach:
E1 = [54 5 2 4;4 5 19 29;31 4 2 9; 1 3 99 34];
A = 18.9;
# get the index ( Column-major order) of the minimum
idx = nthargout (2, #min, abs (E1-A)(:));
# this returns 10
# convert it ro row, column
[r, c] = ind2sub (size (E1), idx)
r = 2
c = 3