-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgen_reg.do
576 lines (472 loc) · 16.9 KB
/
gen_reg.do
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
cls
clear
macro drop _all
/*
Back to Fan's Stata4Econ or other repositories:
- http://fanwangecon.github.io
- http://fanwangecon.github.io/Stata4Econ
- http://fanwangecon.github.io/R4Econ
- http://fanwangecon.github.io/M4Econ
- http://fanwangecon.github.io/CodeDynaAsset/
- http://fanwangecon.github.io/Math4Econ/
- http://fanwangecon.github.io/Stat4Econ/
- http://fanwangecon.github.io/Tex4Econ
Regression Table where:
- shared regression outcome lhs variable
- for each panel, rhs variables differ
- for each column, conditioning differs, but rhs vars the same
*/
///--- File Names
global st_file_root "~\Stata4Econ\table\multipanel\tab_6col3pan\"
global st_log_file "${st_file_root}gen_reg"
global st_out_html "${st_file_root}tab_6col3pan.html"
global st_out_rtf "${st_file_root}tab_6col3pan.rtf"
global st_out_tex "${st_file_root}tab_6col3pan_texbody.tex"
///--- Start log
capture log close
log using "${st_log_file}" , replace
log on
set trace off
set tracedepth 1
/////////////////////////////////////////////////
///--- Load Data
/////////////////////////////////////////////////
set more off
sysuse auto, clear
tab rep78
tab foreign
/////////////////////////////////////////////////
///--- A1. Define Regression Variables
/////////////////////////////////////////////////
* shared regression outcome lhs variable
global svr_outcome "price"
* for each panel, rhs variables differ
global svr_rhs_panel_a "mpg ib1.rep78 displacement gear_ratio"
global svr_rhs_panel_b "headroom mpg trunk weight displacement gear_ratio"
global svr_rhs_panel_c "headroom turn length weight trunk"
* for each column, conditioning differs
global it_reg_n = 6
global sif_col_1 "weight <= 4700"
global sif_col_2 "weight <= 4500"
global sif_col_3 "weight <= 4300"
global sif_col_4 "weight <= 4100"
global sif_col_5 "weight <= 3900"
global sif_col_6 "weight <= 3700"
* esttad strings for conditioning what were included
scalar it_esttad_n = 4
matrix mt_bl_estd = J(it_esttad_n, $it_reg_n, 0)
matrix rownames mt_bl_estd = incdgr4500 incdgr4000 incdgr3500 incdgr3000
matrix colnames mt_bl_estd = reg1 reg2 reg3 reg4 reg5 reg6
matrix mt_bl_estd[1, 1] = (1\1\1\1)
matrix mt_bl_estd[1, 2] = (1\1\1\1)
matrix mt_bl_estd[1, 3] = (0\1\1\1)
matrix mt_bl_estd[1, 4] = (0\1\1\1)
matrix mt_bl_estd[1, 5] = (0\0\1\1)
matrix mt_bl_estd[1, 6] = (0\0\1\1)
global st_estd_rownames : rownames mt_bl_estd
global slb_estd_1 "the weight <= 4700"
global slb_estd_2 "the weight <= 4500"
global slb_estd_3 "the weight <= 4300"
global slb_estd_4 "the weight <= 4100"
/////////////////////////////////////////////////
///--- A2. Define Regression Technical Strings
/////////////////////////////////////////////////
///--- Technical Controls
global stc_regc "regress"
global stc_opts ", noc"
/////////////////////////////////////////////////
///--- B1. Define Regressions Panel A
/////////////////////////////////////////////////
/*
di "$srg_panel_a_col_1"
di "$srg_panel_a_col_2"
di "$srg_panel_a_col_6"
*/
foreach it_regre of numlist 1(1)$it_reg_n {
#delimit;
global srg_panel_a_col_`it_regre' "
$stc_regc $svr_outcome $svr_rhs_panel_a if ${sif_col_`it_regre'} $stc_opts
";
#delimit cr
di "${srg_panel_a_col_`it_regre'}"
}
/////////////////////////////////////////////////
///--- B2. Define Regressions Panel B
/////////////////////////////////////////////////
/*
di "$srg_panel_b_col_1"
di "$srg_panel_b_col_2"
di "$srg_panel_b_col_6"
*/
foreach it_regre of numlist 1(1)$it_reg_n {
#delimit;
global srg_panel_b_col_`it_regre' "
$stc_regc $svr_outcome $svr_rhs_panel_b if ${sif_col_`it_regre'} $stc_opts
";
#delimit cr
di "${srg_panel_b_col_`it_regre'}"
}
/////////////////////////////////////////////////
///--- B3. Define Regressions Panel C
/////////////////////////////////////////////////
/*
di "$srg_panel_c_col_1"
di "$srg_panel_c_col_2"
di "$srg_panel_c_col_6"
*/
foreach it_regre of numlist 1(1)$it_reg_n {
#delimit;
global srg_panel_c_col_`it_regre' "
$stc_regc $svr_outcome $svr_rhs_panel_c if ${sif_col_`it_regre'} $stc_opts
";
#delimit cr
di "${srg_panel_c_col_`it_regre'}"
}
/////////////////////////////////////////////////
///--- C. Run Regressions
/////////////////////////////////////////////////
eststo clear
local it_reg_ctr = 0
foreach st_panel in panel_a panel_b panel_c {
global st_cur_sm_stor "smd_`st_panel'_m"
global ${st_cur_sm_stor} ""
foreach it_regre of numlist 1(1)$it_reg_n {
local it_reg_ctr = `it_reg_ctr' + 1
global st_cur_srg_name "srg_`st_panel'_col_`it_regre'"
di "st_panel:`st_panel', it_reg_ctr:`it_reg_ctr', st_cur_srg_name:${st_cur_srg_name}"
///--- Regression
eststo m`it_reg_ctr', title("${sif_col_`it_regre'}") : ${$st_cur_srg_name}
///--- Estadd Controls
foreach st_estd_name in $st_estd_rownames {
scalar bl_estad = el(mt_bl_estd, rownumb(mt_bl_estd, "`st_estd_name'"), `it_regre')
if (bl_estad) {
estadd local `st_estd_name' "Yes"
}
else {
estadd local `st_estd_name' "No"
}
}
///--- Track Regression Store
global $st_cur_sm_stor "${${st_cur_sm_stor}} m`it_reg_ctr'"
}
di "${${st_cur_sm_stor}}"
}
di "$smd_panel_a_m"
di "$smd_panel_b_m"
di "$smd_panel_c_m"
/////////////////////////////////////////////////
///--- D1. Labeling
/////////////////////////////////////////////////
///--- Title overall
global slb_title "Outcome: Attending School or Not"
global slb_title_inner "\textbf{Male}: Subregression for All Males"
global slb_label_tex "tab:sctp"
global slb_panel_a "Group A: Coefficients for Distance to Elementary School Variables"
global slb_panel_b "Group B: Coefficients for Elementary School Physical Quality Variables"
global slb_panel_c "Group C: More Coefficientss"
global slb_bottom "Controls for each panel:"
global slb_note "${slb_starLvl}. Standard Errors clustered at village level. Each Column is a spearate regression."
///--- Show which coefficients to keep
#delimit;
global svr_coef_keep_panel_a "
mpg
2.rep78 3.rep78
4.rep78 5.rep78
";
global svr_coef_keep_panel_b "
headroom
mpg
trunk
weight
";
global svr_coef_keep_panel_c "
turn
";
#delimit cr
///--- Labeling for for Coefficients to Show
global slb_1st_ele_spc "\vspace*{0mm}\hspace*{2mm}"
global slb_fot_lst_spc "\vspace*{0mm}\hspace*{2mm}"
global rcSpaceInit "\vspace*{-5mm}\hspace*{-8mm}"
#delimit;
global svr_starts_var_panel_a "mpg";
global slb_coef_label_panel_a "
mpg "${slb_1st_ele_spc}miles per gallon"
2.rep78 "${slb_1st_ele_spc}rep78 is 2"
3.rep78 "${slb_1st_ele_spc}rep78 is 3"
4.rep78 "${slb_1st_ele_spc}rep78 is 4"
5.rep78 "${slb_1st_ele_spc}rep78 is 5"
";
#delimit cr
#delimit;
global svr_starts_var_panel_b "headroom";
global slb_coef_label_panel_b "
headroom "${slb_1st_ele_spc}headroom variable"
mpg "${slb_1st_ele_spc}miles per gallon"
trunk "${slb_1st_ele_spc}this is the trunk variable"
weight "${slb_1st_ele_spc}and here the weight variable"
";
#delimit cr
#delimit;
global svr_starts_var_panel_c "turn";
global slb_coef_label_panel_c "
turn "${slb_1st_ele_spc}variable is turn"
";
#delimit cr
/////////////////////////////////////////////////
///--- D2. Regression Display Controls
/////////////////////////////////////////////////
global slb_reg_stats "N ${st_estd_rownames}"
global slb_starLvl "* 0.10 ** 0.05 *** 0.01"
global slb_starComm "nostar"
global slb_sd_tex `"se(fmt(a2) par("\vspace*{-2mm}{\footnotesize (" ") }"))"'
global slb_cells_tex `"cells(b(star fmt(a2)) $slb_sd_tex)"'
global slb_esttab_opt_tex "${slb_cells_tex} booktabs label collabels(none) nomtitles nonumbers star(${slb_starLvl})"
global slb_sd_txt `"se(fmt(a2) par("(" ")"))"'
global slb_cells_txt `"cells(b(star fmt(a2)) $slb_sd_txt)"'
global slb_esttab_opt_txt "${slb_cells_txt} stats(${slb_reg_stats}) collabels(none) mtitle nonumbers varwidth(30) modelwidth(15) star(${slb_starLvl}) addnotes(${slb_note})"
#delimit ;
global slb_panel_a_main "
title("${slb_panel_a}")
keep(${svr_coef_keep_panel_a}) order(${svr_coef_keep_panel_a})
coeflabels($slb_coef_label_panel_a)
";
global slb_panel_b_main "
title("${slb_panel_b}")
keep(${svr_coef_keep_panel_b}) order(${svr_coef_keep_panel_b})
coeflabels($slb_coef_label_panel_b)
";
global slb_panel_c_main "
title("${slb_panel_c}")
keep(${svr_coef_keep_panel_c}) order(${svr_coef_keep_panel_c})
coeflabels($slb_coef_label_panel_c)
";
#delimit cr
/////////////////////////////////////////////////
///--- E. Regression Shows
/////////////////////////////////////////////////
esttab ${smd_panel_a_m}, ${slb_panel_a_main} ${slb_esttab_opt_txt}
esttab ${smd_panel_b_m}, ${slb_panel_b_main} ${slb_esttab_opt_txt}
esttab ${smd_panel_c_m}, ${slb_panel_c_main} ${slb_esttab_opt_txt}
/////////////////////////////////////////////////
///--- F1. Define Latex Column Groups and Column Sub-Groups
/////////////////////////////////////////////////
///--- Column Groups
global it_max_col = 8
global it_min_col = 2
global it_col_cnt = 6
global colSeq "2 4 6 8"
///--- Group 1, columns 1 and 2
global labG1 "All Age 5 to 12"
global labC1 "{\small All Villages}"
global labC2 "{\small No Teachng Points}"
///--- Group 2, columns 3 and 4
global labG2 "Girls Age 5 to 12"
global labC3 "{\small All Villages}"
global labC4 "{\small No Teachng Points}"
///--- Group 3, columns 5 and 6
global labG3 "Boys Age 5 to 12"
global labC5 "{\small All Villages}"
global labC6 "{\small No Teachng Points}"
///--- Column Widths
global perCoefColWid = 1.85
global labColWid = 6
///--- Column Fractional Adjustment, 1 = 100%
global tableAdjustBoxWidth = 1.0
/////////////////////////////////////////////////
///--- F2. Tabling Calculations
/////////////////////////////////////////////////
///--- Width Calculation
global totCoefColWid = ${perCoefColWid}*${it_col_cnt}
global totColCnt = ${it_col_cnt} + 1
global totColWid = ${labColWid} + ${totCoefColWid} + ${perCoefColWid}
global totColWidFootnote = ${labColWid} + ${totCoefColWid} + ${perCoefColWid} + ${perCoefColWid}/2
global totColWidLegend = ${labColWid} + ${totCoefColWid} + ${perCoefColWid}
global totColWidLegendthin = ${totCoefColWid} + ${perCoefColWid}
di "it_col_cnt:$it_col_cnt"
di "totCoefColWid:$totCoefColWid"
di "totCoefColWid:$totCoefColWid"
di "totCoefColWid:$totCoefColWid"
di "totCoefColWid:$totCoefColWid"
di "totCoefColWid:$totCoefColWid"
global ampersand ""
foreach curLoop of numlist 1(1)$it_col_cnt {
global ampersand "$ampersand &"
}
di "ampersand:$ampersand"
global alignCenter "m{${labColWid}cm}"
local eB1 ">{\centering\arraybackslash}m{${perCoefColWid}cm}"
foreach curLoop of numlist 1(1)$it_col_cnt {
global alignCenter "$alignCenter `eB1'"
}
di "alignCenter:$alignCenter"
/////////////////////////////////////////////////
///--- G1. Tex Sectioning
/////////////////////////////////////////////////
#delimit ;
global slb_titling_panel_a "
${svr_starts_var_panel_a} "\multicolumn{$totColCnt}{L{${totColWidLegend}cm}}{${rcSpaceInit}\textbf{${slb_panel_a}}} \\"
";
global slb_refcat_panel_a `"refcat(${slb_titling_panel_a}, nolabel)"';
#delimit cr
#delimit ;
global slb_titling_panel_b "
${svr_starts_var_panel_b} "\multicolumn{$totColCnt}{L{${totColWidLegend}cm}}{${rcSpaceInit}\textbf{${slb_panel_b}}} \\"
";
global slb_refcat_panel_b `"refcat(${slb_titling_panel_b}, nolabel)"';
#delimit cr
#delimit ;
global slb_titling_panel_c "
${svr_starts_var_panel_c} "\multicolumn{$totColCnt}{L{${totColWidLegend}cm}}{${rcSpaceInit}\textbf{${slb_panel_c}}} \\"
";
global slb_refcat_panel_c `"refcat(${slb_titling_panel_c}, nolabel)"';
#delimit cr
#delimit ;
global slb_titling_bottom `"
stats(N $st_estd_rownames,
labels(Observations
"\midrule \multicolumn{${totColCnt}}{L{${totColWid}cm}}{${rcSpaceInit}\textbf{\textit{\normalsize ${slb_bottom}}}} \\ $ampersand \\ ${slb_fot_lst_spc}${slb_estd_1}"
"${slb_fot_lst_spc}${slb_estd_2}"
"${slb_fot_lst_spc}${slb_estd_3}"
"${slb_fot_lst_spc}${slb_estd_4}"))"';
#delimit cr
/////////////////////////////////////////////////
///--- G2. Tex Headline
/////////////////////////////////////////////////
///--- C.3.A. Initialize
global row1 "&"
global row1MidLine ""
global row2 ""
global row2MidLine ""
global row3 ""
///--- B. Row 2 and row 2 midline
* global colSeq "2 3 6"
global cmidrule ""
global colCtr = -1
foreach curCol of numlist $colSeq {
global colCtr = $colCtr + 1
global curCol1Min = `curCol' - 1
if ($colCtr == 0 ) {
global minCoefCol = "`curCol'"
}
if ($colCtr != 0 ) {
global gapCnt = (`curCol' - `lastCol')
global gapWidth = (`curCol' - `lastCol')*$perCoefColWid
di "curCol1Min:$curCol1Min, lastCol:`lastCol'"
di "$gapCnt"
di "\multicolumn{$gapCnt}{C{${gapWidth}cm}}{\small no Control}"
di "\cmidrule(l{5pt}r{5pt}){`lastCol'-$curCol1Min}"
global curRow2MidLine "\cmidrule(l{5pt}r{5pt}){`lastCol'-$curCol1Min}"
global row2MidLine "$row2MidLine $curRow2MidLine"
global curRow2 "\multicolumn{$gapCnt}{L{${gapWidth}cm}}{\small ${labG${colCtr}}}"
global row2 "$row2 & $curRow2"
}
local lastCol = `curCol'
}
///--- C. Row 3
* Initial & for label column
foreach curLoop of numlist 1(1)$it_col_cnt {
global curText "${labC`curLoop'}"
global textUse "(`curLoop')"
if ("$curText" != "") {
global textUse "$curText"
}
global curRow3 "\multicolumn{1}{C{${perCoefColWid}cm}}{$textUse}"
global row3 "$row3 & $curRow3"
}
///--- D. Row 1 and midline:
global row1 "${row1} \multicolumn{${it_col_cnt}}{L{${totCoefColWid}cm}}{${slb_title_inner}}"
global row1MidLine "\cmidrule(l{5pt}r{5pt}){${minCoefCol}-${curCol1Min}}"
///--- C.3.E Print lines
di "$row1 \\"
di "$row1MidLine "
di "$row2 \\"
di "$row2MidLine"
di "$row3 \\"
///--- C.4 Together
#delimit ;
///--- 1. Section
* local section "
* \section{`fileTitle'}\vspace*{-6mm}
* ";
///--- 2. Align and Column Define
local centering "$alignCenter";
global headline "
$row1 \\
$row1MidLine
$row2 \\
$row2MidLine
$row3 \\
";
#delimit cr
/////////////////////////////////////////////////
///--- G4. Head
/////////////////////////////////////////////////
#delimit ;
global adjustBoxStart "\begin{adjustbox}{max width=${tableAdjustBoxWidth}\textwidth}";
global adjustBoxEnd "\end{adjustbox}";
global notewrap "
\addlinespace[-0.5em]
\multicolumn{${totColCnt}}{L{${totColWidFootnote}cm}}{\footnotesize\justify${slb_note}}\\
";
global startTable "\begin{table}[htbp]
\centering
\caption{${slb_title}\label{${slb_label_tex}}}${adjustBoxStart}\begin{tabular}{`centering'}
\toprule
";
global headlineAll "prehead(${startTable}${headline})";
global headlineAllNoHead "prehead(${startTable})";
global postAll "postfoot(\bottomrule ${notewrap} \end{tabular}${adjustBoxEnd}\end{table})";
#delimit cr
/////////////////////////////////////////////////
///--- H1. Output Results to HTML
/////////////////////////////////////////////////
esttab ${smd_panel_a_m} using "${st_out_html}", ${slb_panel_a_main} ${slb_esttab_opt_txt} replace
esttab ${smd_panel_b_m} using "${st_out_html}", ${slb_panel_b_main} ${slb_esttab_opt_txt} append
esttab ${smd_panel_c_m} using "${st_out_html}", ${slb_panel_c_main} ${slb_esttab_opt_txt} append
/////////////////////////////////////////////////
///--- H2. Output Results to RTF
/////////////////////////////////////////////////
esttab ${smd_panel_a_m} using "${st_out_rtf}", ${slb_panel_a_main} ${slb_esttab_opt_txt} replace
esttab ${smd_panel_b_m} using "${st_out_rtf}", ${slb_panel_b_main} ${slb_esttab_opt_txt} append
esttab ${smd_panel_c_m} using "${st_out_rtf}", ${slb_panel_c_main} ${slb_esttab_opt_txt} append
/////////////////////////////////////////////////
///--- H3. Output Results to Tex
/////////////////////////////////////////////////
esttab $smd_panel_a_m using "${st_out_tex}", ///
${slb_panel_a_main} ///
${slb_refcat_panel_a} ///
${slb_esttab_opt_tex} ///
fragment $headlineAll postfoot("") replace
esttab $smd_panel_b_m using "${st_out_tex}", ///
${slb_panel_b_main} ///
${slb_refcat_panel_b} ///
${slb_esttab_opt_tex} ///
fragment prehead("") postfoot("") append
esttab $smd_panel_c_m using "${st_out_tex}", ///
${slb_panel_c_main} ///
${slb_refcat_panel_c} ///
${slb_esttab_opt_tex} ///
${slb_titling_bottom} ///
addnotes(${slb_note}) ///
fragment prehead("") $postAll append
/////////////////////////////////////////////////
///--- I. Out Logs
/////////////////////////////////////////////////
///--- End Log and to HTML
log close
///--- to PDF
capture noisily {
translator set Results2pdf logo off
translator set Results2pdf fontsize 10
translator set Results2pdf pagesize custom
translator set Results2pdf pagewidth 11.69
translator set Results2pdf pageheight 16.53
translator set Results2pdf lmargin 0.2
translator set Results2pdf rmargin 0.2
translator set Results2pdf tmargin 0.2
translator set Results2pdf bmargin 0.2
translate @Results "${st_log_file}.pdf", replace translator(Results2pdf)
}
capture noisily {
erase "${st_log_file}.smcl"
}