Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: new packages for MCMC estimation

From   Sam Schulhofer-Wohl <>
Subject   st: new packages for MCMC estimation
Date   Thu, 5 Jan 2012 16:56:27 -0600

Thanks to Kit Baum, two new packages for Markov chain Monte Carlo
(MCMC) estimation are now available on SSC.

The package -mcmcstats- provides two commands for analyzing results
from MCMC estimation. -mcmcconverge- calculates convergence statistics
for scalar estimands. -mcmcsummarize- calculates summary statistics of
the marginal distribution of scalar estimands. To install, type -ssc
install mcmcstats-.

The package -mcmclinear- uses MCMC to generate draws from the
posterior distribution of linear models. So far, I have implemented
two models: a normal linear regression model (-mcmcreg-) and a normal
linear mixed model (-mcmcmixed-). The normal linear regression model
is the Bayesian equivalent of -regress-, and the normal linear mixed
model is the Bayesian equivalent of -xtmixed-, though -mcmcreg- and
-mcmcmixed- have fewer bells and whistles than -regress- and
-xtmixed-. More models, and more features for the existing models,
could be added if this package proves useful to a significant number
of users. To install, type -ssc install mcmclinear-.

Some examples:

*normal linear regression
. sysuse auto, clear
. mcmcreg mpg weight foreign, saving(mcmcreg_auto.dta) d0(0.01)
*mcmcreg_auto.dta now contains draws from the posterior
. use mcmcreg_auto.dta
. list
. summarize *

*normal linear mixed model with manufacturer-specific random
intercepts and manufacturer-specific random coefficients on weight
. scalar mydelta0=0.01
. sysuse auto
. generate manuf=cond(strpos(make," ")>0,substr(make,1,strpos(make," ")-1),make)
. mcmcmixed mpg weight foreign || manuf:weight,
saving(mcmcmixed_auto2.dta) d0(0.01) delta0(mydelta0)
*mcmcmixed_auto2.dta now contains draws from the posterior
. use mcmcmixed_auto2.dta
. list
. summarize *

-mcmcmixed- is potentially useful in two situations. First, if you
want Bayesian rather than frequentist estimates, -mcmcmixed- produces
them. Second, in large datasets, -mcmcmixed- is much faster than
-xtmixed-. Since (under appropriate conditions) Bayesian estimates
converge to maximum likelihood estimates in large samples, -mcmcmixed-
is a way for frequentists to quickly estimate mixed models in large
datasets. For example, using a 0.5% sample from the 2000 U.S. Census,
I estimated a regression of log(income) on education, experience and
experience squared, with state-specific random intercepts and
state-specific random coefficients on education. This sample has about
700,000 observations once children and people without income are
dropped. Frequentist estimation with

. xtmixed lninc yearsofed exper exper2 || statefip:yearsofed,

took 171 seconds in Stata/MP-4 on a fast computer. Bayesian estimation
with -mcmcmixed- took 16 seconds, including time to run multiple
chains to convergence. The posterior means and standard deviations
from -mcmcmixed- were approximately the same as the point estimates
and standard errors from -xtmixed-. When I increased the sample size
to 1%, -mcmcmixed- took 22 seconds, while -xtmixed- did not reach
convergence after running for 30 minutes (at which point I stopped the

An important caveat to -mcmcmixed- and -mcmcreg- is that these
functions do nothing except generate MCMC draws from the posterior of
the model. The functions do not check whether the Markov chain has
converged; to do that, you can run multiple chains (see -help mcmcreg-
for an example) and then check for convergence using -mcmcconverge-.
The functions also do not summarize the draws from the posterior in
any way; to do that, after checking for convergence, you can drop
early iterations of the chains and then summarize the later iterations
using -mcmcstats-.

Sam Schulhofer-Wohl
Senior Economist
Research Department
Federal Reserve Bank of Minneapolis
90 Hennepin Ave.
Minneapolis MN 55480-0291
(612) 204-5484

Views expressed herein are those of the author and not necessarily
those of the Federal Reserve Bank of Minneapolis or the Federal
Reserve System.
*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index