Advertisement
Guest User

Lalonde-Matching-Shajara

a guest
Mar 24th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.78 KB | None | 0 0
  1. install.packages("Matching")
  2. library(Matching)
  3.  
  4.  
  5. #QUESTION 1:
  6. #seperates our data based on who recieved treatment
  7. control <- nsw_dw[nsw_dw$treat == 0,]
  8. treatment <- nsw_dw[nsw_dw$treat == 1,]
  9.  
  10.  
  11. #calculates and prints difference of means
  12. diff_of_means <- mean(treatment$re78) - mean(control$re78)
  13. print(diff_of_means)
  14.  
  15.  
  16. linear_reg <- lm(re78 ~ treat, data=nsw_dw)
  17. #will show coefficients
  18. print(linear_reg)
  19. summary(linear_reg)
  20. #confint function can find confidence interval
  21. confint(linear_reg)
  22.  
  23. #Question 2:
  24.  
  25. #calculates and prints difference of means
  26. diff_of_means <- mean(treatment$re78) - mean(cps_controls$re78)
  27. print(diff_of_means)
  28.  
  29. combined_data <- rbind(treatment,cps_controls)
  30. linear_reg2 <- lm(re78 ~ treat, data=combined_data)
  31. #will show coefficients
  32. print(linear_reg2)
  33. summary(linear_reg2)
  34. #confint function can find confidence interval
  35. confint(linear_reg2)
  36.  
  37. #QUESTION 3
  38. match_comb<-glm(treat ~ age + education + black + hispanic + married + nodegree+
  39.                   re74 + re75, family=binomial(logit), data=combined_data)
  40.  
  41. D<-combined_data$treat #treatment
  42. Y<-combined_data$re78 #outcome
  43. X  <- match_comb$fitted
  44.  
  45. #propensity score matching estimator
  46. r1  <- Match(Y=Y, Tr=D, X=X, M=1)
  47. summary(r1)
  48.  
  49. #check for balance before and after matching
  50. mb1  <- MatchBalance(treat ~ age + education + black + hispanic + married + nodegree+
  51.                        re74 + re75, match.out=r1,
  52.                      data=combined_data, nboots = 20)
  53.  
  54. #QUESTION 4
  55.  
  56. full_final_data <- cbind(combined_data, match_comb$fitted.values)
  57.  
  58. names(full_final_data) <- c("data_id", "treat", "age", "education","black",
  59.                             "hispanic", "married", "nodegree", "re74", "re75",
  60.                             "re78", "fitted_values")
  61.  
  62. D2<-full_final_data$treat #treatment
  63. Y2<-full_final_data$re78 #outcome
  64. X2 <- cbind(full_final_data$age, full_final_data$education, full_final_data$black,
  65.                           full_final_data$hispanic, full_final_data$married, full_final_data$nodegree,
  66.                           full_final_data$re74, full_final_data$re75, full_final_data$fitted_values)
  67.  
  68. #propensity score matching estimator
  69. r2  <- Match(Y=Y2, Tr=D2, X=X2, M=1, sample = TRUE)
  70. summary(r2)
  71.  
  72. #check for balance before and after matching
  73. mb1  <- MatchBalance(treat ~ age + education + black + hispanic + married + nodegree+
  74.                        re74 + re75, match.out=r1,
  75.                      data=full_final_data, nboots = 20)
  76.  
  77. #QUESTION 5 Part 1 (Almost there!)
  78. install.packages("rbounds")
  79. install.packages("rgenoud")
  80. library(rbounds)
  81. library("rgenoud")
  82.  
  83. X3 = cbind(full_final_data$age, full_final_data$education, full_final_data$black,
  84.           full_final_data$hispanic, full_final_data$married, full_final_data$nodegree,
  85.           full_final_data$re74, full_final_data$re75)
  86.  
  87. BalanceMat <- cbind(full_final_data$age, full_final_data$education, full_final_data$black,
  88.                     full_final_data$hispanic, full_final_data$married, full_final_data$nodegree,
  89.                     full_final_data$re74, full_final_data$re75)
  90.  
  91. genout <- GenMatch(Tr= full_final_data$treat, X=X3, BalanceMatrix=BalanceMat, estimand="ATT", M=1,
  92.                    pop.size=16, max.generations=10, wait.generations=1)
  93.  
  94. Y3=full_final_data$re78
  95.  
  96. mout <- Match(Y=Y3, Tr=full_final_data$treat, X=X3, estimand="ATT", Weight.matrix=genout)
  97. summary(mout)
  98.  
  99. mb <- MatchBalance(full_final_data$treat~full_final_data$age +full_final_data$educ+full_final_data$black+
  100.                      full_final_data$hisp+ full_final_data$married+ full_final_data$nodegree+ full_final_data$re74+ full_final_data$re75,
  101.                    match.out=mout, nboots=20)
  102.                    
  103. mean()
  104. #QUESTION 5 Part 2 (Woohoo!)
  105.  
  106. X4 = cbind(full_final_data$age, full_final_data$education, full_final_data$black,
  107.           full_final_data$hispanic, full_final_data$married, full_final_data$nodegree,
  108.           full_final_data$re75, full_final_data$re74, full_final_data$propensities)
  109.  
  110. BalanceMat <- cbind(full_final_data$age, full_final_data$education,
  111.                     full_final_data$black, full_final_data$hispanic,
  112.                     full_final_data$married, full_final_data$nodegree,
  113.                     full_final_data$re75, full_final_data$re74,
  114.                     full_final_data$propensities)
  115.  
  116. genout <- GenMatch(Tr=full_final_data$treat, X=X, BalanceMatrix=BalanceMat, estimand="ATT", M=1)
  117.  
  118. Y4 = full_final_data$re78
  119.  
  120. mout <- Match(Y=Y, Tr=treat, X=X, estimand="ATT", Weight.matrix=genout)
  121. summary(mout)
  122.  
  123. mb <- MatchBalance(full_final_data$treat~full_final_data$age + full_final_data$education +
  124.                      full_final_data$black + full_final_data$hispanic + full_final_data$married
  125.                    + full_final_data$nodegree,
  126.                    match.out=mout, nboots=20)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement