Announcement

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

  • prediction, unexpected results

    Hi,

    I run a prediction by the first 20 sample (y and x), and I would like to use the results to predict the 25th y_f (a special out of sample prediction). I think this is what I do in the programming, but it gives me the prediction for the 20th sample (in-sample). Why? How can I modify my program to obtain the out-of-sample (25th) prediction?

    I have a loop, and I expect that it will give me prediction for 25th, 26th, 27th.

    clear all

    import excel "xyz1.xlsx", firstrow case(lower) clear

    sca start_T = 20
    sca n1 = 5
    egen t1 = seq()
    gen y_f = .

    forvalues i = `=start_T'/`=start_T+2'{
    reg y xa if t1<= `i' & t1> `i'-start_T // it works
    predict temp in `i+n1'
    replace y_f = temp in `i+n1'
    drop temp
    }



    Thank you very much!!

  • #2
    As before, you are asking what turn out to be similar questions in different threads without any cross-referencing.

    Again, as explained one way or the other in earlier threads.

    Code:
    `i+n1'
    is fantasy syntax. Stata won't add scalars on the fly like that. See e,g. https://www.statalist.org/forums/for...ubsample-by-in


    To debug, you need to work with simpler examples


    Code:
    . local i 20
    
    . scalar n1 = 5
    
    . di `i+n1'
    20
    I agree with anyone thinking that Stata's reaction here is puzzling, even unhelpful. It just ignores the +n1 in the macro reference as so much trailing junk.

    But, to back up, the start of the syntax here is that macname within a local macro reference should be a local macro name.

    `macname'

    What you then learn is that is not a bug to refer to a local macro that doesn't exist: Stata usually just evaluates it as an empty string. (Whether the result of that is legal and correct is a different story).

    What is biting here is that your fantasy syntax isn't construed as illegal but it has a weird consequence, which can only be understood by experiment, or so I suggest. Whether you've unearthed a bug in Stata that -- to be as positive as possible -- won't bite often is a different question.

    Comment


    • #3
      Code:
      local i 20
      scalar n1 = 5
      di `i+n1'
      20
      di `i' + n1
      25

      Comment

      Working...
      X