
//****Deleted all the computer pathways for anonymity purposes**** 
cd 

//ssc install wbopendata
//ssc install estout

wbopendata, indicator("NY.GDP.PCAP.PP.CD; NY.GDP.MKTP.CD; FR.INR.RINR") long clear
ren countryname Country
ren year Year
save data_wb, replace
 
import excel "GlobalAidData-ChineseLoans_v2.xlsx", sheet("Global_CDF2.0 (hidden)") firstrow clear
keep Recipient CommitmentYear Maturity InterestRate GracePeriod
collapse (mean) Maturity InterestRate GracePeriod, by(Recipient CommitmentYear)
ren Recipient Country
ren CommitmentYear Year
save maturity, replace

* load and clean regime data
import excel "Bjørnskov-Rode-integrated-dataset-v3.2.xlsx", sheet("Regime characteristics") firstrow clear
keep country countryisocode year DDregime DDcategory
ren country Country
ren year Year

replace Country = "Virgin Islands (U.S.)" if Country == "US Virgin Islands"
replace Country = "Viet Nam" if Country == "Vietnam"
replace Country = "Turks and Caicos Islands" if Country == "Turks and Caicos"
replace Country = "Trinidad and Tobago" if Country == "Trinidad &Tobago"
replace Country = "Syrian Arab Republic" if Country == "Syria"
replace Country = "St. Vincent and the Grenadines" if Country == "St.Vincent & Grenadines"
replace Country = "St. Kitts and Nevis" if Country == "St. Kitts & Nevis"
replace Country = "Sint Maarten (Dutch part)" if Country == "Sint Maarten"
replace Country = "Saint Lucia" if Country == "St. Lucia"
replace Country = "Micronesia" if Country == "Micronesia, Fed. Sts."
replace Country = "Lao People's Democratic Republic" if Country == "Laos"
replace Country = "Kyrgyz Republic" if Country == "Kyrgyzstan"
replace Country = "Gambia" if Country == "Gambia, The"
replace Country = "Eswatini" if Country == "Swaziland"
replace Country = "Democratic Republic of the Congo" if Country == "Congo, Dem. Rep."
replace Country = "Congo" if Country == "Congo, Republic of"
replace Country = "Cabo Verde" if Country == "Cape Verde"
replace Country = "Brunei Darussalam" if Country == "Brunei"
replace Country = "American Samoa" if Country == "Samoa"

* limit the year span to 2000-2017
keep if inrange(Year,2000,2017)
save data_regime, replace

*-------------------------------------------------------------------------------
* clean main data
import excel "Dataset.xlsx", sheet("Sheet1") clear firstrow

* limit the year span to 2000-2017
keep if inrange(Year,2000,2017)

* merge in regime data
merge 1:1 Country Year using data_regime
tabulate Country _merge 
keep if _merge == 3
drop _merge

merge 1:1 Country Year using maturity
drop if _merge == 2
drop _merge

merge 1:1 Country Year using data_wb
drop if _merge == 2
drop _merge

* year prior to election dummy
replace IfLoanPriorYeartoElection = "" if IfLoanPriorYeartoElection == "Boundary"
destring IfLoanPriorYeartoElection, replace

generate PriorDum = !missing(IfLoanPriorYeartoElection)

* year of election dummy
replace IfLoanYearofElection = "" if IfLoanYearofElection == "Boundary"
destring IfLoanYearofElection, replace

generate YearOfDum = !missing(IfLoanYearofElection)

* either of the two 
egen IfEitherYearDum = rowmax(PriorDum YearOfDum)

* was there an election
destring Wasthereanelection, replace

* save the data
save data_clean_2, replace


gen 	logSumLoans = log(SUMIFLoanAmt)
replace logSumLoans=0 if logSumLoans==.


* sum of loans scaled by gdp
g loans_scaled = SUMIFLoanAmt / ny_gdp_mktp_cd
winsor2 loans_scaled, cuts(0 99)

encode incomelevelname, g(income)
g ln_gdp_pcap = ln(ny_gdp_pcap_pp_cd)
gen logSumLoans_scaled = log(loans_scaled)

save 

clear
import excel firstrow
reshape long Imports Exports, i( Country ) j(Year)
merge 1:1 Country Year using 
drop if _merge==1
encode Country, gen(Country_Code)
tsset Country_Code Year, yearly
gen log_Exports=ln(Exports)
gen log_Imports=ln(Imports)
winsor2 ofDeals , cuts(0 99)
drop _merge 

save, replace

clear 
import excel , sheet("Sheet1") firstrow clear
destring Corruption2000- Voice2017 , replace
reshape long Corruption Voice, i( Country ) j(Year)
sum Corruption Voice
merge 1:1 Country Year using 
drop _merge  
gen Interaction= IfEitherYearDum*Voice
gen GDP_Per_Capita=exp(ln_gdp_pcap)
gen Loan_Amount_Million=SUMIFLoanAmt/1000000
save , replace 


use 
collapse (sum) ofDeals_w ofDeals , by(Year)
twoway (connected ofDeals_w Year)
graph save "Graph" 

clear
use 
collapse loans_scaled loans_scaled_w, by(Year)
twoway (connected loans_scaled_w Year)
graph save "Graph" 

