helske / KFAS

KFAS: R Package for Exponential Family State Space Models
64 stars 17 forks source link

Results for slope and trend using function signal() #49

Closed k41m4n closed 4 years ago

k41m4n commented 4 years ago

Dear Mr Helske, In my stochastic level and slope model (see code below), function coef(outKFS, states = "slope") provides correct values for slope, wheras function signal(outKFS, states = "slope")$signal gives just 0s. In consequence, signal(outKFS, states = "trend")$signal seems not to provide a correct sum of level and slope. The code is below.

Stochastic level and slope

Removing all objects except functions

rm(list = setdiff(ls(), lsf.str()))

Loading data

dataUKdriversKSI <- read.table(text = " 1687 1508 1507 1385 1632 1511 1559 1630 1579 1653 2152 2148 1752 1765 1717 1558 1575 1520 1805 1800 1719 2008 2242 2478 2030 1655 1693 1623 1805 1746 1795 1926 1619 1992 2233 2192 2080 1768 1835 1569 1976 1853 1965 1689 1778 1976 2397 2654 2097 1963 1677 1941 2003 1813 2012 1912 2084 2080 2118 2150 1608 1503 1548 1382 1731 1798 1779 1887 2004 2077 2092 2051 1577 1356 1652 1382 1519 1421 1442 1543 1656 1561 1905 2199 1473 1655 1407 1395 1530 1309 1526 1327 1627 1748 1958 2274 1648 1401 1411 1403 1394 1520 1528 1643 1515 1685 2000 2215 1956 1462 1563 1459 1446 1622 1657 1638 1643 1683 2050 2262 1813 1445 1762 1461 1556 1431 1427 1554 1645 1653 2016 2207 1665 1361 1506 1360 1453 1522 1460 1552 1548 1827 1737 1941 1474 1458 1542 1404 1522 1385 1641 1510 1681 1938 1868 1726 1456 1445 1456 1365 1487 1558 1488 1684 1594 1850 1998 2079 1494 1057 1218 1168 1236 1076 1174 1139 1427 1487 1483 1513 1357 1165 1282 1110 1297 1185 1222 1284 1444 1575 1737 1763 ")[, 1] %>% log() %>% ts(start = 1969, frequency=12)

Defining model

model <- SSModel(dataUKdriversKSI ~ SSMtrend(degree = 2, Q = list(matrix(NA), matrix(NA))), H = matrix(NA)) ownupdatefn <- function(pars, model){ model$H[,,1] <- exp(pars[1]) diag(model$Q[,,1]) <- exp(pars[2:3]) model }

Fitting model

fit <- fitSSM(model, inits = log(c(0.001, 0001, 0001)) , method = "BFGS") outKFS <- KFS(fit$model, smoothing = c("state", "mean", "disturbance"))

Slope estimates

(coef(outKFS, states = "slope")) (signal(outKFS, states = "slope")$signal)

helske commented 4 years ago

Call (coef(outKFS, states = "slope")) gives you the estimated slope state, but your signal gives you the part of Z_t * alpha_t corresponding to the slope state, and as the corresponding value in Z is zero, you get vector of zeros:


> outKFS$model$Z
, , 1

     level slope
[1,]     1     0

So actually you get:

> all.equal(signal(outKFS, states = "trend")$signal, signal(outKFS, states = "level")$signal)
[1] TRUE
> all.equal(signal(outKFS, states = "trend")$signal, coef(outKFS, states = "level"), check.attributes = FALSE)
[1] TRUE
> 
k41m4n commented 4 years ago

Thank you very much for your clarifications that helped me to understand better function signal()