version 9.0
drop _all
clear
capture log close
set memory 80m
set matsize 800
set more off
#delimit ;

*************************************************************************;
**** SET OUTPUT FILE ****;
*************************************************************************;

log using "Q:\DATA\PR\FundCredit\outputSPOCT.smcl", replace;

*************************************************************************;
**** IMPORT DATA ****;
*************************************************************************;

insheet using "Q:\DATA\PR\FundCredit\arrangementsnew.txt";
drop if repeat==1;	* This is to drop COMBINED programs (SBA and EFF). *;
				* sort ifscode year *;

gen newbegdate=date(begdate,"mdy");
gen neworigexpdate=date(origexpdate,"mdy");
gen duration=(neworigexpdate-newbegdate)/365;

sort ifscode year;
save "Q:\DATA\PR\FundCredit\arrangements.dta", replace;

clear;

*************************************************************************;
**** IMPORT DATA ****;
*************************************************************************;

insheet using "Q:\DATA\PR\FundCredit\icrg.txt";
sort ifscode year;
save "Q:\DATA\PR\FundCredit\icrg.dta", replace;
clear;

insheet using "Q:\DATA\PR\FundCredit\ctrydata.txt";
drop if ifscode==353;
sort ifscode year;
save "Q:\DATA\PR\FundCredit\ctrydata.dta", replace;
clear;

*************************************************************************;
**** IMPORT AND DE-TREND GLOBAL DATA ****;
*************************************************************************;

insheet using "Q:\DATA\PR\FundCredit\globaldata.txt";

*** Oil ***;

gen oilp=(oildubai+oiluk)/2;
gen oilpreal=oilp*100/muv;

gen logoil=log(oilpreal);
regress logoil time;
predict logoilhat, xb;
gen devoil=logoil-logoilhat;

*** Interest Rates ***;

gen itermstr=gbond3-gbond10;

gen inflation=((uscpi[_n]/uscpi[_n-1])-1)*100;

gen reali=(((1+(ffrate)/100)/(1+(inflation/100)))-1)*100;

gen realicb10=(((1+(cbond10)/100)/(1+(inflation/100)))-1)*100;

gen realigb10=(((1+(gbond10)/100)/(1+(inflation/100)))-1)*100;
gen realigb3=(((1+(gbond3)/100)/(1+(inflation/100)))-1)*100;
gen realiprime=(((1+(prime)/100)/(1+(inflation/100)))-1)*100;

*** World Real GDP Growth ***;

gen logg7rgdp=log(g7rgdp);
regress logg7rgdp time;
predict logg7rgdphat, xb;
gen devg7rgdp=logg7rgdp-logg7rgdphat;

gen logworldrgdp=log(worldrgdp);
regress logworldrgdp time;
predict logworldrgdphat, xb;
gen devworldrgdp=logworldrgdp-logworldrgdphat;

*** De-Trend Using HP Filters ***;

tsset year;

gen diffhpoil=.;
hprescott oilpreal, stub(hp) smooth(100);
replace diffhpoil=hp_oilpreal_1;
gen hpoil=oilpreal-diffhpoil;
gen overvaloil=log(oilpreal/hpoil);

gen diffhpg7=.;
hprescott g7rgdp, stub(hp) smooth(100);
replace diffhpg7=hp_g7rgdp_1;
gen hpg7=g7rgdp-diffhpg7;
gen overvalg7=log(g7rgdp/hpg7);

gen diffhpworld=.;
hprescott worldrgdp, stub(hp) smooth(100);
replace diffhpworld=hp_worldrgdp_1;
gen hpworld=worldrgdp-diffhpworld;
gen overvalworld=log(worldrgdp/hpworld);

sort year;
save "Q:\DATA\PR\FundCredit\globaldata.dta", replace;
clear;

*************************************************************************;
**** IMPORT DATA ****;
*************************************************************************;

insheet using "Q:\DATA\PR\FundCredit\codes.txt";
sort ifscode;
save "Q:\DATA\PR\FundCredit\codes.dta", replace;
clear;

*************************************************************************;
**** MERGE DATA ****;
*************************************************************************;

use "Q:\DATA\PR\FundCredit\arrangements.dta";

merge ifscode year using "Q:\DATA\PR\FundCredit\ctrydata.dta";
tabulate _merge;
rename _merge _firstmerge;
sort year;

