optimizing.Optimization: Fit a model by MAP.

Description Usage Arguments Value See Also Examples

Description

This function computes a maximum a posteriori estimate by running one or more instances of a numerical optimization procedure to maximize the joint posterior density. If no seed is given, one is automatically generated and stored as an attribute in the returned object. An error is printed if no convergence was achieved after all the runs.

Usage

1
2
3
4
## S3 method for class 'Optimization'
optimizing(object, y, x = NULL, u = NULL,
  v = NULL, nRuns = 1, keep = "best", nCores = 1,
  writeDir = tempdir(), ...)

Arguments

object

An object returned by specify, hmm, compile, draw_samples, fit or optimizing.

y

A numeric matrix with the observation sample. It must have as many rows as the time series length T and as many columns as the dimension of the observation vector R. If not a matrix, the function tries to cast the object to a T\times R matrix.

x

An optional numeric matrix with the covariates for the observation model. It must have as many rows as the time series length T and as many columns as the dimension of the covariate vector M. If not a matrix, the function tries to cast the object to a T\times M matrix. Useful for Hidden Markov Regression Model (also known as Markov-switching regressions).

u

An optional numeric matrix with the covariates for the transition model. It must have as many rows as the time series length T and as many columns as the dimension of the transition covariate vector P. If not a matrix, the function tries to cast the object to a T\times P matrix. Useful for Hidden Markov Models with time-varying transition probabilities.

v

An optional numeric matrix with the covariates for the initial distribution model. It must have as many rows as the number of hidden states K and as many columns as the dimension of the initial covariate vector Q. If not a matrix, the function tries to cast the object to a K\times Q matrix.

nRuns

An optional integer with the number of initializations.

keep

An optional character string specifying whether the function should return the converging instance with the maximum posterior log density (best) or all the instances (all). The latter may be useful for debugging. It defaults to best.

nCores

An optional integer with the number of cores to be used. If equal to one, the instances are run sequentially. Otherwise, doParallel's backend is used for parallel computing. It defaults to one.

writeDir

An optional character string with the path where the Stan file should be written. Useful to inspect and modify the Stan code manually. It defaults to a temporary directory.

...

Arguments to be passed to rstan's optimizing.

Value

An Optimization object if keep is set to best, or an OptimizationList otherwise. In the latter case, the best instance can be obtained with extract_best.

See Also

See optimizing for further details on tunning the optimization procedure.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
## Not run: 
y <- rnorm(1000) # Assume this is your dataset

mySpec   <- hmm(
  K = 2, R = 1,
  observation = Gaussian(
    mu    = Gaussian(0, 10),
    sigma = Student(
      mu = 0, sigma = 10, nu = 1, bounds = list(0, NULL)
    )
  ),
  initial     = Dirichlet(alpha = c(1, 1)),
  transition  = Dirichlet(alpha = c(1, 1)),
  name = "Univariate Gaussian Hidden Markov Model"
)

myModel <- compile(mySpec)

myOpt   <- optimizing(
  mySpec, myModel, y = y, nRuns = 50, nCores = 10, keep = "best"
)

## End(Not run)

luisdamiano/BayesHMM documentation built on May 20, 2019, 2:59 p.m.