Announcement

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

  • Loop variables

    Hello everyone,

    I have a question and would like to ask. I want to regress Y on X, and repeat many times (Y1 on X1, Y2 on X2,...).

    Could you please help how I can do that on Stata?

    Many thanks
    Y1 X1 Y2 X2 Y3 X3 Y4 X4 Y5 X5
    0.637769 0.187455 -2.12231 -0.86289 2.094241 2.539891 -2.34807 -0.33069 3.9823 1.55556
    -0.913 0.467353 -6.56205 -1.22817 -2.22985 -0.36934 -0.27933 0.10573 1.955104 1.076606
    -0.93225 -0.00529 0.885496 -0.89531 -1.7094 -0.45718 0.990099 -0.3785 0.709224 0.234022
    -0.35015 0.059777 0.998789 1.032876 -0.52632 -0.07321 2.380952 0.484324 0.497164 -1.16401
    0.142747 0.537265 -1.61822 1.043759 -2.08696 -0.621 4.901961 2.002086 -0.98592 -1.15499
    0 -0.01536 3.015535 0.032317 -0.70547 -0.21006 2.599179 0.896746 0.282684 0.341454
    -0.33991 -0.03679 -1.30101 -0.58395 0.53286 -1.69638 2.536716 -0.06661 1.813659 0.51247

  • #2
    try this:
    Code:
    forval i=1/5 {
        regress Y`i' X`i'
    }
    if you have more than 5 such combinations, just change the "forval" line to whatever is appropriate; see
    Code:
    h forval

    Comment


    • #3
      Thank Rich.

      This command works properly. Great!

      Regards,

      Comment


      • #4
        Hi Rich Goldstein , I am sorry for bothering you a bit. How can I save these coefficients from these regressions instead of copying one by one?

        Comment


        • #5
          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input float(y1 x1 y2 x2 y3 x3 y4 x4 y5 x5)
          .637769 .187455 -2.12231  -.86289 2.094241 2.539891 -2.34807  -.33069   3.9823  1.55556
            -.913 .467353 -6.56205 -1.22817 -2.22985  -.36934  -.27933   .10573 1.955104 1.076606
          -.93225 -.00529  .885496  -.89531  -1.7094  -.45718  .990099   -.3785  .709224  .234022
          -.35015 .059777  .998789 1.032876  -.52632  -.07321 2.380952  .484324  .497164 -1.16401
          .142747 .537265 -1.61822 1.043759 -2.08696    -.621 4.901961 2.002086  -.98592 -1.15499
                0 -.01536 3.015535  .032317  -.70547  -.21006 2.599179  .896746  .282684  .341454
          -.33991 -.03679 -1.30101  -.58395   .53286 -1.69638 2.536716  -.06661 1.813659   .51247
          end
          
          
          frame create coefficients str32 (dep_var ind_var) float b se
          forvalues i = 1/5 {
              regress y`i' x`i'
              frame post coefficients ("y`i'") ("x`i'") (_b[x`i']) (_se[x`i'])
          }
          
          frame coefficients: list, noobs clean
          At the end of this code, the original data are in the default frame, and the coefficients are in the coefficients frame. You can then do further analyses on the coefficients themselves, or you can save them as a data set for future use.

          Comment


          • #6
            Thank Clyde Schechter. This command works properly.

            Comment


            • #7
              Hi Clyde Schechter , I also want to extract the intercept, so I put as this but it did not work. Could you help me to have a look?

              frame create coefficients str32 (dep_var ind_var) float b se forvalues i = 1/5 { regress y`i' x`i' frame post coefficients ("y`i'") ("x`i'") (_cons[x`i']) (_b[x`i']) (_se[x`i']) } frame coefficients: list, noobs clean

              Comment


              • #8
                You changed the -frame post- command (although not quite correctly), but you also need to change the -frame create- command. Also the syntax for getting the constant term and its standard error is _b[_cons] and _se[_cons]. So
                Code:
                frame create coefficients str32 (dep_var ind_var) float b se cons se_cons
                forvalues i = 1/5 {
                    regress y`i' x`i'
                    frame post coefficients ("y`i'") ("x`i'") (_b[x`i']) (_se[x`i']) ///
                        (_b[_cons]) (_se[_cons])
                }
                
                frame coefficients: list, noobs clean
                A couple of things about your post. "It did not work" is usually not helpful and usually results in a question getting no answer. "It did not work" could mean many different things. Did Stata crash? Did it hang? Did it halt with error messages? Did it proceed but give incorrect results? If the last, in what way were they incorrect? In this particular situation, I could immediately see problems with the code, so I responded accordingly. But in general, the most you could expect to get from "It did not work" is a request for the details of what went wrong.

                In addition, your code was displayed here as a single line jumble. Because it is short, and because I knew it was modeled on the more readable code in #5, I could recognize its elements and figure out what was going on. But, again, in general if one posts jumbled code, the most one can hope for is a request for a cleaner post of the code. Use code delimiters. If you are not familiar with those, read the Forum FAQ with special attention to #12 to learn how. Also, when you are showing code that you want someone to troubleshoot, it is important that you post the exact code you are working with, with no edits at all. That means, among other things, that you should not type it manually into the Forum edit box. Instead you should copy/paste it from your do-file, or log file, or Results window, to assure that it will be exactly what you are using. There is no such thing as a minor change in code.

                Comment


                • #9
                  Thank Clyde Schechter. I added (_b[_cons]) (_se[_cons]) and the output showed the intercept.

                  I also learnt from your suggestions for future communication on this forum. Much appreciate this.

                  Best regards,

                  Comment

                  Working...
                  X