Announcement

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

  • Estimating discretionary accruals using the modified Jones (1991)

    Hello -

    I am venturing into my first archival study and am struggling with figuring out the Stata code to estimate total accruals and discretionary accruals. Could someone please help me?

    I have tried the following code from a blogger but it is not working for me.
    gen sic_2= substr(sic,1,2)
    destring sic_2, replace

    vallist sic_2
    local a =r(list)

    vallist fyear
    local b =r(list)

    gen uhat=.
    xtset gvkey fyear
    gen obs= [_n]
    summ obs
    scalar e= r(min)
    scalar f= r(max)

    gen ta= (ib-oancf)/L.at
    gen x1= 1/L.at
    gen x2= (d.revt – d.rect)/L.at
    gen x3= ppegt/L.at

    foreach i in `a’ {
    foreach x in `b’ {
    forvalues j= `=scalar(e)’/`=scalar(f)’ {
    capture noisily reg ta x1 x2 x3 if sic_2==`i’ & fyear==`x’ & obs != `j’, nocons
    capture noisily predict uhat_2, resid
    capture noisily replace uhat_2=. if e(N) < 10
    capture noisily replace uhat= uhat_2 if sic_2==`i' & fyear==`x' & obs== `j'
    capture noisily drop uhat_2
    di `i'
    di `x'
    di `j'
    }
    }
    }



    Thank you very much.

  • #2
    Duplicate post. Comments posted elsewhere. http://www.statalist.org/forums/foru...ied-jones-1991
    Last edited by Nick Cox; 13 Oct 2014, 16:47.

    Comment


    • #3
      What, specifically, do you mean by "is not working for me?" You have shown us your code (which would be more readable if posted in a code block, but I won't quibble with that now). But what is it doing that is not to your expectations?

      The only obvious problem I can see is that the -vallist- command is a user-written command which, perhaps, is not installed in your setup. If that is the problem, you can get it with -ssc install vallist-. But, better still, -vallist- has been superseded by the official Stata command -levelsof-. See -help levelsof- for how to use it.

      Comment


      • #4
        Hello - My apologies for not being specific enough. Nothing happens when I run the loop. Uhat is not generated and I do not get any error messages. The code is from https://robsonglasscock.wordpress.co...1/#comment-185. Thank you.

        Comment


        • #5
          Please post the complete output from the entire block of code. Do so by pasting it into a code block. (Click on the underlined A button, then click on the # button. Two code delimiters will appear: past everything from the Results window between them.) We need to see the exact output from all of the code to help you diagnose this problem.

          If you are literally getting no output at all from the loop, not even the -display-ed values of `i', `j', and `x' as the loop iterates, then the loop must not be running at all, which probably means that either the value list for sic2 or fyear is empty, or that your data set has no observations by the time you get to running this. (Another possibility is that the code stops running before it even gets to the loop: but you would have some kind of error message in that case.)

          Anyway, show us the exact output you are getting.

          Comment


          • #6
            Here is my results window. Thank you very much.

            Code:
            . gen sic_2= substr(sic,1,2)
            
            . destring sic_2, replace
            sic_2 has all characters numeric; replaced as byte
            
            . 
            . levelsof sic_2
            1 2 10 12 13 14 16 17 20 22 23 24 25 27 28 29 30 31 32 33 34 35 36 37 38 39 42 44 45 46 47 4
            > 8 49 50 51 53 54 55 56 57 58 59 60 61 62 63 64 65 67 70 72 73 75 78 79 80 82 83 87 99
            
            . local a =r(levels)
            
            . 
            . levelsof fyear
            2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
            
            . local b =r(levels)
            
            . 
            . gen uhat=.
            (6581 missing values generated)
            
            . 
            . xtset gvkey fyear
            varlist:  gvkey:  string variable not allowed
            r(109);
            
            end of do-file
            
            r(109);
            
            . do "C:\Users\Zimmy\AppData\Local\Temp\STD00000000.tmp"
            
            . destring gvkey
            must specify either generate or replace option
            r(198);
            
            end of do-file
            
            r(198);
            
            . do "C:\Users\Zimmy\AppData\Local\Temp\STD00000000.tmp"
            
            . destring gvkey, replace
            gvkey has all characters numeric; replaced as long
            
            . xtset gvkey fyear
                   panel variable:  gvkey (unbalanced)
                    time variable:  fyear, 2005 to 2014, but with gaps
                            delta:  1 unit
            
            . 
            . gen obs= [_n]
            
            . summ obs
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                     obs |      6581        3291    1899.915          1       6581
            
            . scalar e= r(min)
            
            . scalar f= r(max)
            
            . 
            . gen ta= (ib-oancf)/L.at
            (1254 missing values generated)
            
            . gen x1= 1/L.at
            (1244 missing values generated)
            
            . gen x2= (d.revt-d.rect)/L.at
            (1277 missing values generated)
            
            . gen x3= ppegt/L.at
            (2340 missing values generated)
            
            . 
            . foreach i in `a' {
              2. foreach x in `b' {
              3. forvalues j= `=scalar(e)'/`=scalar(f)' {
              4. capture noisily reg ta x1 x2 x3 if sic_2==`i' & fyear==`x' & obs != `j', nocons
              5. capture noisily predict uhat_2, resid
              6. capture noisily replace uhat_2=. if e(N) < 10
              7. capture noisily replace uhat= uhat_2 if sic_2==`i' & fyear==`x' & obs== `j'
              8. capture noisily drop uhat_2
              9. di `i'
             10. di `x'
             11. di `j'
             12. }
             13. }
             14. }
            
            . 
            . * more efficient code end
            . 
            . /* Accuracy checking */
            . 
            . bys sic_2:summ uhat
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 1
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 2
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 10
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 12
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 13
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 14
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 16
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 17
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 20
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 22
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 23
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 24
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 25
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 27
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 28
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 29
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 30
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 31
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 32
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 33
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 34
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 35
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 36
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 37
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 38
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 39
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 42
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 44
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 45
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 46
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 47
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 48
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 49
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 50
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 51
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 53
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 54
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 55
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 56
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 57
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 58
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 59
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 60
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 61
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 62
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 63
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 64
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 65
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 67
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 70
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 72
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 73
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 75
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 78
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 79
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 80
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 82
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 83
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 87
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            --------------------------------------------------------------------------------------------
            -> sic_2 = 99
            
                Variable |       Obs        Mean    Std. Dev.       Min        Max
            -------------+--------------------------------------------------------
                    uhat |         0
            
            
            . 
            . 
            end of do-file

            Comment


            • #7
              OK, I see the problem. The code got interrupted with the original -xtset- command because you had a string variable instead of a numeric one. Because the do-file exited at that point, the local macros a and b that were defined in the earlier -vallist- statements went out of scope. So when you then resume the code after changing the -gvkey- to numeric, `a' and `b' are both empty--hence the foreach loops each believe they have nothing to do--and so they do nothing.

              What you need to do is go back to your original code, then patch that code by inserting the -destring gvkey, replace- command before the -xtset- command. Then run the entire thing from the top.
              Last edited by Clyde Schechter; 13 Oct 2014, 19:32. Reason: Correct typos.

              Comment


              • #8
                OK, I see the problem. The code got interrupted with the original -xtset- command because you had a string variable instead of a numeric one. Because the do-file exited at that point, the local macros a and b that were defined in the earlier -vallist- statements went out of scope. So when you the resume the code after changing the -gvkey- to string, `a' and `b' are both empty--hence the foreach loops each believe they have nothing to do--and so they do nothing.
                What you need to do is go back to your original code, then patch that code by inserting the -destring gvkey, replace- command before the -xtset- command. Then run the entire thing from the top

                Apologies for the apparent duplicate post here. I have no idea how it happened, and even less clue why it removed the italics and period from my final sentence. Some kind of database glitch, I guess?
                Last edited by Clyde Schechter; 13 Oct 2014, 19:30.

                Comment


                • #9
                  Thank you very much for your help. I got the loop to run now! However, it won't stop running. It's been running for hours. I have around 7000 observations. Do you have any suggestions for how to revise the code so it doesn't take all day to loop through all of the observations to calculate the regression and residual for each 2-digit SIC code and year of company data?

                  Comment


                  • #10
                    Well, 7,000 observations is not that large a data set, and regressions on 7,000 observations don't take very long. But you have a "combinatorial explosion" on your hands. Looking at your earlier output you have about 60 values of sic_2, 10 values of year, and 6,581 values of obs, for a total of just under 4,000,000 runs through the loops. But here's the thing: with 7,000 observations, nearly all of those 4,000,000 combinations will never even occur in your data. Now, because you loop over obs to exclude individual observations, I don't see an easy way to take that out. But you can certainly replace the double looping on sic_2 and year with a single loop on the combinations of it that actually exist.

                    Code:
                    egen combo = group(sic_2 year)
                    summarize combo
                    forvalues k = 1/`r(max)' {
                      forvalues  j= `=scalar(e)’/`=scalar(f)’ {
                        capture noisily reg ta x1 x2 x3 if combo == `k' & obs != `j', nocons
                        capture noisily predict uhat_2, resid
                        capture noisily replace uhat_2=. if e(N) < 10
                        capture noisily replace uhat= uhat_2 if combo == `k' & obs == `j'
                        capture noisily drop uhat_2
                        display `k', `j'
                      }
                     }
                    This should speed things up considerably.
                    Last edited by Clyde Schechter; 14 Oct 2014, 10:43. Reason: Correct coding error.

                    Comment


                    • #11
                      Thinking about it further, for any given combination of sic_2 and year, presumably only a small fraction of the observations have those particular values. Yet the inner loop goes over all observations in the entire data set, and recalculates the regression excluding no observations many times. So, I think even better will be:

                      Code:
                      egen combo = group(sic_2 year)
                      summarize combo
                      forvalues k = 1/`r(max)' {
                       forvalues  j= `=scalar(e)’/`=scalar(f)’ {
                        if combo[`j'] == `k' {
                         capture noisily reg ta x1 x2 x3 if combo == `k' & obs != `j', nocons
                         capture noisily predict uhat_2, resid
                         capture noisily replace uhat_2=. if e(N) < 10
                         capture noisily replace uhat= uhat_2 if combo == `k' & obs == `j'
                         capture noisily drop uhat_2
                        }
                        display `k', `j'
                         }
                       }
                      The -if combo[`j'] == `k'- guard will keep Stata from computing several regressions that exclude no observations and whose residuals never get stored anywhere anyhow.

                      I think this will speed things up for you considerably.
                      Last edited by Clyde Schechter; 14 Oct 2014, 10:47. Reason: Correct grammar error

                      Comment


                      • #12
                        Thank you very much! I will try this - My loop is still going on my original code because it is going through every observation as you said.

                        Comment


                        • #13
                          Thank you, Clyde. I cited you, and this post, on my blog. As a side note, Nick Cox helped someone else with a similar problem here:

                          http://www.stata.com/statalist/archi.../msg00519.html

                          I originally wrote homemade loops for these estimations because that's how I saw the problem structured in my head. I have been struggling to solve the problem efficiently with built-in Stata commands ever since. In retrospect, I'm kicking myself for not seeing that I could combine the industry and year into one loop. But the if statement you embedded looked like magic to me at first, and I spent a lot longer than I would like to admit figuring out exactly how that changed things. To add insult to injury that exact topic was covered in a Stata Net Course a few weeks ago.

                          Anyway, I learned quite a bit from your answer to Ally, and I appreciate your time.

                          Regards,
                          r

                          Comment


                          • #14
                            Actually, on further reflection, this can be simplified even further. The contents of the loop make use of the values of sic_2 and fyear, but those values are actually encoded in the observations being included in the regression. So, if I understand what is going on correctly, the whole thing can be boiled down to: a simple single loop over observations with minimal preparation beforehand:

                            Code:
                            gen sic_2= substr(sic,1,2)
                            destring sic_2, replace
                            xtset sic_2 fyear
                            gen uhat=.
                            gen ta= (ib-oancf)/L.at
                            gen x1= 1/L.at
                            gen x2= (d.revt – d.rect)/L.at
                            gen x3= ppegt/L.at
                            count
                            forvalues j = 1/`r(N)' {
                                capture noisily reg ta x1 x2 x3 if sic_2 == sic_2[`j'] & fyear == fyear[`j'] & _n != `j', ///
                                     nocons
                                capture noisily predict uhat_2, resid
                                capture noisily replace uhat_2=. if e(N) < 10
                                capture noisily replace uhat= uhat_2 in `j'
                                capture noisily drop uhat_2
                            }
                            which should run even more quickly than my previous suggestion.
                            Last edited by Clyde Schechter; 14 Oct 2014, 21:18.

                            Comment


                            • #15
                              Now that Clyde has worked to get the code in very decent form, further small improvements can be suggested:

                              Code:
                              gen sic_2 = real(substr(sic,1,2))
                              xtset sic_2 fyear
                              gen uhat = .
                              gen ta = (ib - oancf)/L.at
                              gen x1 = 1/L.at
                              gen x2 = (d.revt - d.rect)/L.at
                              gen x3 = ppegt/L.at
                              forvalues j = 1/`=_N' {
                                  capture noisily {
                                      reg ta x1 x2 x3 if sic_2 == sic_2[`j'] & fyear == fyear[`j'] & _n != `j', nocons
                                      if e(N) >= 10 {
                                          predict uhat_2 in `j', resid
                                          replace uhat = uhat_2 in `j'
                                          drop uhat_2
                                      }
                                  }
                              }
                              Commentary:

                              1. The call to destring can be cut. It's more direct just to use the function real(). The extra flexibility and security of destring is not needed here at all.

                              2. count was used to get the number of observations, but Stata already knows that as _N.

                              3. We want to use residuals if and only if they are for a regression with at least 10 observations. That decision can be made once and need not be repeated for every observation, including those not in the regression at all. Hence, replace the if qualifier with an if command.

                              4. predict with in in principle allows Stata to do the calculation just once where it's needed.

                              5. With a regression this simple, you could also sidestep predict. Instead of

                              Code:
                                          predict uhat_2 in `j', resid
                                          replace uhat = uhat_2 in `j'
                                          drop uhat_2
                              you could just go

                              Code:
                                        replace uhat = ta - (_b[x1] * x1 + _b[x2] * x2 + _b[x3] * x3) in `j'
                              Warning: None of this is tested.

                              Naturally, it is supremely rational to spend 10 minutes explaining how to save a few seconds in computation, but some of these issues arise much more widely.
                              Last edited by Nick Cox; 15 Oct 2014, 04:54.

                              Comment

                              Working...
                              X