Announcement

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

  • Predicting Residuals by groups

    Hi,
    I am new to Stata and I need some help on the following problem.
    I have used regression by groups and would now like to predict residuals for each group. Lets say I have estimated the regression based on the industry code of the companies and for each industry code I now need the residuals. The simple predict, residuals is not helping nor is e(sample) as it only computes the regression based on the last estimation. I need to get the residuals based on groups.
    Any help would be much appreciated.

  • #2
    You will want to write a little loop to accomplish this:

    Code:
    gen residuals==.
    levelsof industry_code_variable, local(level)
    foreach i of local level {
        reg..... if industry_code_variable==`i'
        predict _res, res
        replace residuals=_res if industry_code_variable==`i'
        drop _res
        }
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      .
      Last edited by rai rai; 29 May 2016, 22:34.

      Comment


      • #4
        Originally posted by Carole J. Wilson View Post
        You will want to write a little loop to accomplish this:

        Code:
        gen residuals==.
        levelsof industry_code_variable, local(level)
        foreach i of local level {
        reg..... if industry_code_variable==`i'
        predict _res, res
        replace residuals=_res if industry_code_variable==`i'
        drop _res
        }
        Thanks for your help Carole. However I see that the regression and residuals are being computed only for the first industry code, the rest are skipped. I have around 25 different codes and I see that its being computed for the first value of industry code. I am getting an error r(110) and r(111). Could you please advice.
        Last edited by rai rai; 29 May 2016, 23:05.

        Comment


        • #5
          Originally posted by rai rai View Post
          Could you please advice.
          You might want to take a look at this, especially the first subsection.

          The r(110) suggests that you're not dropping the variable that holds the residuals temporarily, and r(111) that you're not referring to a variable that exists or are referring to a variable by an ambiguous abbreviation.

          Comment


          • #6
            The code is working fine. There was just a little problem in the first line of the code. Thanks everyone.

            Comment


            • #7
              rai rai: Please consider using a full real name. For why we ask that and what to do about it, see

              http://www.statalist.org/forums/help#realnames

              http://www.statalist.org/forums/help#adviceextras Section 3

              Comment


              • #8
                rai rai.
                I do share Nick's recommendation about re-registering according to the list requirements.
                Besides, as corrections to others' replies are often helpful (for me, at least), it would be laudable posting what you mean by "...a little problem in the first line of the code..." (double equal sign?). Thanks.
                Kind regards,
                Carlo
                (Stata 19.0)

                Comment


                • #9
                  I appreciate all the suggestions made and would conform to the rules of the forum on being crisp with my answers. However its a knowledge sharing forum and I am only driven by the desire to learn from all the peers and share whatever little I know but I don't think using real name would change this motivation of mine. We do have great authors who used to write under pseudo names and their work was well received by all. By using a pseudo name I don't intend to disrespect anyone its just a manifestation of my privacy (Our propensity to privacy differs). Furthermore, my liberty of asking questions and to gain knowledge increases if I am not under the watchful and judgmental eyes of others (which often happens if I use my real name and my identity is tracked). This is more true for a beginner like me. Thanks for the suggestions but I have a different opinion in this case.

                  Comment


                  • #10
                    rai rai:
                    you're obviously welcome.
                    The previous replies had the only intent to present you the rules of the game.
                    I'm personally not that interested if somebody catches me posting statistical nonsenses (it happened to me in the past and in all likelihood will occur again in the future; the reverse side of the coin is that I can still learn) or writing in a far from Oxonian English (I like this language, but I'm not mother tongue).
                    However the main point remains: it would be interesting to see the correction you made in Carole's code.
                    Kind regards,
                    Carlo
                    (Stata 19.0)

                    Comment


                    • #11
                      rai rai: Using real names is a request, not a rule, but some here are disinclined to support those who don't follow the request.

                      Comment

                      Working...
                      X