Skip to content

Instantly share code, notes, and snippets.

@vikjam
Last active March 24, 2016 01:35
Show Gist options
  • Save vikjam/16746aec002e044ca0a9 to your computer and use it in GitHub Desktop.
Save vikjam/16746aec002e044ca0a9 to your computer and use it in GitHub Desktop.
Simulating an RD.
clear all
set obs 10000
capture program drop detect_rd
program define detect_rd, rclass
syntax varlist(numeric max=1) [if]
if "`if'" == "" {
local ifcond ""
}
else {
local ifcond "& `if'"
}
unab allbins : bin_*
lassoShooting `varlist' bin_* `if', lambda(1000)
local selected_bins = "`r(selected)'"
local cutpoints ""
foreach bin of varlist `selected_bins' {
quietly summ running if `bin' == 1 `ifcond'
local cutpoints "`cutpoints' `r(min)'"
}
binscatter `varlist' running, xq(bins) rd(`cutpoints')
local unused : list allbins - selected_bins
local pct_unused = 100 * (wordcount("`unused'") / wordcount("`allbins'"))
return local selected_bins = "`selected_bins'"
return scalar pct_unused = `pct_unused'
end
gen running = rnormal(0, 10)
summ running
local min = floor(`r(min)')
local max = ceil(`r(max)')
egen decision_rule = cut(running), at(`min', -12, -7, 0, 7, 12, `max')
gen y_rd = 10 * decision_rule + rnormal(0, 10)
gen y_mistake_1 = 10 * running + rnormal(0, 10)
gen y_mistake_2 = 0.25 * running + 0.25 * running^2 + rnormal(0, 10)
gen y_mistake_3 = 0.05 * running + 0.005 * running^2 + 0.005 * running^3 + rnormal(0, 10)
xtile bins = running, nq(60)
forvalues bin = 1/60 {
gen bin_`bin' = (bins > `bin') if !missing(bins)
}
detect_rd y_rd
display _n "Percent unused: `r(pct_unused)'"
graph export "y_rd.png", replace
detect_rd y_mistake_1
display _n "Percent unused: `r(pct_unused)'"
graph export "y_mistake_1.png", replace
detect_rd y_mistake_2
display _n "Percent unused: `r(pct_unused)'"
graph export "y_mistake_2.png", replace
detect_rd y_mistake_3
display _n "Percent unused: `r(pct_unused)'"
graph export "y_mistake_3.png", replace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment