Announcement

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

  • #16
    -This program starts by the function capture, which executes the following code "program drop one_fund". That code drops the program one_fund from memory. Is that line of code not unnecessary if the program has not been coded yet? Or is this just to avoid any possible chance that I already have a program in memory called one_fund?
    It is there precisely because there is a chance you already have such a program. When you develop a program, you often get it wrong the first time (as I have here) and have to go back and change it. Using this -capture program drop program_name- routinely, you never have to think about whether the program is currently in memory.

    -This program is defined as one_fund which makes a regression of Return on ExcessRm, SMB, HML and MOM as independent variables for each observation (referred to by the iterator v). It then generates the variables b_`v' and se_`v', the mean and standard deviation respectively. Does this not mean that it will create two variables for each regression? By which I am wondering if it will create b_`1' and se_`1' for FundNumber 1, b_`2' and se_`2' for FundNumber 2, ... Leading to a creation of 450,670 x 2 variables? I doubt this is the case. I think it will rather lead to the creation of 482 x 2 variables because when the program is run through "runby one_fund, by(FundNumber)", the by(FundNumber) should be enough to have the program only run 482 times. I am uncertain, however.
    It will create exactly 8 variables: the coefficient and standard error of each of ExcessRm, SMB, HML, and MOM

    -The results, which holds a sequence of two generate commands is then ended with "}". There is then an exit of... Stata? I presume the exit command must refer to something else, embedded in the program sequence although I do not know what. And afterwards the program is -end-ed, finishing the definition of the one_fund program.
    When used inside a program, -exit- causes Stata to quit that program and return control to whatever called that program.

    -The program defined earlier is executed with the -runby- command where it is specified to -runby- each FundNumber, which lets me assume that there will only be 482 regressions taking place.
    That is correct.

    The only issue I might be able to think of is that Stata might refuse to perform an iteration for a FundNumber when it has a single missing observation for the Return variable. Which would still mean that there shouldn't be any errors for a FundNumber for which there are no missing observations in the Return variable.
    Well, actually because you have 4 predictors in your model, you need a minimum of 6 observations to get coefficients and standard errors on all of them. If the number of observations falls below that you will start to get predictors omitted due to colinearity. These won't provoke error messages, but will produce missing values in the b and se variables.

    Therefore, I frankly have no idea what the error might be. The only information I can still add is that:
    The error in the program is due to a typo I made: in the -foreach v of varlist...- statement I typed RML when it should be HML. There is no RML variable. If you fix that the program will run properly. My apologies for that error. It's an easy error to make and a hard one to spot because upper case H and R don't look all that different in small typefaces (at least not to my aging eyes.)

    am just trying to understand how the results are being stored, if the program were to have worked properly for my dataset.
    When the program runs correctly, you will have in memory the original data, but only for those fund numbers for which a regression was successfully carried out, and in addition there will be 8 new variables, b_excessRm se_excessRm, b_SMB, se_SMB, b_HML, se_HML, b_MOM, and se_MOM which contain the coefficients and standard errors from the regressions.

    Comment


    • #17
      These have been some very embarrassingly simplistic mistakes I have made. I do apologise for not spotting something as obvious as a simple R instead of an H. The creation of 8 variables is also something blatantly obvious that I should not be making mistakes on.

      The regression program you have provided has indeed worked with the correct definition of HML.

      I will formally analyse this data tomorrow and hopefully will not encounter any more issues with Stata that I cannot handle myself.

      I cannot thank you enough for the immense and continuous help with this issue. I do not think I would have ever been able to figure that one out on my own. For that, I would like to express my sincerest gratitude. I am performing this research for my master's thesis with a partner and we would like to request your permission to mention your name in our acknowledgments for tremendously helping us with our research.
      Last edited by Ewout Vermeersch; 17 Apr 2018, 14:48.

      Comment


      • #18
        With the data analysed, I realised that I am missing the constants of my regressions. This should be very easily solved with a simple extra generation of the _cons variable. I am unsure about the exact syntax, however.

        Code:
        capture program drop one_fund
        program define one_fund
            regress Return ExcessRm SMB HML MOM
            foreach v of varlist ExcessRm SMB HML MOM {
                gen cons_`v' = _b[_cons]
                gen b_`v' = _b[`v']
                gen se_`v' = _se[`v']
            }
            exit
        end
        
        runby one_fund, by(FundNumber) status
        This is my current attempt (additions in bold). This currently saves the value of the constant 4 times for each v. I am unsure how to properly code this program in such a way to only get the constant once.

        Will be updating this post as I continue experimenting.
        Last edited by Ewout Vermeersch; 18 Apr 2018, 07:06.

        Comment


        • #19
          You only need one constant term. So just call it cons, and create it outside the loop.
          Code:
          capture program drop one_fund
          program define one_fund
              regress Return ExcessRm SMB HML MOM
              gen cons = _b[_cons]
              foreach v of varlist ExcessRm SMB HML MOM {
                  gen b_`v' = _b[`v']
                  gen se_`v' = _se[`v']
              }
              exit
          end
          runby one_fund, by(FundNumber) status
          Similarly, if at some point you decide you also want to save other statistics that are not specific to particular variables, such as R2 or rmse, you can grab them from e(), and the command to generate the corresponding variables in your program belongs outside the loop.

          Comment

          Working...
          X