Skip to content

Instantly share code, notes, and snippets.

@MartinMacharia
Created August 22, 2017 09:34
Show Gist options
  • Save MartinMacharia/48347312f0eef7b3aa7bac32b5a1d7d2 to your computer and use it in GitHub Desktop.
Save MartinMacharia/48347312f0eef7b3aa7bac32b5a1d7d2 to your computer and use it in GitHub Desktop.
PSM
data=read.csv(file.choose(), header=T) # Import data called data
View(data)# View data
duplicated(data$Dup) #Check for duplicates on column Dup, not a very efficient way for large dataset
data$Dup[duplicated(data1$Dup)] #Show duplicate entries, don't like how results are displayed
unique(data1[duplicated(data1$Dup),])#Better way of showing unique repeat entries
#PSM
setwd("C:/users/machariam/Desktop/Rwanda PW Impact report")
getwd()
RwandaData=read.csv("CleanV4.csv")
attach(RwandaData)
RwandaData=as.data.frame(na.omit(RwandaData)) #Omit missing values
#sapply(RwandaData, function(x) sum(is.na(x)))#Inspect missing values
#RwandaData=read.csv("CleanV5.csv", header=T, na.strings=c(""," ","NA")) #Insert NAs on missing values
na.omit(RwandaData)
#detach(RwandaData)
attach(RwandaData)
RwandaData[1:10,]
RwandaData[75:79,]
install.packages("MatchIt")
library(MatchIt)
names(RwandaData)
str(RwandaData)
#Nearest Neighbor
m.out = matchit(Use ~ FarmingImportanceCode + TotalHHMembers + NumberBtw14To65 + RespSexCode +RespAge+RespEducCode+TotalMaizeLand +YoungDepRatio+TLU,
data = RwandaData, method = "nearest",
ratio = 1)
summary(m.out)
plot(m.out, type = "jitter")
plot(m.out, type = "hist")
#Nearest Neighbor with caliper
m.out1 = matchit(Use ~ FarmingImportanceCode + TotalHHMembers + NumberBtw14To65 + RespSexCode +RespAge+RespEducCode+TotalMaizeLand +YoungDepRatio+TLU,
data = RwandaData, method = "nearest", caliper=0.16)
plot(m.out1, type = "hist")
summary(m.out1)
plot(m.out1, type = "jitter")
#Exact Matching
m.out2 = matchit(Use ~ FarmingImportanceCode + TotalHHMembers + NumberBtw14To65 + RespSexCode +RespAge+RespEducCode+TotalMaizeLand +YoungDepRatio+TLU,
data = RwandaData, method = "exact")
summary(m.out2)
plot(m.out2, type = "hist")
#Subclassification
m.out3 = matchit(Use ~ FarmingImportanceCode + TotalHHMembers + NumberBtw14To65 + RespSexCode +RespAge+RespEducCode+TotalMaizeLand +YoungDepRatio+TLU,
data = RwandaData, method = "subclass", subclass=4)
summary(m.out3)
plot(m.out3, type = "hist")
#Optimal Matching
install.packages("optmatch")
library(optmatch)
m.out4 = matchit(Use ~ FarmingImportanceCode + TotalHHMembers + NumberBtw14To65 + RespSexCode +RespAge+RespEducCode+TotalMaizeLand +YoungDepRatio+TLU,
data = RwandaData, method = "optimal", ratio=1)
#Coarsened Exact Matching
install.packages("cem")
library(cem)
m.out5 = matchit(Use ~ FarmingImportanceCode + TotalHHMembers + NumberBtw14To65 + RespSexCode +RespAge+RespEducCode+TotalMaizeLand +YoungDepRatio+TLU,
data = RwandaData, method = "cem")
summary(m.out5)
plot(m.out5, type = "hist")
m.out6 = matchit(Use ~ FarmingImportanceCode + TotalHHMembers + NumberBtw14To65 + RespSexCode +RespAge+RespEducCode+TotalMaizeLand +YoungDepRatio+TLU,
data = RwandaData, method = "genetic")
plot(m.out6, type = "hist")
m.out7 = matchit(Use ~ FarmingImportanceCode + TotalHHMembers + NumberBtw14To65 + RespSexCode +RespAge+RespEducCode+TotalMaizeLand +YoungDepRatio+TLU,
data = RwandaData, method = "nearest",
distance ="logit")
plot(m.out7, type = "hist")
m.out8 = matchit(Use ~ FarmingImportanceCode + TotalHHMembers + NumberBtw14To65 + RespSexCode +RespAge+RespEducCode+TotalMaizeLand +YoungDepRatio,
data = RwandaData, method = "nearest", caliper=0.16)
plot(m.out8,type = "hist")
summary(m.out8)
m.data1 <- match.data(m.out)
write.csv(m.data1, file = ("C:/users/machariam/Desktop/Reduced.csv"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment