Skip to contents

This function fits a HMM to fHMM_data via numerical likelihood maximization.

Usage

fit_model(
  data,
  ncluster = 1,
  seed = NULL,
  verbose = TRUE,
  initial_estimate = NULL
)

Arguments

data

An object of class fHMM_data.

ncluster

Set the number of clusters for parallel optimization runs to reduce optimization time. By default, ncluster = 1 (no clustering).

seed

Set a seed for the generation of initial values. No seed by default.

verbose

Set to TRUE to print progress messages.

initial_estimate

Optionally defines an initial estimate for the numerical likelihood optimization. Can be:

  • NULL (the default), in this case

    • applies a heuristic to calculate an initial estimate,

    • or uses the true parameter values if data$controls$origin is TRUE.

  • or an object of class parUncon, for example the estimate of a previously fitted model, i.e. the element model$estimate.

Value

An object of class fHMM_model.

Details

Multiple optimization runs starting from different initial values are computed in parallel if ncluster > 1.

Examples

### 2-state HMM with normal distributions

# define model
controls <- set_controls(states = 2, sdds = "normal", horizon = 80, runs = 5)

# define parameters
parameters <- fHMM_parameters(controls, mu = c(-1, 1), seed = 1)

# sample data
data <- prepare_data(controls, true_parameter = parameters, seed = 1)

# fit model
model <- fit_model(data, seed = 1)
#> Checking start values
#> Maximizing likelihood
#> Computing Hessian
#> Fitting completed

# inspect fit
summary(model)
#> Summary of fHMM model
#> 
#>   simulated hierarchy        LL      AIC      BIC
#> 1      TRUE     FALSE -42.16861 96.33721 110.6294
#> 
#> State-dependent distributions:
#> normal() 
#> 
#> Estimates:
#>                 lb estimate      ub    true
#> Gamma_2.1  0.08275   0.1628  0.2953  0.1632
#> Gamma_1.2  0.11660   0.2288  0.4000  0.3116
#> mu_1      -1.01581  -0.9806 -0.9453 -1.0000
#> mu_2       0.89192   1.0271  1.1623  1.0000
#> sigma_1    0.08113   0.1033  0.1314  0.1008
#> sigma_2    0.38598   0.4728  0.5790  0.6008
plot(model, "sdds")


# decode states
model <- decode_states(model)
#> Decoded states

# predict
predict(model, ahead = 5)
#>   state_1 state_2       lb estimate       ub
#> 1 0.77123 0.22877 -0.83016 -0.52126 -0.21236
#> 2 0.63203 0.36797 -0.63529 -0.24180  0.15169
#> 3 0.54734 0.45266 -0.51673 -0.07177  0.37320
#> 4 0.49581 0.50419 -0.44459  0.03169  0.50797
#> 5 0.46446 0.53554 -0.40070  0.09464  0.58997