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82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424

* Selection

preserve
		
local namestr  `" "\it{A. Whole Sample}" "' 
	di `"`namestr'"'
	
	global varlist0_selection "in_home_survey has_valid_rdt_H"
	global varlist1_selection "has_valid_rdt_H"

la var in_home_survey "Took Home Survey"
la var has_valid_rdt_H "Took Home-Based RDT"


foreach var in $varlist0_selection $varlist1_selection {
	local repx ""
	if "`var'"!="nobs" & "`var'"!="bobs" local repx "replace `var'=. if dropme==1"
	`repx'
	}
	
* Varlists
	local v0= wordcount("$varlist0_selection")
	local v1= wordcount("$varlist1_selection")	
	

local totrow= 2*(`v0'+`v0'+`v1')+3
	matrix table_randomization=J(`totrow',8,.)
	matrix se= J(`totrow',1,.)

local part1 "\it{A. Whole Sample} &"
	
loc f = 2

foreach var in $varlist0_selection {

local lbz: variable label `var'
	local namestr " `namestr' "`lbz'" "\thinspace" "

local q= `f'+1
local part`f' "\enspace `lbz' &"
local part`q' "\thinspace &" 	

*Regressions
reg `var' patient_voucher doctor_voucher $date, clu($cluvar)
	local coeff_patient_voucher: display %19.3f _b[patient_voucher]
	local coeff_doctor_voucher:  display %19.3f _b[doctor_voucher]
	local se_patient_voucher: display %19.3f _se[patient_voucher]
	local se_doctor_voucher:  display %19.3f _se[doctor_voucher]
	
	matrix table_randomization[`f',2] =`coeff_patient_voucher'
	matrix table_randomization[`f',3] =`coeff_doctor_voucher'

	matrix table_randomization[`f'+1,2] =`se_patient_voucher'
	matrix table_randomization[`f'+1,3] =`se_doctor_voucher'

	matrix se[`f'+1,1]=1

	*P-value
	test patient_voucher==doctor_voucher
	local p_value1: display %7.3f `r(p)'
	matrix table_randomization[`f',4] =`p_value1'
	
	*P-value
	test patient_voucher==doctor_voucher==0
	local p_value2: display %7.3f `r(p)'
	matrix table_randomization[`f',5] =`p_value2'

	*Number of observations
	matrix table_randomization[`f',6] =e(N)
	
	*Average for treatment group
	sum `var' if doctor_voucher+patient_voucher==0 & e(sample)
	local control_mean: display %19.3f r(mean)
	local control_sd: display %19.3f r(sd)

	matrix table_randomization[`f',1] =`control_mean'
	matrix table_randomization[`f'+1,1] =`control_sd'
	
	*Increase the counter of the row 
	loc f = `f' + 2

}

local sustring ""
local sutest1 ""
local sutest2 ""

local part`f' "\it{B. Selected for Home Survey} &"
	
local f = `f'+1

local namestr " `namestr' "\it{B. Selected for Home Survey}" "

foreach var in $varlist0_selection {


local lbz: variable label `var'
	local namestr " `namestr' "`lbz'" "\thinspace" "
	
local q= `f'+1
local part`f' "\enspace `lbz' &"
local part`q' "\thinspace &"
	
*Regressions
reg `var'  patient_voucher doctor_voucher $date if selected_home_survey==1
	est store E`var'
	local sustring "`sustring' E`var'"
	local sutest2 "`sutest2' [E`var'_mean]patient_voucher=[E`var'_mean]doctor_voucher="
	local sutest1 "`sutest1' [E`var'_mean]patient_voucher=[E`var'_mean]doctor_voucher="
 reg `var' patient_voucher doctor_voucher $date if selected_home_survey==1, clu($cluvar)
	local coeff_patient_voucher: display %19.3f _b[patient_voucher]
	local coeff_doctor_voucher:  display %19.3f _b[doctor_voucher]
	local se_patient_voucher: display %19.3f _se[patient_voucher]
	local se_doctor_voucher:  display %19.3f _se[doctor_voucher]
	
	matrix table_randomization[`f',2] =`coeff_patient_voucher'
	matrix table_randomization[`f',3] =`coeff_doctor_voucher'

	matrix table_randomization[`f'+1,2] =`se_patient_voucher'
	matrix table_randomization[`f'+1,3] =`se_doctor_voucher'

	matrix se[`f'+1,1]=1

	*P-value
	test patient_voucher==doctor_voucher
	local p_value1: display %7.3f `r(p)'
	matrix table_randomization[`f',4] =`p_value1'
	
	*P-value
	test patient_voucher==doctor_voucher==0
	local p_value2: display %7.3f `r(p)'
	matrix table_randomization[`f',5] =`p_value2'

	*Number of observations
	matrix table_randomization[`f',6] =e(N)
	
	*Average for treatment group
	sum `var' if doctor_voucher+patient_voucher==0 & e(sample)
	local control_mean: display %19.3f r(mean)
	local control_sd: display %19.3f r(sd)

	matrix table_randomization[`f',1] =`control_mean'
	matrix table_randomization[`f'+1,1] =`control_sd'
	
	*Increase the counter of the row 
	loc f = `f' + 2

}
	
mat li table_randomization


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


local part`f' "\it{C. Took Home Survey} &"
	
local f = `f'+1

local namestr " `namestr' "\it{C. Took Home Survey}" "

foreach var in $varlist1_selection {


local lbz: variable label `var'
	local namestr " `namestr' "`lbz'" "\thinspace" "
	
local q= `f'+1
local part`f' "\enspace `lbz' &"
local part`q' "\thinspace &"
	
* REGRESSIONS
reg `var'  patient_voucher doctor_voucher $date if in_home_survey==1
	est store E`var'
	local sustring "`sustring' E`var'"
	local sutest2 "`sutest2' [E`var'_mean]patient_voucher=[E`var'_mean]doctor_voucher="
	local sutest1 "`sutest1' [E`var'_mean]patient_voucher=[E`var'_mean]doctor_voucher="
 reg `var' patient_voucher doctor_voucher $date if in_home_survey==1, clu($cluvar)
	local coeff_patient_voucher: display %19.3f _b[patient_voucher]
	local coeff_doctor_voucher:  display %19.3f _b[doctor_voucher]
	local se_patient_voucher: display %19.3f _se[patient_voucher]
	local se_doctor_voucher:  display %19.3f _se[doctor_voucher]
	
	matrix table_randomization[`f',2] =`coeff_patient_voucher'
	matrix table_randomization[`f',3] =`coeff_doctor_voucher'

	matrix table_randomization[`f'+1,2] =`se_patient_voucher'
	matrix table_randomization[`f'+1,3] =`se_doctor_voucher'

	matrix se[`f'+1,1]=1

	*P-value
	test patient_voucher==doctor_voucher
	local p_value1: display %7.3f `r(p)'
	matrix table_randomization[`f',4] =`p_value1'
	
	*P-value
	test patient_voucher==doctor_voucher==0
	local p_value2: display %7.3f `r(p)'
	matrix table_randomization[`f',5] =`p_value2'

	*Number of observations
	matrix table_randomization[`f',6] =e(N)
	
	*Average for treatment group
	sum `var' if doctor_voucher+patient_voucher==0 & e(sample)
	local control_mean: display %19.3f r(mean)
	local control_sd: display %19.3f r(sd)

	matrix table_randomization[`f',1] =`control_mean'
	matrix table_randomization[`f'+1,1] =`control_sd'
	
	*Increase the counter of the row 
	loc f = `f' + 2
	
}

local sutest1 "`sutest1' [Egender_mean]patient_voucher"
local sutest2 "`sutest2' 0"
	
local df= e(df_r)
		
mat li table_randomization
matrix rownames table_randomization= `namestr'

local bc = rowsof(table_randomization)

matrix stars = J(`bc',6,0)

	local end0= `v0'*2
	local start1= `end0'+3
	local end1= `start1' + `v1'*2
	local start2= `end1'+3
	local end2= `totrow'-1
	
forvalues i=4(1)5{

forvalues k = 2(2)`end0' {

	if table_randomization[`k',`i']<=.01{
	local star "1"
	}
	if table_randomization[`k',`i']>.01 & table_randomization[`k',`i']<=.05{
	local star "2"
	}
	if table_randomization[`k',`i']>.05 & table_randomization[`k',`i']<=.10{
	local star "3"
	}
	if table_randomization[`k',`i']>.10{
	local star "0"
	}
	di
    matrix stars[`k',`i'] = `star'
}

forvalues k = `start1'(2)`end1' {

	if table_randomization[`k',`i']<=.01{
	local star "1"
	}
	if table_randomization[`k',`i']>.01 & table_randomization[`k',`i']<=.05{
	local star "2"
	}
	if table_randomization[`k',`i']>.05 & table_randomization[`k',`i']<=.10{
	local star "3"
	}
	if table_randomization[`k',`i']>.10 {
	local star "0"
	}
	di
    matrix stars[`k',`i'] = `star'
}

forvalues k = `start2'(2)`end2' {

	if table_randomization[`k',`i']<=.01{
	local star "1"
	}
	if table_randomization[`k',`i']>.01 & table_randomization[`k',`i']<=.05{
	local star "2"
	}
	if table_randomization[`k',`i']>.05 & table_randomization[`k',`i']<=.10{
	local star "3"
	}
	if table_randomization[`k',`i']>.10 {
	local star "0"
	}
	di
    matrix stars[`k',`i'] = `star'
}
}

// use critical values from t-distribution, just as is done in regressions
local cv10= abs(invt(`df',.05))
local cv5= abs(invt(`df',.025))
local cv1= abs(invt(`df',.005))
	
forvalues i=2(1)3{

	forvalues k = 2(2)`end0' {

		local ttest=abs(table_randomization[`k',`i']/table_randomization[`k'+1,`i'])
		if `ttest'<`cv10' {
		local star "0"
		}
		if `ttest'>=`cv10' & `ttest'<`cv5' {
		local star "3"
		}
		if `ttest'>=`cv5' & `ttest'<`cv1' {
		local star "2"
		}
		if `ttest'>=`cv1' & `ttest'!=. {
		local star "1"
		}
		di
		matrix stars[`k',`i'] = `star'
	}

	forvalues k = `start1'(2)`end1' {

		local ttest=abs(table_randomization[`k',`i']/table_randomization[`k'+1,`i'])
		if `ttest'<`cv10' {
		local star "0"
		}
		if `ttest'>=`cv10' & `ttest'<`cv5' {
		local star "3"
		}
		if `ttest'>=`cv5' & `ttest'<`cv1' {
		local star "2"
		}
		if `ttest'>=`cv1' & `ttest'!=. {
		local star "1"
		}
		di
		matrix stars[`k',`i'] = `star'	
		
		}	
	forvalues k = `start2'(2)`end2' {

		local ttest=abs(table_randomization[`k',`i']/table_randomization[`k'+1,`i'])
		if `ttest'<`cv10' {
		local star "0"
		}
		if `ttest'>=`cv10' & `ttest'<`cv5' {
		local star "3"
		}
		if `ttest'>=`cv5' & `ttest'<`cv1' {
		local star "2"
		}
		if `ttest'>=`cv1' {
		local star "1"
		}
		di
		matrix stars[`k',`i'] = `star'	
		
		}

}

drop _all
svmat table_randomization
svmat stars
svmat se
g t0=""
	forval i=1/`totrow' {
		di "`part`i''"
		replace t0= "`part`i''" if _n==`i'
	}
	
forval var=1/5 {
		tostring table_randomization`var', format("%9.3f") replace force
			replace table_randomization`var'="" if table_randomization`var'=="."
			replace table_randomization`var'="0"+table_randomization`var' if substr(table_randomization`var',1,1)=="."
		replace table_randomization`var'= table_randomization`var'+"\sym{*}" if stars`var'==3
		replace table_randomization`var'= table_randomization`var'+"\sym{**}" if stars`var'==2
		replace table_randomization`var'= table_randomization`var'+"\sym{***}" if stars`var'==1
		
	}
replace table_randomization1= "["+table_randomization1+"]" if se1==1

forval i=2/3 {
		replace table_randomization`i'="("+table_randomization`i'+")" if se1==1
	}

forval i=1/5 {
		replace table_randomization`i'= table_randomization`i'+"&"
	}
		tostring table_randomization6, format("%9.0f") replace force
		replace table_randomization6="" if table_randomization6=="."
		replace table_randomization6="0"+table_randomization6 if substr(table_randomization6,1,1)=="."


		replace table_randomization6= table_randomization6+"\\" if table_randomization6!=""
		replace table_randomization6= table_randomization6+"& \\" if table_randomization6==""	
		
set obs `=_N+2'		
	g index= _n
	replace index=0 if _n==_N-1
	sort index
order t0 table_randomization1-table_randomization8
keep t0-table_randomization8 
list, clean 

#delimit ;
local fn "$footnote0_alt $footnote1";
local text0 "\begin{table}[h!] \begin{adjustbox}{max width=1\textwidth} \begin{threeparttable} \caption{Selection into Analysis Samples by Treatment} \label{selection} { 
			\begin{tabular}{l*{7}{c}} \hline\hline & \multicolumn{1}{c}{(1)} & \multicolumn{1}{c}{(2)} & \multicolumn{1}{c}{(3)} & \multicolumn{1}{c}{(4)} & \multicolumn{1}{c}{(5)} & \multicolumn{1}{c}{(6)}  \\
			&  &  \multicolumn{2}{c}{Regression Coefficients} & \multicolumn{2}{c}{P-Values} &  \\ 
			\cmidrule(lr){3-4} \cmidrule(lr){5-6}  & 
				\multicolumn{1}{c}{\shortstack{Control\\Mean}} & \multicolumn{1}{c}{\shortstack{Patient\\Voucher}} & \multicolumn{1}{c}{\shortstack{Doctor\\ Voucher}}  & 
				\multicolumn{1}{c}{\shortstack{Joint Test\\ PV=DV}} & \multicolumn{1}{c}{\shortstack{Joint Test \\ PV=DV=0}} & \multicolumn{1}{c}{N} \\ \hline";
local textlast "\hline \end{tabular} } \begin{tablenotes}[flushleft] \small 
		\item `fn' 
		\end{tablenotes} \end{threeparttable} \end{adjustbox} \end{table}";
#delimit cr	
	replace t0="`text0'" if _n==1
	replace t0="`textlast'" if _n==_N
*	replace t0=" & " if t1!="" & _n>=2 & _n<_N & t0==""

outfile t0-table_randomization6 using "$tables/B5_selection.tex", noquote replace wide		

restore