Skip to content

Instantly share code, notes, and snippets.

View carlislerainey's full-sized avatar

Carlisle Rainey carlislerainey

View GitHub Profile
@carlislerainey
carlislerainey / comb_mi.R
Last active July 27, 2017 17:11
A function to combine the sets of estimates from multiply imputed data sets into a single set of coefficients, standard errors, and a covariance matrix.
##########################################
## A Function to Combine Estimates from MI
##########################################
comb.mi <- function(models) {
# Arguments
# models: a list of models, one estimated on each of m MI data sets.
# Note: I borrow the notation below mostly from Rubin's *Multiple Imputation
@carlislerainey
carlislerainey / beta-model.R
Created September 17, 2014 17:06
A function to estimate the beta model (w/ standard errors)
# log-likelihood for beta
ll.fn.beta <- function(theta, y) {
alpha <- theta[1] # optim() requires a single parameter vector
beta <- theta[2]
ll <- alpha*sum(log(y)) + beta*sum(log(1 - y)) -
length(y)*log(beta(alpha, beta))
return(ll)
}
# function to estimate beta model
@carlislerainey
carlislerainey / logit-model.R
Last active August 29, 2015 14:06
A function to estimate a logit model given X and y.
# define log-likelihood function
ll.logit <- function(beta, y, X) {
p <- plogis(X%*%beta)
loglik <- sum(y*log(p)) + sum((1 - y)*log(1 - p))
return(loglik)
}
# optimize
logit <- function(y, X) {
init.par <- rep(0, ncol(X))
@carlislerainey
carlislerainey / fearon.R
Last active August 29, 2015 14:06
Replicate Fearon and Laitin (2003) (Table 1, Model 1)
# load packages
library(foreign) # for read.dta()
library(arm) # for display()
# load data
fl <- read.dta("http://crain.co/am-files/data/fearon-laitin.dta")
# something weird is going on
table(fl$onset) # wtf?
fl$onset[fl$onset == 4] <- 1 # recode weird case
@carlislerainey
carlislerainey / logit-qi-incomplete-example.R
Last active August 29, 2015 14:06
An incomplete example of how to use predicted probabilities to be completed in class.
# load libraries
library(arm)
# read data
d <- read.csv("http://crain.co/am-files/data/turnout-small.csv")
# estimate simple logit model
m <- glm(vote ~ educate + age + income,
family = binomial, data = d)
@carlislerainey
carlislerainey / count-models.R
Created October 8, 2014 18:25
Estimate and check the fit of Poisson and Negative-Binomial Models
# load packages
library(MASS)
library(compactr)
library(compactr)
# read data
d <- read.csv("http://crain.co/am-files/data/hks.tab", sep = "\t")
keep <- c("osvAll", "troopLag", "policeLag", "militaryobserversLag",
"brv_AllLag", "osvAllLagDum", "incomp", "epduration", "lntpop",
@carlislerainey
carlislerainey / ordered-probit-model.R
Created October 15, 2014 17:43
Estimate an ordered probit model.
# replication of Hill, Moore, and Mukherjee (2013)
# load packages
library(arm)
# load data
d <- read.csv("http://crain.co/am-files/data/hmm.csv")
# examine data
@carlislerainey
carlislerainey / polytomous-outcome.R
Created October 29, 2014 17:39
Load data and formula for model of polytomous outcomes.
d <- read.dta("http://crain.co/am-files/data/nes92.dta")
f <- vote ~ conservative + education + union +
income + black + distance_to_clinton +
distance_to_bush + economy_worse
@carlislerainey
carlislerainey / illustrate-separation-package.R
Last active August 29, 2015 14:08
Illustrate the separation package
# install packages (if necessary)
install.packages("devtools")
install.packages("arm")
# install separation and compactr from GitHub
devtools::install_github("carlislerainey/compactr")
devtools::install_github("carlislerainey/separation")
# load packages
library(separation)
@carlislerainey
carlislerainey / beta-binomial.R
Created November 12, 2014 18:08
Illustrate the idea of a prior distribution.
n <- 0
k <- 0
a <- c(1, 2, 3, 4, 5)
b <- c(1, 2, 3, 4, 5)
col <- 1:length(a)
names <- c("Carlisle", "B", "C", "D", "E")
plot(NULL, xlim = c(0, 1), ylim = c(0, 3),
xlab = expression(pi),