Announcement

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

  • Store Coefficient after loop regressions

    Dear all,

    For our master paper, we need to replicate the Fama and French three factor model. We started with the one factor model to try the different regressions. We are struggling to do certain regressions and afterwards extract the different coefficients.
    At the moment, we have 520 variables. We have one dependent variable: MktRF (Fama and French one factor model) and 519 independent variables (companies) with the daily excess returns over 20 years.
    We were able to do our loop regression (for our 519 firms: from the variable SOLVAY to ADDECOGROUP), by executing the following command:

    foreach x of varlist SOLVAY-ADDECOGROUP{
    regress `x' MktRF
    }

    The problem now is that we need to list all the coefficients of MktRF and _cons and generate these coefficients as 2 different variables called ‘coefficientMktRF’ and ‘coefficient_cons’.

    We were able to use the Statsby command, but this command only lists the two coefficients of our last regression (our last firm) and not of our 519 regressions.

    Is there anyone who can help us to proceed our master thesis?

  • #2
    You could try something like this
    Code:
    tempname results output
    foreach x of varlist SOLVAY-ADDECOGROUP{
    regress `x' MktRF
    mat `results' =r(table)
    mat `output' = (nullmat(`output') \\`results'[1,1..2])
    }
    mat colnames `output' = coefficientMktRF coefficient_cons
    svmat  `output', names(col)
    The code is not tested but should be useful as an inspiration.

    Comment


    • #3
      I tried the code and it worked. Thank you very much!

      I do have another question. Is it possible to store the R-squared of the 519 regressions and store it as well as a variable? I tried to replace coefficientMktRF coefficient_cons in the code with e(r2) but that doesn't work.

      Is there someone who can tell me how I can store these 519 R-square outcomes of the regressions in a new variable?

      Thank you in advance!

      Comment


      • #4
        I suggest that you make yourself more familiar with how estimation results are stored, how to work with matrices and macros in Stata so that you can understand and modify my code yourself.
        See
        Code:
        help estimation
        help matrix
        help matrix subscripting
        help matrix extraction
        help matrix rownames
        help matrix_functions
        help macro
        Take it as a good exercise to make yourself more familiar with some concepts in Stata. Once you understood these, you can easily solve similar problems yourself.

        Comment


        • #5
          Hi Sven-Kristjan,

          Thank you very much! After reading all the help files, I understand your code a lot more. With all this information, I tried again to store the 519 r2 in a new variable with the following code:

          tempname results output
          foreach x of varlist SOLVAY-ADDECOGROUP {
          regress `x' MktRF
          mat `results' = r(r2)
          mat `output' = (nullmat(`output') \\`results'[1,1])
          }
          mat colnames `output' = R-squared
          svmat `output', names(col)

          Unfortunately, Stata gives me an error code for the svmat `output', names(col) command:
          invalid syntax
          r(198);

          Which I don't understand because that last command worked perfectly when storing the coefficientMktRF and the coefficient_cons as a variable.
          Can you explain to me why I receive this error code? Is there still something wrong with my commands?

          Last edited by Gonne Van Mechelen; 24 Mar 2020, 05:23.

          Comment


          • #6
            Sven-Kristjan Bormann

            Hi Sven,

            I am still not able to store my R-squared values of my 514 regressions. Can you give me any further tips or do you know what is wrong with my code?

            Thank you in advance

            Comment


            • #7
              r(r2) will just be missing after regress, although you have yet to notice that.

              I don't see what the problem is specifically, but your route seems rather circuitous:

              save r-sq to a matrix

              append that to another matrix

              put the results in a new variable.

              The matrices aren't needed here. The temporary names aren't needed either. Here is a pattern that works.

              Code:
              sysuse auto, clear
              
              gen rsq = .
              
              local i = 1
              
              foreach x of varlist mpg length price {
                  regress `x' weight
                  replace rsq = e(r2) in `i'
                  local ++i
              }
              You may want to save the variable names too.
              Last edited by Nick Cox; 02 Apr 2020, 05:19.

              Comment


              • #8
                Thank you very much Nick! It worked!

                Comment


                • #9
                  Nick Cox The " rather circuitous" code was my initial suggestion and is not Gonne Van Mechelen's code. The code is based on an approach which I use for an r-class command which returns a matrix with some results.
                  The same question was already asked in this thread and answered by Bruce Weaver. Accidentally, your code is the same code that I had in mind in this other thread suggested there as a programming exercise.

                  Comment


                  • #10
                    We have all been there: we.solved something in a complicated way and then realised (sometimes years later) that there was a simpler way to do it.

                    Comment

                    Working...
                    X