Announcement

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

  • #16
    By mdate, how can i get count of stocks (or rt observations) in each of the 25 size-idiovol portfolios in context of our code in #6 and #9. The output should be in the data editor format.

    Comment


    • #17
      The output should be in the data editor format.
      What does that mean?

      By mdate, how can i get count of stocks (or rt observations) in each of the 25 size-idiovol portfolios in context of our code in #6 and #9.
      Just before -by mdate *_quintile, sort: keep if _n == 1- insert the command: -by mdate *_quintile, sort: gen stock_count = _N-, and change -reshape wide vw_mean_@rt ew_mean_@rt, i(mdate) j(group) string- to -reshape wide @stock_count vw_mean_@rt ew_mean_@rt, i(mdate) j(group) string-

      Comment


      • #18
        Code updated perfectly.

        Comment


        • #19
          Code:
          by mdate, sort: egen mcap_quantile = xtile(mcap), nq(2)
          by mdate: egen mom_quantile = xtile(mom), nq(3)
          
          capture program drop one_weighted_return
          program define one_weighted_return
              egen numerator = total(mcap*rt)
              egen denominator = total(mcap)
              gen vw_mean_rt = numerator/denominator
              exit
          end
          
          runby one_weighted_return, by(mdate mcap_quantile mom_quantile)
          
          by mdate mcap_quantile mom_quantile, sort: egen ew_mean_rt = mean(rt)
          
          keep mdate *_quantile *_mean_rt
          by mdate *_quantile, sort: gen stock_count = _N
          by mdate mcap_quantile mom_quantile, sort: keep if _n == 1
          
          gen str32 group = "mcap_q" + string(mcap_quantile) +"_mom_q" + string(mom_q) + "_"
          drop *_quantile
          reshape wide @stock_count vw_mean_@rt ew_mean_@rt, i(mdate) j(group) string

          In the given code, I want to define break points for mcap and mom sorts.

          The break point for mcap should be median mcap value of the sample and the break points for mom should be 30th and 70th percentiles of the mom values in the sample.

          How should i incorporate these changes into the code.
          Last edited by Sartaj Hussain; 13 Jul 2021, 10:43.

          Comment


          • #20
            What you've got is very close. The only problem is that for mom you are not getting the 30th and 70th percentiles as cutpoints--you are getting terciles, which correspond to cutpoints at the 33 and 67 percentiles. So handling mom is a little more complicated:

            Code:
            _pctile mom, percentiles(30 70)
            gen cutoffs = r(r1) in 1
            replace cutoffs = r(r2) in 2
            xtile mom_quantile = mom, cutpoints(cutoffs)
            Just replace your -by mdate: egen mom_quantile = xtile(mom), nq(3) - command with the above code and you should be good to go.

            Comment


            • #21
              That is great indeed.

              Now, to remove missing observations from the given cross-sectional data set, can the below code work perfectly or it needs any modification:

              Code:
              foreach v of var * {
              drop if missing(`v')
              }

              Comment


              • #22
                This code will drop all observations which contain a missing value on any variable. You will be left with all and only those observations that have non-missing values for every variable. If that is what you want to do, it will work correctly.

                Comment


                • #23
                  Yes. That is what is intended.

                  Comment


                  • #24
                    Code:
                      by mdate, sort: egen idiovol_decile = xtile(idiovol), nq(10)
                    
                    capture program drop one_weighted_return
                    program define one_weighted_return
                        egen numerator = total(mcap*rt)
                        egen denominator = total(mcap)
                        gen vw_mean_rt = numerator/denominator
                        exit
                    end
                    
                    runby one_weighted_return, by(mdate idiovol_decile)
                    
                    by mdate idiovol_decile, sort: egen ew_mean_rt = mean(rt)
                    
                    keep mdate idiovol_decile *_mean_rt
                    by mdate idiovol_decile, sort: keep if _n == 1
                    Now, there is one more thing. By sorting on idiovol, the above code generates 10 decile portfolio of stock returns (rt). For each of these deciles portfolios, I need average values of their characteristic variables: (idiovol, beta, mcap, size, bmratio, mom, rev, illiq, coskew and idioskew).
                    Last edited by Sartaj Hussain; 15 Jul 2021, 10:31.

                    Comment


                    • #25
                      Code:
                      collapse (mean) idiovol beta mcap size bmratio mom rev illiq coskew idioskew, by(mdate idiovol_decile)

                      Comment


                      • #26
                        Is this command is to be executed after the main code in #24. Moreover, the new code is giving one value per decile per month for all the variables. It needs to be transformed into a grand monthly mean for all deciles/variables. Moreover, how can we get a pooled mean for all deciles instead by month.
                        Last edited by Sartaj Hussain; 15 Jul 2021, 11:16.

                        Comment


                        • #27
                          Is this command is to be executed after the main code in #24
                          Yes.

                          Moreover, the new code is giving one value per decile per month for all the variables. It needs to be transformed into a grand monthly mean for all deciles/variables. Moreover, how can we get a pooled mean for all deciles instead by month.
                          I don't understand. You seem to be asking for two different things here. What exactly do you want?

                          Comment


                          • #28
                            Yes, these are actually two things. When the code is run, it yields mean values of decile portfolio characteristics by mdate. So literally, it gives one mean value for each characteristics each month. While as i require one mean value per characteristic variable as a whole for each decile portfolio.

                            Comment


                            • #29
                              So, without applying the code in #25
                              Code:
                              collapse (mean) idiovol beta mcap size bmratio mom rev illiq coskew idioskew, by(idiovol_decile)

                              Comment


                              • #30
                                A few days back, the following code was suggested in the present thread to carry out 2x3 independent sorts. This code does the sorts and dissections of data on monthly basis. However, using the same code, now i want to do sorts on annual basis on June month end each year. So, each year, first sort is to be done using recent June month value of sort variable, i.e. (mcap) and the second sort should be done using recent March month value of sort variable, i,e, (mom). Remaining part of the code is ok. I request for necessary modification of the given code to incorporate above changes:

                                Code:
                                by mdate, sort: egen mcap_quantile = xtile(mcap), nq(2)
                                _pctile mom, percentiles(30 70)
                                gen cutoffs = r(r1) in 1
                                replace cutoffs = r(r2) in 2
                                xtile mom_quantile = mom, cutpoints(cutoffs)
                                
                                capture program drop one_weighted_return
                                program define one_weighted_return
                                    egen numerator = total(mcap*rt)
                                    egen denominator = total(mcap)
                                    gen vw_mean_rt = numerator/denominator
                                    exit
                                end
                                
                                runby one_weighted_return, by(mdate mcap_quantile mom_quantile)
                                
                                by mdate mcap_quantile mom_quantile, sort: egen ew_mean_rt = mean(rt)
                                
                                keep mdate *_quantile *_mean_rt
                                by mdate *_quantile, sort: gen stock_count = _N
                                by mdate mcap_quantile mom_quantile, sort: keep if _n == 1
                                
                                gen str32 group = "mcap_q" + string(mcap_quantile) +"_mom_q" + string(mom_q) + "_"
                                drop *_quantile
                                reshape wide @stock_count vw_mean_@rt ew_mean_@rt, i(mdate) j(group) string
                                Last edited by Sartaj Hussain; 10 Aug 2021, 10:46.

                                Comment

                                Working...
                                X