Announcement

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

  • Double loop with forvalues

    Dear Statalist members,

    I am trying to run my regression through 2 loops. I want to run a regression for j = SAMPLE_60_1 until SAMPLE_60_10 (a dummy variable) and i for Halfyear == 13 until Halfyear == 33 (denoting the halfyear subsample).
    I run a regression for each half year on a part of sample (when SAMPLE_60_`j' equals 1) and use it to forecast the remainder of the data (when SAMPLE_60_`j' equals 0). I regress the forecasted values on the observed dependent variable and save this in a file using outreg2.

    I expect the ouput to be like this:
    HY13S1 HY14S1 HY15S1 HY16S1 HY17S1 HY18S1 HY13S1 HY13S2 ....
    COEFF
    ...
    However, it only calculates HY13S1 and HY14S1 and then stoppes as all variables in the regression for HY14S1 are omitted. However, I thought by using
    Code:
    capture
    I step around this problem.


    My question: Do correctly loop using forvalues?

    Simplified code
    Code:
    forvalues j = 1/10 {
         forvalues i = 13/18 {
              capture xtreg DEP IDEP1 IDEP2 IDEP3 i.Year, fe if Halfyear==`i' & SAMPLE_60_`j' == 1
              capture predict HY`i'_fcst_B`j' if Halfyear==`i' & SAMPLE_60_`j' == 0, xb
              capture xtreg HY`i'_fcst_B`j' DEP
              capture outreg2 using doc, append
         }
    }


    Actual code
    Code:
    forvalues j = 1/3 {
         forvalues i = 13/18 {
              capture quietly xtreg W_l_G_Spread L(1).W_l_G_Spread  L(0/1).W_l_LiqM L(0/1).W_l_Price_Equity L(0).W_l_EvolM L(0/2).W_l_brent i.Year, fe if Halfyear==`i' & SAMPLE_60_`j' == 1
              capture quietly predict HY`i'_fcst_B`j' if Halfyear==`i' & SAMPLE_60_`j' == 0, xb
              capture quietly xtreg HY`i'_fcst_B`j' W_l_G_Spread
              capture quietly outreg2 using E_S60_HF_M1A, append dec(3) ctitle(HF`i'S`j')
         }
    }

    Thanks in advance for your time!

  • #2
    So, the moral of the story is that -capture- never solves any problems. All it does is squelch the complaint about the problem and allow you to skip over it. -capture- should rarely be used in the way you show. Rather, it should be used in conjunction with a subsequent -if--else- construct to handle the problem and non-problem cases separately. By just putting -capture- in front of the commands, you are simply allowing Stata to breeze on by even though your regressions are not producing any output. So Stata is not stopping: it's just skipping over things.

    The real problem here is not the code. The real problem here is why "all the variables in the regression for HY14S1 are omitted." (By the way, you have two regressions. Which one is experiencing this problem? Or is it both?) Either way it suggests there is something wrong with the data for half year 14 and I recommend you focus on that. Once you understand what is going on with your data, it will be easier to figure out what to do next.

    I would also note that when you want help with code, in addition to showing the code and describing what's going wrong, it is usually necessary to also post example data so that people can have something to test revised code on, or to explore the data to discover why the data itself is unsuitable for the intended analyses.

    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 15.1 or a fully updated version 14.2, it is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment

    Working...
    X