Announcement

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

  • Question on Matrix command

    I am regressing stock returns (of "Firms v") on an index ("Index") during each Quarter ("Quarter"). Hence my STATA file has a column "Daily date", a column "Quarter" (i from from 1 to 12 -as there are 12 Quarters in the file), a column "Index" (that is a Daily Index) and 140 columns named "firm1" .... "firm140" (that show Daily stock returns of firms 1 to 140).

    I want the R2 (r-squares) of the daily regression of the 140 stock returns on the index during each quarter (there would be 12 R2 per firm, as there are 12 Quarters in the STATA file).

    I was able to develop the following code:

    foreach v of varlist Firm* {
    forvalues i=1/12 {
    regress `v' Index if Quarter==`i'
    scalar R2_`v'_`i'=e(r2)
    }
    }
    scalar list

    The code works well but returns a list of scalars R2_`v'_`i' when what I need would be to create a Matrix (or table) that has rows ā€œiā€ (the 12 Quarters) and columns ā€œvā€ (the 140 Firms, with the R2_`v'_`i' value that the code gives). Any hint on how to do this ? (I believe the Matrix function in STATA has to be used, but so far, I have not been able to write the code to create this Matrix in/ from the above code).

    Thank you.

    Best,

    Emmanuel

  • #2
    The documentation emphasizes newer abilities of the -matrix- command, but assigning to an element of a matrix is documented. I believe you are looking for Section 9, item 7 of Chapter 14 of https://www.stata.com/manuals/u14.pdf:

    7. When defining an element of a matrix, use
    matrix matname[i,j] = expression
    where i and j are scalar expressions. The matrix matname must already exist.
    Example:
    matrix A = J(2,2,0)
    matrix A[1,2] = sqrt(2)
    The first example (without subscripts) redefines A using the -J- matrix function.


    Comment


    • #3
      May I suggest an alternative approach?

      The core of Stata are datasets, and in the latest version of Stata, creating a dataset with your results is easily accomplished. The outline is in
      Code:
      help frames
      help frames post
      The code you show above could be modified as in the following example.
      Code:
      sysuse auto, clear
      frame create results str20 varname int foreign float r_2
      foreach v of varlist length weight {
      forvalues i=0/1 {
      quietly regress `v' price if foreign==`i'
      frame post results ("`v'") (`i') (e(r2))
      }
      }
      frame results: list
      Code:
      . sysuse auto, clear
      (1978 automobile data)
      
      . frame create results str20 varname int foreign float r_2
      
      . foreach v of varlist length weight {
        2. forvalues i=0/1 {
        3. quietly regress `v' price if foreign==`i'
        4. frame post results ("`v'") (`i') (e(r2))
        5. }
        6. }
      
      . frame results: list
      
           +------------------------------+
           | varname   foreign        r_2 |
           |------------------------------|
        1. |  length         0   .2509309 |
        2. |  length         1   .6715736 |
        3. |  weight         0   .4521182 |
        4. |  weight         1   .7841617 |
           +------------------------------+

      Comment


      • #4
        The core of Stata are datasets, and in the latest version of Stata, creating a dataset with your results is easily accomplished.
        Strongly agree. Let me take that a step farther. In Stata, matrices are somewhat cumbersome to work with and usually are not useful unless you are specifically going to do matrix algebra. Absent that condition, you are nearly always better off creating a data set than a matrix.

        Comment


        • #5
          Strongly agree with #3 and #4 and would just add that if you find that your subsequent work with Stata necessarily involves dealing with matrix computations it will probably be worth investing some time learning Mata, Stata's matrix programming language. It is far more flexible and powerful than Stata's native matrix commands.

          See
          Code:
          help mata

          Comment

          Working...
          X