iegraph : pause and testing grey

iegraph : pause and testing grey

	*Set standardized settings
	ieboilstart, v(11.1)
	`r(version)'

	*Create filepaths
	global ietoolkit "C:\Users\wb503680\Box Sync\DIME WOrk\GitFolder\ietoolkit"
	*global ietoolkit "C:\Users\wb462869\Box Sync\Stata\Stata work\Commands\ietoolkit"
	global testOutput "$ietoolkit/test/ieimpgraphOutput"

	*Set cd folder to catch and cd leakage
	cd "$ietoolkit\test\cdjunk"

	*Load the command
	do "$ietoolkit\iegraph.do"

	pause on

	**********************
	*Graph 1 - single treatment

	*open an example data set and create a mock dummy
	sysuse auto, clear
	gen Treatment = (weight >= 3019.5)

	*Run the impact regression
	reg price  Treatment
	*Create the graph
	iegraph Treatment, noconfbars title("Treatment effect on price")  save("$testOutput/Graph1.gph") yzero grey
	pause
	iegraph Treatment, title("Treatment effect on price") save("$testOutput/Graph1.gph") yzero grey
	pause

	**********************
	*Graph 2 - multiple treatment

	*open an example data set and create mutually exclusive mock dummies
	sysuse auto, clear
	tab rep78, gen(d_)

	*Run the impact regression
	reg price  d_2 d_3 d_4 d_5 weight length gear_ratio
	*Create the graph
	iegraph d_2 d_3,  title("Treatment effect on price") save("$testOutput/Graph2.gph") yzero grey
	pause
	iegraph d_2 d_3 d_4,  title("Treatment effect on price") save("$testOutput/Graph3.gph") yzero grey
	pause
	iegraph d_2 d_3 d_4 d_5,  title("Treatment effect on price") save("$testOutput/Graph4.gph") yzero note("Stata Command iegraph. copyright KBjark") grey
	pause
	iegraph d_2 d_3 d_4 d_5,   title("Treatment effect on price") save("$testOutput/Graph2.gph") yzero grey
	pause

	*Set standardized settings
	ieboilstart, v(11.1)
	`r(version)'

	*Create filepaths
	global ietoolkit "C:\Users\wb503680\Box Sync\DIME WOrk\GitFolder\ietoolkit"
	*global ietoolkit "C:\Users\wb462869\Box Sync\Stata\Stata work\Commands\ietoolkit"
	global testOutput "$ietoolkit/test/ieimpgraphOutput"

	*Set cd folder to catch and cd leakage
	cd "$ietoolkit\test\cdjunk"

	*Load the command
	do "$ietoolkit\iegraph.do"

	**********************
	*Graph 1 - single treatment

	*open an example data set and create a mock dummy
	sysuse auto, clear
	gen Treatment = (weight >= 3019.5)

	*Run the impact regression
	reg price  Treatment
	*Create the graph
	iegraph Treatment, noconfbars title("Treatment effect on price") save("$testOutput/Graph1.gph") yzero
	iegraph Treatment, title("Treatment effect on price") save("$testOutput/Graph1.gph") yzero

	**********************
	*Graph 2 - multiple treatment

	*open an example data set and create mutually exclusive mock dummies
	sysuse auto, clear
	tab rep78, gen(d_)

	*Run the impact regression
	reg price  d_2 d_3 d_4 d_5 weight length gear_ratio
	*Create the graph
	iegraph d_2 d_3,  title("Treatment effect on price") save("$testOutput/Graph2.gph") yzero
	iegraph d_2 d_3 d_4,  title("Treatment effect on price") save("$testOutput/Graph3.gph") yzero
	iegraph d_2 d_3 d_4 d_5,  title("Treatment effect on price") save("$testOutput/Graph4.gph") yzero note(Stata Command iegraph. copyright KBjark)
	iegraph d_2 d_3 d_4 d_5,  noconfbars title("Treatment effect on price") save("$testOutput/Graph2.gph") yzero


Star option added

Star option added

The graph now shows stars and observation count.

cap	program drop	ieimpgraph
	program define 	ieimpgraph
*	preserve
	syntax varlist

	mat beta_ = e(b)

	local counter = 0
	local textfile = "Mrijan"
	local date = "$S_DATE"

	qui{

	foreach var of local varlist{
		cap assert inlist(`var',0,1) | missing(`var')
			if _rc {
				noi display as error "{phang} The variable `var' is not a dummy. Treatment variable needs to be a dummy(0 or 1) variable. {p_end}"
				noi display ""
				error 149
				}
			else {
				}

		local counter = `counter' + 1
		di `counter'

		scalar coeff_`var' = _b[`var']
		noi display coeff_`var'

		scalar coeff_se_`var' = _se[`var']
		noi display coeff_se_`var'

		count if e(sample) == 1 & `var' == 1 //Count one tmt group at the time
		scalar obs_`var' = r(N)
		noi display obs_`var'

	}

	local count: word count `varlist'
	local graphCount = `count' + 1
	//tokenize "`varlist'"
	//noi di `count'

	//Make all vars tempvars (maybe do later)
	//Make sure that missing is properly handled
	tempvar anyTMT
	egen `anyTMT' = rowmax(`varlist') if e(sample) == 1
	tempvar control
	gen `control' = (`anyTMT' == 0) if !missing(`anyTMT')

	noi sum `e(depvar)' if e(sample) == 1 & `control' == 1
	scalar ctl_N		= r(N)
	scalar ctl_mean	  	= r(mean)
	scalar ctl_mean_sd 	= r(sd)

		foreach var of local varlist {
			scalar  conf_int_min_`var'   =	coeff_`var'-(1.96*coeff_se_`var') + ctl_mean
			scalar  conf_int_max_`var'   =	coeff_`var'+(1.96*coeff_se_`var') + ctl_mean

			test `var'
			local star_`var' " "

			scalar  pvalue =r(p)
			if pvalue < 0.10 {
				local star_`var' "*"
			}
			if pvalue < 0.05 {
				local star_`var' "**"
			}
			if pvalue < 0.01 {
				local star_`var' "***"
			}
			noi display "`star_`var''"
			scalar tmt_mean_`var' = ctl_mean + coeff_`var'
		}

	tempname 	newHandle
	tempfile	newTextFile
	cap file close 	`newHandle'
	file open  	`newHandle' using "`newTextFile'", text write append
	file write `newHandle' ///
		"order" 	_tab "xLabeled"	_tab "mean" _tab "coeff" _tab "conf_int_min" _tab "conf_int_max" _tab "obs" _tab "star" _n 	///
		%9.3f (1) _tab "Control"  _tab %9.3f (ctl_mean)      _tab  		 			_tab 		 			  	  _tab 			 				_tab %9.3f (ctl_N) 	   _tab			  	  _n
	file close `newHandle'
	tempvar newCounter
	gen `newCounter' = 2
	foreach var in `varlist' {
		file open  	`newHandle' using "`newTextFile'", text write append
		file write `newHandle' ///
		%9.3f (`newCounter') _tab "`var'" _tab %9.3f (tmt_mean_`var') _tab  %9.3f (coeff_`var')  _tab %9.3f (conf_int_min_`var') _tab %9.3f (conf_int_max_`var') _tab %9.3f (obs_`var')   _tab "`star_`var''"  _n
		file close `newHandle'
		replace `newCounter' = `newCounter' + 1
		}

	copy "`newTextFile'"  "mainMasterDofile.txt" , replace

	insheet using mainMasterDofile.txt, clear

	if order == 1 local obsControl = obs
	local starOption `" xlabel(1 "(N=`obsControl')" "'
	*noi di "`starOption'" /

	forval x = 2/`graphCount' {
			noi di "Hello"
			list obs if order == `x'
			local obsTreat = obs[`x']
			local starTreat = star[`x']
			local starOption `" `starOption' `x' "(N= `obsTreat')  `starTreat'" "'
			noi di "Hello2"
		}

	local starOption `" `starOption' , noticks labsize(medsmall) ) "'
	/* noi di "Mrijan rimal"
	noi di `" "`starOption'"  "'
	noi di "Mrijan rimal" */

	local graphname gph_new
	local colourOption ""

	forval colourLoop = 1/`graphCount' {

		local colour

		noi di 0

		local colourNum = mod(`colourLoop', 5)

		if `colourNum' == 1 local colour = "215 25 28"
		if `colourNum' == 2 local colour = "253 174 93"
		if `colourNum' == 3 local colour = "255 255 191"
		if `colourNum' == 4 local colour = "171 217 233"
		if `colourNum' == 0 local colour = "43 123 182"

		noi di 1

		local colourOption `"`colourOption' (bar mean order if order == `colourLoop', color("`colour'")) "'

		noi di 2

		noi di `"`colourOption'"'

	}

	noi di 3

	local confIntGraph = "(rcap conf_int_max conf_int_min order, lc(gs)) (scatter mean order, msym(none)  mlabs(medium) mlabpos(10) mlabcolor(black)), "

	local orderOption = "legend(order("

	forval y = 1(1)`graphCount'{
		local orderOption  "`orderOption' `y'"
	}

	local orderOption "`orderOption' ))"

	noi di 4

	noi di `" graph twoway `colourOption' `confIntGraph' `orderOption' "'

	graph twoway `colourOption' `confIntGraph' `orderOption' `starOption' saving("newfile.gph", replace)

	noi di 5

	graph export graphname.png, replace
}
*restore
end

cap	program drop	ieimpgraph
	program define 	ieimpgraph
*	preserve
	syntax varlist

	mat beta_ = e(b)

	local counter = 0
	local textfile = "Mrijan"
	local date = "$S_DATE"

	qui{

	foreach var of local varlist{
		cap assert inlist(`var',0,1) | missing(`var')
			if _rc {
				noi display as error "{phang} The variable `var' is not a dummy. Treatment variable needs to be a dummy(0 or 1) variable. {p_end}"
				noi display ""
				error 149
				}
			else {
				}

		local counter = `counter' + 1
		di `counter'

		scalar coeff_`var' = _b[`var']
		noi display coeff_`var'

		scalar coeff_se_`var' = _se[`var']
		noi display coeff_se_`var'

		count if e(sample) == 1 & `var' == 1 //Count one tmt group at the time
		scalar obs_`var' = r(N)
		noi display obs_`var'

	}

	local count: word count `varlist'
	local graphCount = `count' + 1
	//tokenize "`varlist'"
	//noi di `count'

	//Make all vars tempvars (maybe do later)
	//Make sure that missing is properly handled
	tempvar anyTMT
	egen `anyTMT' = rowmax(`varlist') if e(sample) == 1
	tempvar control
	gen `control' = (`anyTMT' == 0) if !missing(`anyTMT')

	noi sum `e(depvar)' if e(sample) == 1 & `control' == 1
	scalar ctl_N		= r(N)
	scalar ctl_mean	  	= r(mean)
	scalar ctl_mean_sd 	= r(sd)

		foreach var of local varlist {
			scalar  conf_int_min_`var'   =	coeff_`var'-(1.96*coeff_se_`var') + ctl_mean
			scalar  conf_int_max_`var'   =	coeff_`var'+(1.96*coeff_se_`var') + ctl_mean

			test `var'
			local star_`var'

			scalar  pvalue =r(p)
			if pvalue < 0.10 {
				local star_`var' "*"
			}
			if pvalue < 0.05 {
				local star_`var' "**"
			}
			if pvalue < 0.01 {
				local star_`var' "***"
			}
			noi display "`star_`var''"
			scalar tmt_mean_`var' = ctl_mean + coeff_`var'
		}

	tempname 	newHandle
	tempfile	newTextFile
	cap file close 	`newHandle'
	file open  	`newHandle' using "`newTextFile'", text write append
	file write `newHandle' ///
		"order" 	_tab "xLabeled"	_tab "mean" _tab "coeff" _tab "conf_int_min" _tab "conf_int_max" _tab "obs" _tab "star" _n 	///
		%9.3f (1) _tab "Control"  _tab %9.3f (ctl_mean)      _tab  		 			_tab 		 			  	  _tab 			 				_tab %9.3f (ctl_N) 	   _tab			  	  _n
	file close `newHandle'
	tempvar newCounter
	gen `newCounter' = 2
	foreach var in `varlist' {
		file open  	`newHandle' using "`newTextFile'", text write append
		file write `newHandle' ///
		%9.3f (`newCounter') _tab "`var'" _tab %9.3f (tmt_mean_`var') _tab  %9.3f (coeff_`var')  _tab %9.3f (conf_int_min_`var') _tab %9.3f (conf_int_max_`var') _tab %9.3f (obs_`var')   _tab "`star_`var''"  _n
		file close `newHandle'
		replace `newCounter' = `newCounter' + 1
		}

	copy "`newTextFile'"  "mainMasterDofile.txt" , replace

	insheet using mainMasterDofile.txt, clear

	if order == 1 local obsControl = obs
	local starOption `" xlabel(1 (N=`obsControl') "'
	noi di "`starOption'"
	forval x = 2/`graphCount' {

	if order == `x' local obsTreat = obs
	if order == `x' local starTreat = star

	local starOption `" "`starOption'" `x' `obsTreat' "`starTreat'" "'

	}
	noi di "`starOption'"

	local graphname gph_new
	local colourOption ""

	forval colourLoop = 1/`graphCount' {

		local colour

		noi di 0

		local colourNum = mod(`colourLoop', 5)

		if `colourNum' == 1 local colour = "215 25 28"
		if `colourNum' == 2 local colour = "253 174 93"
		if `colourNum' == 3 local colour = "255 255 191"
		if `colourNum' == 4 local colour = "171 217 233"
		if `colourNum' == 0 local colour = "43 123 182"

		noi di 1

		local colourOption `"`colourOption' (bar mean order if order == `colourLoop', color("`colour'")) "'

		noi di 2

		noi di `"`colourOption'"'

	}

	noi di 3

	local confIntGraph = "(rcap conf_int_max conf_int_min order, lc(gs)) (scatter mean order, msym(none)  mlabs(medium) mlabpos(10) mlabcolor(black)), "

	local orderOption = "legend(order("

	forval y = 1(1)`graphCount'{
		local orderOption  "`orderOption' `y'"
	}

	local orderOption "`orderOption' ))"

	noi di 4

	noi di `" graph twoway `colourOption' `confIntGraph' `orderOption' "'

	graph twoway `colourOption' `confIntGraph' `orderOption' saving("newfile.gph", replace)

	noi di 5

	graph export graphname.png, replace
}
*restore
end


iefolder - run file - add new master

iefolder - run file - add new master

	*Set standardized settings
	ieboilstart, v(11.1)
	`r(version)'

	*Folder globals
	global box 			"C:\Users\wb462869\Box Sync"
	global ief 			"$box\Stata\Stata work\Commands\ietoolkit"
	*global ief 			"C:\Users\Kristoffer\Documents\GitHub\ietoolkit"

	*Load the command
	qui do "$ief\functions.do"
	qui do "$ief\iefolder.do"
	*Set cd folder to catch and cd leakage
	cd "$ief\test\cdjunk"

	*Global to the project fodler in box
	global projectABC	"$ief\test\testOutput\Project ABC Uganda"

	*Clean up the folder fomr last run
	do "$ief\deletefolder.do"
	cap deleteFolder "$projectABC\DataWork"