merge year using "Q:\DATA\PR\FundCredit\globaldata.dta";
tabulate _merge;
rename _merge _secondmerge;

sort ifscode;
merge ifscode using "Q:\DATA\PR\FundCredit\codes.dta";
tabulate _merge;
rename _merge _thirdmerge;

sort ifscode year;
merge ifscode year using "Q:\DATA\PR\FundCredit\icrg.dta";
tabulate _merge;
rename _merge _fourthmerge;

*************************************************************************;
**** DATA CLEAN-UP ****;
*************************************************************************;

drop if year<1970;
replace arrtypeid=0 if arrtypeid~=1 & arrtypeid==.;
drop if lic==.;
drop if lmic==.;
drop if umic==.;
drop if ohic==.;

sort ifscode year;

by ifscode (year) : gen cntind=_n;
by ifscode (year) : gen cnttot=_N;

drop if cnttot~=41;

egen groups=group(ifscode);
sort groups;
gen groupsn=groups[_N];

sort ifscode year;
destring quota, replace;

*************************************************************************;
**** DATA FOR THE BELOW COUNTRIES HAD NGDP AND NGDPDOLL SCREWED UP ****;
*************************************************************************;

replace ggbgdp=ggb*100/ngdp if (ifscode==218 | ifscode==534 | ifscode==694 | ifscode==853);
replace bmgdp=bm*100/ngdp if (ifscode==218 | ifscode==534 | ifscode==694 | ifscode==853);
replace dcgdp=dc*100/ngdp if (ifscode==218 | ifscode==534 | ifscode==694 | ifscode==853);
replace cuagdp=cuadoll*100/ngdpdoll if (ifscode==218 | ifscode==534 | ifscode==694 | ifscode==853);
replace tdebtgdp=tdebt*100/ngdpdoll if (ifscode==218 | ifscode==534 | ifscode==694 | ifscode==853);
replace stdebtgdp=stdebt*100/ngdpdoll if (ifscode==218 | ifscode==534 | ifscode==694 | ifscode==853);
replace pkfnetgdp=(pkf-fdi)*100/ngdpdoll if (ifscode==218 | ifscode==534 | ifscode==694 | ifscode==853);

*************************************************************************;
**** CREATE SBA DEPENDENT VARIABLE ****;
*************************************************************************;

gen sba=1 if arrtypeid==1 | arrtypeid==2;
replace sba=0 if sba~=1;

**** TREATING SOME COUNTRIES DIFFERENTLY ****;

* Brazil;
replace sba=0 if ifscode==223 & year==1998;
replace sba=1 if ifscode==223 & year==1999;

* Korea;
replace sba=0 if ifscode==542 & year==1997;
replace sba=1 if ifscode==542 & year==1998;

* Indonesia;
replace sba=0 if ifscode==536 & year==1997;

destring origamt, replace force;
gen origamt2=origamt;
* replace origamt=origamt/duration;

sort ifscode year;
by ifscode (year) : replace origamt=origamt[_n-1] if ifscode==223 & year==1999;
by ifscode (year) : replace origamt=origamt[_n-1] if ifscode==542 & year==1998;
by ifscode (year) : replace origamt=0 if ifscode==223 & year==1998;
by ifscode (year) : replace origamt=0 if ifscode==542 & year==1997;
by ifscode (year) : replace origamt=0 if ifscode==536 & year==1997;

**** PREVIOUS PROGRAM VARIABLES ****;

sort ifscode year;

by ifscode (year) : gen program1=sba[_n-1];
by ifscode (year) : gen program2=sba[_n-2];

by ifscode (year) : gen programtminus1d2=1 if sba[_n-2]==1 & 
duration[_n-2]>=2.5;
by ifscode (year) : replace programtminus1d2=0 if programtminus1d2~=1;

by ifscode (year) : gen programtminus1d1=1 if sba[_n-1]==1 & duration[_n-1]>=1.5;
by ifscode (year) : replace programtminus1d1=0 if programtminus1d1~=1;

destring prec, force replace;
replace prec=0 if prec~=1;

*************************************************************************;
**** CREATING EXCHANGE RATE OVERVALUATION SERIES FOR EACH COUNTRY ****;
*************************************************************************;

tsset groups time;
sort ifscode year;
gen diffhpreer=.;

by ifscode (year) : hprescott reer, stub(hp) smooth(100);

sort groups year;

local i=1;
while `i'<=groupsn {;
replace diffhpreer=hp_reer_`i' if groups==`i';
drop hp_reer_`i';
local i=`i'+1;
};

gen hpreer=reer-diffhpreer;
gen overvalreer=log(reer/hpreer);

sort ifscode year;
by ifscode (year) : gen overvalreerlag1=overvalreer[_n-1];
by ifscode (year) : gen overvalreerdif1=overvalreer[_n]-overvalreer[_n-1];

*************************************************************************;
**** GENERATING A VARIABLE THAT IDENTIFIES HIGH ACCESS CASES ****;
*************************************************************************;

gen origamtpercentq=origamt2/quota*100;
gen highaccess=1 if origamtpercentq>300 & origamtpercentq~=.;
replace highaccess=0 if highaccess~=1;

*************************************************************************;
**** DELETING COUNTRIES THAT NEVER HAD PROGRAMS ****;
*************************************************************************;

* This drops countries like Malaysia, which only had a BSFF (Buffer Stock
Fund Facility). *;

sort ifscode year;
by ifscode (year) : egen sbacnt=sum(sba);
by ifscode (year) : gen existsba=1 if sbacnt==0;

*************************************************************************;
**** TRANSFORMING DATA ****;
*************************************************************************;

*** Exchange Rate Regime ***;

by ifscode (year) : gen depr=((erate[_n]/erate[_n-1])-1)*100;
by ifscode (year) : gen fixerate=1 if depr==0;
by ifscode (year) : replace fixerate=0 if fixerate~=1;

*** Reserve Cover in Months ***;

destring fxres, replace;
by ifscode (year) : gen rescov=-12*(fxres/imp);

destring bm, replace;
by ifscode (year) : gen bmres=(bm/erate)/fxres;

*** Monetary Data Transformations ***;

by ifscode (year) : gen dcreal=(((1+(dcgr)/100)/(1+(cpiper/100)))-1)*100;
by ifscode (year) : gen bmreal=(((1+(bmgr)/100)/(1+(cpiper/100)))-1)*100;

*** Rex Transform for Inflation Rate ***;

by ifscode (year) : gen cpirex=100*(cpiper/100)/(1+(cpiper/100)) if cpiper>=0;
by ifscode (year) : replace cpirex=100*(cpiper/100)/(1-(cpiper/100)) if cpiper<0;

*** Other ***;

gen realitot=(((1+(ffrate)/100)/(1+(totgr/100)))-1)*100;

by ifscode (year) : gen pkfnetgdpL1=pkfnetgdp[_n-1];
by ifscode (year) : gen pkfnetgdpL2=pkfnetgdp[_n-2];
by ifscode (year) : gen pkfnetgdpave=(pkfnetgdp[_n]+pkfnetgdp[_n-1]+
pkfnetgdp[_n-2])/3 if pkfnetgdp~=. & pkfnetgdpL1~=. & pkfnetgdpL2~=.;
gen diffpkfnet=pkfnetgdp-pkfnetgdpave;

by ifscode (year) : gen cuagdpL1=cuagdp[_n-1];
by ifscode (year) : gen cuagdpL2=cuagdp[_n-2];
by ifscode (year) : gen cuagdpave=(cuagdp[_n]+cuagdp[_n-1]+
cuagdp[_n-2])/3 if cuagdp~=. & cuagdpL1~=. & cuagdpL2~=.;
gen diffcua=cuagdp-cuagdpave;

by ifscode (year) : gen ggbgdpL1=ggbgdp[_n-1];
by ifscode (year) : gen ggbgdpL2=ggbgdp[_n-2];
by ifscode (year) : gen ggbgdpave=(ggbgdp[_n]+ggbgdp[_n-1]+
ggbgdp[_n-2])/3 if ggbgdp~=. & ggbgdpL1~=. & ggbgdpL2~=.;
gen diffggb=ggbgdp-ggbgdpave;

