Change ordering of esttab output - 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

Related

How to calculate a probability vector and an observation count vector for a range of bins?

I want to test the hypothesis whether some 30 occurrences should fit a Poisson distribution.
#GNU Octave
X = [8 0 0 1 3 4 0 2 12 5 1 8 0 2 0 1 9 3 4 5 3 3 4 7 4 0 1 2 1 2]; #30 observations
bins = {0, 1, [2:3], [4:5], [6:20]}; #each bin can be single value or multiple values
I am trying to use Pearson's chi-square statistics here and coded the below function. I want a Poisson vector to contain corresponding Poisson probabilities for each bin and count the observations for each bin. I feel the loop is rather redundant and ugly. Can you please let me know how can I re-factor the function without the loop and make the whole calculation cleaner and more vectorized?
function result= poissonGoodnessOfFit(bins, observed)
assert(iscell(bins), "bins should be a cell array");
assert(all(cellfun("ismatrix", bins)) == 1, "bin entries either scalars or matrices");
assert(ismatrix(observed) && rows(observed) == 1, "observed data should be a 1xn matrix");
lambda_head = mean(observed); #poisson lambda parameter estimate
k = length(bins); #number of bin groups
n = length(observed); #number of observations
poisson_probability = []; #variable for poisson probability for each bin
observations = []; #variable for observation counts for each bin
for i=1:k
if isscalar(bins{1,i}) #this bin contains a single value
poisson_probability(1,i) = poisspdf(bins{1, i}, lambda_head);
observations(1, i) = histc(observed, bins{1, i});
else #this bin contains a range of values
inner_bins = bins{1, i}; #retrieve the range
inner_bins_k = length(inner_bins); #number of values inside
inner_poisson_probability = []; #variable to store individual probability of each value inside this bin
inner_observations = []; #variable to store observation counts of each value inside this bin
for j=1:inner_bins_k
inner_poisson_probability(1,j) = poisspdf(inner_bins(1, j), lambda_head);
inner_observations(1, j) = histc(observed, inner_bins(1, j));
endfor
poisson_probability(1, i) = sum(inner_poisson_probability, 2); #assign over the sum of all inner probabilities
observations(1, i) = sum(inner_observations, 2); #assign over the sum of all inner observation counts
endif
endfor
expected = n .* poisson_probability; #expected observations if indeed poisson using lambda_head
chisq = sum((observations - expected).^2 ./ expected, 2); #Pearson Chi-Square statistics
pvalue = 1 - chi2cdf(chisq, k-1-1);
result = struct("actual", observations, "expected", expected, "chi2", chisq, "pvalue", pvalue);
return;
endfunction
There's a couple of things worth noting in the code.
First, the 'scalar' case in your if block is actually identical to your 'range' case, since a scalar is simply a range of 1 element. So no special treatment is needed for it.
Second, you don't need to create such explicit subranges, your bin groups seem to be amenable to being used as indices into a larger result (as long as you add 1 to convert from 0-indexed to 1-indexed indices).
Therefore my approach would be to calculate the expected and observed numbers over the entire domain of interest (as inferred from your bin groups), and then use the bin groups themselves as 1-indices to obtain the desired subgroups, summing accordingly.
Here's an example code, written in the octave/matlab compatible subset of both languges:
function Result = poissonGoodnessOfFit( BinGroups, Observations )
% POISSONGOODNESSOFFIT( BinGroups, Observations) calculates the [... etc, etc.]
pkg load statistics; % only needed in octave; for matlab buy statistics toolbox.
assert( iscell( BinGroups ), 'Bins should be a cell array' );
assert( all( cellfun( #ismatrix, BinGroups ) ) == 1, 'Bin entries either scalars or matrices' );
assert( ismatrix( Observations ) && rows( Observations ) == 1, 'Observed data should be a 1xn matrix' );
% Define helpful variables
RangeMin = min( cellfun( #min, BinGroups ) );
RangeMax = max( cellfun( #max, BinGroups ) );
Domain = RangeMin : RangeMax;
LambdaEstimate = mean( Observations );
NBinGroups = length( BinGroups );
NObservations = length( Observations );
% Get expected and observed numbers per 'bin' (i.e. discrete value) over the *entire* domain.
Expected_Domain = NObservations * poisspdf( Domain, LambdaEstimate );
Observed_Domain = histc( Observations, Domain );
% Apply BinGroup values as indices
Expected_byBinGroup = cellfun( #(c) sum( Expected_Domain(c+1) ), BinGroups );
Observed_byBinGroup = cellfun( #(c) sum( Observed_Domain(c+1) ), BinGroups );
% Perform a Chi-Square test on the Bin-wise Expected and Observed outputs
O = Observed_byBinGroup; E = Expected_byBinGroup ; df = NBinGroups - 1 - 1;
ChiSquareTestStatistic = sum( (O - E) .^ 2 ./ E );
PValue = 1 - chi2cdf( ChiSquareTestStatistic, df );
Result = struct( 'actual', O, 'expected', E, 'chi2', ChiSquareTestStatistic, 'pvalue', PValue );
end
Running with your example gives:
X = [8 0 0 1 3 4 0 2 12 5 1 8 0 2 0 1 9 3 4 5 3 3 4 7 4 0 1 2 1 2]; % 30 observations
bins = {0, 1, [2:3], [4:5], [6:20]}; % each bin can be single value or multiple values
Result = poissonGoodnessOfFit( bins, X )
% Result =
% scalar structure containing the fields:
% actual = 6 5 8 6 5
% expected = 1.2643 4.0037 13.0304 8.6522 3.0493
% chi2 = 21.989
% pvalue = 0.000065574
A general comment about the code; it is always preferable to write self-explainable code, rather than code that does not make sense by itself in the absence of a comment. Comments generally should only be used to explain the 'why', rather than the 'how'.

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.

Failing to solve a simple ODE with Octave

I am new to Octave, so I am trying to make some simple examples work before moving onto more complex projects.
I am trying to resolve the ODE dy/dx = a*x+b, but without success. Here is the code:
%Funzione retta y = a*x + b. Ingressi: vettore valori t; coefficienti a,b
clear all;
%Inizializza argomenti
b = 1;
a = 1;
x = ones(1,20);
function y = retta(a, x, b) %Definisce funzione
y = ones(1,20);
y = a .* x .+ b;
endfunction
%Calcola retta
x = [-10:10];
a = 2;
b = 2;
r = retta(a, x, b)
c = b;
p1 = (a/2)*x.^2+b.*x+c %Sol. analitica di dy/dx = retta %
plot(x, r, x, p1);
% Risolve eq. differenziale dy/dx = retta %
y0 = b; x0 = 0;
p2 = lsode(#retta, y0, x)
And the output is:
retta3code
r =
-18 -16 -14 -12 -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 16 18 20 22
p1 =
Columns 1 through 18:
82 65 50 37 26 17 10 5 2 1 2 5 10 17 26 37 50 65
Columns 19 through 21:
82 101 122
error: 'b' undefined near line 9 column 16
error: called from:
error: retta at line 9, column 4
error: lsode: evaluation of user-supplied function failed
error: lsode: inconsistent sizes for state and derivative vectors
error: /home/fabio/octave_file/retta3code.m at line 21, column 4
So, the function retta works properly the first time, but it fails when used in lsode.
Why does that happen? What needs to be changed to make the code work?
Somehow you still miss some important parts of the story. To solve an ODE y'=f(y,x) you need to define a function
function ydot = f(y,x)
where ydot has the same dimensions as y, both have to be vectors, even f they are of dimension 1. x is a scalar. For some traditional reason, lsode (a FORTRAN code used in multiple solver packages) prefers the less used order (y,x), in most text books and other solvers you find the order (x,y).
Then to get solution samples ylist over sample points xlist you call
ylist = lsode("f", y0, xlist)
where xlist(1) is the initial time.
The internals of f are independent of the sample list list and what size it has. It is a separate issue that you can use multi-evaluation to compute the exact solution with something like
yexact = solexact(xlist)
To pass parameters, use anonymous functions, like in
function ydot = f(y,x,a,b)
ydot = [ a*x+b ]
end
a_val = ...
b_val = ...
lsode(#(y,x) f(y,x,a_val, b_val), y0, xlist)
The code as modified below works, but I'd prefer to be able to define the parameters a and b out of the function and then pass them to rdot as arguments.
x = [-10,10];
a = 1;
b = 0;
c = b;
p1 = (a/2).*(x.^2)+b.*x+c %Sol. analitica di dy/dx = retta %
function ydot = rdot(ydot, x)
a = 1;
b = 0;
ydot = ones(1,21);
ydot = a.*x .+ b;
endfunction
y0 = p1(1); x0 = 0;
p2 = lsode("rdot", y0, x, x0)'
plot(x, p1, "-k", x, p2, ".r");

Fe and first difference in a regression table

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.

Verilog Code: Output Malfunction

The following code is meant to output a 1 in the case of wires S1 and X being asserted and wire S0 being deasserted. However, when I run the wave form, the output is constantly 0.
The logic equations governing the wires are:
S1 = (S0 & ~X) | (S1 & ~S0 & X)
S0 = X
O = (S1 & S0)
Is there a problem with my code:
module Dff1(D, clk, Q, Qbar);
input D, clk;
output reg Q;
output Qbar;
initial begin
Q = 0;
end
assign Qbar = ~Q;
always #(posedge clk)
Q = D;
endmodule
module Mod1 (clk, X, O);
input clk, X;
output O;
wire S1, S0, Q1, Q0, Q1bar, Q0bar;
assign S1 = (S0 & ~X) | (S1 & ~S0 & X);
Dff1 C1(S1, clk, Q1, Q1bar);
assign S0 = X;
Dff1 C0(S0, clk, Q0, Q0bar);
assign O = (S1 & S0);
endmodule
module test_bench ();
wire clk;
reg osc;
reg [1:0] R;
reg Seqinput;
integer num;
initial begin
osc = 0;
num = 0;
Seqinput = 0;
end
initial begin
$dumpfile("dump.vcd");
$dumpvars;
#20000 $finish;
end
always begin
#10 osc = ~osc;
num = (num >= 7) // counter incremented by 1 from 0..7
? 0 : (num + 1);
if ((num % 2) == 0) begin // every other time step
R = $random % 2; // $random generates a 32-bit signed
// random number
// -1 <= $random % 2 <= 1
if (R > 0)
Seqinput = 1; // input is 1
else
Seqinput = 0; // input is 0
end
end
assign clk=osc;
wire Out1;
Mod1 Mod1instance(clk, Seqinput, Out1);
endmodule
Explained with substitution:
S1 = (S0 & ~X) | (S1 & ~S0 & X) sub S0 with X
S1 = ((X) & ~X) | (S1 & ~(X) & X) X & ~X == 0
S1 = ( 0 ) | ( S1 & 0 ) S1 & 0 == 0;
S1 = ( 0 ) | ( 0 )
S1 = 0
Since the assignment of S1 dependent on its current value, it is considered asynchronous feedback logic. This is normally something you don't want to do. I believe the real equation you want is:
S1 = (Q0 & ~X) | (Q1 & ~Q0 & X)
This makes the code synchronous and predictable. Q1 and Q0 are the previous clocked values of S1 and S0 respectively.
Also, it is important to use non-blocking assignments when assigning (<=) flops. Verilog is a non-determent simulator. This means operations scheduled in the same region can happen in any order. Using non-blocking on a flop moves the assignment to the NBA region while its evaluation in kept in the active region.
always #(posedge clk)
Q <= D;