Disclaimer : I make NO guarantees on this code.  It is shared for the purposes of sharing best practices and crowd sourcing solutions.  Researchers should ALWAYS verify that code is functioning as intended.


Clustered standard errors are used when your analysis should account for correlation of error terms within certain clusters such as firms that are repeated within a data set.  Generally speaking, Stata can calculate clustered standard errors when you use the following option at the end of your command: vce(cl [varname]).

The challenge with using this option is that it accounts for what is called a one-way cluster.  When using panel data, however, you may want to consider using two-way clustered standard errors.  These will allow you to control for clusters across two dimensions (i.e. firm & time).  For more information on when you may like to use two-way clustered standard errors, you can refer to the following papers:

Petersen, M. A. (2009). Estimating standard errors in finance panel data sets: Comparing approaches. Review of financial studies22(1), 435-480.

Cameron, A. C., Gelbach, J. B., & Miller, D. L. (2012). Robust inference with multiway clustering. Journal of Business & Economic Statistics.

Thompson, S. B. (2011). Simple formulas for standard errors that cluster by both firm and time. Journal of Financial Economics99(1), 1-10.

At the present time, there is no code that is built into Stata that allows you to calculate two-way clustered standard errors.  Fortunately, Mitchell Peterson at Northwestern University has written such code for OLS, tobit, logit, and probit models.  The ADO files with this code are available on his website.

In using Dr. Peterson’s ADO files, I found that I had a need for additional output such as chi-squared values, model significance, and information on screened variables.  With his permission, I am sharing my additions to his tobit2 code.

If you are using Petersen’s logit2 or probit2 code, you may have some difficulties calculating marginal effects as typing “mfx” or “margins, dydx(*)” after using this code will not yield the marginal effects.  This is because these ADO files do not mark the estimation sample or have a dedicated predict function.  Don’t worry, though. For post-estimation code that can help you calculate marginal effects when using these ADO files, click here.