Announcement

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

  • #16
    Hi Abraham! Your code is really fast. I feel it needs to be modified so that it serves my purpose of finding SD by country and industry but excluding focal firm ,i.e focal firms is one in front of which we are doing the calculations. For example, if we have 3 firms then each firm becomes focal firm by turn. So starting with the first firm, we shall calculate SD for for firm 2 and 3 and write the results in SDx of firm 1 . Similarly, for firm 2, the sd for ID ==1 and ID==3 will be calculated, and replaced in SDx of firm 2, and so on. Thanks.
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

    Comment


    • #17
      Attaullah,

      The Mata code does exactly what the Stata code you wanted me to translate does (with the addition of
      Code:
      loc=="`c'" & industry==`i'
      into your code where you use replace.) You should get the same results using your Stata code and the Mata code.

      To illustrate, let's say for a country c and industry i with firms f1 f2 f3 f4, the SDx for f1 in 2004 would be the SD of ROA of firms f2 f3 f4 in 2000-2003 in country c and industry i.

      You don't want the calculation to be restricted to a time window as the previous code did?

      Abraham

      Comment


      • #18
        Hi Abraham, I found a mistake in my Stata code. The last line where I replace SD values in SDx should be written as
        Code:
        quietly replace SDx_st= r(sd) if gvkey==`g' & fyear==`y'+4 & loc=="`c'" & industry==`i'
        . Rest of the code ok, so my final question would be how incorporate that change in your Mata code. Also, what would you suggest for a beginner to start with for learning mata, consider as if I am learning ABC in mata. Thank you so much for the guidance and kindness.
        Regards
        --------------------------------------------------
        Attaullah Shah, PhD.
        Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
        FinTechProfessor.com
        https://asdocx.com
        Check out my asdoc program, which sends outputs to MS Word.
        For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

        Comment


        • #19
          Attaullah

          The Mata code is fine because I knew that there was a mistake in your Stata code and took care of it. If you run both codes you will get the same results.

          About learning Mata, you may look at the series of papers published in the Stata Journal called "Mata Matters". Just google "Mata Matters". Beside this series and the Manual, I am not aware of any material for beginners. May be others will have something to say.

          Abraham

          Comment


          • #20
            Yes both the codes generate same results, except the rolling window. The Stata code uses rolling window of 4 years and replaces results in SDx in the fifth year and the Mata code generates results cumulatively from 2nd year onward til 5th year and replace the results in SDx one year forward. so if only two years data is available, the mata code would find SD for those two years and replace results in the 3rd year.
            Regards
            --------------------------------------------------
            Attaullah Shah, PhD.
            Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
            FinTechProfessor.com
            https://asdocx.com
            Check out my asdoc program, which sends outputs to MS Word.
            For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

            Comment


            • #21
              Attaullah,

              Now windows with less than 4 years are defined as missing.

              Code:
              egen idnew = group(loc industry)
              gen SDx = .
              mata
              mata clear
              st_view(loc =.,.,"loc")
              st_view(industry=.,.,"industry")
              st_view(gvkey=.,.,"gvkey")
              st_view(fyear=.,.,"fyear")
              st_view(ROA=.,.,"ROA")
              st_view(SDx=.,.,"SDx")
              st_view(idnew=.,.,"idnew")
              p = panelsetup(idnew,1)
              for (i=1; i<=rows(p); i++) {
                  for (o=p[i,1]; o<=p[i,2]; o++) {
                      h = J(1,1,0)
                      y = J(1,1,.)
                      for    (t=p[i,1]; t<=p[i,2]; t++) {
                          if ( gvkey[t,1]!=gvkey[o,1]&ROA[t,1]!=.&(fyear[o,1]>fyear[t,1])&(fyear[o,1]<fyear[t,1]+5)){
                          h = h \ ROA[t,1]
                          y = y \ fyear[t,1]
                          }            
                      }
                      if ( rows(h)>=2  ) {        
                      if ( max(y[(2..rows(y)),.])-min(y[(2..rows(y)),.])==3)SDx[o,1]=sqrt(variance(h[(2..rows(h)),.]))
                      }
                  }
              }
              end
              Abraham

              Comment


              • #22
                Abraham
                Your help is greatly appreciated. You have put too much efforts in working this out for me. One last request if possible for you, that is , if you could add comment to each line of the code regarding what each line does, so that if I want to modify it in future and for learning.
                Regards
                --------------------------------------------------
                Attaullah Shah, PhD.
                Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
                FinTechProfessor.com
                https://asdocx.com
                Check out my asdoc program, which sends outputs to MS Word.
                For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

                Comment


                • #23
                  Attaullah,

                  Here is some explanation.

                  Code:
                  egen idnew = group(loc industry) //creats id for distinct combination of country and industry
                  gen SDx = .
                  mata  //calls Mata
                  mata clear  //clears previous objects from Mata memory
                  st_view(loc =.,.,"loc")  //creates a view onto variable loc from Stata data set; any manipulation to the variable in Mata will also affect the variable in Stata
                  st_view(industry=.,.,"industry")
                  st_view(gvkey=.,.,"gvkey")
                  st_view(fyear=.,.,"fyear")
                  st_view(ROA=.,.,"ROA")
                  st_view(SDx=.,.,"SDx")
                  st_view(idnew=.,.,"idnew")
                  p = panelsetup(idnew,1) //creates a matrix where the size of the rows is the number of distinct panels and the size of the column is 2
                                          //It allows us to loop only through individual panel instead of all the data
                                          //p[i,1], the first column of p, indicates where the observation of panel i starts in the data
                                          //p[i,2], the second column of p, indicates where the observation of panel i ends                        
                  
                  for (i=1; i<=rows(p); i++) { //loops from 1 to rows(p), which is the number of distinct panels in the data
                      for (o=p[i,1]; o<=p[i,2]; o++) { //within panel i, loop from the start of panel i to the end of panel i
                          h = J(1,1,.) //initiate a scalar to store the values of ROA. It will grow to a column vector as additional elements are added based on fulfilling conditions we will set later
                          y = J(1,1,.) //initiate a scalar to store the values of the fyear variable. I will also grow to a column vector
                          for    (t=p[i,1]; t<=p[i,2]; t++) { //within each observation o in panel i, loop through all observations of panel i
                              if ( gvkey[t,1] != gvkey[o,1] & ROA[t,1] !=. & (fyear[t,1] > fyear[o,1] - 5 & fyear[t,1] < fyear[o,1] ) ){ //set of conditions:
                              //o indicates the focal observation, t indicates the observation we are looping through
                              //gvkey[t,1] != gvkey[o,1]  gvkey of the observations we are looping through is different from the focal observation
                              //ROA[t,1] !=.  the ROA of the observation we are looping through is not missing
                              //fyear[t,1] > fyear[o,1] - 5 & fyear[t,1] < fyear[o,1] the fyear should be between 5 years before the year of the focal observation and less than the year of the focal observation
                              h = h \ ROA[t,1] //adds the ROA if conditions are fulfilled
                              y = y \ fyear[t,1] //adds the fyear if conditions are fulfilled
                              }            
                          }
                          if ( rows(h)>=2  ) { //the rows size of h must be > 1 because the first row is irrelevant as we used it to the initiate the vector
                          if ( max(y[(2..rows(y)),.])-min(y[(2..rows(y)),.])==3) SDx[o,1]=sqrt(variance(h[(2..rows(h)),.])) //we want to compute SD only if there are four years of ROA before the focal observation
                          //thus we look at the maximum and the minimum values of years in y excluding the first row (y[(2..rows(y)),.], this tells it to start from the second row of y to the last row of y) and subtract
                          //If it equals 3 we know that there are four years of ROA data before the focal observation
                          //if this condition is fulfilled, we calculate the SDx again excluding the first row
                          }
                      }
                  }
                  end
                  Abraham

                  Comment


                  • #24
                    Wow! I can see how dedicated and sincere you are to what you do. So great of you Abraham. Thanks for everything, both for the fish and telling us the way how to catch it.
                    Regards
                    --------------------------------------------------
                    Attaullah Shah, PhD.
                    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
                    FinTechProfessor.com
                    https://asdocx.com
                    Check out my asdoc program, which sends outputs to MS Word.
                    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

                    Comment

                    Working...
                    X