'How to estimate the Kalman Filter with 'KFAS' R package, with an AR(1) transition equation and covariates?

I am using 'KFAS' package from R to estimate a state-space model with the Kalman filter. My measurement and transition equations are:

y_t = b_0 + b_1xx_t + Z_t * x_t + \eps_t (measurement)

x_t = T_t * x_{t-1} + R_t * \eta_t (transition),

with \eps_t ~ N(0,H_t) and \eta_t ~ N(0,Q_t),

where xx_t are covariates. I have read this question and wrote the following code

library(KFAS)
set.seed(100)
xx <- rnorm(200)
beta0 <- 0.1
beta1 <- 0.1

eps <- rt(200, 4, 1)
y <- as.matrix(beta0 + beta1*xx + (arima.sim(n=200, list(ar=0.6), innov = rnorm(200)*sqrt(0.5)) + eps), 
               ncol=1)

Zt <- 1
Ht <- matrix(NA)
Tt <- matrix(NA)
Rt <- 1
Qt <- matrix(NA)

ss_model <- SSModel(y ~  xx + SSMcustom(Z = Zt, T = Tt, R = Rt, 
                                        Q = Qt), H = Ht)


updatefn <- function(pars, model) {
  model$H[1] <- pars[1]
  model$T[1] <- pars[2]
  model$Q[1] <- pars[3]
  model
}


fit <- fitSSM(ss_model, c(1, 0.5, 1), updatefn, method = "L-BFGS-B", 
              lower = c(0, -0.99, 0), upper = c(100, 0.99, 100))

I get the error

    Error in is.SSModel(do.call(updatefn, args = c(list(inits, model), update_args)),  : 
  System matrices (excluding Z) contain NA or infinite values, covariance matrices contain values larger than 1e+07

I have tried to change the initial vector to c(1, 0.5, 1, 1, 1) but it returns the same message. Does anyone know how can I do this?

Thanks!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source