******************************************************************************

	*Creat a new project
	iefolder new project 			, projectfolder("$projectABC")

	*Add a unit of observation
	iefolder new master students 	, projectfolder("$projectABC")
	iefolder new master schools 	, projectfolder("$projectABC")
	*iefolder new master students 	, projectfolder("$projectABC")

	*Create a new folder called baseline
	iefolder new round baseline		, projectfolder("$projectABC")

	*Create a new folder for endline with abbreviation EL
	iefolder new round endline		, projectfolder("$projectABC") abb("EL")

	*Show example of test for imput
	*iefolder new round baseline , projectfolder("$projectABC")

	*Show that it is scalabe
	iefolder new round  midline_studnets 	, projectfolder("$projectABC") 	abb("ML_stud")
	iefolder new round  midline_teachers 	, projectfolder("$projectABC") 	abb("ML_tch")
	iefolder new master teachers 			, projectfolder("$projectABC")
	iefolder new round  midline_schools 	, projectfolder("$projectABC")  abb("ML_schl")

	*Add a more unit of observations

	*Set standardized settings
	ieboilstart, v(11.1)
	`r(version)'

	*Folder globals
	global box 			"C:\Users\wb462869\Box Sync"
	global ief 			"$box\Stata\Stata work\Commands\ietoolkit"
	*global ief 			"C:\Users\Kristoffer\Documents\GitHub\ietoolkit"

	*Load the command
	do "$ief\functions.do"
	do "$ief\iefolder.do"
	*Set cd folder to catch and cd leakage
	cd "$ief\test\cdjunk"

	*Global to the project fodler in box
	global projectABC	"$ief\test\testOutput\Project ABC Uganda"

	*Clean up the folder fomr last run
	do "$ief\deletefolder.do"
	cap deleteFolder "$projectABC\DataWork"

******************************************************************************

	*Creat a new project
	iefolder new project 		, projectfolder("$projectABC")

	*Create a new folder called baseline
	iefolder new round baseline	, projectfolder("$projectABC")

	*Create a new folder for endline with abbreviation EL
	iefolder new round endline	, projectfolder("$projectABC") abb("EL")

	*Show example of test for imput
	*iefolder new round baseline , projectfolder("$projectABC")

	*Show that it is scalabe
	iefolder new round midline_studnets , projectfolder("$projectABC") 	abb("ML_stud")
	iefolder new round midline_teachers , projectfolder("$projectABC") 	abb("ML_tch")
	iefolder new round midline_schools 	, projectfolder("$projectABC")  	abb("ML_schl")


changes to difindiff

changes to difindiff

program diffindiff

local model `1'
local dv `2'
local unit `3'

