Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help with a given stata code

    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
    Last edited by Lisa Meier; 23 Nov 2017, 04:35.

  • #2
    What is your question to us? Can we walk you through someone else's code that you only partially gave us? That is obviously not going to work.

    My impression is that you don't know where to start. In that case you just need face to face consulting. So my suggestion is that you find someone locally who can sit with you and go through this step by step.

    Alternatively, you can try the authors, though you need to think carefully about answerable questions you might want to ask them before sending them an email.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Thats correct, I dont know where so start

      My stata knowledge is not that deep so I maybe thought that its possible to recunstruct the steps just from some codes or at least give me a point to start and I can figure out the rest of my own !
      Last edited by Lisa Meier; 23 Nov 2017, 04:37.

      Comment


      • #4
        Any good program usually comes with a help file. If there is no help file, that doesn't mean that it is a bad program, although the probability is high(er). It may just mean that the authors are posting code as a matter of record and with the signal to (experienced) Stata users that they may (will) need to adapt this if (e.g.) their variable names are present.

        Without knowing exactly what it does, I see that the program is a messy mixture of general code and code assuming that the main data has (and doesn't have) particular variable names. No doubt it works as expected withdata like the authors' data; in any Stata programming course it would get a low grade. But it's really not trying even a little to be robust and general, so the low grade comment is irrelevant from most points of view, just as a warning: watch out; this isn't general code. It's a do-file with added program flavour.

        Unfortunately (in one sense) I have to agree with Maarten. You need a collaborator or a tutor who knows Stata or to be prepared to learn Stata to the point where you understand the code any way. I would give better news if I could.

        Comment


        • #5
          Allright thank you !

          Comment


          • #6
            Goodmorning. I'm doing an analysis on a sample of 59 banks (DATASET UNBALANCED).
            I'm following the paper based on the costruction of deltaCOVAR written by A.Brunnermeier.
            I just know the commands to calculate Var and Covar in order to estimate deltaCOVAR finally.

            The commands are:

            qreg Xi IBOVESPAReturnlag IBOVESPAVolatilitylag LiquiditySpreadlag ChangeCreditSpreadlag ChangeYieldSlopelag ChangeTbilllag ,quantile(.05)
            predict Xi_predict95 ,xb

            gen Xi_predict95lag = Xi_predict95[_n-1]


            qreg XSYST Xi_predict95 IBOVESPAReturnlag IBOVESPAVolatilitylag LiquiditySpreadlag ChangeCreditSpreadlag ChangeYieldSlopelag ChangeTbilllag,quantile(.05)
            predict XSYST_predict95,xb

            qreg Xi IBOVESPAReturnlag IBOVESPAVolatilitylag LiquiditySpreadlag ChangeCreditSpreadlag ChangeYieldSlopelag ChangeTbilllag,quantile(.50)
            predict Xi_predict50,xb

            gen Xi_predict50lag = Xi_predict50[_n-1]

            qreg XSYST Xi_predict50 IBOVESPAReturnlag IBOVESPAVolatilitylag LiquiditySpreadlag ChangeCreditSpreadlag ChangeYieldSlopelag ChangeTbilllag,quantile(.50)
            predict XSYST_predict9550,xb

            gen deltacovar = XSYST_predict95 - XSYST_predict9550

            The problem is that i obtained the same value of Xi_predict95 for each banks in spite of each banks have different Xi. Probably i must use the command "forvalues...." but i don't have results probably because i must distinguish id banks and time variabile because it's UNBALANCED.

            forvalues i = 1/59 {
            qreg Xi IBOVESPAReturnlag IBOVESPAVolatilitylag LiquiditySpreadlag ChangeCreditSpreadlag ChangeYieldSlopelag ChangeTbilllag if IDBanks ==`i', quantile(.05)
            predict var5_`i' if IDBaks==`i', xb `i'
            replace var5_`i'=0 if var5_`i'==.
            replace var5=var5+var5_`i'
            drop var5_`i'
            }

            Someone, can help me with the right commands?
            Thank you so much,

            Comment


            • #7
              Elisa, you should probably start a new thread with a descriptive title instead of tacking something on to an unrelated old thread.

              Your code would be easier to read if you used code tags. See pt. 12 of the FAQ.

              The answer may be obvious to someone. But, if you can share 100 records from your data that will reproduce the problem that might help, See the description of dataex in that same FAQ.
              -------------------------------------------
              Richard Williams, Notre Dame Dept of Sociology
              StataNow Version: 18.5 MP (2 processor)

              EMAIL: [email protected]
              WWW: https://www3.nd.edu/~rwilliam

              Comment


              • #8
                Hi Lisa!! It has been a while since you post this, but 5 years later I have the same problem. I have to estimate CoVaR and I'm trying to use the same code as the authors but I can't really understand it. Can you help me? Thanks!

                Comment

                Working...
                X