Skip to content

HMM$fit_stan(laplace = TRUE) not working #38

@TheoMichelot

Description

@TheoMichelot

See the reproducible example below. The same model works with laplace = FALSE.

library(hmmTMB)
data(energy, package = "MSwM")

dists <- list(Price = "norm")
par0_2s <- list(Price = list(mean = c(3, 6), sd = c(1, 1)))
f <- list(Price = list(mean = ~s(EurDol, k = 10, bs = "cs"),
                       sd = ~poly(EurDol, 3)))
hid <- MarkovChain$new(data = energy, n_states = 2, initial_state = "stationary")
obs <- Observation$new(data = energy, n_states = 2, dists = dists, 
                       par = par0_2s, formulas = f)
hmm <- HMM$new(obs = obs, hid = hid)

hmm$fit_stan(chains = 1, iter = 2000, laplace = TRUE)

leading to the error:

Error in initSanitizer(x) : 
  Detected initial parameter of the wrong length.

It seems to be a problem with using the option init = "par" when tmbstan() is called in HMM$fit_stan(). There seems to be a mismatch between the parameter vector stored in the model (obs$env$par), which includes random effects, and the parameter vector expected by tmbstan() with laplace = TRUE, which doesn't.

A possible fix would be to replace init = "par" by init = "random" when laplace = TRUE in HMM$fit_stan().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions