Skip to content

Instantly share code, notes, and snippets.

@seanjtaylor
Last active August 29, 2015 14:25
Show Gist options
  • Save seanjtaylor/2a1c3a5b0fa1576148ec to your computer and use it in GitHub Desktop.
Save seanjtaylor/2a1c3a5b0fa1576148ec to your computer and use it in GitHub Desktop.
How to estimate a BTL model in R.
df <- data.frame(higher = c('US', 'CA', 'MX'),
lower = c('CA', 'MX', 'MX'))
levels <- c('US', 'CA', 'MX')
X.l <- model.matrix(~ 0 + factor(higher, levels = levels), data = df)
X.r <- model.matrix(~ 0 + factor(lower, levels = levels), data = df)
X <- X.l - X.r
colnames(X) <- levels # makes it easier to interpret regression output
# The higher one is always better than the lower one.
y <- rep(1, nrow(X))
library(arm)
## Use a slight prior to stabilize estimates.
m <- bayesglm(y ~ 0 + X, family = binomial, prior.scale = 1)
summary(m)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment