Announcement

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

  • #16
    No, he's not seeing the id's displayed, because he omitted the -levelsof- command, so local ids is empty and the loops are being skipped. That's also why he's not getting error messages. Had he included that the code would have tripped over Carole's next observation that there is only one observation per id, so that calculation of covariances for individual id's is impossible. That would have manifested itself in the "insufficient observations r(2001)" error message.

    Erin, it was pointed out to you before that you cannot calculate covariances when there is only one observation. Why are you persisting in this impossible quest?

    Comment


    • #17
      I see your point. It is obvious that there is just one observation for the single id per ymdate, but on the other hand there are also several observations for the single id within the considered time period. But I understood that this does not matter.

      However, is it possible to generate my betas for a fixed 24-month-rolling period?
      That means for the variable mktrf, I would calculate the cov-var matrix for the horizon Jan 1996 to Dec 1997 first and then for the time period Jan 1998 to Dec 1999 etc.?

      In fact, the following two lines would appear one after another for the 24-month-rolling periods:

      corr ew_mean_excess_ret mktrf if mktrf < 0
      gen βmktrf = `r(cov_12)'/`r(Var_2)'

      Comment


      • #18
        I don't understand the structure of your data. YOu say in #17 that there are several observations for the single id within the considered time period, but that was not true in the data you sent as an example. Anyway, I'm going to disregard that example and assume you actually do have panel data and that you want rolling 12 month calculations for each id. This can be done using the -rolling- command prefix.

        Code:
        xtset id monthly_date
        rolling cov_12 =r(cov_12) var_2 = r(Var_2), window(12) saving(cov_and_var, replace): ///
            corr ew_mean_excess_ret mktrf if mktrf < 0, cov
        use cov_and_var, clear
        gen βmktrf = cov_12/var_2

        Comment


        • #19
          Thank you Clyde. This code seems to work out, but it takes a lot of time as Stata runs the calculation for each id.



          How can I adjust the command for all other factors besides mktrf (like rmw or cma) as well?
          As a result, I also want to get betas for the other factors:
          corr ew_mean_excess_ret rmw if rmw < 0, cov
          gen βrmw = `r(cov_12)'/`r(Var_2)'
          corr ew_mean_excess_ret cma if cma < 0, cov
          gen βcma = `r(cov_12)'/`r(Var_2)'
          etc.


          Does the following addition work:

          xtset id monthly_date
          rolling cov_12 =r(cov_12) var_2 = r(Var_2), window(12) saving(cov_and_var, replace): ///
          corr ew_mean_excess_ret mktrf if mktrf < 0, cov
          corr ew_mean_excess_ret rmw if rmw < 0, cov
          corr ew_mean_excess_ret cma if cma < 0, cov
          use cov_and_var, clear
          gen βmktrf = cov_12/var_2
          gen βrmw = cov_12/var_2
          gen βcma = cov_12/var_2

          Comment


          • #20
            So you can do this in a loop:

            Code:
            local predictors mkrtf rmw cma // AND ANY OTHERS YOU WANT
            
            xtset id monthly_date
            
            foreach p of local predictors {
                rolling cov_12 =r(cov_12) var_2 = r(Var_2), window(12) saving(`p'_cov_and_var, replace): ///
                    corr ew_mean_excess_ret `p' if `p' < 0, cov
            }
            
            // NOW CALCULATE THE BETAS
            
            foreach p of local predictors {
                use using `p'_cov_and_var, clear
                gen β`p' = cov_12/var_2
                save, replace
            }
            At the end of this code, you wlll have a Stata data set for each of your variables mkrtf, rmw, cma (and any others you decide on) giving the id's, time windows, and corresponding betas.


            Comment


            • #21
              Perfect! Thank you so much Clyde. This was exactly what I was looking for.

              Finally, is there an option that keeps also other variables in the data set apart from the local predictors (rmw etc.). Before running this code, I had for example also a "size" variable for each id and month. How can I keep this column in my data set at the end of this code?

              Or is it the easiest way to merge this data set at the end of this code with the one before this code in order to do further calculations with variables that are not included in the beta computation?

              Comment


              • #22
                the -rolling- prefix has a -keep()- option that does precisely this. So, the rolling command would look like:

                Code:
                rolling cov_12 =r(cov_12) var_2 = r(Var_2), window(12) saving(`p'_cov_and_var, replace) keep(size): ///
                        corr ew_mean_excess_ret `p' if `p' < 0, cov
                Now, it is my understanding that you can only specify a single variable in the -keep()- option, not a list of several. If you need to incorporate more than one such variable, I think you will have to -merge- them back in. The most convenient place to do that would be in the second loop, right before the -save- command.

                Comment

                Working...
                X