Announcement

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

  • store R squared and residual of foreach loop regression

    Dear all,

    For our master paper, we need to replicate the Fama and French three factor model. We started with the one factor model.
    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 execute our loop regression (for our 519 firms: from the variable SOLVAY to ADECCOGROUP), by executing the following command:

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

    Now, we have our 519 regressions results and we want to store the 519 residual values in a new variable and store the 519 R-squared values in another variable.

    For the residual values, we tried to use this command:
    predict residual, r

    But unfortunately, with this command, Stata gives us the daily residuals and not the residuals per firm obtained from the loop regression.

    For the R-squared values, we tried to use this command:
    tempname results output
    foreach x of varlist SOLVAY-ADECCOGROUP {
    regress `x' MktRF
    mat `results' = r(r2)
    mat `output' = (nullmat(`output') \\`results'[1,1])
    }
    mat colnames `output' = R-squared
    svmat `output', names(col)

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

    Does anyone know how we can solve this?

  • #2
    Hello Gonne Van Mechelen. Does this "toy" example do what you would like to do?

    Code:
    clear
    sysuse auto
    
    foreach x of varlist weight-displacement {
     quietly regress `x' mpg
     predict resid_`x', residual
     generate Rsq_`x' = e(r2) if _n==1 // Store Rsq on 1st row only
    }
    PS- Please see the FAQ advice about using code delimiters when posting code or output. (To enclose a block of text in code delimiters, highlight it, and then click the # button on the toolbar.)
    --
    Bruce Weaver
    Email: [email protected]
    Version: Stata/MP 19.5 (Windows)

    Comment


    • #3
      Hi Bruce,

      Thank you for your help! I tried your code and with this code, Stata created a lot of extra variables.
      For the residual, he created a variable for every firm and calculated the daily residual. But our goal is to store the residuals of the regressions of the firms in one 1 variable instead of creating for every firm a variable with the daily residual values.

      Furthermore, for the R-squared it created 519 variables: for every firm he returns the R squared of the regression. Do you know if it is possible to store all these R squared values in 1 variable?

      Thank you for the tip about using code delimiters!

      Comment


      • #4
        Like I already suggested in your initial thread here, you should get more familiar with some of Stata's concepts in order to modify the existing codes. What you want is easily possible. You just need to understand what exactly my code example does. Then you can modify it according to your needs.

        You can also take the code presented by Bruce Weaver and modify it accordingly. His code actually shorter and easier to modify than my own matrix based approach.
        I suggest that you first try it yourself. If you do not manage, then show us your code.
        What you will probably need (amongst other things), is a way to keep track of how many regressions you have run and a way to change the value of specific observation in the variable which contains your result.
        If I counted correctly, then you need add four lines of code and change one existing line.
        Take it as an exercise for other problems which require a similar approach.

        Comment


        • #5
          The code you posted suggested that you have a wide data file. Perhaps if you reshape it to long (see -reshape- command), you can get what you want. It would help immensely if you provided a small "toy" example of what the data look like now, and what you want when all is done. Use -dataex- (mentioned in the FAQ) to make the toy data accessible to others.

          Cheers,
          Bruce
          --
          Bruce Weaver
          Email: [email protected]
          Version: Stata/MP 19.5 (Windows)

          Comment

          Working...
          X