R package apsrtable

apsrtable() is an R package, with detailed documentation, for formatting model output from several models side-by-side in Latex (example PDF).

The package vignette includes a worked example of how to customize the display; it should be helpful if you are adding support for a model. A version of the vignette was published in The Political Methodologist 16.2 (Spring 2009). To install, just tell R:

install.pacakges("apsrtable")

It accepts lm and glm objects with either the standard or user-supplied variance-covariance matrix, placing standard errors below coefficients and in parentheses; it adorns estimates with either one or multiple glittering stars and other symbols; and uses dcolumn to align output, with the pleasant side effect of math-mode for model output (no xtable $-$ negative signs!). Model naming is flexible, either auto-numbering with an adjustable counter, or using a vector of user-supplied names; selected coefficients can be omitted from the output; and the order of coefficients is determined by the models supplied and several options for processing them. The latex it produces is formatted to be legible in tex source. It also plays well with Sweave. Here’s the output from the example:

apsrtb>      apsrtable(lm.D90, lm.D9, digits=1, align="center", 
apsrtb+                stars="default", model.counter=0, order="rl")
\begin{table}[!ht]
\caption{}
\label{}
\begin{tabular}{cD{.}{.}{-1}D{.}{.}{-1}}\hline
 & \multicolumn{1}{c}{Model 0} & \multicolumn{1}{c}{Model 1}\\ 
%           & Model 0 & Model 1\\ 
(Intercept) &         & 5.0 ***\\ 
            &         & (0.2)  \\ 
groupTrt    & 4.7 *** & -0.4   \\ 
            & (0.2)   & (0.3)  \\ 
groupCtl    & 5.0 *** &        \\ 
            & (0.2)   &        \\ 
$N$         & 20      & 20     \\ 
$R^2$       & 1.0     & 0.1    \\ 
adj. $R^2$  & 1.0     & 0.0    \\\hline
\multicolumn{3}{l}{Standard errors in parentheses}\\
\end{table}

MCMCpack with hierarchical IRT

PolMeth 2008 Poster!

I made a poster for PolMeth 2008 about the model, perhaps a bit glib, but I liked it and had fun making it.

The devel version of MCMCpack includes my hierarchical model in “new” Scythe calls. This means: faster model fitting through very slick templating. However, you may still want to use the code below, which is built against old Scythe. Why? The database part described in this paper and this presentation. Because measurement models often have lots and lots of parameters and can take a very long time to converge (though this is faster than the canonical 1d irt model), and can quickly exceed even modern memory capacity. On the distant horizon is to create a flexible storage-and-replication model for MCMCpack.

modMCMCpack.tgz. I recommend installing this package into a sandbox lib.loc and not your main R library. MCMCirtHier1d has begun talks to join MCMCpack, but for now it's struggling with the acquis codebasaire.

wget http://malecki.wustl.edu/modMCMCpack.tgz
tar -zxvf modMCMCpack.tgz
R CMD INSTALL -l ~/R MCMCpack

A set of covariates Xj are used to predict ideal points θ ~ N(X'β, σ2) with standard Normal, IG prior on β and σ2. The full conditional distribution for (binary) θ is described in Fox 2007. a,b∈η is the typical IRT update. β and σ2 are Normal regression updates.

Prerequisites: Hier IRT also implements a new storage technique for MCMC draws. Thus SQLite3 and RSQLite are up to you to find and install. On a Mac, you can simply fink install sqlite3 sqlite3-dev and in ubuntu or debian it should be doable with apt-get install sqlite3 libsqlite3-dev. Similarly, in (sudo'd) R you should be able to install.packages("RSQLite"). Note that it defaults to storing both item (bill) and person parameters and that memory is no longer a limitation on storage.

Fitting a model: A data.frame with named covariates is expected. The following code fits a model to the MCMCpack SupremeCourt dataset using covariates for the party of the appointing president and for sex.

library(MCMCpack,lib.loc="~/R")
data(SupremeCourt)
file.remove("Hier1d.db")
presparty <- c(1,1,0,1,1,1,1,0,0)
sex <- c(0,0,1,0,0,0,0,1,0)
Xjdata <- data.frame(presparty=presparty,sex=sex)
   posterior1 <- MCMCirtHier1d(t(SupremeCourt),
                   alpha.start=.2, beta.start=.2,
                   burnin=500, mcmc=200000, thin=20, 
                   verbose=500,
                   Xjdata=Xjdata,
                   betastart=c(-.5,.5),sigmastart=1,
                   drop.tables=TRUE)
dbh <- dbConnect("SQLite","Hier1d.db")
beta <- dbReadTable(dbh,"beta")
theta <- dbReadTable(dbh,"theta")
a <- dbReadTable(dbh,"a")

The development version of the code puts bill parameters a,b into separate database tables because SQLite defines a maximum of 2000 columns per table. This means we can analyze a maximum of 2000 bills (1000 if η are both stored in one table). You can overcome this by installing sqlite3 and RSQLite with the configure variable SQLITE_MAX_COLUMN=n. RSQLite builds and installs its own SQLite library and is needed to create and work with the samples in R. I don't know which one is actually used by the #include <sqlite3.h> in MCMCirtHier1d.cc so I suggest doing it to both.

Code Snippets &c.

Here are code snippets I use all the time that may be of more general use.

plants