Page 1 of 1

Recursive forecasting in procedure

Posted: Fri Sep 04, 2009 8:38 am
by jonasdovern
Hallo,

I think I came across a similar problem once...but I couldn't figure out anymore how it was solved back then. I want to produce recursive forecasts with a model in a procedure and I also want to re-estimate the involved equations in a recursive fashion. I.e. something like

Code: Select all

PROCEDURE MODELFIT
	*
	option model	*mod
	option integer	neqns
        (...some more options...)
        *
	* Recursive estimation and forecasting
	do time=sfroml,sto
		do k=1,neqns
	   		linreg(noprint,equation=%modeleqn(mod,k)) %modeldepvars(mod)(k) efrom time-1
		end do k
		forecast(print,model=mod,results=fcs,from=time,steps=fsteps)
        end do time
END PROCEDURE
Apparently, this type of code doesn't really use the recursively estimated equations for forecasting but always the original parameter values recorded for the equations of the model. So, what do I have to change to "update" the model during the time-loop?

Re: Recursive forecasting in procedure

Posted: Fri Sep 04, 2009 9:42 am
by jonasdovern
Is it not working because %modeleqn(m,k) only generates a copy of an equation? In that case: What would be an easy way to access the "true" equation connected to the model inside the procedure?

Jonas

Re: Recursive forecasting in procedure

Posted: Fri Sep 04, 2009 11:13 am
by moderator
Right, you need to do something else to actually change the coefficients in the model. There are a lot of ways to do this. Here's one approach, which:

* saves the current model coefficients into an array
* runs the regression for equaiton K
* replaces column K of the coefficient array with the new coefficients.
* resets the model coefficients using the revised coefficient array

Code: Select all


do time=sfroml,sto

 * Create an array containing current model coefficients
 compute mc = %modelgetcoeffs(mod)

 do k=1,neqns
  linreg(noprint,equation=%modeleqn(mod,k)) %modeldepvars(mod)(k) efrom time-1

  * Replace column K of MC with new coefficients for equation K (the %VALID() check on beta 
  *    is required because the equations could have different sizes)

   ewise mc(i,j) = %if( j==k, %if( %valid(%beta(i)),%beta(i),0.0 ) , mc(i,j) )

 end do k

 * Store new coefficients back into the MODEL: 
 compute %modelsetcoeffs(mod,mc)

 forecast(print,model=mod,results=fcs,from=time,steps=fsteps)
end do time


Re: Recursive forecasting in procedure

Posted: Tue Sep 08, 2009 2:03 am
by jonasdovern
Thanks indeed. This is a nice short - and most importantly - generally applicable way to handle such situations.