di "OK, you asked me to generate DD charts"
di "for `model', unit: `unit', and DVs: `dv'"

//testing
* local model xtpoisson
* local dv numuser
* local unit geoid10
//

* use ${stash}panel`unit', clear
* use ${stash}panel`unit', clear

gen post =  month > mofd(date("10-1-2007","MDY"))

runreg `model' "`dv'" `unit'
loadreg `model' "`dv'" `unit'
writereg `model'_`unit'

end

program loadreg

local model `1'
local dv `2'
local unit `3'

di "storing estimates from `model' for `dv' (`unit')"

est clear
foreach x in `dv'{
estimates use ${myestimates}`model'_`x'_2014_`unit'
estadd local unitfe "Yes", replace
estadd local monthfe "Yes", replace
eststo est`x'
}
end

program runreg
local model `1'
local dv `2'
local unit `3'

est clear
di "-----"
di "  Running `model' for `dv'"
di "-----"
foreach x in `dv'{
di "now processing `x'"
`model' `x' 1.treat#1.post i.month, fe vce(robust)
esttab, keep(1.treat#1.post) p
estimates save ${myestimates}`model'_`x'_2014_`unit', replace
}
end

program writereg

local filename `1'

esttab using "${tables}`filename'.tex", keep(1.treat#1.post) se ar2 nonotes star(+ 0.15 * 0.10 ** 0.05 *** 0.01) coeflabels(1.treat#1.post "Post X TIGER" _cons "Constant") mtitles("Contrib" "Users" "New Users" "New Users(6+)" "New Super Users" "Super Users") replace booktabs  s(unitfe monthfe N, label("County FE" "Month FE"))

end

program diffindiff

local model `1'
local dv `2'
local unit `3'

di "OK, you asked me to generate DD charts"
di "for `model', unit: `unit', and DVs: `dv'"

//testing
* local model xtreg
* local dv numuser
* local unit geoid10
//

use ${stash}panel`unit', clear

gen post =  month > mofd(date("10-1-2007","MDY"))

runreg `model' "`dv'" `unit'
loadreg `model' "`dv'" `unit'
writereg `model'_`unit'

end

program loadreg

local model `1'
local dv `2'
local unit `3'

di "storing estimates from `model' for `dv' (`unit')"

est clear
foreach x in `dv'{
estimates use ${myestimates}`model'_`x'_2014_`unit'
estadd local unitfe "Yes", replace
estadd local monthfe "Yes", replace
eststo est`x'
}
end

program runreg
local model `1'
local dv `2'
local unit `3'

est clear
di "-----"
di "  Running `model' for `dv'"
di "-----"
foreach x in `dv'{
di "now processing `x'"
`model' `x' 1.treat#1.post i.month, fe vce(robust)
esttab, keep(1.treat#1.post) p
estimates save ${myestimates}`model'_`x'_2014_`unit', replace
}
end

program writereg

local filename `1'

esttab using "${tables}`filename'.tex", keep(1.treat#1.post) se ar2 nonotes star(+ 0.15 * 0.10 ** 0.05 *** 0.01) coeflabels(1.treat#1.post "Post X TIGER" _cons "Constant") mtitles("Contrib" "Users" "New Users" "New Users(6+)" "New Super Users" "Super Users") replace booktabs  s(unitfe monthfe N, label("County FE" "Month FE"))

end


Guard against operating on strings.

Guard against operating on strings.

This means we can, shakily, assume that SF_vdata() and SF_vstore()
are safe in the C code.

program _svm, plugin /*load the C extension if not already loaded*/

program define svm_train, eclass
  version 13
  syntax varlist (numeric) [if] [in], [options_go_here]

  /* call down into C */
  plugin call _svm `varlist' `if' `in', "train"

  ereturn clear
  /* fixup the e() dictionary */

  * set standard Stata regression properties
  ereturn local cmd = "svm_train"
  ereturn local cmdline = "`e(cmd)' `0'"
  ereturn local predict = "svm_predict"
  ereturn local model = "svm" /*TODO: be more specific? e.g. svr, nu-SVC, etc?*/
  ereturn local title = "Support Vector Machine"

  gettoken depvar : varlist
  ereturn local depvar = "`depvar'"

  * export the svm_model structure
  _svm_model2stata
end

program _svm, plugin /*load the C extension if not already loaded*/

program define svm_train, eclass
  version 13
  syntax varlist [if] [in], [options_go_here]

  /* call down into C */
  plugin call _svm `varlist' `if' `in', "train"

  ereturn clear
  /* fixup the e() dictionary */

  * set standard Stata regression properties
  ereturn local cmd = "svm_train"
  ereturn local cmdline = "`e(cmd)' `0'"
  ereturn local predict = "svm_predict"
  ereturn local model = "svm" /*TODO: be more specific? e.g. svr, nu-SVC, etc?*/
  ereturn local title = "Support Vector Machine"

  gettoken depvar : varlist
  ereturn local depvar = "`depvar'"

  * export the svm_model structure
  _svm_model2stata
end