Dear Gretl Team,
I'd tried to replicate the example 27.2 of the Gretl User’s Guide but
I'd found an error:
<error message>
'Uhat' is not the name of a variable
Error executing script: halting
</error message>
I think the problem is only due to the name "muhat" and "Uhat".
Um abraço,
Henrique
################################
#                                                     #
#   Example 27.2: Local level model   #
#                                                     #
################################
function matrix local_level (series y)
    /* starting values */
    scalar s1 = 1
    scalar s2 = 1
    /* Kalman filter set-up */
    kalman
        obsy y
        obsymat 1
        statemat 1
        statevar s2
        obsvar s1
    end kalman --diffuse
    /* ML estimation */
    mle ll = ERR ? NA : $kalman_llt
        ERR = kfilter()
        params s1 s2
    end mle
    return s1 ~ s2
end function
function series loclev_sm (series y, scalar s1, scalar s2)
    /* return the smoothed estimate of \mu_t */
    kalman
        obsy y
        obsymat 1
        statemat 1
        statevar s2
        obsvar s1
    end kalman --diffuse
    series ret = ksmooth()
    return ret
end function
/* -------------------- main script -------------------- */
nulldata 200
set seed 202020
setobs 1 1 --special
true_s1 = 0.25
true_s2 = 0.5
v = normal() * sqrt(true_s1)
w = normal() * sqrt(true_s2)
mu = 2 + cum(w)
y = mu + v
matrix Vars = local_level(y)           # estimate the variances
muhat = loclev_sm(y, Vars[1], Vars[2]) # compute the smoothed state
foreign language=R --send-data
    y <- gretldata[,"y"]
    a <- StructTS(y, type="level")
    a
    StateFromR <- as.ts(tsSmooth(a))
    gretl.export(StateFromR)
end foreign
append @dotdir/StateFromR.csv
ols Uhat 0 StateFromR --simple