Announcement

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

  • foreach, loops

    Hi

    I want to run the below code, but can't get it right.
    As I have a panel data with many IDs and months. I want to run the univariate Garch model as "arch X, arch(1/1) garch(1/1)", but can't get the estimation as per ID, in the below loop. X is the only variable that I am considering.

    program define one_id
    arch X, arch(1/1) garch(1/1)
    foreach x in X {
    gen coeff_`x' = _b[`x']
    gen t_`x' = coeff_`x'/se_`x'
    gen p_`x' = 2*t(e(df_r), -abs( t_`x'))
    }
    gen constant_term = _b[_cons]
    predict V
    predict resid, resid
    exit
    end
    runby one_id,by(FI) status
    Can someone help how to modify the above code to suit my requirements?
    Last edited by Aamina Khurraa; 06 Nov 2019, 11:08.

  • #2
    Hi Aamina,

    where have you defined X?
    Alfonso Sanchez-Penalver

    Comment


    • #3
      The code includes a loop over one item and as such can be simplified down to

      Code:
      program define one_id
          arch X, arch(1/1) garch(1/1)
          gen coeff_X = _b[X]
          gen t_X = coeff_X/se_X
          gen p_X = 2*t(e(df_r), -abs(t_X))
          gen constant_term = _b[_cons]
          predict V
          predict resid, resid
      end
      
      runby one_id, by(FI) status
      and what hits me first is that se_X is never defined before it is used.



      Comment


      • #4
        Thanks Nick Cox

        I have tried this bit of a hint, but again in vain. This is not picking the foreach syntax and hence errors in all groups (IDs). I have also incorporated se_x. I am not sure if GARCH models are compatible with foreach loops, otherwise I can't think of any solution for the panel data.

        The code:
        "arch X, arch(1/1) garch(1/1)" however, does work for the entire panel data, if not for individual IDs.

        Comment


        • #5
          The tsset setting that makes arch for the whole dataset possible at all won't be compatible necessarily with whatever else you need.

          Comment


          • #6
            Since Nick mentioned -tsset-, I should point out that -runby- does not preserve -xtset- or -tsset- settings. So if your program uses a command that requires the data to be -xtset- or -tsset- (as, I believe, -garch- and -arch- do), you have to do the -xtset- or -tsset- inside the program before you call those commands.

            Comment


            • #7
              So could we find a way to do these individual garch estimations for a panel of say 360 firms?

              Comment


              • #8
                Hi

                Still stuck with multiple time series in my panel sample. Can you elaborate further how would I add the xtset command within the foreach loop (as is mentioned in #6) as I have tried many ways, but couldn't get it right.

                Regards

                Comment

                Working...
                X