Skip to content

Instantly share code, notes, and snippets.

Avatar

Benjamin Daniels bbdaniels

View GitHub Profile
@bbdaniels
bbdaniels / quick-binomial-power.do
Created May 23, 2023 19:42
Quick binomial power graph
View quick-binomial-power.do
power oneproportion 0.62 0.5 , n(50(1)150) graph(recast(line) plotopts(lw(thick) lc(red)) xline(100 131,lc(black)) ylab(0 "0%" 0.5 "50%" 0.8 "80%" 0.69 "69%" 1 "100%") yline(.69 .8,lc(black)) note("") title("") subtitle("") )
@bbdaniels
bbdaniels / bias-controls.co
Created March 23, 2023 19:02
Debiasing regression with controls
View bias-controls.co
// 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
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
bbdaniels / import-infill.do
Created January 13, 2023 17:23
Rapid import and infill of semi-structured data
View import-infill.do
global data "${wd}/ppol_528_GuzmanLidia/data/Data_exp"
local data : dir "${data}/xlsx/" files *
clear
tempfile allData
save `allData' , emptyok
cap prog drop infill
prog def infill
syntax anything
qui foreach var of varlist `anything' {
@bbdaniels
bbdaniels / graph-combine.do
Created December 20, 2022 16:05
Graph combine with tempfiles
View graph-combine.do
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
bbdaniels / percent-graph-bar.do
Created October 26, 2022 19:55
Percent signs in graph bar in stata
View percent-graph-bar.do
local nb=`.Graph.plotregion1.barlabels.arrnels'
forval i=1/`nb' {
di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
.Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
}
.Graph.drawgraph
@bbdaniels
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
bbdaniels / bar-compare.do
Created March 26, 2021 18:20
Compare discrete distributions using bar graphs in Stata
View bar-compare.do
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
bbdaniels / regression-margins.do
Last active February 25, 2021 15:09
Using programs in Stata
View regression-margins.do
// 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
bbdaniels / posty.do
Created February 4, 2021 20:58
Arbitrary Monte Carlo dataset with -post- in Stata
View posty.do
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)