Announcement

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

  • Statsby & Loop

    Dear members,

    I'm trying to combine the command "statsby" in a loop to obtain a new dataset composed only by the R2 derived from equations done on this dataset

    This is how my dataset looks like:
    YEAR | VAR A | VAR B | VAR C
    1 | z | y | d
    2 | z | y | d
    3 | z | y | d
    4 | z | y | d
    5 | z | y | d
    6 | z | y | d
    For each year and each VAR, I would like to run the following AR(2) equation: reg VARx L1.VARx L2.VARx and store the R2 of each equation to derive a new dataset that would look like:

    YEAR | VAR A | VAR B | VAR C
    1 | | |
    2 | | |
    3 | r2(y3a) | r2(y3b) | r2(y3c)
    4 | r2(y4a) | r2(y4b) | r2(y4c)
    5 | r2(y5a) | r2(y5b) | r2(y5c)
    6 | r2(y6a) | r2(y6b) | r2(y6c)

    I've therefore created created the following loop:

    ________________________

    foreach var of varlist _all {
    local vl = "`vl' `var'"
    }
    local exclude "year"
    local vl: list vl - exclude // this is to remove year from the var list

    foreach var of local vl {
    preserve
    keep year `var' // this is to consider only one variable per time
    tsset year


    levelsof year, local(levels)
    foreach x of local levels {
    statsby e(r2), by(year) clear: `var' L1.`var' L2.`var' if year== `x' // this should create the R2 for each year
    }
    restore
    }

    ________________________


    unfortunately it doesn't work. Could you please give me some advice?

    many thanks,

    Alessandro Giovannini








  • #2
    Alessandro,

    There may be other issues, but right away I can see that your statsby command is missing the name of the regression command you want to run. Try:

    Code:
    statsby e(r2), by(year) clear: reg `var' L1.`var' L2.`var' if year== `x'
    If this doesn't help, then you need to be more specific when you say "it doesn't work".

    Regards,
    Joe

    Comment


    • #3
      What does "doesn't work" mean? Post exact output to increase your chances of getting help.

      Guesses are:

      You did not tsset your data
      You are getting an insufficient observations error due to the if qualifier (if year == `x')

      What does it mean to run a regression for a particular year using time series data? It seems like you're trying to do some kind of rolling regression using only three data points: x_t x_t-1 x_t-2.
      Other details could probably be improved (e.g. the use of preserve / restore).

      See

      help tsset
      help corrgram
      help rolling


      and more generally, help time series.

      Consider also help postfile.
      You should:

      1. Read the FAQ carefully.

      2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

      3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

      4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

      Comment


      • #4
        Originally posted by Joe Canner View Post
        I can see that your statsby command is missing the name of the regression command you want to run.
        Very important point.
        You should:

        1. Read the FAQ carefully.

        2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

        3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

        4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

        Comment

        Working...
        X