Skip to content

Instantly share code, notes, and snippets.


Benjamin Daniels bbdaniels

View GitHub Profile
bbdaniels /
Created March 23, 2023 19:02
Debiasing regression with controls
// Part 1: De-biasing a parameter estimate using controls
// Develop some data generating process for data X’s and for outcome Y, with some (potentially multi-armed) treatment variable and treatment effect. Like last week, you should strongly consider "simulating" data along the lines of your group project.
// This DGP should include strata groups and continuous covariates, as well as random noise. Make sure that the strata groups affect the outcome Y and are of different sizes, and make the probability that an individual unit receives treatment vary across strata groups. You will want to create the strata groups first, then use a command like expand or merge to add them to an individual-level data set.
cap prog drop runregs
prog def runregs, rclass
syntax anything // sample size goes here (divided by ten)
bbdaniels / data.csv
Created March 1, 2023 15:58
Make map images reproducibly using google maps API in R
View data.csv
id latitude longitude type
1 38.922893 -77.06741979 T
2 38.92846854 -77.03038385 T
3 38.90462752 -77.0336025 C
4 38.90776671 -77.00978448 C
5 38.87686969 -77.01313188 T
bbdaniels /
Created January 13, 2023 17:23
Rapid import and infill of semi-structured data
global data "${wd}/ppol_528_GuzmanLidia/data/Data_exp"
local data : dir "${data}/xlsx/" files *
tempfile allData
save `allData' , emptyok
cap prog drop infill
prog def infill
syntax anything
qui foreach var of varlist `anything' {
bbdaniels /
Created December 20, 2022 16:05
Graph combine with tempfiles
local graphs ""
local x = 1
foreach var of varlist ///
diarrhea_history_duration tb_history_sputum ///
malaria_history_fevertype pph_history_pph ///
diabetes_history_numblimb {
local title : var lab `var'
local graphs `"`graphs' "\``var''" "'
tempfile `var'
bbdaniels /
Created October 26, 2022 19:55
Percent signs in graph bar in stata
local nb=`.Graph.plotregion1.barlabels.arrnels'
forval i=1/`nb' {
di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
bbdaniels / latex-pdf.tex
Created May 24, 2021 17:28
Add arbitrary PDF in LaTeX
View latex-pdf.tex
\usepackage{pdfpages} % Allow PDF insert for non-TeX elements
\includepdf[noautoscale=true, pages=1]{pdf/15633-Analytics_coverFINAL_print.pdf}
\includepdf[noautoscale=true, pages=2]{pdf/15633-Analytics_coverFINAL_print.pdf}
bbdaniels /
Created March 26, 2021 18:20
Compare discrete distributions using bar graphs in Stata
sysuse auto.dta, clear
local style "s(0) w(1) lc(none) discrete frac"
tw ///
(histogram rep78 if foreign == 1 ///
, fc(black%50) barwidth(0.9) `style') ///
(histogram rep78 if foreign == 0 ///
, fc(red%50) barwidth(0.8) `style')
bbdaniels /
Last active February 25, 2021 15:09
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
bbdaniels /
Created February 4, 2021 20:58
Arbitrary Monte Carlo dataset with -post- in Stata
sysuse auto.dta , clear
levelsof foreign, local(levels)
tempfile posty
postfile posty str10(a) b c d using `posty' , replace
foreach level in `levels' {
local l : label (foreign) `level'
post posty ("`l'") (2) (3) (4)
bbdaniels /
Last active February 22, 2021 16:22
Asking for help with statistical programming

Asking for help with errors in statistical programming

No matter how experienced you are in statistical programming, you are certain to encounter errors in your code from time to time. You will recognize some of them and be able to solve them quickly, but you will never stop encountering unexpected new errors that you need help to resolve. Asking for help with statistical programming is hard. You need to give enough information to the person helping you so that they can produce the exact same problem you have