Category Archives: Code

Plot multiple kernel densities on one plot in Stata

If you want to compare kernel density estimates across years for a particular variable, putting each estimate on one graph will make it easy. The process is fairly straightforward in Stata (and even easier in Matlab…). First, we start with the simple ‘kdensity‘ command

kdensity income if year == 1990

Next, we append this command with the ‘addplot‘ function:

kdensity income if year == 1990, addplot(kdensity income if year == 1991)

and we can add even more with the ‘||’ syntax:

kdensity income if year == 1990, addplot(kdensity income if year == 1991 || kdensity income if year == 1992)

If we could use the ‘by’ option, this process would be much cleaner. Finally, we add a legend:

kdensity income if year == 1990, addplot(kdensity income if year == 1991 || kdensity income if year == 1992) legend(ring(0) pos(2) label(1 "1990") label(2 "1991") label(3 "1992"))

LateX files for top finance journals

Richard Stanton provides an excellent source of tex files (styles, bst, etc.) for major finance journals.

Some notes:

  • Inline subsubsections for RFS can be done using


Stata latex summary tables: a hack

The Stata package “eststo” has a nice set of commands to create summary statistics tables. These commands can create “fragments” that you can dynamically add to tables in a main tex document. Unfortunately, the output of the following example starts with an ‘hline’ and doesn’t play well with latex:

eststo clear
eststo: estpost tabstat var1 var2 var3, listwise statistics(mean sd) columns(statistics)

Saving to a tex file:

esttab using "../writing/tables/summary.tex", main(mean) aux(sd) nostar unstack nonote nomtitle nonumber replace label fragment

creates a tex file that starts with ‘hline.’ When combined with a tabular environment with the ‘input’ command (here input{tables/summary.tex}) you will get a “Misplaced noalign” error. I found a partial solution online with some changes.

1. Add makeatletter followed by newcommand*ExpandableInput[1]{@@input#1 } to your header

2.  Make sure the header also has makeatother as well

3.  Replace the input command with ExpandableInput{tables/summary.tex}

4.  If you want to end the table with more ‘hline”s, put a line break after the above command ‘\’

“Simple” as that….

Simple Bootstrap Sample in Matlab

Let x be your vector of data which has to be bootstrapped. For each instance of the loop write:

x = x(floor(rows(x)*rand(rows(x),1))+1,:); % bootstrap observations

Now we have a matrix with the same number of rows and columns, but with re-sampled data with replacement. A one line bootstrap! (Modified code from John Cochrane)

Using Remote Files in Matlab

If you have an FTP server set-up somewhere with ample space and bandwidth, Matlab can store and retrieve its files and data remotely. Just use these simple commands to connect and disconnect:

% File to connect to the server where my Matlab/data resides

% connect to the db
f = ftp(‘’, ‘user’, ‘password’);

% change the directory
cd(f, ‘matlab’);

% now change the directory that we want to download the directory
cd ‘/’;

% Download the directory
mget(f, ‘remote_dir’);

% *****************
% *****************

% when done move a directory here
cd ..
% now move the directory back to the server
mput(f, ‘remote_dir’);
disp(‘Files have been put back on the server’);

% close the connect

Running sums in Stata

Perhaps it is bad that I didn’t know this before, but the following code for Stata would have saved a week off of my dissertation work.  Suppose that you have data structured like so:


and you want to create a new variable that is the total amount as of each date for each firm.  In Stata, you simply type:

sort firm_id date
bysort firm_id: gen total_t = sum(amount)

Note the use of ‘gen‘ rather than ‘egen.’ The ‘sum’ command differs by the type of generate command (i.e. gen or egen), so about 500 lines of loops written in Stata code could be condensed in a few lines.  Stata needs to fix the ‘egen’ and ‘gen’ distinction or I need to port more of my projects to R.