Hello everyone, I'm just doing my first steps with stata.
I'm currently working on a CoVaR quantil estimation/regression analysis at my university. Some economical papers (Adrian & Brunnermeier) already did my analysis and i have a stata code from them, but honestly I dont know how to put it in praxis.
The data are different european banks and their value developments in time if you need that information. I want to do a quantil regression on them.
It has 40 steps and I need help in understanding, how this code works and basically how I can copy their steps using my own data.
Here are the steps of their code
. //new CoVaR Quantile Estimation program
.
.
. capture program drop covar
. program covar
1. syntax ,sys(integer) id(varlist) tvar(varlist) svarlist(varlist) quant(real)
2. sort `id' `tvar'
3. drop if `id' == .
4. tempvar id2
5. egen `id2' = group(`id') if `id' ~= `sys'
6. replace `id2' = 0 if `id' == `sys'
7. sum `id2' if `id2' ~= 0
8. local idmin = r(min)
9. local idmax = r(max)
10. sort `id2' `tvar'
11.
. preserve
12. display "firm is `firm'"
13. display "sys is `sys'"
14. display "idvar is `id'"
15. display "asofQtr is `asofQtr'"
16. display "statevars are `svarlist'"
17.
. keep `id2' `tvar' ret `svarlist'
18.
. //reshape data
. reshape wide ret, i(`tvar') j(`id2')
19. save temp, replace
20. sort `tvar'
21.
. //estimate covar for each firm in this loop
. quietly forvalues firm = `idmin'(1)`idmax' {
22. noisily display "Estimating CoVaR for `firm'"
23. //conditional CoVaR, get betas
. qreg ret0 ret`firm' `svarlist', q(`quant')
24. gen cbeta = _b[ret`firm'] if e(sample)
25.
. //conditional VaRs, get qtile and median VaR
. qreg ret`firm' `svarlist', q(`quant')
26. predict cVaR if e(sample)
27. qreg ret`firm' `svarlist', q(50)
28. predict cmed if e(sample)
29.
. //calculate cCoVaR
. gen cCoVaR`firm' = cbeta*(cVaR-cmed)
30. drop ret`firm' cVaR cmed cbeta
31. }
32. drop ret0 `svarlist'
33. //now we should just have `tvar' and all the firm cCoVaRs
. reshape long cCoVaR, i(`tvar') j(`id2')
34. drop if cCoVaR == .
35. sort `id2' `tvar'
36. tempfile estimates
37. save `estimates', replace
38.
. //merge back in with input data
. restore
39. merge 1:1 `id2' `tvar' using `estimates'
40. drop _merge
41.
. end
For example, in the first step they opened the quantil regression modul in stata and then...
Maybe the code says something else haha, I'm just trying to figure it out
I'm currently working on a CoVaR quantil estimation/regression analysis at my university. Some economical papers (Adrian & Brunnermeier) already did my analysis and i have a stata code from them, but honestly I dont know how to put it in praxis.
The data are different european banks and their value developments in time if you need that information. I want to do a quantil regression on them.
It has 40 steps and I need help in understanding, how this code works and basically how I can copy their steps using my own data.
Here are the steps of their code
. //new CoVaR Quantile Estimation program
.
.
. capture program drop covar
. program covar
1. syntax ,sys(integer) id(varlist) tvar(varlist) svarlist(varlist) quant(real)
2. sort `id' `tvar'
3. drop if `id' == .
4. tempvar id2
5. egen `id2' = group(`id') if `id' ~= `sys'
6. replace `id2' = 0 if `id' == `sys'
7. sum `id2' if `id2' ~= 0
8. local idmin = r(min)
9. local idmax = r(max)
10. sort `id2' `tvar'
11.
. preserve
12. display "firm is `firm'"
13. display "sys is `sys'"
14. display "idvar is `id'"
15. display "asofQtr is `asofQtr'"
16. display "statevars are `svarlist'"
17.
. keep `id2' `tvar' ret `svarlist'
18.
. //reshape data
. reshape wide ret, i(`tvar') j(`id2')
19. save temp, replace
20. sort `tvar'
21.
. //estimate covar for each firm in this loop
. quietly forvalues firm = `idmin'(1)`idmax' {
22. noisily display "Estimating CoVaR for `firm'"
23. //conditional CoVaR, get betas
. qreg ret0 ret`firm' `svarlist', q(`quant')
24. gen cbeta = _b[ret`firm'] if e(sample)
25.
. //conditional VaRs, get qtile and median VaR
. qreg ret`firm' `svarlist', q(`quant')
26. predict cVaR if e(sample)
27. qreg ret`firm' `svarlist', q(50)
28. predict cmed if e(sample)
29.
. //calculate cCoVaR
. gen cCoVaR`firm' = cbeta*(cVaR-cmed)
30. drop ret`firm' cVaR cmed cbeta
31. }
32. drop ret0 `svarlist'
33. //now we should just have `tvar' and all the firm cCoVaRs
. reshape long cCoVaR, i(`tvar') j(`id2')
34. drop if cCoVaR == .
35. sort `id2' `tvar'
36. tempfile estimates
37. save `estimates', replace
38.
. //merge back in with input data
. restore
39. merge 1:1 `id2' `tvar' using `estimates'
40. drop _merge
41.
. end
For example, in the first step they opened the quantil regression modul in stata and then...
Maybe the code says something else haha, I'm just trying to figure it out

Comment