On Fri, 18 Sep 2009, David Hamilton wrote:
Hello, I'm new to Gretl, and while I think I've picked things
up
pretty quickly I'm stumped with what is probably a simple problem for
regular users. When I execute the muhat function in script below I get
a "Data types not conformable for operation" error message. It
appears that the second function doesn't like the matrix reference
arguments, but I don't see why that would be a problem. I've searched
the archives for similar problems but haven't found a solution to my
problem. (FYI I'm running Gretl 1.8.4 on an XP machine.) Your help
would be greatly appreciated.
Script:
function matrix local_level (series y)
/* starting values */
scalar s1 = 1
scalar s2 = 1
/* set up Kalman matrices */
matrix H = {1 ; 1}
matrix F = {1, 0 ; 0, 0.97}
matrix Q = {s1, 0 ; 0, s2}
/* Kalman filter set-up */
kalman
obsy y
obsymat H
statemat F
statevar Q
end kalman --diffuse
/* ML estimation */
mle ll = ERR ? NA : $kalman_llt
Q[1,1] = s1
Q[2,2] = s2
ERR = kfilter()
params s1 s2
end mle
return s1 ~ s2
end function
function list loclev_sm (series y, scalar s1, scalar s2)
kalman
obsy y
obsymat H
statemat F
statevar Q
end kalman --diffuse
matrix ret = ksmooth()
series wt = ret[,1]
series xt = ret[,2]
list components = wt xt
return components */
end function
/* -------------------- execute -------------------- */
matrix Vars = local_level(y)
list muhat = loclev_sm(y, Vars[,1], Vars[,2])
First of all, an apology and a promise: I'm afraid it can be very
difficult to ensure correct propagation of errors from deep within
function execution, and we do not have this right yet; but we'll
try harder to get it right. (I can almost see why some people
resort to horrors such as longjmp().)
Second, a tip: if you're trying to do something fairly complicated
in terms of programming in gretl, and it's not working, try the
script in an environment where you can see stderr. (This may not
be easy on MS Windows, admittedly.) Or at least turn on debugging
(see gretl help on the "set" command).
But anyway, running your script using gretlcli (command-line
client) on Linux, the problem is immediately apparent. Consider
your function loclev_sm:
function list loclev_sm (series y, scalar s1, scalar s2)
kalman
obsy y
obsymat H
statemat F
statevar Q
end kalman --diffuse
matrix ret = ksmooth()
series wt = ret[,1]
series xt = ret[,2]
list components = wt xt
return components */
end function
As the chapter on functions in the Gretl User's Guide explains, so
far as functions are concerned there are no global variables in
gretl. A function knows about only those variables that are
passed to it as arguments, or local variables defined within the
function. So:
function list loclev_sm (series y, scalar s1, scalar s2)
kalman
obsy y
obsymat H ...
Beep! What is H supposed to be? It's undefined. Either pass H as
an argument or define it within the function prior to starting
your kalman block.
Allin Cottrell