Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- install.packages("Matching")
- library(Matching)
- #QUESTION 1:
- #seperates our data based on who recieved treatment
- control <- nsw_dw[nsw_dw$treat == 0,]
- treatment <- nsw_dw[nsw_dw$treat == 1,]
- #calculates and prints difference of means
- diff_of_means <- mean(treatment$re78) - mean(control$re78)
- print(diff_of_means)
- linear_reg <- lm(re78 ~ treat, data=nsw_dw)
- #will show coefficients
- print(linear_reg)
- summary(linear_reg)
- #confint function can find confidence interval
- confint(linear_reg)
- #Question 2:
- #calculates and prints difference of means
- diff_of_means <- mean(treatment$re78) - mean(cps_controls$re78)
- print(diff_of_means)
- combined_data <- rbind(treatment,cps_controls)
- linear_reg2 <- lm(re78 ~ treat, data=combined_data)
- #will show coefficients
- print(linear_reg2)
- summary(linear_reg2)
- #confint function can find confidence interval
- confint(linear_reg2)
- #QUESTION 3
- match_comb<-glm(treat ~ age + education + black + hispanic + married + nodegree+
- re74 + re75, family=binomial(logit), data=combined_data)
- D<-combined_data$treat #treatment
- Y<-combined_data$re78 #outcome
- X <- match_comb$fitted
- #propensity score matching estimator
- r1 <- Match(Y=Y, Tr=D, X=X, M=1)
- summary(r1)
- #check for balance before and after matching
- mb1 <- MatchBalance(treat ~ age + education + black + hispanic + married + nodegree+
- re74 + re75, match.out=r1,
- data=combined_data, nboots = 20)
- #QUESTION 4
- full_final_data <- cbind(combined_data, match_comb$fitted.values)
- names(full_final_data) <- c("data_id", "treat", "age", "education","black",
- "hispanic", "married", "nodegree", "re74", "re75",
- "re78", "fitted_values")
- D2<-full_final_data$treat #treatment
- Y2<-full_final_data$re78 #outcome
- X2 <- cbind(full_final_data$age, full_final_data$education, full_final_data$black,
- full_final_data$hispanic, full_final_data$married, full_final_data$nodegree,
- full_final_data$re74, full_final_data$re75, full_final_data$fitted_values)
- #propensity score matching estimator
- r2 <- Match(Y=Y2, Tr=D2, X=X2, M=1, sample = TRUE)
- summary(r2)
- #check for balance before and after matching
- mb1 <- MatchBalance(treat ~ age + education + black + hispanic + married + nodegree+
- re74 + re75, match.out=r1,
- data=full_final_data, nboots = 20)
- #QUESTION 5 Part 1 (Almost there!)
- install.packages("rbounds")
- install.packages("rgenoud")
- library(rbounds)
- library("rgenoud")
- X3 = cbind(full_final_data$age, full_final_data$education, full_final_data$black,
- full_final_data$hispanic, full_final_data$married, full_final_data$nodegree,
- full_final_data$re74, full_final_data$re75)
- BalanceMat <- cbind(full_final_data$age, full_final_data$education, full_final_data$black,
- full_final_data$hispanic, full_final_data$married, full_final_data$nodegree,
- full_final_data$re74, full_final_data$re75)
- genout <- GenMatch(Tr= full_final_data$treat, X=X3, BalanceMatrix=BalanceMat, estimand="ATT", M=1,
- pop.size=16, max.generations=10, wait.generations=1)
- Y3=full_final_data$re78
- mout <- Match(Y=Y3, Tr=full_final_data$treat, X=X3, estimand="ATT", Weight.matrix=genout)
- summary(mout)
- mb <- MatchBalance(full_final_data$treat~full_final_data$age +full_final_data$educ+full_final_data$black+
- full_final_data$hisp+ full_final_data$married+ full_final_data$nodegree+ full_final_data$re74+ full_final_data$re75,
- match.out=mout, nboots=20)
- mean()
- #QUESTION 5 Part 2 (Woohoo!)
- X4 = cbind(full_final_data$age, full_final_data$education, full_final_data$black,
- full_final_data$hispanic, full_final_data$married, full_final_data$nodegree,
- full_final_data$re75, full_final_data$re74, full_final_data$propensities)
- BalanceMat <- cbind(full_final_data$age, full_final_data$education,
- full_final_data$black, full_final_data$hispanic,
- full_final_data$married, full_final_data$nodegree,
- full_final_data$re75, full_final_data$re74,
- full_final_data$propensities)
- genout <- GenMatch(Tr=full_final_data$treat, X=X, BalanceMatrix=BalanceMat, estimand="ATT", M=1)
- Y4 = full_final_data$re78
- mout <- Match(Y=Y, Tr=treat, X=X, estimand="ATT", Weight.matrix=genout)
- summary(mout)
- mb <- MatchBalance(full_final_data$treat~full_final_data$age + full_final_data$education +
- full_final_data$black + full_final_data$hispanic + full_final_data$married
- + full_final_data$nodegree,
- match.out=mout, nboots=20)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement