Announcement

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

  • Nested Loops and Lagged Explanatory Variables

    Hello Dear Statalist Members,

    I am using nested loops for a linear regression with the following code:

    forvalues l=1/4 {
    forvalues q=1/4 {
    forvalues p=1/4 {
    forvalues m=1/4 {
    forvalues n=1/4 {
    forvalues s=1/4 {

    reg var38 L(0/`l').s_gapcdax L(0/`q').s_gaphp L(0/`p').s_gapcrr L(0/`m').s_gapinvr L(0/`n').s_gapgdp L(0/`s').s_gapm2fer, robust
    }
    }
    }
    }
    }
    }

    My goal was to get the regression results for all the possible combinations of the lags for the independent variables, for example:

    s_gapcdax (-1)
    s_gaphp (-3)
    s_gapcrr (-2)
    s_gapinvr(-4)
    s_gapgdp(-2)
    s_gapm2fer(-1)

    and other combinations.
    However, at the end I get all the lags from 1 to 4 of all the variables in one regression.
    Could someone, please, tell me, what is wrong with my code? I would appreciate any help.

  • #2
    Welcome to Statalist!

    I think what you want is to replace your reg command with
    Code:
    reg var38 L`l'.s_gapcdax L`q'.s_gaphp L`p'.s_gapcrr L`m'.s_gapinvr L`n'.s_gapgdp L`s'.s_gapm2fer, robust
    assuming that you desire 46 = 4096 distinct regressions.

    Comment


    • #3
      William Lisowski Thank You very much! That was exactly what I wanted. One more question regarding this code.

      I have been trying to extract the regression coefficients, their significance levels, r2 and rmse in a separate table in order to compare the regression results. I have used the following code:

      forvalues l=1/4 {
      forvalues q=1/4 {
      forvalues p=1/4 {
      forvalues m=1/4 {
      forvalues n=1/4 {
      forvalues s=1/4 {
      reg var38 L`l'.s_gapcdax L`q'.s_gaphp L`p'.s_gapcrr L`m'.s_gapinvr L`n'.s_gapgdp L`s'.s_gapm2fer, robust
      est store m
      }
      }
      }
      }
      }
      }
      estout m, cells(b(star fmt(3))) stats(r2 rmse)


      At the end I get the table only for the last 4096-th regression with the lag L4 for all the variables.

      I have also tried with the command statsby:

      Code:
      forvalues l=1/4 {
      forvalues q=1/4 {
      forvalues p=1/4 {
      forvalues m=1/4 {
      forvalues n=1/4 {
      forvalues s=1/4 {
      statsby _b _r2 _rmse, by() saving(my_regs):reg var38 L`l'.s_gapcdax L`q'.s_gaphp L`p'.s_gapcrr L`m'.s_gapinvr L`n'.s_gapgdp L`s'.s_gapm2fer, robust
      }
      }
      }
      }
      }
      }
      use my_regs.dta


      For statsby I don't have specific names for the regressions to put them in by().

      With statsby as well as with estout i can't figure out how to label each of the 4096 regressions in order to get the results for each of them in the table like this together with significance levels:
      Reg. # 1 Reg. #4096
      b1 b1
      b2 b2
      b3 b3
      b4 b4
      b5 b5
      b6 b6
      R R
      RMSE RMSE
      Am I right using one of these commands getting a table like this? And how can I label/give names to each of the regressions in order to run one of these commands?
      Thanks again!

      Comment


      • #4
        I think you want to start by considering Stata's official estimates save command. Start by looking at help estimates to see all that is available. Since you have 4096 regressions, estimates store will not do the job for you.

        The statsby command is meant to function like the by command, running a given command repeatedly for different groups of observations. This is not what you need.

        Comment


        • #5
          Thank You very much for help!! I will try the other commands.

          Comment


          • #6
            Have a look here:
            http://www.statalist.org/forums/foru...imates-to-file

            essentially what I would do is after the reg command, write
            estimates save filname, append

            then after the STER (stored estimates file) is created, you can access it with the code posted in the thread I linked to. and then you can esttab it out to csv one by one, or several at once, etc.
            just note that you cannot have an unlimited amount of estimates "stored" in any given time, I don't recall the limit right now but I think it's several hundreds. So in the do where you pull back the regressions from the STER, I would put an "estimates drop _all" after some counter reaches some number.

            or maybe divide the "estimates save filename" to several files - which will also help you with making better sense of the results instead of pouring through thousands of tables at once. for example write "estimates save filename`t', append" with t changes every 200 models or so.

            Comment


            • #7
              Ariel Karlinsky Thank You for the help. I have used the code below to generate the results:

              forvalues l=1/4 {
              forvalues q=1/4 {
              forvalues p=1/4 {
              forvalues m=1/4 {
              forvalues n=1/4 {
              forvalues s=1/4 {
              reg var38 L`l'.s_gapcdax L`q'.s_gaphp L`p'.s_gapcrr L`m'.s_gapinvr L`n'.s_gapgdp L`s'.s_gapm2fer, robust
              estimates save mydata, append
              }
              }
              }
              }
              }
              }

              And it says that "4096 sets of estimation results in file", which was the number of my regressions. However, I couldn't figure out, how to use the rest of the commands posted in Your link, because the regressions there were stored with specific names (e.g. "estimates store r1"), but with the loop and 4096 regressions it doesn't seem to be manageable.
              I am not sure, if I have understood correctly what I could do with the code in the link. If so, I would be thankful, if You have corrected me.

              Comment


              • #8
                your'e on the right track
                Now you have 4096 regressions stored in a STER file called mydata. now what you want to do is to aceess these regressions and output them to a file. Here'e a code that does that, and I'll explain it a bit too:

                Code:
                clear all
                cd "F:\"
                cd "Stored Estimates"
                ****MACROS****
                local STER filename
                **************
                estimates clear
                estimates describe using `STER'
                forvalues j=1/`r(nestresults)' {
                    estimates use `STER', number(`j')
                    estimates store e`j'
                    esttab e`j' using reg-models.csv , nogaps label append
                    estimates drop e`j'
                }
                So the first lines up to the macros are just setting stata to the directory where the STER file is.
                estimates describe using filename will give you an output along the lines of:
                Code:
                13 sets of estimation results in file
                
                  Estimation results saved on 21dec2015 10:46, produced by
                
                     . reg var38
                And it saves the total number of estimates (13 in this example, 4096 in yours) in the local macro r(nestresults), so what the loop does is esentially pull out all the regressions stored in the file one by one, "name" them and then output them with esttab/estout (which i'm assuming your'e familiar with, and if your'e not you defnitly should: http://repec.org/bocode/e/estout/esttab.html). the "estimates drop" line is to avoid the limit of stored estimates stata allows to have, as I've mentioned earlier.

                ​Good luck!

                Comment


                • #9
                  Ariel Karlinsky Many many thanks for the code and explanation!! I have applied it and got the output on .csv.


                  Code:

                  clear all
                  cd "C:"
                  cd "Users\Tata\Desktop\Stataresults"
                  local mydata
                  estimates clear
                  estimates describe using "C:\Users\Tata\Desktop\Stataresults\mydata.ste r"
                  forvalues j=1/`r(nestresults)' {
                  estimates use "C:\Users\Tata\Desktop\Stataresults\mydata.ste r", number(`j')
                  estimates store e`j'
                  esttab e`j' using reg-models.csv , not noconstant r2 noobs lines nogaps label append
                  estimates drop e`j'
                  }



                  If there is something to improve, please let me know.
                  Thank You again!

                  Comment


                  • #10
                    Looks fine I see that you are familiar with esttab/estout, so of course you can output several models into 1 "wide" table and so can look through say 1000 tables with 4 columns each instead of 4096.. but that's up to you how you format with esttab so it will be as comfortable as possible for you to see what you want to see

                    Comment


                    • #11
                      Thanks for the hint, I will try that out too!

                      Comment

                      Working...
                      X