Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. foreach m in leased_up_opp forecast_kravg30_p25 received_cmto_services working black college_plus {
  2.  
  3. use "C:\Users\Anna\Downloads\cmto_seattle.dta",clear
  4.  
  5. *Calculate difference in means (with unequal variances)
  6. reg `m' treatment_group, hc2
  7.  
  8. *Store the difference in means as a local variable
  9. local b0 = _b[t]
  10.  
  11. *Start a file that will store the estimates
  12. capture postclose rtpf
  13. postfile rtpf s diff using "C:\Users\Anna\Downloads\rtest.dta" , replace
  14.  
  15. *Set the seed because we will be generating random variables below
  16. *This makes the analysis re-producible
  17. set seed 2110
  18.  
  19. *Drop variables that will be used in the loop in case they are already defined
  20. cap drop ts
  21. cap drop ys
  22.  
  23. *Calculate fraction of sample that was treated
  24. sum treatment_group
  25. local fractiontreated = r(mean)
  26.  
  27. *Perform 250,000 draws from the assignment vector, storing the difference in means in each run
  28. forv s = 1/100 {
  29.  
  30. *Generate placebo assignment vector based on a Unif r.v., preserving the probability of treatment
  31. gen ts = uniform() <= `fractiontreated'
  32.  
  33. *Generate outcome variable for the tests below
  34. *Here assuming simple Fisher null of 0 effects
  35. gen ys = `m'
  36.  
  37. *Calculate difference in means of ys across placebo treatment group and placebo control group
  38. qui reg ys ts, hc2
  39.  
  40. *Save the iteration number and difference in means
  41. post rtpf ( `s' ) ( _b[ts] )
  42.  
  43. *Drop variables for next iteration of the loop
  44. drop ts ys
  45. }
  46.  
  47. *Close and save the "rtest.dta" file
  48. postclose rtpf
  49.  
  50. *Load in "rtest.dta"
  51. use "C:\Users\Anna\Downloads\rtest.dta", clear
  52.  
  53. *Calculate p-value
  54. count if abs( diff) >= abs( `b0')
  55. loc p = (r(N)+1) / (250000+1)
  56. di "rand. test p-val: " `p'
  57.  
  58. *Generate cumulative CDF
  59. gen absdiff = abs(diff)
  60. cumul absdiff, gen(cdf)
  61.  
  62. *Plot empirical CDF
  63. twoway scatter cdf absdiff , msize(vsmall) xline(0.18) graphregion(fcolor(white)) title(`m')
  64.  
  65. graph export "C:\Users\Anna\Downloads\ecdf_`m'.png"
  66.  
  67. }
  68.  
  69.  
  70. *Set the seed because we will be generating random variables below
  71. *This makes the analysis re-producible
  72. set seed 2110
  73. set more off
  74.  
  75.  
  76. use "C:\Users\Anna\Downloads\cmto_seattle.dta",clear
  77.  
  78.  
  79. *** Question 6
  80.  
  81. * i is the difference between the control
  82. foreach m in leased_up_opp forecast_kravg30_p25 received_cmto_services working black college_plus {
  83. *Start a file that will store the estimates
  84. capture postclose rtpf
  85. postfile rtpf s i diff pvalue using "C:\Users\Anna\Downloads\rtest_`m'.dta" , replace
  86.  
  87. *Calculate difference in means (with unequal variances)
  88. reg `m' treatment_group, hc2
  89.  
  90. *Store the difference in means as a local variable
  91. * b[t] is the treatment effect
  92. local b0 = _b[t]
  93.  
  94. forvalue i= -.2(.01)0.2 {
  95.  
  96. *Drop variables that will be used in the loop in case they are already defined
  97. cap drop ts
  98. cap drop ys
  99.  
  100. *Calculate fraction of sample that was treated
  101. sum treatment_group
  102. local fractiontreated = r(mean)
  103.  
  104. *Perform 250,000 draws from the assignment vector, storing the difference in means in each run
  105. forv s = 1/3 {
  106.  
  107. *Generate placebo assignment vector based on a Unif r.v., preserving the probability of treatment
  108.  
  109. * ts is our random assignment (what is the probability that this happened by chance)
  110. *treatment_group is what is actually assigned (according to data)
  111.  
  112. gen ts = uniform() <= `fractiontreated'
  113.  
  114. *generate the outcome variable!
  115. g ys =.
  116.  
  117. *Generate outcome variable for the tests below
  118. *Here assuming treatment effects
  119.  
  120. replace ys= `m' - `i' if ts!=treatment_group & treatment_group==1
  121. replace ys= `m' + `i' if ts!=treatment_group & treatment_group==0
  122. *Here assuming simple Fisher null of 0 effects
  123. replace ys= `m' if ts==treatment_group
  124.  
  125. *Calculate difference in means of ys across placebo treatment group and placebo control group
  126. qui reg ys ts, hc2
  127.  
  128. *Save the iteration number and difference in means
  129. *post rtpf ( `s' ) (`i') ( _b[ts] )
  130.  
  131. count if (abs( _b[ts] ) - `i') >= abs( `b0')
  132. loc p = (r(N)+1) / (100+1)
  133. *local p = (abs( _b[ts] ) - `i') >= abs( `b0')
  134. di "rand. test p-val: " `p'
  135. post rtpf ( `s' ) (`i') ( _b[ts] ) (`p')
  136.  
  137. *Drop variables for next iteration of the loop
  138. drop ts ys
  139. }
  140. }
  141. *Close and save the "rtest.dta" file
  142. postclose rtpf
  143.  
  144. }
  145.  
  146.  
  147.  
  148.  
  149. *Calculate p-value
  150. * b0 is difference/ diff
  151. *i is the treatment effect
  152. g test = (abs( diff) - i >= abs( `b0') - i)
  153.  
  154. *sum by each treatment
  155.  
  156. count if abs( diff) >= abs( `b0')
  157. loc p = (r(N)+1) / (100+1)
  158. di "rand. test p-val: " `p'
  159.  
  160. *Generate cumulative CDF
  161. gen absdiff = abs(diff)
  162. cumul absdiff, gen(cdf)
  163.  
  164. *generating a dummy variable and assigning it to p-values that are
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement