Skip to content

Instantly share code, notes, and snippets.

@bbdaniels
Last active February 25, 2021 15:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbdaniels/6353d31a4df46be87db8cde53d431c65 to your computer and use it in GitHub Desktop.
Save bbdaniels/6353d31a4df46be87db8cde53d431c65 to your computer and use it in GitHub Desktop.
Using programs in Stata
// Regression margins visualizer
cap prog drop regmargins
prog def regmargins
// Syntax command
syntax anything
// Expected syntax is like regress
// Second entry should be factor variable (i.) and we will visualize levels
// Run regression
qui reg `anything'
// Calculate margins
local categorical : word 2 of `anything'
local categorical = substr("`categorical'",3,.)
// Some setup
qui levelsof `categorical' , local(levels)
local x = 0
foreach level in `levels' {
local ++x
local plotopts = "`plotopts'" ///
+ " plot`x'opts(lc(none))"
}
qui margins `categorical'
// Graph margins
marginsplot ///
, recast(bar) ///
plotopts(barwidth(0.8)) ///
by(`categorical') ///
plot(`categorical') ///
`plotopts' ///
recastci(rspike) ///
ciopts(lc(black)) ///
legend(off) xtit(" ") xlab(none) ///
yscale(r(0)) ylab(#6) ytit(" ")
// End
end
// End of dofile
// Example do-file for programming session
// Define the program
cap prog drop mysample
prog def mysample
// [ritest] requirements
syntax , ///
[resampvar(string asis)] ///
[run(string asis)]
qui {
randtreat ///
, gen(treatment) replace ///
strata(strata) unequal(1/3 2/3) ///
misfits(wglobal)
} // end qui
end
// A basic stratified approach
clear
set obs 10
gen strata = _n
expand 6
gen uid = _n
// Use the program to generate "true" sample
mysample
// Generate true outcome DGP with positive treatment effect
gen y = treatment*runiform() + strata + rnormal()
// Use [ritest] to get null distribution
ritest treatment _b[treatment] ///
, reps(100) samplingprogram(mysample) kdensityplot ///
: reg y treatment i.strata
// End of do-file
// Example do-file for programming session
// Define the program
cap prog drop normaldistro
prog def normaldistro , rclass // Allow returning values to memory
syntax
qui {
clear
set obs 100
gen x1 = rnormal() // Arbitrary covariate
gen rand = rnormal() // 50-50 treatment
egen rank = rank(rand)
gen treatment = rank <= 50
// DGP
gen y = x1 + treatment*runiform() // Heterogeneous, positive effect
reg y treatment
mat a = r(table)
return scalar beta = a[1,1]
return scalar pval = a[4,1]
} // end qui
end
// Use the program
tempfile sims
simulate beta=r(beta) pval=r(pval) ///
, reps(1000) seed(725485) saving(`sims') ///
: normaldistro
use `sims' , clear
local style "start(-0.5) barwidth(0.09) width(.1) fc(gray) freq"
tw ///
(histogram beta , `style' lc(none) ) ///
(histogram beta if pval < 0.05 , `style' lc(black) fc(none) ) ///
, xtit("") legend(on ring(0) pos(1) order(2 "p < 0.05") region(lc(none)))
// End of do-file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment