helske / KFAS

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

LDL Composition of H failed. #51

Closed Vera1996 closed 3 years ago

Vera1996 commented 4 years ago

Dear,

I get an error when I run my code. The error is: In logLik.SSModel(object = model, check.model = FALSE, ... : LDL decomposition of H failed. How can I fix this? I think my update function is correct with the right parameters but I really don't see the fold in my code. Can you please help me? Another question about another subject, the hessian matrix. You want the eigenvalues to be positive otherwise there is no optimum. Is there a function within the package that requires the eigenvalues to be positive? I hope you can help me with both questions. Thanks a lot!!

model <- SSModel(covid_new[, 1:4] ~ SSMtrend(2, Q = list(matrix(NA, 4, 4), matrix(NA, 4, 4))) + SSMregression(list(~index$Germany, ~index$Netherlands, ~index$Spain, ~index$Sweden)), H = matrix(NA, 4, 4))

updatefn <- function(pars, model, ...) { Q <- diag(exp(pars[1:4])) Q[upper.tri(Q)] <- pars[5:10] model["Q", etas = "level"] <- crossprod(Q) Q <- diag(exp(pars[11:14])) Q[upper.tri(Q)] <- pars[15:20] model["Q", etas = "slope"] <- crossprod(Q) H <- diag(exp(pars[21:24])) H[upper.tri(Q)] <- pars[25:30] model["H"] <- crossprod(H) model }

init <- chol(cov(covid_new[, 1:4])) fitinit <- fitSSM(model, updatefn = updatefn, inits = rep(c(diag(init), init[upper.tri(init)]), 3), method = "BFGS", hessian = TRUE) -fitinit$optim.out$val

helske commented 3 years ago

I don't immediately see any problems in your code but I would need minimal reproducible example to really figure out what is going on. It could be that there are some numerical issues due to poor initial values for example. KFAS does not check Hessian.

Vera1996 commented 3 years ago

Thanks. I already figured it out. I changed some initial values and now I found an optimum!