Announcement

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

  • Generating a variable from regression output

    Hello Everyone,
    I have a small issue. I am trying to generate a variable from a regression output. Here, I am presenting my regression command.

    clear all
    use https://stats.idre.ucla.edu/stat/stata/faq/hsb2
    local dv read
    local iv write math
    keep `dv' `iv'
    reg `dv' `iv'

    I realize that the model is non-sensical, but the substance of the regression is irrelevant to me at this point. I am trying to figure out how to automate the construction of the following variable:

    gen y = 7.541 + .3283*math + .5196*write

    I, manually, did this from the regression output. For accuracy and better workflow management, how do I get Stata to get the information from the regression and generate this variable? I have been stuck on this for some time. Any help will be greatly appreciated.

    Thank you in advance,
    George

  • #2
    Code:
    predict
    has this as its first purpose. See the help on regression postestimation.

    Comment


    • #3
      Code:
      predict y
      Added: Crossed with #2

      Comment


      • #4
        Thank you. I will check into the predict command.

        Comment


        • #5
          I, finally, had some time to try predict and predict y. This is not what I am trying to convey. Let me try again using a different example, and possibly some additional explanation. I am getting closer, but I need a little assistance to bring this out.

          After seeing that predict is not really what I desired, I wrote it as a loop this time. Here is my syntax:

          ----------------------- copy starting from the next line -----------------------
          Code:
          clear 
          sysuse auto
          generate weight2 = weight^2
          regress mpg weight weight2 displ gear 
          mat temp = r(table)
          local colname:colnames temp //creates a local that contains all of the column names in the local
          local colnum=colsof(temp) //creates a local colnum that counts the number of columns for 'b'
          forvalues i=1/`colnum'{ 
                  local x:word `i' of `colname'
                  local b = temp[1,`i']
                  local cons = temp[1,5]
                  display "variable name is: `cons' + `b'*`x'"
                  }
          
          This code resulted in the following:   
          
          variable name is: 52.38079797850564 + -.0143803117072887*weight
          variable name is: 52.38079797850564 + 1.39764786645e-06*weight2
          variable name is: 52.38079797850564 + -.0030501250034161*displacement
          variable name is: 52.38079797850564 + -.211057072011991*gear_ratio
          variable name is: 52.38079797850564 + 52.38079797850564*_cons
          ------------------ copy up to and including the previous line ------------------

          I need it to be the following:
          variable name is: 52.38079797850564 + -.0143803117072887*weight + 1.39764786645e-06*weight2 + -.0030501250034161*displacement + -.211057072011991*gear_ratio

          How do I do this? Once I know this, I am on my way.

          Comment


          • #6
            You've said you want to "generate a variable," but what you show as code seems to indicate that you want to display a string containing a conventional representation of a regression equation, rather than create a variable. Your code is relatively close to the latter, but needs a little modification:
            Code:
            clear
            sysuse auto
            generate weight2 = weight^2
            regress mpg weight weight2 displ gear
            mat temp = r(table)
            local colname:colnames temp //creates a local that contains all of the column names in the local
            local colnum=colsof(temp) //creates a local colnum that counts the number of columns for 'b'
            local equation = ""
            forvalues i=1/`colnum'{
               local x:word `i' of `colname'
               local b = temp[1,`i']
               local equation = " + `b' * `x' `equation'"   
            }
            local equation = subinstr("`equation'", "* _cons", "", .)
            display "`equation'"
            Presuming this is what you want, I'd encourage you to indicate what you would like to do with this string. Most of the things I can think of for which this might be used are more easily done some other way in Stata, and if I'm right, I think you'd find knowing some other way to be useful.

            Comment


            • #7
              Mike, this is really close. My purpose, really, is to generate a numeric variable. I am trying to figure out a way to end up with a numeric syntax that creates variable y and it needs to be created as follows:
              gen y = 52.38079797850564 + -.211057072011991 * gear_ratio + -.0030501250034161 * displacement + 1.39764786645e-06 * weight2 + -.0143803117072887 * weight. I feel like your help up to this point is getting it closer to the final process. At the end, y is a numeric variable that I can use for other analyses. This was the only way that I could think of to get this where I need it. Any thoughts? Thank you for your help up this point.

              Comment


              • #8
                Why do you say -predict- isn't what you want. It calculates exactly the expression you show in #7.

                Comment


                • #9
                  If I am to test my mind reading abilities: I think what Original Poster wants is to automate setting up the estimated regression equation in his paper.

                  If I am right about this, the following might do it. Everything up to the loop in #5 stays the same. Then

                  Code:
                  . local holder
                  
                  . forvalues i=1/`colnum'{ 
                    2.         local x:word `i' of `colname'
                    3.         local b = temp[1,`i']
                    4. local holder "`holder' + `b'*`x'"
                    5. }
                  
                  . dis "`holder'"
                   + -.0143803117072872*weight + 1.39764786645e-06*weight2 + -.0030501250034142*displacement + -.21105
                  > 70720117674*gear_ratio + 52.38079797850261*_cons
                  :

                  Comment


                  • #10
                    I should start reading more carefully previous posts, Original Poster should start following the advice he asked for and received (-predict- is exactly what OP needs here) and otherwise if there is any need for this to be done manually, starting where Mike finished in #6:

                    Code:
                    . gen newvar = `equation'
                    
                    . qui regress mpg weight weight2 displ gear
                    
                    . predict newvar2
                    (option xb assumed; fitted values)
                    
                    . compare newvar newvar2
                    
                                                            ---------- difference ----------
                                                count       minimum      average     maximum
                    ------------------------------------------------------------------------
                    newvar=newvar2                 74
                                           ----------
                    jointly defined                74             0            0           0
                                           ----------
                    total                          74
                    
                    . summ newvar newvar2
                    
                        Variable |        Obs        Mean    Std. Dev.       Min        Max
                    -------------+---------------------------------------------------------
                          newvar |         74     21.2973    4.744436   13.77947   30.42675
                         newvar2 |         74     21.2973    4.744436   13.77947   30.42675

                    Comment


                    • #11
                      I apologize for doubting Clyde and Nick that suggested predict from the outset. Honestly, I didn't see its power. Now my eyes are wide open. Mike and Joro, thank you very much for drilling this down manually. I see everything that I need to see. I appreciate everyone's generosity of time and effort helping me solve this problem.

                      Comment

                      Working...
                      X