Announcement

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

  • Fitted and residual values routine?

    Dear STATA list users,
    I am relatively new to STATA and I need help with the following problem. I have a panel dataset and I want to calculate the residuals and fitted values of a preliminary regression, where each fitted and residual value is calculated for the unit of observation of my panel, and for all the years of the panel. I have currently 212 such units and so far I have been inputting the following commands in the example below about 212 times,
    EXAMPLE:
    xi: reg iGDP jGDP if PairsofAdjacentstatesij =="OR, ID", robust
    predict double resid155 if PairsofAdjacentstatesij =="OR, ID", residuals
    replace final=resid155 if PairsofAdjacentstatesij=="OR, ID"
    order resid155

    and for the the fitted values, again for 212 pairs of adjacent states:
    xi: reg iGDP jGDP if PairsofAdjacentstatesij =="OR, ID", robust
    predict common155 if PairsofAdjacentstatesij =="OR, ID"
    replace finalcommon=common155 if PairsofAdjacentstatesij=="OR, ID"
    . order common155

    However there must be a faster way of doing this instead of manually entering the commands.

    Can someone help wit this or indicate a good source (manual, website) to lear about this?

    Thank you very much for all the help you can provide on this.

    Elena


  • #2
    Well, taking your code at face value, it seems you want to loop over the values of a variable PairsofAdjacentstatesij.

    [code]
    levelsof PairsofAdjacentstatesij, local(pairs)

    foreach p of local pairs {
    regress iGDP jGDP if PairsofAdjacentstatesij == `"`p'"', robust
    // etc.
    }

    The -foreach- command is well worth learning. Check out -help foreach- and the corresponding section in the manual.

    By the way, leaving out the xi: on your regress command was deliberate on my part. First of all, as you wrote the code, it does nothing anyway, there being no i.* variables in the command. But even if your real code contains i.* variables, you are better off using factor variable notation and getting rid of the obsolete -xi- command. See -help fvvarlist- for more information about factor variable notation.
    Last edited by Clyde Schechter; 25 Oct 2014, 13:27.

    Comment


    • #3
      I do not know how to thank you! This works, it seems!

      Comment


      • #4
        Hi: I have one more question, if I could trouble you again.

        When I write the code you suggested (see below) STATA wants a new name for my residuals at every stage of the loop, naturally. If it is not too much trouble, how do I tell STATA to rename the residuals? Thank you. Elena

        levelsof PairsofAdjacentstatesij, local(pairs)

        foreach p of local pairs {
        regress iGDP jGDP if PairsofAdjacentstatesij == `"`p'"', robust
        predict double resid`"`p'"' if PairsofAdjacentstatesij ==`"`p'"', residuals
        replace final=resid`"`p'"' if PairsofAdjacentstatesij== `"`p'"'

        }
        resid already defined

        Comment


        • #5
          There is no way to get the -predict- command to do this in a single step: it always generates a new variable. But

          Code:
          levelsof PairsofAdjacentstatesij, local(pairs)
             foreach p of local pairs {
           regress iGDP jGDP if PairsofAdjacentstatesij == `"`p'"', robust
           predict double resid if PairsofAdjacentstatesij ==`"`p'"', residuals
           replace final=resid if PairsofAdjacentstatesij== `"`p'"'
           drop resid
             }
          should do it. After the replace statement executes, the information in resid is no longer needed, so you can just drop it, and predict will re-create it the next time through the loop.

          Comment


          • #6
            Unfortunately this is not working, I still get the code to get stuck after one loop, once the resid variable is generated. I understand what you suggest but -somehow- it does not seem to work. Thanks for your help anyway.

            Comment


            • #7
              Post the exact code you are using and the exact results that Stata is giving you and someone may be able to help. "It does not seem to work" doesn't give any clue as to what is going wrong. Open the advanced editor by pushing on the underlined A button, and then click on the # button. A pair of code block delimiters will appear. Paste the code and output directly from the results window between the delimiters. Do not re-type anything: directly paste from the Results window so that we can be sure that we are seeing exactly what you did and exactly how Stata responded to it.

              Another thing that is likely to be helpful in this situation is to put
              Code:
              set tracedepth 1
              set trace on
              at the beginning of the code you are running. That way we will be able to see what is going on inside the loop and where any error messages you are getting are being generated.

              Comment


              • #8
                "Elena": there is more about how to do what Clyde suggests -- and why -- in the Forum FAQ. Please re-read it. And also please note the strong preference for use of real names (firstname lastname). The FAQ explains how to re-register (it's very easy). Thank you.

                Comment


                • #9
                  I was wrong! The suggested code works. I have re-registered with my full name but I wanted to say thank you very much for all your help.

                  Comment

                  Working...
                  X