Get a slice of a mcmclist faster

Say you sampled a large amount of parameters from a posterior and stored the parameters in a mcmclist object. In order to analyze your results, you want to slice the mcmclist and only select a subset of the parameters. One way to do this is:


where mcmcsample is your mcmclist with the posterior sample and i the parameter you are interested in. Turns out, this function is very slow. A faster way is to use this little function:

getMCMCSample <- function(mcmclist,i){
  chainext <- function(x,i) return(x[,i])
  return(as.mcmc.list(lapply(mcmclist, chainext, i = i)))


# Run a toy model
counts <- c(18,17,15,20,10,20,25,13,12)
outcome <- gl(3,1,9)
treatment <- gl(3,3)
posterior1 <- MCMCpoisson(counts ~ outcome + treatment)
posterior2 <- MCMCpoisson(counts ~ outcome + treatment)
posterior3 <- MCMCpoisson(counts ~ outcome + treatment)
mcmclist <- mcmc.list(posterior1,posterior2,posterior3)

system.time(getMCMCSample(mcmclist, 2:3))

The build-in way takes 0.003 sec on my machine, while getMCMCSample gets it done in 0.001. For this little example, the difference is negligible. But as it turns out, for large posterior samples, it really makes a difference.

Update: Apps, packages and services for Political Scientists

Last year, I listed a couple of Apps that I found particular useful for doing political science research. Over the year, I started using a couple of new ones.


  • Papers – organizes your PDF library + citation database
  • New: DEVONThink – Manage files, notes, lists – everything. I use it to organize my paper library and notes. Works like a charm.
  • Mendeley – manages citation database (desktop and online app)
  • New: Versions – Subversion Client for Mac, simple but with a nice timeline visualization that makes it easy to find previous versions.
  • RStudio – a nice IDE for R if you don’t like plain coding
  • TextMate – Code Editor for R, Latex, Python and what-not


  • countrycode – R package that “can convert to and from 7 different country coding schemes. It uses regular expressions to convert long country names (e.g. Sri Lanka) into any of those coding schemes [Correlates of War character, CoW-numeric, ISO3-character, ISO3-numeric, ISO2-character, IMF, FIPS 10-4, official English short country names (ISO), continent, region], or into standardized country names (official short English).”
  • datasciencetoolkit – Python toolbox to extract various data from texts
  • BeautifulSoup – helps to scrap websites with a few lines of Python code
  • New: Osborne’s Games – Latex files to draw extensive and normal form games
  • NewtikzDevice – R graphics output in a native Latex.


  • AWS/EC2 – run R and Python in the cloud to save time
  • TileMill – makes nice maps to visualize geocoded data
  • Detexify – translates handwritten symbols in LaTex code
  • RegEr – instant regular expression tester

This issue of The Political Methodologist discusses how to organize research, especially the data analysis workflow. Worth reading!