Announcement

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

  • Error last estimates not found.

    Hello
    I am trying to run a regression among 220 firms from 1998-2018 to predict a yearly beta value. However, when I try to run the code I first get the error no observations due to not all firms having observations every year due to bankruptcies and so on. Then when I try to suppress this error using capture, which is a function I have never used before, I get the error "last estimates not found". Can someone explain to me where this error arises and possibly suggest an improvement to my current code? My current code looks like this:

    Code:
    forvalues i=1/220 {
    forvalues j=1998/2018 {
    capture reg ret ewretd if fakeID==`i' & estimationwindow==1 & fyear == `j'
    predict R_hat_`i' if fakeID==`i' & eventwindow==1 & fyear == `j', xb
    replace R_hat = R_hat_`i' if fakeID==`i' & fyear == `j'
    drop R_hat_`i'
    replace sigmahat=e(rmse) if fakeID==`i' & tau==0 & fyear == `j'
    }
    }
    Thanks in advance.

  • #2
    The problem is that you used -capture- and then disregarded the result. -capture- allowed the do-file to continue running even though the regression itself failed due to no observations. But if there are no observations, then there are no coefficient estimates posted and -predict- has nothing to work with. Moral of the story: when you use -capture- to permit your program to continue on in the case of predictable, anticipated errors, you must nevertheless determine when that has happened, and, then guard the rest of the code from trying to use results that in fact don't exist. So try it like this:

    Code:
    forvalues i=1/220 {
        forvalues j=1998/2018 {
            capture reg ret ewretd if fakeID==`i' & estimationwindow==1 & fyear == `j'
            if c(rc) == 0 {    // SUCCESSFUL REGRESSION, CALCULATE R_hat
                predict R_hat_`i' if fakeID==`i' & eventwindow==1 & fyear == `j', xb
                replace R_hat = R_hat_`i' if fakeID==`i' & fyear == `j'
                drop R_hat_`i'
                replace sigmahat=e(rmse) if fakeID==`i' & tau==0 & fyear == `j'
            }
            else if !inlist(c(rc), 2000, 2001) {    // UNANTICIPATED PROBLEM -- COMPLAIN AND HALT!
                display as error `"Unexpected error performing regression with fakeID == `i' & fyear == `j'"'
                exit c(rc)
            }
        }
    }
    This way, after the regression is attempted, if it was successful, you go on to calculate R_hat. If it failed for any reason other than no observations (2000) or insufficient observations (2001) then Stata will halt and give you an appropriate error message so you can investigate what went wrong. And if the regression failed for no or insufficient observations, then Stata will skip the calculation of R_hat and move on to the next value of j (or i).

    By the way, I strongly advise you to get in the habit of indenting your code. It makes it much easier to see what is going on, and you also will have an easier time finding problems like unbalanced curly braces. Stata doesn't care if you indent, but you should always code not just with Stata's parser in mind but with human readers (including yourself some time in the future when you've forgotten this code but had to go back and look at it again) too.

    Added: Apparently #3 crossed with this. That code will suppress the problem you reported in #1, but it is not safe code. If there is something else wrong with your regression command (perhaps some exotic numerical instability in the calculations, or perhaps you mistyped the command and it refers to a non-existent variable, or your misspelled -regress-, etc.) the code in #3 will just blunder ahead and you will be left with partially or totally missing results and no understanding what went wrong or where. It takes a little extra time and effort to code safely, but in the long run it will save you time chasing down mysteries and produces more reliable results. Safe code takes a proactive stance towards finding problems at the earliest possible moment and calling them to your attention in the clearest way possible.
    Last edited by Clyde Schechter; 23 Jun 2019, 15:37.

    Comment


    • #3
      You need to use a capture {} block. Also, I assume that R_hat and sigmahat were previously defined.
      For example:

      Code:
      webuse grunfeld, clear
      gen rhat = .
      //Note year starts at 1935
      forv i = 1930/1945 {
          capture  {
              reg invest mvalue if year == `i'
              predict xb if year == `i'
              replace rhat = xb if year == `i'
              drop xb
          }
      }

      Comment


      • #4
        Thanks for the answer to you both.

        The code seems to work. I thank you for your advice, Clyde Schechter, in coding and I will try and start to indent when coding in Stata.

        Comment

        Working...
        X