Announcement

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

  • time series regression error

    Hi
    I am trying to do time series regression, however I got error message. Here is my code:

    encode Ticker , generate(id)
    tsset id Week
    gen beta=.
    gen IC=.

    egen mi=min(id)
    egen mx=max(id)
    local mi
    local mx
    local hi=weekmax
    local lo=weekmax-156

    forvalues i = `lo'/`hi' {
    forvalues d = `mi'/`mx' {
    local j = `i' - 11
    capture regress N_Return N_Forcast_return if inrange(Week, `j', `i')& id==`d'
    replace beta=_b[N_Forcast_return] if id==`d'
    replace IC=(e(r2))^0.5 if beta>0 & id==`d'
    replace IC=(e(r2))^0.5 if beta<0 & id==`d'
    }
    }

    When I just run regress N_Return N_Forcast_return if inrange(Week, 500, 580)& id==250, it works.
    my error message: invalid syntax

    Can anyone helps me to debug it?

    Thank you.

    Best,
    Xixi Lin

  • #2
    Code:
    encode Ticker , generate(id)
    tsset id Week
    gen beta=.
    gen IC=.
    
    egen mi=min(id)
    egen mx=max(id)
    local mi
    local mx
    local hi=weekmax
    local lo=weekmax-156
    
    forvalues i = `lo'/`hi' {
    forvalues d = `mi'/`mx' {
    local j = `i' - 11
    capture regress N_Return N_Forcast_return if inrange(Week, `j', `i')& id==`d'
    replace beta=_b[N_Forcast_return] if id==`d'
    replace IC=(e(r2))^0.5 if beta>0 & id==`d'
    replace IC=(e(r2))^0.5 if beta<0 & id==`d'
    }
    }
    The code that is in red is the source of your problem. You used egen to create variables mi and mx that are not really variables, just constants containing the smallest and largest values of id. There is nothing inherently wrong with that, though it is wasteful. Then you define two local macros mi and mx, but you leave them empty. So when Stata gets to the -forvalues d = ...- statement and substitutes nothing for `mi' and `mx', it is faced with

    Code:
    forvalues d = / {
    which is not legal syntax.

    What you want to do is put the minimum and maximum values of id into the local macros mi and mx directly. So the first red code should be:

    Code:
    summ id
    local mi = r(min)
    local mx = r(max)
    If you do that, it should all run smoothly. (No changes to the -forvalues d = ...- command needed.)

    Comment


    • #3
      Hi Clyde,

      I changed my code according to your suggestion:

      encode Ticker , generate(id)
      tsset id Week
      gen beta=.
      gen IC=.

      summ id
      local mi = r(min)
      local mx = r(max)
      local hi=weekmax
      local lo=weekmax-156

      forvalues i = `lo'/`hi' {
      forvalues d = `mi'/`mx' {
      local j = `i' - 11
      capture regress N_Return N_Forcast_return if inrange(Week, `j', `i')& id==`d'
      replace beta=_b[N_Forcast_return] if id==`d'
      replace IC=(e(r2))^0.5 if beta>0 & id==`d'
      replace IC=(e(r2))^0.5 if beta<0 & id==`d'
      }
      }

      but still get error message:no variables defined

      Do you know what is wrong with my code? Thank you.

      Best,
      Xixi Lin

      Comment


      • #4
        First I'll make a guess. Then I'll tell you how to pursue it more systematically if my guess is wrong.

        I'm guessing that on some of your iterations through the loops, the -regress- command has an empty estimation sample: that is, there are no observations that actually satisfy the condition inrange(Week, `j', `i') & id == `d'. I say that because -regress- commands can produce obscure error messages that don't immediately describe the problem (but rather reflect some consequence of the problem that arises deep inside the -regress- code), and because the definitions of i and j are a little funky in your code. Why do I say that i and j are funky? Well, i is the value of the loop index in the outermost -forvalues- statement. And that -forvalues- statement is bounded by `lo' and `hi'. Looking higher up in the code we see the definitions for those.

        Now, you don't show us weekmax. Given that you didn't get an error up there, weekmax must exist, and it is either a scalar or a variable. I'm guessing it's a variable. In that case -local hi = weekmax- means set the value of local macro hi to be the value of weekmax in the first observation of the data set. You then set local macro lo to be that same number minus 156. Now perhaps that is all exactly right. Certainly it is legal in Stata. But it's a little bit unusual. When I see a construction like that, it's more likely to be a mistake. So I'm guessing that you intended something else for weekmax, and that that error propagated itself through `lo' and `hi' to `i' and `j', and that led to some empty regressions.

        Now, if that doesn't turn out to be it, here's what I recommend you do. One of the problems with these -forvalues- loops is that you don't get a lot of information about what is happening inside them while they run. (For working code that's a good thing because complete output could be overwhelming.) So I would precede the -forvalues i =...- statement with

        Code:
        set tracedepth 1
        set trace on
        You will get a torrent of output now. But the good thing is that you will be able to see exactly which statement threw the error message, and you will also get to see the actual values of `i', `j', and `d' that were in use when it happened. That should help you then pin down the ultimate cause of the problem. (If my original guess is correct, the values of `i' and `j' will be numbers that surprise you and that cause the regression to have no observations to work on. It might be the regress command that complains, or the statement following it, because if the regression didn't actually occur, then _b[N_forecast_return] wouldn't exist.)

        Comment


        • #5
          Hi Clyde,

          weekmax=582. You are right, for some period there is no observations because it is an unbalanced data. id does not appear for all the weeks. That is why I use capture reg, is there a way to let the loop ignore if there is no observation for id=='d' in the period? I want beta=. if no observation in the period is found. Do you know how to fix that?

          Best,
          Xixi Lin

          Comment


          • #6
            OK. -capture reg- does not keep the regression from trying to happen, it just tells Stata to keep quiet about any problems it finds, and keep moving on. You then run into problems with subsequent code that requires the results of the regression. The trick is that -capture- doesn't entirely suppress the problem with the regression, it "captures" it in c(rc). So you can test that and condition the subsequent code on it.

            So I think the following code will work:

            Code:
            forvalues i = `lo'/`hi' {
                forvalues d = `mi'/`mx' {
                    local j = `i' - 11
                    capture regress N_Return N_Forcast_return if inrange(Week, `j', `i')& id==`d'
                    if c(rc) == 0 {
                       replace beta=_b[N_Forcast_return] if id==`d'
                       replace IC=(e(r2))^0.5 if beta>0 & id==`d'
                       replace IC=(e(r2))^0.5 if beta<0 & id==`d'
                    }
                }  
            }
            c(rc) will be 0 if the regression was successful, and then Stata will go on to replace beta and IC. If the regression failed for any reason, c(rc) will contain whatever error code was returned by -regress-, and those -replace- commands (and the errors they would throw) will be skipped.

            By the way, your two -replace IC=...- commands could be combined into one: -replace IC = =(e(r2))^0.5 if beta!=0 & id==`d'-, right?

            Comment


            • #7
              It works! Thank you so much!

              Comment

              Working...
              X