sp: Calculate the conditional state probabilities.

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/sp.R


Returns the probabilities that the underlying hidden state is equal to each of the possible state values, at each time point, given the observation sequence. Also can return the fitted conditional means, if requested, given that the observations are numeric.


sp(y, model = NULL, tpm=NULL, Rho=NULL, ispd=NULL, X=NULL,
   addIntercept=NULL, means = FALSE, warn=TRUE)



The observations on the basis of which the probabilities of the underlying hidden states are to be calculated. May be a (one or two column) matrix of observations, or a list each component of which is such a matrix. If y is missing it is set equal to the y component of model, given that that argument is non-NULL and that that component exist. Otherwise an error is given.


An object of class hmm.discnp as returned by hmm().


The transition probability matrix for the underlying hidden Markov chain. Ignored if model is not NULL. Ignored if model is not NULL (in which case tpm is extracted from model).


An object specifying the distribution of the observations, given the underlying state. I.e. the “emission” probabilities. See hmm(). Ignored if model is not NULL (in which case Rho is extracted from model).


Vector specifying the initial state probability distribution of the underlying hidden Markov chain. Ignored if model is not NULL (in which case ispd is extracted from model). If both model and ispd are NULL then ispd is calculated to be the stationary distribution of the chain as determined by tpm.


An optional numeric matrix, or a list of such matrices, of predictors. The use of such predictors is (currently, at least) applicable only in the univariate emissions setting, and then only if Rho is supplied in the “newstyle” format. If X is a list it must be of the same length as y and all entries of this list must have the same number of columns. The number of rows of each entry must be equal to the length of the corresponding entry of y. If X is a matrix then y should be a vector or one-column matrix (or a list with a single entry equal to such).


Logical scalar. See the documentation of hmm().


A logical scalar; if means is TRUE then the conditional expected value of (each column of) the observations (given the observation sequence) is calculated at each time point. If means is TRUE and the observation values are not numeric, then an error is given.


Logical scalar; should a warning be issued if Rho hasn't got relevant dimension names? (Note that if this is so, then the corresponding dimension names are formed from the sorted unique values of y or of the appropriate column(s) of y. And if this is so, then the user should be sure that the ordering of the entries of Rho corresponds properly to the the sorted unique values of y.) This argument is passed to the utility function check.yval() which actually issues the warning if warn=TRUE.


The conditional mean value at time t is calculated as

SUM_k gamma_t(k)*mu_k

where gamma_t(k) is the conditional probability (given the observations) that the hidden Markov chain is in state k at time t, and mu_k is the expected value of an observation given that the chain is in state k.


If means is TRUE then the returned value is a list with components


The conditional probabilities of the states at each time point.


The conditional expectations of the observations at each time point.

Clearly this makes sense only if the observations are numeric.

Otherwise the returned value consists of probs as described above.

If there is a single matrix of observations y then probs is a matrix whose rows correspond to the states of the hidden Markov chain, and whose columns correspond to the observation times. If the observations consist of a list of observation vectors, then probs is a list of such matrices, one for each matrix of observations.

Likewise for the means component of the list returned when the argument means is TRUE.


Rolf Turner [email protected]

See Also

hmm(), mps(), viterbi(), pr(), fitted.hmm.discnp()


P <- matrix(c(0.7,0.3,0.1,0.9),2,2,byrow=TRUE)
R <- matrix(c(0.5,0,0.1,0.1,0.3,
y.num   <- rhmm(ylengths=rep(300,20),nsim=1,tpm=P,Rho=R,drop=TRUE)
fit.num <- hmm(y.num,K=2,verb=TRUE,keep.y=TRUE,itmax=10)
cpe1    <- sp(model=fit.num,means=TRUE) # Using the estimated parameters.
cpe2    <- sp(y.num,tpm=P,Rho=R,means=TRUE,
              warn=FALSE) # Using the ``true'' parameters.
# The foregoing would issue a warning that Rho had no row names
# were it not for the fact that "warn" has been set to FALSE.

hmm.discnp documentation built on Nov. 12, 2018, 1:04 a.m.