Announcement

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

  • Predict values for each regression in a loop and store in single variable

    Dear all,

    I'm trying to run an out-of-sample forecast in Stata using the forvalue command. I have a total of 17 years in my data file.

    I first run an in-sample regression and predict.

    Code:
    reg share share_lagged1 poll incumbent
    predict p_insample
    I then want to run a regressions for all years in the sample while always excluding one year from it. I do that in the following way:

    Code:
    forval i = 1/17 {
    regress share share_lagged1 poll incumbent if electionID!=`i'
    }
    My question is how to run the -predict- command after every regression in the loop and store the prediction for electionID!=i to the corresponding electionID.

    As an example I want to run
    Code:
    reg share share_lagged1 poll incumbent if electionID != 2
    and then use this model to -predict- the share for electionID=2. Then do this for all years in my sample.

    Here's an example of my data:


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input year electionID share share_lagged1 poll incumbent
    2012 16 26.299999237060547 34.599998474121094 31 0
    2012 16  8.600000381469727  6.699999809265137  4 0
    2012 16 11.300000190734863 12.100000381469727 17 1
    2012 16                2.5  5.599999904632568  5 0
    2012 16 39.099998474121094               34.5 34 1
    2017 17                  . 26.299999237060547 32 0
    2017 17                  .  8.600000381469727  7 0
    2017 17                  . 11.300000190734863 12 1
    2017 17                  .                2.5  5 0
    2017 17                  . 39.099998474121094 32 1
    end

    Thank you very much in advance!





  • #2
    Try this:

    Code:
    gen p_outsample = .
    forval i = 1/17 {
        regress share share_lagged1 poll incumbent if electionID!=`i'
        predict temp
        replace p_outsample = temp if electionID == `i'
        drop temp
    }
    Not tested. Beware of typos, other errors. But the gist of it is you create a variable with all missing values to hold your final results. Then each time you go through the loop you predict into a throwaway variable, copy the predictions into that final result variable just for the observations that are appropriate.

    Note: The reason this isn't tested is that the example data you show cannot support these regressions: the -regress- command produces a no observations error with electionID == 17. If that condition can arise in your real data, you will have to add some complexity to the code to deal with it. See -help capture-.

    Thank you for using -dataex- on your very first post!

    Comment


    • #3
      Dear Clyde,

      Thanks so much for your very fast reply. That command works a charm on my dataset! Thank you very much for the help.

      Best,

      Olaf

      Comment

      Working...
      X