*** ("###################################################################") 

*** "LIQUIDITY COINSURANCE AND BANK CAPITAL"
*** Fabio Castiglionesi - Fabio Feriozzi - Gyöngyi Lóránth - Loriana Pelizzon

*** PRINCIPAL COMPONENT ANALYSIS

*** Code developed by Mario Bellia (bellia@unive.it)

*** ("###################################################################") 



*This initial dataset includes also banks that does not report Liquid Asset (that are dropped
for the following analysis)


*** ("####################################") 
*** Table 3 – Principal component analysis****
*** ("####################################") 


use "/Research/SDI-FDIC Data/Files for JCMB/1_PCA_DATA.dta"
set matsize 5000
egen countquarter=group(closdate)

*Reshape
keep DepTA closdate idrssd 
reshape wide DepTA, i(closdate) j(idrssd)
egen countquarter=group(closdate)
save "/Research/SDI-FDIC Data/Files for JCMB/TS_DepTA.dta"

log using "/Research/SDI-FDIC Data/Files for JCMB/PCA_DepTA.txt",replace text
forvalues i= 1/74{
use "/Research/SDI-FDIC Data/Files for JCMB/TS_DepTA.dta",replace
gen count= `i' 
keep if countquarter>= count & countquarter<=count+7

** Drop missing
foreach var  of varlist Dep* {
quietly:count if `var'==.
if r(N) > 0 {
drop `var'
}
else {
}
}
*******
display("Ciclo n. ") _continue 
display(count) 
  list closdate in 1/1, noheader
list closdate in -1/-1, noheader
*******
pca DepTA* 
********************************
*Save EigenValues in a matrix
matrix IND=count[1]
matrix Eigen=e(Ev)
matrix EigenRedux= Eigen[1,1..10]
if count==1{
matrix TEMP3=EigenRedux
}
else{
matrix TEMP3=Results\EigenRedux
}
matrix Results=TEMP3
matrix drop IND Eigen EigenRedux TEMP3 
**********************************
drop count 
}
matrix list Results
drop _all
svmat double Results
outsheet using "/Research/SDI-FDIC Data/Files for JCMB/PCA_DepTA.csv", delimiter(";") replace
log close

