Announcement

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

  • Error in loop

    Dear all,

    I use the following loop in order to reconstruct a rolling Fama & MacBeth regression:

    Code:
    fvset base 3 dummy_momentum6m dummy_nearness12mhigh
    
    tsset permno time
    
    statsby _b _se, by(permno) : reg h_return24m ln_size_ i.dummy_nearness12mhigh i.dummy_momentum6m, vce(robust)
    
    local window=12
    local vars "ln_size_ i.dummy_nearness12mhigh i.dummy_momentum6m"  
    qui{
    sum permno, meanonly
    local start=r(min)
    local end=`start'+`window'
    local fin=r(max)
    gen _start=.
    gen _end=.
    
    foreach var in `vars' cons {
        gen b_`var'=.
        gen se_`var'=.
        }
    
    local i=1
    while `end'<=`fin' {
         replace _start=`start' in `i'
        replace _end=`end' in `i'
    
        foreach var in `vars' cons {
            qui sum _b_`var' if inrange(permno, `start' , `end')
            replace b_`var' = r(mean) in `i'
            replace se_`var'= (r(sd)/sqrt(r(N))) in `i'
            }
        local start=`start'+1
        local end=`end'+1
        local i=`i'+1
    }
    keep _start _end se_* b_*
    drop if _start==.
    }
    However, I get the following error messages

    Code:
    local vars "ln_size_ i.dummy_nearness12mhigh i.dummy_momentum6m"  
    invalid syntax
    r(198);
    Code:
    . foreach var in `vars' cons {
    .     gen b_`var'=.
    .     gen se_`var'=.
    .     }
    { required
    {
    local i=1
    Code:
    . keep _start _end se_* b_*
    . drop if _start==.
    . }
    r(100);
    Can anyone explain what exactly is going wrong in the loop? I would like to know how I get the loop to run correctly.

    Thank you very much!

  • #2
    I cannot replicate your difficulty with the -local vars- command. It runs without error on my system. Is your Stata installation up to date? Have you tried re-launching Stata, or re-booting your computer and trying again? Are you sure that's exactly what you typed? In any case, do also try it again without the quotation marks. They shouldn't be causing you problems, but, on the other hand, they are not necessary.

    As for your loop, I, again, cannot reproduce your error, but I get a different, and expected, error message. Because `vars' includes i.dummy_nearness12mhigh and i.dummy_momentum6m, when you hit the second iteration of the loop and try to create b_i.dummy_nearness12mhigh, Stata appropriately complains that the variable name is illegal.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      As for your loop, I, again, cannot reproduce your error, but I get a different, and expected, error message. Because `vars' includes i.dummy_nearness12mhigh and i.dummy_momentum6m, when you hit the second iteration of the loop and try to create b_i.dummy_nearness12mhigh, Stata appropriately complains that the variable name is illegal.
      Thank you for your reply. I wasn't aware of the fact that b_i.dummy_nearness12mhigh is an illegal name. I updated my loop with your feedback:

      Code:
      tsset permno time
      
      statsby _b _se, by(permno) : reg return ln_size_ dummynearness12mhigh_w dummynearness12mhigh_l dummymomentum12m_w dummymomentum12m_l, vce(robust)
      
      local window=6
      local vars ln_size_ dummynearness12mhigh_w dummynearness12mhigh_l dummymomentum12m_w dummymomentum12m_l  
      qui{
      sum permno, meanonly
      local start=r(min)
      local end=`start'+`window'
      local fin=r(max)
      gen _start=.
      gen _end=.
      
      foreach var in `vars' cons {
          gen b_`var'=.
          gen se_`var'=.
          }
      
      local i=1
      while `end'<=`fin' {
           replace _start=`start' in `i'
          replace _end=`end' in `i'
      
          foreach var in `vars' cons {
              qui sum _b_`var' if inrange(permno, `start' , `end')
              replace b_`var' = r(mean) in `i'
              replace se_`var'= (r(sd)/sqrt(r(N))) in `i'
              }
          local start=`start'+1
          local end=`end'+1
          local i=`i'+1
      }
      keep _start _end se_* b_*
      drop if _start==.
      }
      It works much better as I get only "dots" during
      Code:
      statsby _b _se, by(permno) : reg return ln_size_ dummynearness12mhigh_w dummynearness12mhigh_l dummymomentum12m_w dummymomentum12m_l, v
      > ce(robust)
      -part. However, I still get an error:

      Code:
      .         }
      .     local start=`start'+1
      .     local end=`end'+1
      .     local i=`i'+1
      . }
      observation numbers out of range
      . keep _start _end se_* b_*
      . drop if _start==.
      . }
      r(198);
      Do you have any idea how to overcome this?

      Kind regards and thanks for your help,

      Huib

      Comment


      • #4
        I think your logic with i, start, end, and fin is probably faulty here. The "observation number out of range" message here has to be coming from one of the commands that includes an -in `i'- clause, and Stata is telling you that `i' has been increased to a value that is beyond the end of the data set. You bump up i each time you go through the loop. You also move start and end up by 1 each time you go through the loop. Now the loop continues when `end' > `fin'. But `fin' corresponds to the largest value of permno in the data set, and you start out with `start' equal to the smallest value of permno. If the permno values are not simply consecutive numbers (and I'm guessing they are not), then the loop will try to iterate (highest permno) - (lowest permno) times, and this number can easily be much larger than the number of observations in the data set.

        I don't grasp what you are actually trying to calculate in this loop. I can't quite wrap my head around it and come up with anything that makes sense to me. So I'm not able to suggest a correction to your code. I'm hoping that the above paragraph, combined with your understanding of what you're trying to do here, will suffice.

        I will note this: it appears that what you are doing has something to do with calculating means and standard deviations of certain variables over certain ranges of values of permno. If that is right, look into Robert Picard, Nick Cox, and Roberto Ferrer's lovely program -rangestat- which you can get from SSC. It may well enable you to reduce that entire loop to a single line of code.

        Comment


        • #5
          Again, I would like to thank you for taking the time to look into my problem and I appreciate your advice.

          Originally posted by Clyde Schechter View Post
          If the permno values are not simply consecutive numbers (and I'm guessing they are not)
          This is indeed true, the permno values are not consecutive numbers. The lowest value for permno is 10006 and the highest value is 93436. There are a total of 5138 unique permno values.

          Please allow me to explain what I am trying to do. I use the following loop in order to reconstruct a rolling Fama & MacBeth regression where the Fama-MacBeth procedure consist of the following three steps:
          1. Run N time-series regressions.
          2. Perform one cross-sectional regression, where the N coefficient estimates from (1) are your explanatory variables.
          3. Repeat (1) and (2) going ahead in time to get a time-series of coefficient estimates from (2). Use this time-series to obtain the "average coefficient" and its standard error.
          BasicalIy I am trying to recreate the xtfmb command (install ssc xtfmb) and after reading the following topic (https://www.statalist.org/forums/for...nd-fm-vs-xtfmb) I believed that the code posted is the rolling equivalent of the xtfmb command.

          I hope that this information makes my problem a little bit more clear. Do you know if there is a way to use the "hard"(/actual number) values in the loop?


          Comment


          • #6
            I spotted my errror as I should have used time instead of permno in the by-function part. Please forgive me for wasting your time!

            Comment


            • #7
              No need to apologize. I assure you there isn't a single person on this forum who has not made similar errors at some point. And sometimes it's just really hard to see them until somebody else points you in the right direction.

              As for wasting my time, like everyone else here, I choose to respond to whichever posts I feel I can make a useful comment on and which interest me. So if I waste my time, the fault is purely my own.

              Comment


              • #8
                Note that the code posted in #1 appears to be an adaptation of a 2016 Statalist post by Carole J. Wilson.

                You can easily implement a rolling Fama-Macbeth two-step regression using rangestat (from SSC). To install, type in Stata's Command window:
                Code:
                ssc install rangestat
                The following example performs the procedure on a window of 11 years:
                Code:
                * step 1: cross-sectional regression per time period
                webuse grunfeld, clear
                rangestat (reg) invest mvalue kstock, interval(year 0 0)
                
                * reduce to results per time period
                bysort year: keep if _n == 1
                keep year b_*
                
                * step 2: average estimates over a rolling window of 11 period
                rangestat (mean) b_* (sd) b_* (count) N=year, interval(year -10 0)
                
                * require full sample per rolling window
                drop if N < 11
                
                * calculate standard errors
                foreach v in mvalue kstock cons {
                    gen fmse_`v' = b_`v'_sd / sqrt(N)
                }
                list year b_*_mean fmse*
                and the results:
                Code:
                . list year b_*_mean fmse*
                
                     +----------------------------------------------------------------------------+
                     | year   b_mvalu~n   b_kstoc~n   b_cons_m~n   fmse_m~e   fmse_k~k   fmse_c~s |
                     |----------------------------------------------------------------------------|
                  1. | 1945    .0994992   .11895319   -12.440371   .0072946   .0305753   3.682477 |
                  2. | 1946   .10272196   .11962666   -13.017342   .0080951   .0303152   3.523762 |
                  3. | 1947   .11001465   .12416612     -14.7402   .0095384   .0279698   2.378176 |
                  4. | 1948   .11930133   .10050449   -13.656084   .0107238   .0299908   3.048406 |
                  5. | 1949   .12780846   .07267906   -11.586976   .0100058   .0271177    3.45679 |
                     |----------------------------------------------------------------------------|
                  6. | 1950   .13787162   .05260995   -10.770653   .0087192   .0251487   3.324724 |
                  7. | 1951   .14584812    .0240329   -5.9356327   .0084789   .0243643   4.323408 |
                  8. | 1952   .15349877   .00176381   -3.7708758   .0091028   .0201537   4.335404 |
                  9. | 1953   .15712138   .00428466    -6.728536   .0093942   .0211529   5.977849 |
                 10. | 1954   .15856702   .02482672   -16.785744   .0088998   .0358454   13.00523 |
                     +----------------------------------------------------------------------------+
                The following replicates the results for the final window (1954) using xtfmb (from SSC):
                Code:
                webuse grunfeld, clear
                xtfmb invest mvalue kstock if inrange(year, 1944, 1954)
                and the results:
                Code:
                . xtfmb invest mvalue kstock if inrange(year, 1944, 1954)
                
                Fama-MacBeth (1973) Two-Step procedure           Number of obs     =       110
                                                                 Num. time periods =        11
                                                                 F(  2,    10)     =    224.22
                                                                 Prob > F          =    0.0000
                                                                 avg. R-squared    =    0.8714
                ------------------------------------------------------------------------------
                             |            Fama-MacBeth
                      invest |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                -------------+----------------------------------------------------------------
                      mvalue |    .158567   .0088998    17.82   0.000     .1387371     .178397
                      kstock |   .0248267   .0358454     0.69   0.504    -.0550418    .1046953
                       _cons |  -16.78574   13.00523    -1.29   0.226    -45.76321    12.19172
                ------------------------------------------------------------------------------
                
                
                .

                Comment


                • #9
                  Thank you both for your answers!

                  Comment


                  • #10
                    @Robert Picard Hi Robert, thank you for providing the codes which are very helpful! I am also using the Fama MacBeth regression to predict the next month portfolio returns based on the current month's portfolio information, which is very similar with this original post but my case is not in a rolling window, so in this case, do you possibly know how to adjust the following code to satisfy that?

                    Here is my dataset:

                    Code:
                    * Example generated by -dataex-. To install: ssc install dataex
                    clear
                    input float(bygroup ym) double(b_mktrf b_cons)
                    1 276 .8156591313276137 -.0050817259693675355
                    1 277 .8156591313276137 -.0050817259693675355
                    1 278 .8156591313276137 -.0050817259693675355
                    1 279 .8156591313276137 -.0050817259693675355
                    1 280 .8156591313276137 -.0050817259693675355
                    1 281 .8156591313276137 -.0050817259693675355
                    1 282 .8156591313276137 -.0050817259693675355
                    1 283 .8156591313276137 -.0050817259693675355
                    1 284 .8156591313276137 -.0050817259693675355
                    1 285 .8156591313276137 -.0050817259693675355
                    1 286 .8156591313276137 -.0050817259693675355
                    1 287 .8156591313276137 -.0050817259693675355
                    1 288 .8156591313276137 -.0050817259693675355
                    1 289 .8156591313276137 -.0050817259693675355
                    1 290 .8156591313276137 -.0050817259693675355
                    1 291 .8156591313276137 -.0050817259693675355
                    1 292 .8156591313276137 -.0050817259693675355
                    1 293 .8156591313276137 -.0050817259693675355
                    1 294 .8156591313276137 -.0050817259693675355
                    1 295 .8156591313276137 -.0050817259693675355
                    1 296 .8156591313276137 -.0050817259693675355
                    1 297 .8156591313276137 -.0050817259693675355
                    1 298 .8156591313276137 -.0050817259693675355
                    1 299 .8156591313276137 -.0050817259693675355
                    1 300 .8156591313276137 -.0050817259693675355
                    2 276 .8593988336759226   .000754324081336093
                    2 277 .8593988336759226   .000754324081336093
                    2 278 .8593988336759226   .000754324081336093
                    2 279 .8593988336759226   .000754324081336093
                    2 280 .8593988336759226   .000754324081336093
                    2 281 .8593988336759226   .000754324081336093
                    2 282 .8593988336759226   .000754324081336093
                    2 283 .8593988336759226   .000754324081336093
                    2 284 .8593988336759226   .000754324081336093
                    2 285 .8593988336759226   .000754324081336093
                    2 286 .8593988336759226   .000754324081336093
                    2 287 .8593988336759226   .000754324081336093
                    2 288 .8593988336759226   .000754324081336093
                    2 289 .8593988336759226   .000754324081336093
                    2 290 .8593988336759226   .000754324081336093
                    2 291 .8593988336759226   .000754324081336093
                    2 292 .8593988336759226   .000754324081336093
                    2 293 .8593988336759226   .000754324081336093
                    2 294 .8593988336759226   .000754324081336093
                    2 295 .8593988336759226   .000754324081336093
                    2 296 .8593988336759226   .000754324081336093
                    2 297 .8593988336759226   .000754324081336093
                    2 298 .8593988336759226   .000754324081336093
                    2 299 .8593988336759226   .000754324081336093
                    2 300 .8593988336759226   .000754324081336093
                    3 276 .8439623407192625  .0053948957226539985
                    3 277 .8439623407192625  .0053948957226539985
                    3 278 .8439623407192625  .0053948957226539985
                    3 279 .8439623407192625  .0053948957226539985
                    3 280 .8439623407192625  .0053948957226539985
                    3 281 .8439623407192625  .0053948957226539985
                    3 282 .8439623407192625  .0053948957226539985
                    3 283 .8439623407192625  .0053948957226539985
                    3 284 .8439623407192625  .0053948957226539985
                    3 285 .8439623407192625  .0053948957226539985
                    3 286 .8439623407192625  .0053948957226539985
                    3 287 .8439623407192625  .0053948957226539985
                    3 288 .8439623407192625  .0053948957226539985
                    3 289 .8439623407192625  .0053948957226539985
                    3 290 .8439623407192625  .0053948957226539985
                    3 291 .8439623407192625  .0053948957226539985
                    3 292 .8439623407192625  .0053948957226539985
                    3 293 .8439623407192625  .0053948957226539985
                    3 294 .8439623407192625  .0053948957226539985
                    3 295 .8439623407192625  .0053948957226539985
                    3 296 .8439623407192625  .0053948957226539985
                    3 297 .8439623407192625  .0053948957226539985
                    3 298 .8439623407192625  .0053948957226539985
                    3 299 .8439623407192625  .0053948957226539985
                    3 300 .8439623407192625  .0053948957226539985
                    4 276 .8766212028249569   .014639934835795159
                    4 277 .8766212028249569   .014639934835795159
                    4 278 .8766212028249569   .014639934835795159
                    4 279 .8766212028249569   .014639934835795159
                    4 280 .8766212028249569   .014639934835795159
                    4 281 .8766212028249569   .014639934835795159
                    4 282 .8766212028249569   .014639934835795159
                    4 283 .8766212028249569   .014639934835795159
                    4 284 .8766212028249569   .014639934835795159
                    4 285 .8766212028249569   .014639934835795159
                    4 286 .8766212028249569   .014639934835795159
                    4 287 .8766212028249569   .014639934835795159
                    4 288 .8766212028249569   .014639934835795159
                    4 289 .8766212028249569   .014639934835795159
                    4 290 .8766212028249569   .014639934835795159
                    4 291 .8766212028249569   .014639934835795159
                    4 292 .8766212028249569   .014639934835795159
                    4 293 .8766212028249569   .014639934835795159
                    4 294 .8766212028249569   .014639934835795159
                    4 295 .8766212028249569   .014639934835795159
                    4 296 .8766212028249569   .014639934835795159
                    4 297 .8766212028249569   .014639934835795159
                    4 298 .8766212028249569   .014639934835795159
                    4 299 .8766212028249569   .014639934835795159
                    4 300 .8766212028249569   .014639934835795159
                    end
                    format %tm ym
                    Code:
                    egen bygroup = group(P1 double_P2), label
                    * step 1: cross-sectional regression for each portfolio per month
                    rangestat (reg) exret_m mktrf, interval(bygroup 0 0)
                    
                    * reduce to results per time period
                    bysort bygroup ym: keep if _n == 1
                    keep bygroup ym b_*
                    
                    * step 2: average estimates
                    rangestat (mean) b_* (sd) b_* (count) N=ym, interval(ym 0 0)
                    Many thanks for your help in advance!
                    Last edited by Jae Li; 01 Apr 2018, 07:42.

                    Comment

                    Working...
                    X