Skip to content

Instantly share code, notes, and snippets.

@diamonaj
Created March 15, 2023 16:35
Show Gist options
  • Save diamonaj/f5963fc6725920781e71a4cda04faecb to your computer and use it in GitHub Desktop.
Save diamonaj/f5963fc6725920781e71a4cda04faecb to your computer and use it in GitHub Desktop.
# DATA PREPROCESSING
foo <- read.csv("https://tinyurl.com/y2zt2hyc")
foo <- foo[, c(6:8, 11:16, 99, 50, 114, 49, 63, 136, 109, 126, 48, 160, 142, 10)]
foo <- foo[c(-19, -47), ]
which(is.na(foo) == TRUE)
head(foo)
# What is the 3 digit country code associated with the first row of the data set?
foo$clust2[1]
foo$yrbeg[1]
# Treatment is peacekeeping (2)
# Outcome is peacebuilding success (1)
# Confounder is any of 3, 4, or 5
# SUTVA could be violated, since interventions in one country may affect the
# outcomes of another (most likely neighboring) country. Furthermore,
# countries do not receive exactly the same treatment, since peacekeeping
# mission vary along several dimensions.
# DATA ANALYSIS
# How many treated units?
sum(foo$untype4)
# How many control units?
length(foo$untype4) - sum(foo$untype4)
# P-SCORE MATCHING
library(Matching)
# Compute the propensity scores
lm.prop <- glm(untype4 ~ wartype + logcost + wardur + factnum + factnum2 + trnsfcap + treaty +
develop + exp + decade, data=foo, family="binomial")
X <- lm.prop$fitted.values
# Do the matching
matchout.prop <- Match(Y = foo$pbs2s3, Tr=foo$untype4, X=X)
# Check the balance
mb.out <- MatchBalance(untype4 ~ wartype + logcost + wardur + factnum + factnum2 + trnsfcap + treaty +
develop + exp + decade, data=foo, match.out = matchout.prop, nboots=1000)
mb.out$AMsmallestVarName
mb.out$AMsmallest.p.value
# MULTIVARIATE MATCHING
attach(foo)
X <- cbind(wartype, logcost, wardur, factnum, factnum2, trnsfcap, treaty, develop, exp, decade)
detach(foo)
matchout <- Match(Y = foo$pbs2s3, Tr=foo$untype4, X=X)
mb.out2 <- MatchBalance(untype4 ~ wartype + logcost + wardur + factnum + factnum2 + trnsfcap + treaty +
develop + exp + decade, data=foo, match.out = matchout, nboots=1000)
mb.out2$AMsmallestVarName
mb.out2$AMsmallest.p.value
# Risk of confounding addressed: NO
# GENETIC MATCHING
genout <- GenMatch(X = X, Tr = foo$untype4)
matchout.gen <- Match(X = X, Tr = foo$untype4, Y = foo$pbs2s3, Weight.matrix=genout)
mb.out3 <- MatchBalance(untype4 ~ wartype + logcost + wardur + factnum + factnum2 + trnsfcap + treaty +
develop + exp + decade, data=foo, match.out = matchout.gen, nboots=1000)
mb.out3$AMsmallestVarName
mb.out3$AMsmallest.p.value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment