Announcement

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

  • Predictions on Panel Data Using a "forvalues" Loop

    I am using a panel dataset, whereby I have data on log stock returns for a cross-section of companies. I am trying to use these stock returns to create a proxy for volatility using GARCH(1,1) methodology.

    I want to run GARCH on each company individually, and then use the results to predict variance values. The code that I have come up with so far is:

    forvalues companyid = 1(1)292{
    arch Returns if companyid == `i', arch(1) garch(1)
    predict Residual, r
    predict ProxyVariance, variance
    }

    However, this returns a syntax error.

    Could anyone advise me on why this is not working?

    Kind regards,
    Jack



  • #2
    You refer to a local macro you never define. Here that implies that Stata sees your first line inside the loop as

    Code:
    arch Returns if companyid ==, arch(1) garch(1)
    which makes no sense.

    There are at least two more bugs lurking beyond that.

    Second time around the loop, predict Residuals would fail because the variable already exists. Same problem would invalidate your second predict command.

    Something more like this should be closer to what you want.


    Code:
    gen Residual = . 
    gen ProxyVariance = . 
    
    forvalues i = 1/292 {
        tempvar res pvar 
        arch Returns if companyid == `i', arch(1) garch(1)
        predict `res', r
        replace Residual = `res' if companyid == `i' 
        predict `pvar', variance
        replace Proxy = `pvar' if companyid == `i' 
        drop `res' `pvar' 
    }
    You posted an overlapping question at https://stats.stackexchange.com/ques...for-panel-data

    Our policy on cross-posting is quite explicit. You are asked to tell us about it. Do please read the FAQ Advice as prompted.

    Independently of that, it's my view that the CV question is off-topic there and likely to be closed.

    Comment


    • #3
      Thank you for your help, Nick. That's precisely what I was looking for!

      Apologies for the cross-posting, I wasn't aware of the policy. I will read the FAQ advice!

      Jack

      Comment

      Working...
      X