HMMFit: Fit an Hidden Markov Model

View source: R/RHmm.R

HMMFitR Documentation

Fit an Hidden Markov Model

Description

This function returns an HMMFitClass object which contains the results of the Baum-Welch algorithm for the user's data

Usage

   HMMFit(obs, dis="NORMAL", nStates=2, asymptCov=FALSE, ...)

Arguments

obs

A vector, a matrix, a data frame, a list of vectors or a list of matrices of observations. See section obs parameter.

dis

Distribution name. In 'NORMAL', 'DISCRETE' or 'MIXTURE'. Default 'NORMAL'.

nStates

Number of hidden states. Default 2.

asymptCov

A boolean. asymptCov=TRUE if the asymptotic covariance matrix is computed. Default FALSE.

...

Other parameters. See Details.

Details

Typical usages are:

  • HMMFit(obs, dis="NORMAL", nStates=, ..., asymptCov=FALSE)

  • HMMFit(obs, dis="DISCRETE", nStates=, levels=NULL, ..., asymptCov=FALSE)

  • HMMFit(obs, dis="MIXTURE", nStates=, nMixt=, ..., asymptCov=FALSE)

The differents arguments are:

obs

A vector, a matrix, a data frame, a list of vectors or a list of matrices of observations. See section obs parameter.

dis

Distribution name. In 'NORMAL', 'DISCRETE' or 'MIXTURE'. Default 'NORMAL'.

nStates

Number of hidden states. Default 2.

nMixt

Number of mixtures of normal distributions if dis ='MIXTURE'

levels

A character vector of all different levels of 'obs'. By Default (levels=NULL), this vector is computed from 'obs'.

asymptCov

A boolean. asymptCov=TRUE if the asymptotic covariance matrix is computed. Default FALSE.

...

optional parameter:

control:

A list of control parameters for the Baum-Welch algorithm. See control parameter

Value

a ‘HMMFitClass’ object with the following elements:

HMM

A HMMClass object with the fitted values of the model. See HMMSet.

LLH

Log-likelihood

BIC

BIC criterium

nIter

Number of iterations of the Baum-Welch algorithm

relVariation

last relative variation of the LLH function

asymptCov

Asymptotic covariance matrix of independant parameters. NULL if not computed.

obs

the observations.

call

The call object of the function call

obs parameter

If you fit the model with only one sample, obs is either a vector (for univariate distributions) or a matrix (for multivariate distributions) or a data frame. In the two last cases, the number of columns of obs defines the dimension of observations.

If you fit the model with more than one sample, obs is a list of samples. Each element of obs is then a vector (for univariate distributions) or a matrix (for multivariate distributions). The samples do not need to have the same length.

For discrete distributions, obs can be a vector (or a list of vectors) of any type of R factor objects.

control parameter

init

Kind of initialisation ='KMEANS' (for univariate or multivariate normal distributions), 'RANDOM' or 'USER'. Default 'RANDOM', see Random Initialization

iter

Maximum number of iterations for the Baum-Welch algorithm. Default 500

tol

Tolerance of the relative log-likehood augmentation. Default 1e-6

verbose

=0, no details, =1 iterations are displayed. Default 0

nInit

Number of random initialisations. Default 5

nIterInit

Number of maximum iterations of the Baum-Welch algorithm in the random initialisation phase. Default 5

initPoint

An HMMClass object used to initialize the parameters of the Baum-Welch algorithm. Default NULL.
if initPoint != NULL, init is set to "USER"

random initialization

'initProb' and 'transMat' parameters are uniformly drawn.

For univariate normal distributions, empirical mean m and variance \sigma^2 of all the samples are computed. Then for every states, an initial value of the 'mean' parameter is uniformly drawn between m - 3\,\sigma and m - 3\,\sigma and an initial value of the 'var' parameter is uniformly drawn between \frac{1}{2}\,\sigma^2 and 3\,\sigma^2.
For multivariate normal distributions, the same procedure is applied for each component of the mean vectors. The initial covariance matrix is diagonal, and each initial variance is computed as for univariate models.
For mixtures of univariate normal distributions, initial values for 'mean' and 'var' parameters are computed the same way than for normal distributions. The initial value of 'proportion' parameter is uniformly drawn.
For mixtures of multivariate normal distributions, the same procedure is applied for each component of the mean vectors, all the covariance matrices are diagonal and each initial variance is computed as for univariate models. The initial value of 'proportion' parameter is also uniformly drawn.
For discrete distributions, the initial values of 'proba' parameters are uniformly drawn.
Of course, the initial values of the parameters 'initProba', 'proba', 'proportion' and 'transMat' are standardized to ensure that they can represent probabilities vectors or transition matrices.

asymptotic covariance matrix

The asymptotic covariance matrix of estimates is computed using the Lystig and Hugues's algorithm.See asymptoticCov.
The summary and print.summary methods display the results.

References

Bilmes Jeff A. (1997) A Gentle Tutorial of the EM Algorithm and its Application to Parameter Estimation for Gaussian Mixture and Hidden Markov Models http://ssli.ee.washington.edu/people/bilmes/mypapers/em.ps.gz

Examples

    # Fit a 3 states 1D-gaussian model
    data(n1d_3s)
    HMMFit(obs_n1d_3s, nStates=3)

    # Fit a 3 states gaussian HMM for obs_n1d_3s
    # with iterations printing and kmeans initialization
    Res_n1d_3s <- HMMFit(obs=obs_n1d_3s, nStates=3,
        control=list(verbose=1, init="KMEANS"),
        asymptCov=TRUE)
    summary(Res_n1d_3s)

    # Fit a 2 states 3D-gaussian model
    data(n3d_2s)
    summary(HMMFit(obs_n3d_2s, asymptCov=TRUE))

    # Fit a 2 states mixture of 3 normal distributions HMM
    # for data_mixture
    data(data_mixture)
    ResMixture <- HMMFit(data_mixture, nStates=2, nMixt=3,
        dis="MIXTURE")

    # Fit a 3 states discrete HMM for weather data
    data(weather)
    ResWeather <- HMMFit(weather, dis='DISCRETE', nStates=3)
 

RHmm documentation built on Nov. 30, 2023, 7:22 p.m.

Related to HMMFit in RHmm...