Announcement

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

  • Foreach loop using macro in numlist

    Probably a simple syntax issue, but I can't figure this out after reading the documentation on foreach, macros, and numlist. I run the following example and get an 'invalid syntax' error for the second forvalues loop. It seems like the local macro k is being being input to the forvalues function as '322 - 10' instead of '312', but I don't know how resolve this. When k is set to 312 instead of a function, the code works. But I want k to change depending on the iteration of the first forvalues loop.

    For context, I'm trying to get out-of-sample estimates for a variable window of data--i.e. I have two months of data, I want to omit moving, multi-sized windows of data, run the estimation, and then compare the predicted values to the omitted observed values.

    Code:
    set seed 12345
    set obs 100
    gen dayofyear = 265 + _n
    gen y = 10* exp( -exp( -0.1 * (dayofyear - 315))) + rnormal()
    replace y = 0 if dayofyear < 300 | y < 0 
    line y dayofyear
    
    local windowstart 310
    local windowend 322
    local windowlengthmin 10
    local windowlengthmax 11
    
    forvalues i=`windowlengthmin'(1)`windowlengthmax' {
    
        local k `windowend' - `i'
        //local k 312
    
        forvalues j=`windowstart'(1)`k' {
    
            preserve
            keep if dayofyear < `j' | dayofyear >= (`i' + `j')
            nl (y = {b1 = 10} * exp( -exp( -{b2 = 0.1} * (dayofyear - {b3 = 315}))))
            estimates store est1
            
            restore
            preserve
            estimates restore est1        
            gen yhat = _b[b1:_cons] * exp( -exp( - _b[b2:_cons]* (dayofyear -  _b[b3:_cons])))
            keep if dayofyear >= `windowstart' & dayofyear < (`i' + `windowstart')
                
            mkmat yhat, mat(Yhat)
            mkmat y, mat(Y)
            matrix E = (Yhat - Y)' * (Yhat - Y)
            
            if `i'==`windowlengthmin' & `j' == `windowstart' matrix Error1 = E
            else matrix Error1 = (Error1 \ E)
            restore
            
        }
    }
            
    matrix list Error1

  • #2
    Welcome to Statalist, and thank you for the well-presented extract of your code. It made it easy to see the problem and to build the example below,

    Consider the following example built from your code.
    Code:
    local windowstart 310
    local windowend 322
    local windowlengthmin 10
    local windowlengthmax 11
    
    forvalues i=`windowlengthmin'(1)`windowlengthmax' {
    
        display " i is now `i' "
        local k `windowend' - `i'
        macro list _k
        local m= `windowend' - `i'
        macro list _m
        
    }
    Code:
     i is now 10 
    _k:             322 - 10
    _m:             312
     i is now 11 
    _k:             322 - 11
    _m:             311
    When you omit the equal sign the expression is copied into the local macro without evaluating it. Adding the equal sign signals that the right hand side is to be interpreted as an expression rathr than as a string of characters. The end result was that when it got to your inner loop, after macro substitution it tried to execute
    Code:
    forvalues j=310(1)322 - 10 {
    a syntax error.

    Comment


    • #3
      Thank you very much William! I figured it was something simple. I really appreciate your help.

      Comment

      Working...
      X