by ifscode (year) : gen rgdpgrL1=rgdpgr[_n-1];
by ifscode (year) : gen rgdpgrL2=rgdpgr[_n-2];
by ifscode (year) : gen rgdpgrave=(rgdpgr[_n]+rgdpgr[_n-1]+
rgdpgr[_n-2])/3 if rgdpgr~=. & rgdpgrL1~=. & rgdpgrL2~=.;
gen diffrgdpgr=rgdpgr-rgdpgrave;

*** Generating GDP in SDRs ****;

gen sdrgdp=ngdpdoll*sdrerate;

*************************************************************************;
**** VOLATILITY INDICATORS ****;
*************************************************************************;

tsset ifscode year;
mvsumm devoil, generate(sddevoil) stat(sd) window(10) end force;
mvsumm itermstr, generate(sditermstr) stat(sd) window(10) end force; 
mvsumm reali, generate(sdreali) stat(sd) window(10) end force; 
mvsumm rgdpgr, generate(sdrgdpgr) stat(sd) window(10) end force; 
mvsumm cpirex, generate(sdcpirex) stat(sd) window(10) end force; 
mvsumm cpiper, generate(sdcpiper) stat(sd) window(10) end force; 
mvsumm cuagdp, generate(sdcuagdp) stat(sd) window(10) end force; 
mvsumm ggbgdp, generate(sdggbgdp) stat(sd) window(10) end force; 
mvsumm totgr, generate(sdtotgr) stat(sd) window(10) end force; 
mvsumm tprgdpgr, generate(sdtprgdpgr) stat(sd) window(10) end force; 

mvsumm devoil, generate(sddevoil5) stat(sd) window(5) end force;
mvsumm itermstr, generate(sditermstr5) stat(sd) window(5) end force; 
mvsumm reali, generate(sdreali5) stat(sd) window(5) end force; 
mvsumm rgdpgr, generate(sdrgdpgr5) stat(sd) window(5) end force; 
mvsumm cpirex, generate(sdcpirex5) stat(sd) window(5) end force; 
mvsumm cpiper, generate(sdcpiper5) stat(sd) window(5) end force; 
mvsumm cuagdp, generate(sdcuagdp5) stat(sd) window(5) end force; 
mvsumm ggbgdp, generate(sdggbgdp5) stat(sd) window(5) end force; 
mvsumm totgr, generate(sdtotgr5) stat(sd) window(5) end force; 
mvsumm tprgdpgr, generate(sdtprgdpgr5) stat(sd) window(5) end force; 

*************************************************************************;
**** OIL EXPORTERS ****;
*************************************************************************;

destring oiltb, replace;
by ifscode (year) : gen oilgdp=oiltb/ngdpdoll;

gen dummyoil=1 if oiltb>0;
replace dummyoil=0 if dummyoil~=1;

gen intdevoil=dummyoil*devoil;
gen lnquota=log(quota);
gen quotaembi=quota*embi;

gen isddevoil=sddevoil*dummyoil;

*************************************************************************;
**** LAGGED AND DIFFERENCE ****;
*************************************************************************;

by ifscode (year) : gen intdevoillag1=intdevoil[_n-1];
by ifscode (year) : gen devoillag1=devoil[_n-1];
by ifscode (year) : gen tprgdpgrlag1=tprgdpgr[_n-1];
by ifscode (year) : gen rescovlag1=rescov[_n-1];
by ifscode (year) : gen pkfnetgdplag1=pkfnetgdp[_n-1];
by ifscode (year) : gen pkfnetgdpavelag1=pkfnetgdpave[_n-1];
by ifscode (year) : gen bmreallag1=bmreal[_n-1];
by ifscode (year) : gen bmgdplag1=bmgdp[_n-1];
by ifscode (year) : gen rgdpgrlag1=rgdpgr[_n-1];
by ifscode (year) : gen realilag1=reali[_n-1];

by ifscode (year) : gen itermstrlag1=itermstr[_n-1];
by ifscode (year) : gen realigb10lag1=realigb10[_n-1];
by ifscode (year) : gen realigb3lag1=realigb3[_n-1];
by ifscode (year) : gen tdebtgdplag1=tdebtgdp[_n-1];
by ifscode (year) : gen cuagdplag1=cuagdp[_n-1];
by ifscode (year) : gen ggbgdplag1=ggbgdp[_n-1];
by ifscode (year) : gen cpirexlag1=cpirex[_n-1];
by ifscode (year) : gen totgrlag1=totgr[_n-1];
by ifscode (year) : gen stdreslag1=stdres[_n-1];
by ifscode (year) : gen isddevoillag1=isddevoil[_n-1]; 
by ifscode (year) : gen sddevoillag1=sddevoil[_n-1]; 
by ifscode (year) : gen sdrealilag1=sdreali[_n-1]; 
by ifscode (year) : gen sdtotgrlag1=sdtotgr[_n-1];
by ifscode (year) : gen dacctlag1=dacct[_n-1];

by ifscode (year) : gen intdevoildif1=intdevoil[_n]-intdevoil[_n-1];
by ifscode (year) : gen devoildif1=devoil[_n]-devoil[_n-1];
by ifscode (year) : gen tprgdpgrdif1=tprgdpgr[_n]-tprgdpgr[_n-1];
by ifscode (year) : gen rescovdif1=rescov[_n]-rescov[_n-1];
by ifscode (year) : gen pkfnetgdpdif1=pkfnetgdp[_n]-pkfnetgdp[_n-1];
by ifscode (year) : gen pkfnetgdpavedif1=pkfnetgdpave[_n]-pkfnetgdpave[_n-1];
by ifscode (year) : gen bmrealdif1=bmreal[_n]-bmreal[_n-1];
by ifscode (year) : gen bmgdpdif1=bmgdp[_n]-bmgdp[_n-1];
by ifscode (year) : gen rgdpgrdif1=rgdpgr[_n]-rgdpgr[_n-1];
by ifscode (year) : gen realidif1=reali[_n]-reali[_n-1];
by ifscode (year) : gen realigb10dif1=realigb10[_n]-realigb10[_n-1];
by ifscode (year) : gen realigb3dif1=realigb3[_n]-realigb3[_n-1];
by ifscode (year) : gen tdebtgdpdif1=tdebtgdp[_n]-tdebtgdp[_n-1];
by ifscode (year) : gen cuagdpdif1=cuagdp[_n]-cuagdp[_n-1];
by ifscode (year) : gen ggbgdpdif1=ggbgdp[_n]-ggbgdp[_n-1];
by ifscode (year) : gen cpirexdif1=cpirex[_n]-cpirex[_n-1];
by ifscode (year) : gen totgrdif1=totgr[_n]-totgr[_n-1];
by ifscode (year) : gen stdresdif1=stdres[_n]-stdres[_n-1];
by ifscode (year) : gen bmgdplagdif1=bmgdp[_n-1]-bmgdp[_n-2];
by ifscode (year) : gen cuagdplagdif1=cuagdp[_n-1]-cuagdp[_n-2];
by ifscode (year) : gen ggbgdplagdif1=ggbgdp[_n-1]-ggbgdp[_n-2];
by ifscode (year) : gen cpirexlagdif1=cpirex[_n-1]-cpirex[_n-2];

gen size=ngdpdoll/worldngdp;
sort ifscode year;
by ifscode (year) : gen sizelag1=size[_n-1];
by ifscode (year) : gen sizedif1=size[_n]-size[_n-1];

*************************************************************************;
**** DROPPING SOME DATA ****;
*************************************************************************;

*** Drops TEs ***;

drop if te==1 & year<1995;

*** Drops Countries That Never Had Program ***;

* EXISTSBA is defined in such way that if equal to 1, then it means that this country never had an SBA. *;

drop if existsba==1;

*** Dropping Problematic Data Countries ***;

* Dropping Ecuador before 1981 *;
drop if ifscode==248 & year<=1980;

* Dropping Equatorial Guinea Altogether *;
drop if ifscode==642;

*************************************************************************;
**** ESTIMATIONS ****;
*************************************************************************;

*** GENERATES EXISTENCE DUMMY ***;
*** THIS IS NEEDED TO KEEP A SAMPLE OF CONSTANT SIZE ***;

tsset groups time;

xi: logit sba program1 programtminus1d1 program2 programtminus1d2 l1.intdevoil l1.devoil l1.itermstr l1.tdebtgdp l1.rescov l1.pkfnetgdp l1.rgdpgr l1.cuagdp l1.ggbgdp l1.bmgdp l1.cpirex l1.stdres diffcua quota time if (ifscode~=819 | ifscode~=913 | ifscode~=965) & year<=2005, robust;

gen existsample=1 if e(sample);

count if sba==1 & existsample==1;

*** COUNTS PROGRAMS PER YEAR WITHIN THE SAMPLE ***;

sort year groups;
by year (groups) : egen sbayrcnt=sum(sba) if existsample==1;

*** LOGIT ESTIMATION ***;

tsset groups time;

xi: logit sba program1 programtminus1d1 program2 programtminus1d2 l1.intdevoil l1.devoil l1.itermstr l1.tdebtgdp l1.stdres l1.rescov l1.pkfnetgdp l1.rgdpgr l1.cuagdp l1.ggbgdp l1.bmgdp l1.cpirex if existsample==1, robust;

fitstat;
lstat, cutoff(0.196667);

predict prob1, p;
gen ppp1=1 if prob1>=0.196667 & prob1~=.;
replace ppp1=0 if ppp1~=1 & prob1~=.;
replace ppp1=. if year>=2006;

predict pred1 if existsample==1, xb;
gen IMR1=normden(pred1)/norm(pred1);

predict pred1special, xb;
gen IMR1special=normden(pred1special)/norm(pred1special);

gen PDF1=normden(pred1);
gen CDF1=norm(pred1);

gen quotaCDF=quota*CDF1;
gen tdebtgdpCDF=tdebtgdplag1*CDF1; 
gen diffcuaCDF=diffcua*CDF1; 
gen stdresCDF=stdreslag1*CDF1; 
gen timeCDF=time*CDF1;

replace origamt=0 if origamt==.;
gen origamtdoll=(origamt/1000)/sdrerate;
gen original=origamtdoll*100/ngdpdoll;
gen lnoriginal=log(1+original);
gen originalrex=original/(1+original);

*** ADDITIONAL DUMMIES TO DEAL WITH HIGH ACCESS CASES ***;

gen t95=1 if year>=1995;
replace t95=0 if t95~=1;
gen largedebt=1 if tdebtgdp>=40;
replace largedebt=0 if largedebt~=1;
gen large=1 if sdrgdp>=64;
replace large=0 if large~=1;
gen rexsba=sba*t95*largedebt*large if year>=1995;
replace rexsba=0 if rexsba~=1;

gen rexsbaCDF=rexsba*CDF1; 

*** ACCESS ESTIMATION ***;

tsset groups time;

xtgls lnoriginal quotaCDF tdebtgdpCDF diffcuaCDF stdresCDF PDF1 rexsba time if existsample==1, corr(ar1) p(h) force;

predict pred2 if e(sample), xb;

corr lnoriginal pred2 if e(sample);
di r(rho)^2;
gen resfull2=lnoriginal-pred2;
summarize pred2 resfull2 lnoriginal if resfull2~=.;

*************************************************************************;
*** Assuming Access with Probability Less Than 0.1966679 is Zero ***;
*************************************************************************;

gen origt1=exp(pred2);
gen origt2=(origt1-1)*ngdpdoll/100;
gen accessis=origt2*ppp1;
gen accessis2=origt2*ppp1;
gen accessis3=origt2*ppp1;
gen accessfsdris=accessis*sdrerate;
gen accessfis=accessis;

* Amounts in US$ *;
gen accessestis=accessfis if existsample==1;
replace accessestis=0 if existsample~=1;
gen accessactis=origamtdoll if existsample==1;
replace accessactis=0 if existsample~=1;

* Amounts in SDR *;
gen accessestsdris=accessfsdris if existsample==1;
replace accessestsdris=0 if existsample~=1;
gen accessactsdris=origamt/1000 if existsample==1;
replace accessactsdris=0 if existsample~=1;

sort year groups;
by year (groups) : egen accessestsdrsumis=sum(accessestsdris);
by year (groups) : egen accessactsdrsumis=sum(accessactsdris);

replace accessactsdrsumis=. if year>=2006;
tsline accessestsdrsumis accessactsdrsumis if existsample==1, lwidth(medthick);

*************************************************************************;
* LOGIT UP TO 2002 -----> FOR OUT OF SAMPLE EXERCISES *;
*************************************************************************;

tsset groups time;

xi: logit sba program1 programtminus1d1 program2 programtminus1d2 l1.intdevoil l1.devoil l1.itermstr l1.tdebtgdp l1.stdres l1.rescov l1.pkfnetgdp l1.rgdpgr l1.cuagdp l1.ggbgdp l1.bmgdp l1.cpirex if existsample==1 & year<=2002, robust;

fitstat;
lstat, cutoff(0.208092);

predict prob3 if existsample==1, p;
gen ppp3=1 if prob3>=0.208092 & prob3~=.;
replace ppp3=0 if ppp3~=1 & prob3~=.;

predict pred3, xb;
gen IMR3=normden(pred3)/norm(pred3);

gen PDF3=normden(pred3);
gen CDF3=norm(pred3);

gen quotaCDF3=quota*CDF3;
gen tdebtgdpCDF3=tdebtgdplag1*CDF3; 
gen diffcuaCDF3=diffcua*CDF3; 
gen stdresCDF3=stdreslag1*CDF3; 
gen timeCDF3=time*CDF3;

count if sba==1 & existsample==1 & year<=2002;

lstat, cutoff(0.208092);

count if prob3>0.208092 & sba==1 & (year>2002 & year<=2005) & existsample==1 & prob3~=.;
count if prob3<0.208092 & sba==1 & (year>2002 & year<=2005) & existsample==1 & prob3~=.;
count if prob3>0.208092 & sba~=1 & (year>2002 & year<=2005) & existsample==1 & prob3~=.;
count if prob3<0.208092 & sba~=1 & (year>2002 & year<=2005) & existsample==1 & prob3~=.;

list ctry year prob3 sba if prob3>0.208092 & sba==1 & (year>2002 & year<=2005) & existsample==1 & prob3~=.;
list ctry year prob3 sba if prob3<0.208092 & sba==1 & (year>2002 & year<=2005) & existsample==1 & prob3~=.;
list ctry year prob3 sba if prob3>0.208092 & sba~=1 & (year>2002 & year<=2005) & existsample==1 & prob3~=.;
list ctry year prob3 sba if prob3<0.208092 & sba~=1 & (year>2002 & year<=2005) & existsample==1 & prob3~=.;

gen rexsbaCDF3=rexsba*CDF3; 

xtgls lnoriginal quotaCDF3 tdebtgdpCDF3 diffcuaCDF3 stdresCDF3 PDF3 rexsba time if existsample==1, corr(ar1) p(h) force;

predict pred4, xb;

corr lnoriginal pred4 if e(sample);
di r(rho)^2;
gen resfull4=lnoriginal-pred4;
summarize pred4 resfull4 lnoriginal if resfull4~=.;

sort year (groups);
by year (groups) : egen sbayrcntoos=sum(ppp3) if prob3~=.;

gen origt1oos=exp(pred4);
gen origt2oos=(origt1oos-1)*ngdpdoll/100;

*************************************************************************;
*** Assuming Access with Probability Less Than 0.210 is Zero ***;
*************************************************************************;

gen accessoos=origt2oos*ppp1;
gen accessfsdroos=accessoos*sdrerate;
gen accessfoos=accessoos;

* Amounts in US$ *;
gen accessestoos=accessfoos if existsample==1;
replace accessestoos=0 if existsample~=1;
gen accessactoos=origamtdoll if existsample==1;
replace accessactoos=0 if existsample~=1;

* Amounts in SDR *;
gen accessestsdroos=accessfsdroos if existsample==1;
replace accessestsdroos=0 if existsample~=1;
gen accessactsdroos=origamt/1000 if existsample==1;
replace accessactsdroos=0 if existsample~=1;

sort year groups;
by year (groups) : egen accessestsdrsumoos=sum(accessestsdroos);
by year (groups) : egen accessactsdrsumoos=sum(accessactsdroos);

replace accessactsdrsumoos=. if year>=2006;
tsline accessestsdrsumoos accessactsdrsumoos, lwidth(medthick);

*************************************************************************;
**** Close Output Log ****;
*************************************************************************;

tsset groups time;
save "Q:\DATA\PR\FundCredit\finaldatabaseSPOCT.dta", replace;

sort ifscode year;
outsheet
ifscode year itermstr itermstrlag1
using "Q:\DATA\PR\FundCredit\iterm.txt", replace;

log close;
view "Q:\DATA\PR\FundCredit\outputSPOCT.smcl";
