I have to work with some code that isn't mine and that uses a lot of loops to create a table.
To put it in context, I first need to create a
As for the first-stage table, the regressions are correct and everything comes out as it should.
However, for the 2sls, the table is not what you would expect. Here is a small set of data corresponding to the variables of interest here. The code below is also used.
Could you please help me? I've been stuck on this for a while and don't know how to solve the problem. Many thanks in advance.
To put it in context, I first need to create a
- (i) first-stage table and
- (ii) then do a two-stage least squares (econometrics fans will certainly be familiar with this).
As for the first-stage table, the regressions are correct and everything comes out as it should.
However, for the 2sls, the table is not what you would expect. Here is a small set of data corresponding to the variables of interest here. The code below is also used.
Could you please help me? I've been stuck on this for a while and don't know how to solve the problem. Many thanks in advance.
Code:
* Example generated by -dataex-. For more info, type help dataex clear input float(pop_00_03 pop_04_14 pop_04_22 pop_16_22 cumpower_solar_norm_06_14 cumpower_solar_norm_06_22 cumpower_solar_norm_18_22) double com_pv_potential float(com_sensitivity_solar_1 inter_solar_above_1 prov_code) -207 1469 1002 -324 .2067 .2067 0 1213.258903503418 1 1213.2589 1 646 1175 1554 344 .9285 .9285 0 1266.2647552490234 1 1266.2648 1 366 686 896 344 2.6577 26.6727 24 1314.7562217712402 1 1314.7562 1 6649 21947 34186 9625 6.66833 7.34433 .6000004 1316.0530490875244 1 1316.053 1 31 -114 23 215 .4492 .4492 0 1339.9544219970703 1 1339.9545 1 378 864 846 82 1.8905 2.1185002 .22800016 1430.6792602539063 1 1430.6792 1 3875 5451 1755 -2642 26.74 614.3524 587.61237 1669.8718795776367 1 1669.872 10 244 -1230 -3825 -1990 54.3235 585.7555 531.432 1661.23876953125 1 1661.2388 10 -338 -886 -1870 -738 7.3299 280.7089 273.379 1653.3397336006165 0 0 10 -287 -914 -1924 -769 2.59 86.59 84 1662.4372177124023 0 0 10 -1188 -1589 -2964 -994 235.6368 363.2628 127.626 1642.3391647338867 1 1642.339 10 1405 -608 -2278 -1300 111.68807 613.2584 501.5549 1655.7666759490967 1 1655.7667 10 509 -1348 -2319 -592 4.13814 4.13814 0 1638.1639709472656 1 1638.164 10 1089 1588 -1823 -2937 10.7538 77.474 66.7202 1642.0976028442383 1 1642.0977 10 -562 -664 -2313 -1236 1.7 2.855 1.1549999 1597.5255527496338 1 1597.5255 10 -1531 -2487 -6024 -2662 21.85632 21.87874 0 1630.155107975006 1 1630.155 10 12404 30402 30496 438 132.26796 467.3235 334.93 1561.922212600708 1 1561.9222 11 3967 20492 19475 1431 9.60316 9.60316 0 1738.1756172180176 1 1738.1757 11 -89 -885 -2954 -1544 2.78 2.880585 .0999999 1670.3062381744385 1 1670.3063 11 2508 5970 6047 448 14.2629 244.69165 230.42876 1688.9799499511719 1 1688.98 11 11829 19822 29343 6119 13.9427 14.14255 0 1629.9501266479492 1 1629.95 11 -164 -588 -1136 -204 2.5619 2.5619 0 1579.6495881080627 0 0 12 333 -626 -1921 -832 2.1876001 2.1876001 0 1573.5992584228516 1 1573.5992 12 244 2535 2557 313 16.0095 16.0095 0 1597.2728004455566 1 1597.273 12 747 341 -142 -233 .3982 .3982 0 1607.2020378112793 1 1607.202 12 end
Code:
***************************************************** * 2. Define some macros for further reg and 2sls ***************************************************** local inst = 1 // = 1 use environmental sustainability index above one as instrument // = 2 use solar/wind potential // = 3 use interaction // = 4 use 1; 2 & 3 together local energy = 1 // = 1 if solar // = 2 if wind // Estimation *local vlist "logpop pop immig_flow emig_flow births" local vlist "pop" foreach x of local vlist { if "`x'" == "pop" { local title "Change in population" } else if "`x'" == "logpop" { local title "Log change in population" } else if "`x'" == "immig_flow" { local title "In-migrations (cumulative over)" } else if "`x'" == "emig_flow" { local title "Out-migrations (cumulative over)" } else { local title "Births (cumulative over)" } ***************************************************** * 3. We focus here on solar (`energy' = 1) ***************************************************** if `energy' == 1 { eststo clear forvalues j = 1 / 3 { if `inst' == 1 { if `j' == 1 { local kreg "cumpower_solar_norm_06_22" } else if `j' == 2 { local kreg "cumpower_solar_norm_06_14" } else { local kreg "cumpower_solar_norm_18_22" } local iv "com_sensitivity_solar_1" local labtitle "envsustain_above_one" } else if `inst' == 2 { if `j' == 1 { local kreg "cumpower_solar_norm_06_22" } else if `j' == 2 { local kreg "cumpower_solar_norm_06_14" } else { local kreg "cumpower_solar_norm_18_22" } local iv "com_pv_potential" local labtitle "solar_potential" } else if `inst' == 3 { if `j' == 1 { local kreg "cumpower_solar_norm_06_22" } else if `j' == 2 { local kreg "cumpower_solar_norm_06_14" } else { local kreg "cumpower_solar_norm_18_22" } local iv "inter_solar_above_1" local labtitle "interaction" } else { if `j' == 1 { local kreg "cumpower_solar_norm_06_22" } else if `j' == 2 { local kreg "cumpower_solar_norm_06_14" } else { local kreg "cumpower_solar_norm_18_22" } local iv "inter_solar_above_1 com_pv_potential com_sensitivity_solar_1" local labtitle "envsust_solar_pot_inter" } local labels_kreg "" foreach l of local kreg { local lab : variable label `l' local labels_kreg `" `labels_kreg' "`lab'" "' } local lab2 : variable label `x'_00_03 local labels `" `labels' "`lab2'" "' local num_iv: word count `iv' local num_kreg: word count `kreg' local predictors = `num_iv'+`num_kreg' - 1 // we take out the constant matrix est_00_03_1 = J(2,`predictors',.) matrix est_04_22_1 = J(2,`predictors',.) matrix est_04_22_2 = J(2,`predictors',.) matrix est_04_22_3 = J(2,`predictors',.) matrix est_04_14_1 = J(2,`predictors',.) matrix est_04_14_2 = J(2,`predictors',.) matrix est_04_14_3 = J(2,`predictors',.) matrix est_16_22_1 = J(2,`predictors',.) matrix est_16_22_2 = J(2,`predictors',.) matrix est_16_22_3 = J(2,`predictors',.) matrix pv_00_03_1 = J(1,`predictors',.) matrix pv_04_22_1 = J(1,`predictors',.) matrix pv_04_22_2 = J(1,`predictors',.) matrix pv_04_22_3 = J(1,`predictors',.) matrix pv_04_14_1 = J(1,`predictors',.) matrix pv_04_14_2 = J(1,`predictors',.) matrix pv_04_14_3 = J(1,`predictors',.) matrix pv_16_22_1 = J(1,`predictors',.) matrix pv_16_22_2 = J(1,`predictors',.) matrix pv_16_22_3 = J(1,`predictors',.) ***************************************************** * 4. First stage regressions for solar ***************************************************** // First stage regressions matrix fs_s_1 = J(2, `predictors', .) matrix fs_s_2 = J(2, `predictors', .) matrix fs_s_3 = J(2, `predictors', .) matrix pv_fs_s_1 = J(1, `predictors', .) matrix pv_fs_s_2 = J(1, `predictors', .) matrix pv_fs_s_3 = J(1, `predictors', .) local labels_iv "" foreach l of local iv { local lab : variable label `l' local labels_iv `" `labels_iv' "`lab'" "' } foreach var of local kreg { log using "AM/Estimates/Tables/Long_term_effects/IV/First_stage/FS_comlevel_solar_`var'_iv`inst'.txt", replace text // No controls reg `var' `iv', r forvalues i = 1 / `predictors' { matrix fs_s_1[1, `i'] = e(b)[1, `i'] matrix fs_s_1[2, `i'] = r(table)[2, `i'] matrix pv_fs_s_1[1, `i'] = r(table)[4, `i'] } reg `var' `iv' `x'_00_03, r forvalues i = 1 / `predictors' { matrix fs_s_2[1, `i'] = e(b)[1, `i'] matrix fs_s_2[2, `i'] = r(table)[2, `i'] matrix pv_fs_s_2[1, `i'] = r(table)[4, `i'] } // Control for pre-trend and province FE reg `var' `iv' `x'_00_03 i.prov_code, r forvalues i = 1 / `predictors' { matrix fs_s_3[1, `i'] = e(b)[1, `i'] matrix fs_s_3[2, `i'] = r(table)[2, `i'] matrix pv_fs_s_3[1, `i'] = r(table)[4, `i'] } log close } matrix colnames fs_s_1 = `labels_iv' matrix colnames fs_s_2 = `labels_iv' matrix colnames fs_s_3 = `labels_iv' matrix colnames pv_fs_s_1 = `labels_iv' matrix colnames pv_fs_s_2 = `labels_iv' matrix colnames pv_fs_s_3 = `labels_iv' estadd matrix fs_s_1 = fs_s_1, replace estadd matrix fs_s_2 = fs_s_2, replace estadd matrix fs_s_3 = fs_s_3, replace estadd matrix pv_fs_s_1 = pv_fs_s_1, replace estadd matrix pv_fs_s_2 = pv_fs_s_2, replace estadd matrix pv_fs_s_3 = pv_fs_s_3, replace if `j' == 1 { local ivpre1 "\begin{tabularx}{\textwidth}{X c c c}" local ivpre2 "\toprule" local ivpost1 " & \multicolumn{3}{c}{Change in solar} \\" local ivpost2 "\cmidrule(lr){2-4}" local ivpost3 "& (1) & (2) & (3) \\ " local ivpost4 "\midrule " local ivpost5 "\multicolumn{4}{l}{\textit{Panel A. 1st and 2nd wave: 2006-2022}} \\" local ivpost6 "Pre-trend & & \checkmark & \checkmark \\" local ivpost7 "Province FE & & & \checkmark \\" esttab using "AM/Estimates/Tables/Long_term_effects/IV/First_stage/2solar_First_stage_comlevel_`labtitle'.tex", cells("fs_s_1[1](fmt(2) star pvalue(pv_fs_s_1)) fs_s_2[1](fmt(2) star pvalue(pv_fs_s_2)) fs_s_3[1](fmt(2) star pvalue(pv_fs_s_3))" /// "fs_s_1[2](fmt(2) par) fs_s_2[2](fmt(2) par) fs_s_3[2](fmt(2) par)") replace varwidth(40) starlevels(* 0.10 ** 0.05 *** 0.01) noobs nodepvars nonumbers nomtitles nolines /// collabels(none) prehead("{" "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" "\renewcommand{\arraystretch}{1.2}" "\setlength{\tabcolsep}{6pt}" "`ivpre1'" "`ivpre2'") posthead("`ivpost1'" "`ivpost2'" "`ivpost3'" "`ivpost4'" "`ivpost5'") prefoot("`ivpost6'" "`ivpost7'") postfoot(" \\") substitute("Envir" "Environmental") } else if `j' == 2 { local ivpost8 "\multicolumn{4}{l}{\textit{Panel B. 1st wave: 2006-2014}} \\" esttab using "AM/Estimates/Tables/Long_term_effects/IV/First_stage/2solar_First_stage_comlevel_`labtitle'.tex", cells("fs_s_1[1](fmt(2) star pvalue(pv_fs_s_1)) fs_s_2[1](fmt(2) star pvalue(pv_fs_s_2)) fs_s_3[1](fmt(2) star pvalue(pv_fs_s_3))" /// "fs_s_1[2](fmt(2) par) fs_s_2[2](fmt(2) par) fs_s_3[2](fmt(2) par)") append varwidth(40) starlevels(* 0.10 ** 0.05 *** 0.01) noobs nodepvars nonumbers nomtitles nolines /// collabels(none) prehead("") posthead("`ivpost8'") prefoot("`ivpost6'" "`ivpost7'") postfoot(" \\") substitute("Envir" "Environmental") } else { local ivpost9 "\multicolumn{4}{l}{\textit{Panel C. 2nd wave: 2018-2022}} \\" esttab using "AM/Estimates/Tables/Long_term_effects/IV/First_stage/2solar_First_stage_comlevel_`labtitle'.tex", cells("fs_s_1[1](fmt(2) star pvalue(pv_fs_s_1)) fs_s_2[1](fmt(2) star pvalue(pv_fs_s_2)) fs_s_3[1](fmt(2) star pvalue(pv_fs_s_3))" /// "fs_s_1[2](fmt(2) par) fs_s_2[2](fmt(2) par) fs_s_3[2](fmt(2) par)") append varwidth(40) starlevels(* 0.10 ** 0.05 *** 0.01) noobs nodepvars nonumbers nomtitles nolines /// collabels(none) prehead("") posthead("`ivpost9'") prefoot("`ivpost6'" "`ivpost7'") postfoot("\hline" "\end{tabularx}" "\vspace{0.5em}" "\noindent" "{\footnotesize{\textit{Notes.}} \sym{*} p$<0.10$, \sym{**} p$<0.05$, \sym{***} p$<0.01$}" "}") substitute("Envir" "Environmental") } ***************************************************** * 5. Second stage regressions (2sls) for solar ***************************************************** // Second stage regressions // Change 2000-2003 ivregress 2sls `x'_00_03 (`kreg' = `iv'), r forvalues i = 1 / `predictors' { matrix est_00_03_1[1, `i'] = e(b)[1, `i'] matrix est_00_03_1[2, `i'] = r(table)[2, `i'] matrix pv_00_03_1[1, `i'] = r(table)[4, `i'] } // Change 2004-2022, no controls forvalues i = 1 / `predictors' { ivregress 2sls `x'_04_22 (`kreg' = `iv'), r matrix est_04_22_1[1, `i'] = e(b)[1, `i'] matrix est_04_22_2[2, `i'] = r(table)[2, `i'] matrix pv_04_22_1[1, `i'] = r(table)[4, `i'] } // Change 2004-2022, pre-trend ivregress 2sls `x'_04_22 (`kreg' = `iv') `x'_00_03, r forvalues i = 1 / `predictors' { matrix est_04_22_2[1, `i'] = e(b)[1, `i'] matrix est_04_22_2[2, `i'] = r(table)[2, `i'] matrix pv_04_22_2[1, `i'] = r(table)[4, `i'] } // Change 2004-2022, pre-trend + province FE ivregress 2sls `x'_04_22 (`kreg' = `iv') `x'_00_03 i.prov_code, r forvalues i = 1 / `predictors' { matrix est_04_22_3[1, `i'] = e(b)[1, `i'] matrix est_04_22_3[2, `i'] = r(table)[2, `i'] matrix pv_04_22_3[1, `i'] = r(table)[4, `i'] } // Change 2004-2014, no controls ivregress 2sls `x'_04_14 (`kreg' = `iv'), r forvalues i = 1 / `predictors' { matrix est_04_14_1[1, `i'] = e(b)[1, `i'] matrix est_04_14_1[2, `i'] = r(table)[2, `i'] matrix pv_04_14_1[1, `i'] = r(table)[4, `i'] } // Change 2004-2014, pre-trend ivregress 2sls `x'_04_14 (`kreg' = `iv') `x'_00_03, r forvalues i = 1 / `predictors' { matrix est_04_14_2[1, `i'] = e(b)[1, `i'] matrix est_04_14_2[2, `i'] = r(table)[2, `i'] matrix pv_04_14_2[1, `i'] = r(table)[4, `i'] } // Change 2004-2014, pre-trend + province FE ivregress 2sls `x'_04_14 (`kreg' = `iv') `x'_00_03 i.prov_code, r forvalues i = 1 / `predictors' { matrix est_04_14_3[1, `i'] = e(b)[1, `i'] matrix est_04_14_3[2, `i'] = r(table)[2, `i'] matrix pv_04_14_3[1, `i'] = r(table)[4, `i'] } // Change 2016-2022, no controls ivregress 2sls `x'_16_22 (`kreg' = `iv'), r forvalues i = 1 / `predictors' { matrix est_16_22_1[1, `i'] = e(b)[1, `i'] matrix est_16_22_1[2, `i'] = r(table)[2, `i'] matrix pv_16_22_1[1, `i'] = r(table)[4, `i'] } // Change 2016-2022, pre-trend ivregress 2sls `x'_16_22 (`kreg' = `iv') `x'_00_03, r forvalues i = 1 / `predictors' { matrix est_16_22_2[1, `i'] = e(b)[1, `i'] matrix est_16_22_2[2, `i'] = r(table)[2, `i'] matrix pv_16_22_2[1, `i'] = r(table)[4, `i'] } // Change 2016-2022, pre-trend + province FE ivregress 2sls `x'_16_22 (`kreg' = `iv') `x'_00_03 i.prov_code, r forvalues i = 1 / `predictors' { matrix est_16_22_3[1, `i'] = e(b)[1, `i'] matrix est_16_22_3[2, `i'] = r(table)[2, `i'] matrix pv_16_22_3[1, `i'] = r(table)[4, `i'] } matrix colnames est_00_03_1 = `labels_kreg' matrix colnames est_04_22_1 = `labels_kreg' matrix colnames est_04_22_2 = `labels_kreg' matrix colnames est_04_22_3 = `labels_kreg' matrix colnames est_04_14_1 = `labels_kreg' matrix colnames est_04_14_2 = `labels_kreg' matrix colnames est_04_14_3 = `labels_kreg' matrix colnames est_16_22_1 = `labels_kreg' matrix colnames est_16_22_2 = `labels_kreg' matrix colnames est_16_22_3 = `labels_kreg' matrix colnames pv_00_03_1 = `labels_kreg' matrix colnames pv_04_22_1 = `labels_kreg' matrix colnames pv_04_22_2 = `labels_kreg' matrix colnames pv_04_22_3 = `labels_kreg' matrix colnames pv_04_14_1 = `labels_kreg' matrix colnames pv_04_14_2 = `labels_kreg' matrix colnames pv_04_14_3 = `labels_kreg' matrix colnames pv_16_22_1 = `labels_kreg' matrix colnames pv_16_22_2 = `labels_kreg' matrix colnames pv_16_22_3 = `labels_kreg' estadd matrix est_00_03_1 = est_00_03_1, replace estadd matrix est_04_22_1 = est_04_22_1, replace estadd matrix est_04_22_2 = est_04_22_2, replace estadd matrix est_04_22_3 = est_04_22_3, replace estadd matrix est_04_14_1 = est_04_14_1, replace estadd matrix est_04_14_2 = est_04_14_2, replace estadd matrix est_04_14_3 = est_04_14_3, replace estadd matrix est_16_22_1 = est_16_22_1, replace estadd matrix est_16_22_2 = est_16_22_2, replace estadd matrix est_16_22_3 = est_16_22_3, replace estadd matrix pv_00_03_1 = pv_00_03_1, replace estadd matrix pv_04_22_1 = pv_04_22_1, replace estadd matrix pv_04_22_2 = pv_04_22_2, replace estadd matrix pv_04_22_3 = pv_04_22_3, replace estadd matrix pv_04_14_1 = pv_04_14_1, replace estadd matrix pv_04_14_2 = pv_04_14_2, replace estadd matrix pv_04_14_3 = pv_04_14_3, replace estadd matrix pv_16_22_1 = pv_16_22_1, replace estadd matrix pv_16_22_2 = pv_16_22_2, replace estadd matrix pv_16_22_3 = pv_16_22_3, replace // Output if `j' == 1 { local pre1 "\begin{tabularx}{\linewidth}{p{5cm}cp{0.5cm}p{2cm}p{2cm}p{2cm}p{0.5cm}p{2cm}p{2cm}p{2cm}p{0.5cm}p{2cm}p{2cm}p{2cm}}" local pre2 "\hline" local post1 "`title' & 2000-2003 & & \multicolumn{3}{c}{2004-2022} & & \multicolumn{3}{c}{2004-2014} & & \multicolumn{3}{c}{2016-2022} \\" local post2 "\cline{2-2} \cline{4-6} \cline{8-10} \cline{12-14}" local post3 "& (1) & & (2) & (3) & (4) & & (5) & (6) & (7) & & (8) & (9) & (10) \\ \hline" local post4 "\multicolumn{14}{l}{\textit{Panel A. 1st and 2nd wave: 2006-2022}} \\" local post5 "Province FE & & & & & \checkmark & & & & & & & & \\" esttab using "AM/Estimates/Tables/Long_term_effects/IV/2lte_solar_IV_comlevel_`x'_`labtitle'.tex", cells("est_00_03_1[1](fmt(2) star pvalue(pv_00_03_1)) . est_04_22_1[1](fmt(2) star pvalue(pv_04_22_1)) est_04_22_2[1](fmt(2) star pvalue(pv_04_22_2)) est_04_22_3[1](fmt(2) star pvalue(pv_04_22_3)) . . . . . . . ." /// "est_00_03_1[2](fmt(2) par) . est_04_22_1[2](fmt(2) par) est_04_22_2[2](fmt(2) par) est_04_22_3[2](fmt(2) par) . . . . . . . .") replace varwidth(40) starlevels(* 0.10 ** 0.05 *** 0.01) noobs nodepvars nonumbers nomtitles nolines /// collabels(none) prehead("{" "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" "`pre1'" "`pre2'") posthead("`post1'" "`post2'" "`post3'" "`post4'") prefoot("`post5'") postfoot(" \\") substitute("AAA" "06-22" " . " " " " (.) " " ") } else if `j' == 2 { local post6 "\multicolumn{14}{l}{\textit{Panel B. 1st wave: 2006-2014}} \\" local post7 "Province FE & & & & & & & & & \checkmark & & & & \\" esttab using "AM/Estimates/Tables/Long_term_effects/IV/2lte_solar_IV_comlevel_`x'_`labtitle'.tex", cells("est_00_03_1[1](fmt(2) star pvalue(pv_00_03_1)) . . . . . est_04_14_1[1](fmt(2) star pvalue(pv_04_14_1)) est_04_14_2[1](fmt(2) star pvalue(pv_04_14_2)) est_04_14_3[1](fmt(2) star pvalue(pv_04_14_3)) . . . ." /// "est_00_03_1[2](fmt(2) par) . . . . . est_04_14_1[2](fmt(2) par) est_04_14_2[2](fmt(2) par) est_04_14_3[2](fmt(2) par) . . . .") append varwidth(40) starlevels(* 0.10 ** 0.05 *** 0.01) noobs nodepvars nonumbers nomtitles nolines /// collabels(none) prehead("") posthead("`post6'") prefoot("`post7'") postfoot(" \\") substitute("BBB" "06-14" " . " " " " (.) " " ") } else { local post8 "\multicolumn{14}{l}{\textit{Panel C. 2nd wave: 2018-2022}} \\" local post9 "Province FE & & & & & & & & & & & & & \checkmark \\" esttab using "AM/Estimates/Tables/Long_term_effects/IV/2lte_solar_IV_comlevel_`x'_`labtitle'.tex", cells("est_00_03_1[1](fmt(2) star pvalue(pv_00_03_1)) . . . . . . . . . est_16_22_1[1](fmt(2) star pvalue(pv_16_22_1)) est_16_22_2[1](fmt(2) star pvalue(pv_16_22_2)) est_16_22_3[1](fmt(2) star pvalue(pv_16_22_3))" /// "est_00_03_1[2](fmt(2) par) . . . . . . . . . est_16_22_1[2](fmt(2) par) est_16_22_2[2](fmt(2) par) est_16_22_3[2](fmt(2) par)") append varwidth(40) starlevels(* 0.10 ** 0.05 *** 0.01) noobs nodepvars nonumbers nomtitles nolines /// collabels(none) prehead("") posthead("`post8'") prefoot("`post9'") postfoot("\hline" "\end{tabularx}" "}") substitute("CCC" "18-22" " . " " " " (.) " " ") } } } }
Comment