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
0 Response to Foreach loop using macro in numlist
Post a Comment