Announcement

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

  • Prediction using a forvalues Loop

    I am trying to generate a value for the expected change in the unemployment rate (expdump) by creating a forvalues loop. There are a total of 892 periods in my data set. I would like each prediction to incorporate data from all of the previous periods, which is why I'm using a loop.

    In this loop, I am trying to run the predict command and have it generate one new variable column in my data set called expdump, which would represent the expected change in unemployment. When I run the code below, though, it gives an error that says "expdump already defined."

    Any advice on how to solve this issue?

    Here is the code I have currently:

    forvalues i= 180/891 {
    regress dump ipgrate1 ipgrate2 ipgrate3 dump1 dtb3 dba if period in 1/`i'
    predict expdump if period==`i'+1
    }

    Thank you!

  • #2
    This kind of question is often addressed here e.g. the very recent thread https://www.statalist.org/forums/for...forvalues-loop

    However, your twist

    Code:
     if period in 1/`i'
    is very confused about if and in. in refers only to observation numbers, not values of a variable. Also

    Code:
    if period
    .

    by itself is legal, but not what you want. It just restricts regression to non-zero values of
    period, which here is no restriction.

    You may want something more like


    Code:
    gen expdump = .
    forvalues i= 180/891 {
         tempvar work
         regress dump ipgrate1 ipgrate2 ipgrate3 dump1 dtb3 dba if inrange(period, 1, `i')
         predict `work'
         replace expdump = `work' if period == `i' + 1
         drop `work'
    }


    but without any kind of data example I am guessing.
    Last edited by Nick Cox; 09 Jan 2018, 12:07.

    Comment


    • #3
      This works perfectly. Thank you so much!

      Comment


      • #4
        Hi I was wondering how you could code forecast measures for this. E.g. How would you find the mean absolute error?

        Thanks!

        Comment

        Working...
        X