clear
use 
graph bar (mean) loans_scaled_w, over(IfEitherYearDum)
graph bar (mean) Loan_Amount_Million, over(IfEitherYearDum)
graph save "Graph" 
graph bar (mean) ofDeals, over(IfEitherYearDum)
graph save "Graph" 

graph bar (mean) Loan_Amount_Million, over(DDregime)

clear
use 
keep if IfEitherYearDum==1
collapse ofDeals_w Voice, by( Year)
graph twoway (lfit ofDeals_w Voice) (scatter ofDeals_w Voice)
graph save 

clear
use 
keep if IfEitherYearDum==0
collapse ofDeals_w Voice, by( Year)
graph twoway (lfit ofDeals_w Voice) (scatter ofDeals_w Voice)

graph save
gr combine 
graph save 


clear
use 
keep if IfEitherYearDum==1
collapse loans_scaled_w Voice, by( Year)
graph twoway (lfit loans_scaled_w Voice) (scatter loans_scaled_w Voice)
graph save "Graph" 

clear
use 
keep if IfEitherYearDum==0
collapse loans_scaled_w Voice, by( Year)
graph twoway (lfit loans_scaled_w Voice) (scatter loans_scaled_w Voice)
graph save "Graph"

gr combine 
graph save "Graph"

clear
use 
xtreg ofDeals_w IfEitherYearDum i.DDregime Interaction log_Exports log_Imports ln_gdp_pcap  Voice , fe 
estimate store FE1
xtreg ofDeals_w IfEitherYearDum i.DDregime Interaction log_Exports log_Imports ln_gdp_pcap  Voice ,re 
estimate store RE1
hausman  RE1 FE1
**Fixed Effect Model 

xtreg loans_scaled_w IfEitherYearDum i.DDregime Interaction log_Exports log_Imports ln_gdp_pcap  Voice , fe 
estimate store FE2
xtreg loans_scaled_w IfEitherYearDum i.DDregime Interaction log_Exports log_Imports ln_gdp_pcap  Voice , re 
estimate store RE2
hausman FE2 RE2
**Fixed Effect Model 

xtreg ofDeals_w IfEitherYearDum i.DDregime log_Exports log_Imports ln_gdp_pcap , fe cluster(Country_Code)
outreg2 using myreg.doc, replace ctitle(Model 1)
xtreg loans_scaled_w IfEitherYearDum i.DDregime log_Exports log_Imports ln_gdp_pcap , fe cluster(Country_Code)
outreg2 using myreg.doc, append ctitle(Model 2)


***Final Models**************
xtreg ofDeals_w IfEitherYearDum  , fe cluster(Country_Code)
outreg2 using myreg.doc, replace ctitle(Model 1)
xtreg ofDeals_w IfEitherYearDum  ln_gdp_pcap , fe cluster(Country_Code)
outreg2 using myreg.doc, append ctitle(Model 2)
xtreg ofDeals_w IfEitherYearDum  ln_gdp_pcap log_Exports log_Imports  , fe cluster(Country_Code)
outreg2 using myreg.doc, append ctitle(Model 3)
xtreg ofDeals_w IfEitherYearDum  ln_gdp_pcap log_Exports log_Imports  i.DDregime  Voice , fe cluster(Country_Code)
outreg2 using myreg.doc, append ctitle(Model 4)
xtreg ofDeals_w IfEitherYearDum  ln_gdp_pcap log_Exports log_Imports  i.DDregime Voice Interaction , fe cluster(Country_Code)
outreg2 using myreg.doc, append ctitle(Model 5)

xtreg loans_scaled_w IfEitherYearDum  , fe robust
outreg2 using myreg.doc, replace ctitle(Model 1)
xtreg loans_scaled_w IfEitherYearDum  ln_gdp_pcap , fe robust
outreg2 using myreg.doc, append ctitle(Model 2)
xtreg loans_scaled_w IfEitherYearDum  ln_gdp_pcap log_Exports log_Imports  , fe robust
outreg2 using myreg.doc, append ctitle(Model 3)
xtreg loans_scaled_w IfEitherYearDum  ln_gdp_pcap log_Exports log_Imports  i.DDregime  Voice , fe robust
outreg2 using myreg.doc, append ctitle(Model 4)
xtreg loans_scaled_w IfEitherYearDum  ln_gdp_pcap log_Exports log_Imports  i.DDregime Voice Interaction , fe robust
outreg2 using myreg.doc, append ctitle(Model 5)

**Dignostics***

xtserial ofDeals_w IfEitherYearDum  ln_gdp_pcap log_Exports log_Imports  DDregime Voice Interaction 
xtreg ofDeals_w IfEitherYearDum  ln_gdp_pcap log_Exports log_Imports  i.DDregime Voice Interaction , fe 
xttest3
xtserial loans_scaled_w IfEitherYearDum  ln_gdp_pcap log_Exports log_Imports  DDregime Voice  
xtreg loans_scaled_w IfEitherYearDum  ln_gdp_pcap log_Exports log_Imports  i.DDregime Voice Interaction , fe 
xttest3

****Robustness Check
xtreg ofDeals_w IfEitherYearDum  GDP_Per_Capita Exports Imports  i.DDregime  Voice , fe robust
outreg2 using myreg.doc, replace ctitle(Model Number of Deals)
xtreg loans_scaled_w IfEitherYearDum  GDP_Per_Capita Exports Imports  i.DDregime  Voice , fe robust
outreg2 using myreg.doc, append ctitle(Model Loan Amounts)




