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, init = NULL)

Arguments

data

An object of class fHMM_data.

ncluster

Set the number of clusters for parallelization. By default, ncluster = 1.

seed

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

verbose

Set to TRUE to print progress messages.

init

Optionally an object of class parUncon for initialization. This can for example be the estimate of a previously fitted model model, i.e. the element model$estimate. The initial values are computed via replicate(n, jitter(init, amount = 1), simplify = FALSE), where n <- data$controls$fit$runs.

Value

An object of class fHMM_model.

Details

The function is parallelized if ncluster > 1.

Examples

### 2-state HMM with normal distributions

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

# 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 -62.46398 136.928 152.559
#> 
#> State-dependent distributions:
#> normal() 
#> 
#> Estimates:
#>                 lb estimate      ub    true
#> Gamma_2.1  0.07189   0.1360  0.2423  0.1632
#> Gamma_1.2  0.14611   0.2643  0.4299  0.3116
#> mu_1      -1.01493  -0.9813 -0.9477 -1.0000
#> mu_2       0.91994   1.0404  1.1609  1.0000
#> sigma_1    0.08027   0.1015  0.1283  0.1008
#> sigma_2    0.41670   0.4953  0.5887  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.13598 0.86402  0.03891  0.76552 1.49212
#> 2 0.21754 0.78246 -0.07314  0.60063 1.27441
#> 3 0.26645 0.73355 -0.14035  0.50174 1.14383
#> 4 0.29579 0.70421 -0.18066  0.44243 1.06551
#> 5 0.31339 0.68661 -0.20483  0.40685 1.01854