Announcement

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

  • How to generate a matrix with coefficients from 216 monthly regressions

    Dear Statalist community,

    I have here panel data of about 5000 stocks with monthly return data, as well as accounting and binary sustainability data.
    The model I have to run is a cross-sectional regression of the form Return(t,j) = acc1(t,j) + sust2(t,j) + ... at month t and stock j. I have to run this for every month in the sample period.
    What I need from these regressions is a matrix that gives me the coefficients, t-statistics, and Rsquares (3 columns) for every month (216 rows), and then I need to calculate the average of each of these coefficients.

    I have absolutely no idea how to go about it! I would know how to do this in eviews but for operational purposes I am now using stata. Here is the eviews code:

    matrix(216,3) coefficients=na
    for !n = 1 to 216
    !k = !n+1
    equation regression!n.ls ret!k c log(mv!n) log(pb!n) log(psales!n) log(dvol!n) dy!n env!n emp!n div!n sin!n
    coefficients(!n,1)=regression!n.@coefs(1)
    coefficients(!n,2)=regression!n.@coefs(2)
    coefficients(!n,3)=regression!n.@coefs(3)
    coefficients(!n,4)=regression!n.@coefs(4)
    coefficients(!n,5)=regression!n.@coefs(5)
    coefficients(!n,6)=regression!n.@coefs(6)
    coefficients(!n,7)=regression!n.@coefs(7)
    coefficients(!n,8)=regression!n.@coefs(8)
    coefficients(!n,9)=regression!n.@coefs(9)
    coefficients(!n,10)=regression!n.@coefs(10)
    next

    Thank you people!
    Last edited by Benjamin Arndt; 30 Jun 2015, 06:30.

  • #2
    What about statsby?

    Code:
    statsby _b _se r2=e(r2), by(month) clear: reg return acc sust
    Jorge Eduardo Pérez Pérez
    www.jorgeperezperez.com

    Comment


    • #3
      I forgot the last step: from the dataset generated you can use mkmat to create the matrix you need.
      Jorge Eduardo Pérez Pérez
      www.jorgeperezperez.com

      Comment


      • #4
        Thank you for the reply, Jorge! If I get this running that would be phenomenal. Unfortunately something does not work yet, as I get the following error:

        Code:
        statsby _b _se r2=e(r2), by(month) clear: reg Returns ROA LogBM LogSize
        (running regress on estimation sample)
        no observations
        an error occurred when statsby executed regress
        r(2000);

        The variables are certainly well-specified so I dont understand why there is a problem with the regression
        Also, I guess _se stands for standard error. Can I also get the t-statistic or do I have to compute (0-b)/se manually and if so, how do I put that in the new dataset/matrix?

        Best,
        Benjamin

        Note: Perhaps (month) is not enough for stata to know that the different stocks are meant to be the cross-section and months the time-series loop?

        Comment


        • #5
          You should check for missing values in your variables.
          Jorge Eduardo Pérez Pérez
          www.jorgeperezperez.com

          Comment


          • #6
            I do have missing values, but why does that impair my regression? Normally missing values are simply left out in the estimation process or not?

            Comment


            • #7
              This is from the help:

              error . . . . . . . . . . . . . . . . . . . . . . . . Return code 2000
              no observations;
              You have requested some statistical calculation and there are
              no observations on which to perform it. Perhaps you specified
              if or in and inadvertently filtered all the data.

              You should count the number of observations for which all of the variables involved in the regression are nonmissing, overall and by month.



              Jorge Eduardo Pérez Pérez
              www.jorgeperezperez.com

              Comment


              • #8
                Hello Jorge, thank you. I guess I could have solved this myself. I have specified the equation differently which solves the no observations problem. However now the output does not generate 216 months of data but only 12. I need the full sample from 1995 to 2013 and every month in that sample with that regression. The code is currently:

                Code:
                statsby _b _se r2=e(r2), by(month) clear: reg RET LogSize1 LogBM1 Return6to2


                To be sure - The _se command gives me the standard error right? I prefer the t-statistic because I have to do further calculations with it.

                Cheers,
                Benjamin.

                Comment


                • #9
                  Code:
                   statsby _b _se r2=e(r2), by(NamesDate) saving(regression) : reg RET LogSize1 LogBM1 Return6to2


                  Problem solved! lol
                  thank you


                  Comment

                  Working...
                  X