| Title: | Penalized Estimation of Flexible Hidden Markov Models for Time Series of Counts |
|---|---|
| Description: | Provides tools for penalized estimation of flexible hidden Markov models for time series of counts w/o the need to specify a (parametric) family of distributions. These include functions for model fitting, model checking, and state decoding. For details, see Adam, T., Langrock, R., and Weiß, C.H. (2019): Penalized Estimation of Flexible Hidden Markov Models for Time Series of Counts. <arXiv:1901.03275>. |
| Authors: | Timo Adam |
| Maintainer: | Timo Adam <[email protected]> |
| License: | GPL-3 |
| Version: | 0.1.0 |
| Built: | 2026-06-01 08:10:54 UTC |
| Source: | https://github.com/cran/countHMM |
Estimates the parameters of a hidden Markov model using maximum penalized likelihood estimation. For details, see Adam et al. (2019).
fitMod(x,N=2,probs0=NULL,gamma0=NULL,delta0=NULL,stationary=TRUE,lambda=NULL,sup=NULL,m=3, inflation=NULL)fitMod(x,N=2,probs0=NULL,gamma0=NULL,delta0=NULL,stationary=TRUE,lambda=NULL,sup=NULL,m=3, inflation=NULL)
x |
Vector containing the observed time series of counts. |
N |
Integer, number of states. Default is |
probs0 |
Matrix with |
gamma0 |
Initial parameter values for the transition probabilities of the Markov chain underlying the observed counts. Matrix with |
delta0 |
Initial parameter values for the initial probabilities of the Markov chain underlying the observed counts. Vector of length |
stationary |
Logical, determines whether the initial distribution of the Markov chain underlying the observed counts is the stationary distribution. Default is |
lambda |
Vector of length |
sup |
Integer, determines the upper bound of the support of the state-dependent distributions. If |
m |
Integer, order of the difference penalties. Default is |
inflation |
Count probabilities to be excluded from penalization (e.g. in the presence of zero-inflation). Default is |
An object of type countHMM.
Adam, T., Langrock, R., and Weiß, C.H. (2019): Penalized Estimation of Flexible Hidden Markov Models for Time Series of Counts. arXiv:https://arxiv.org/pdf/1901.03275.pdf.
# importing example data x = read.table("http://www.hmms-for-time-series.de/second/data/earthquakes.txt")$V2 # model fitting lambda = rep(10^4,2) fitMod(x=x,lambda=lambda)# importing example data x = read.table("http://www.hmms-for-time-series.de/second/data/earthquakes.txt")$V2 # model fitting lambda = rep(10^4,2) fitMod(x=x,lambda=lambda)
Computes the penalized negative log-likelihood using the forward algorithm as described in Adam et al. (2019). Not intended to be run by the user (internal function, called by the function fitMod).
nLogLike(parvect,x,N,stationary,lambda,sup,m,inflation)nLogLike(parvect,x,N,stationary,lambda,sup,m,inflation)
parvect |
Vector of working parameters (as returned by |
x |
Vector of observed counts. |
N |
Integer, number of states. |
stationary |
Logical, determines whether the initial distribution of the Markov chain underlying the observed counts is the stationary distribution. |
lambda |
Vector of length |
sup |
Integer, determines the upper bound of the support of the state-dependent distributions. If |
m |
Integer, order of the difference penalties. |
inflation |
Count probabilities to be excluded from penalization (e.g. in the presence of zero-inflation). |
Numeric, the penalized negative log-likelihood.
Adam, T., Langrock, R., and Weiß, C.H. (2019): Penalized Estimation of Flexible Hidden Markov Models for Time Series of Counts. arXiv:https://arxiv.org/pdf/1901.03275.pdf.
# importing example data x = read.table("http://www.hmms-for-time-series.de/second/data/earthquakes.txt")$V2 # computing the penalized negative log-likelihood parvect = pn2pw(N=2,probs=cbind(dpois(x=0:41,lambda=14),dpois(x=0:41,lambda=26)), gamma=matrix(c(0.95,0.05,0.05,0.95),ncol=2),delta=NULL,stationary=TRUE) lambda = rep(10^4,2) nLogLike(parvect=parvect,x=x,N=2,stationary=TRUE,lambda=lambda,sup=41,m=3,inflation=FALSE)# importing example data x = read.table("http://www.hmms-for-time-series.de/second/data/earthquakes.txt")$V2 # computing the penalized negative log-likelihood parvect = pn2pw(N=2,probs=cbind(dpois(x=0:41,lambda=14),dpois(x=0:41,lambda=26)), gamma=matrix(c(0.95,0.05,0.05,0.95),ncol=2),delta=NULL,stationary=TRUE) lambda = rep(10^4,2) nLogLike(parvect=parvect,x=x,N=2,stationary=TRUE,lambda=lambda,sup=41,m=3,inflation=FALSE)
Plots the estimated state-dependent distributions.
plotMod(mod)plotMod(mod)
mod |
An object of type |
A plot of the estimated state-dependent distributions.
# importing example data x = read.table("http://www.hmms-for-time-series.de/second/data/earthquakes.txt")$V2 # model fitting lambda = rep(10^4,2) mod = fitMod(x=x,lambda=lambda) # plotting the estimated state-dependent distributions plotMod(mod)# importing example data x = read.table("http://www.hmms-for-time-series.de/second/data/earthquakes.txt")$V2 # model fitting lambda = rep(10^4,2) mod = fitMod(x=x,lambda=lambda) # plotting the estimated state-dependent distributions plotMod(mod)
Plots the Viterbi-decoded time series using different colors for the different states.
plotObs(mod)plotObs(mod)
mod |
An object of type |
A plot of the Viterbi-decoded time series.
# importing example data x = read.table("http://www.hmms-for-time-series.de/second/data/earthquakes.txt")$V2 # model fitting lambda = rep(10^4,2) mod = fitMod(x=x,lambda=lambda) # plotting the Viterbi-decoded time series plotObs(mod)# importing example data x = read.table("http://www.hmms-for-time-series.de/second/data/earthquakes.txt")$V2 # model fitting lambda = rep(10^4,2) mod = fitMod(x=x,lambda=lambda) # plotting the Viterbi-decoded time series plotObs(mod)
Plots quantile-quantile and autocorrelation function plots of the midpoints of the pseudo residual segments. For details, see Zucchini et al. (2016).
plotRes(mod)plotRes(mod)
mod |
An object of type |
A plot with two windows, the first of which displays the quantile-quantile function and the second of which displays the autocorrelation function of the pseudo-residuals.
Zucchini W., MacDonald, I.L., and Langrock, R. (2016): Hidden Markov models for time series: An introduction using R, 2nd edition. Chapman & Hall/CRC, Boca Raton.
# importing example data x = read.table("http://www.hmms-for-time-series.de/second/data/earthquakes.txt")$V2 # model fitting lambda = rep(10^4,2) mod = fitMod(x=x,lambda=lambda) # plotting the pseudo residuals plotRes(mod)# importing example data x = read.table("http://www.hmms-for-time-series.de/second/data/earthquakes.txt")$V2 # model fitting lambda = rep(10^4,2) mod = fitMod(x=x,lambda=lambda) # plotting the pseudo residuals plotRes(mod)
Transforming natural parameters to working parameters. Not intended to be run by the user (internal function, called by the functions nLogLike and fitMod).
pn2pw(N,probs,gamma,delta,stationary)pn2pw(N,probs,gamma,delta,stationary)
N |
Number of states. |
probs |
Matrix with |
gamma |
Transition probability matrix of the Markov chain underlying the observed counts with |
delta |
Initial distribution vector of length |
stationary |
Logical, determines whether the initial distribution of the Markov chain underlying the observed counts is the stationary distribution. |
A vector of working parameters.
# transforming natural parameters to working paramters pn2pw(N=2,probs=cbind(dpois(x=0:41,lambda=14),dpois(x=0:41,lambda=26)), gamma=matrix(c(0.95,0.05,0.05,0.95),ncol=2),delta=NULL,stationary=TRUE)# transforming natural parameters to working paramters pn2pw(N=2,probs=cbind(dpois(x=0:41,lambda=14),dpois(x=0:41,lambda=26)), gamma=matrix(c(0.95,0.05,0.05,0.95),ncol=2),delta=NULL,stationary=TRUE)
Computes the pseudo-residuals.
psRes(mod)psRes(mod)
mod |
An object of type |
A matrix with 3 rows, the first of which corresponds to the lower limits, the second of which corresponds to the midpoints, and the third of which corresponds to the upper limits of the pseudo-residual segments. For details, see Zucchini et al. (2016).
Zucchini W., MacDonald, I.L., and Langrock, R. (2016): Hidden Markov Models for Time Series: An Introduction Using R, 2nd Edition. Chapman & Hall/CRC. doi:https://doi.org/10.1201/b20790.
# importing example data x = read.table("http://www.hmms-for-time-series.de/second/data/earthquakes.txt")$V2 # model fitting lambda = rep(10^4,2) mod = fitMod(x=x,lambda=lambda) # computing the pseudo-residuals psRes(mod)# importing example data x = read.table("http://www.hmms-for-time-series.de/second/data/earthquakes.txt")$V2 # model fitting lambda = rep(10^4,2) mod = fitMod(x=x,lambda=lambda) # computing the pseudo-residuals psRes(mod)
Transforming working parameters to natural parameters. Not intended to be run by the user (internal function, called by the functions nLogLike and fitMod).
pw2pn(N,parvect,stationary,sup)pw2pn(N,parvect,stationary,sup)
N |
Integer, number of states. |
parvect |
Vector of working parameters. |
stationary |
Logical, determines whether the initial distribution of the Markov chain underlying the observed counts is the stationary distribution. |
sup |
Integer, determines the upper bound of the support of the state-dependent distributions. |
A list of natural parameters.
# transforming natural parameters to working paramters parvect = pn2pw(N=2,probs=cbind(dpois(x=0:41,lambda=14),dpois(x=0:41,lambda=26)), gamma=matrix(c(0.95,0.05,0.05,0.95),ncol=2),delta=NULL,stationary=TRUE) # transforming working parameters to natural parameters pw2pn(N=2,parvect=parvect,stationary=TRUE,sup=41)# transforming natural parameters to working paramters parvect = pn2pw(N=2,probs=cbind(dpois(x=0:41,lambda=14),dpois(x=0:41,lambda=26)), gamma=matrix(c(0.95,0.05,0.05,0.95),ncol=2),delta=NULL,stationary=TRUE) # transforming working parameters to natural parameters pw2pn(N=2,parvect=parvect,stationary=TRUE,sup=41)
Decodes the states of the Markov chain underlying the observed time series of counts using the Viterbi algorithm. For details, see Zucchini et al. (2016).
stateDec(mod)stateDec(mod)
mod |
An object of type |
A vector of Viterbi-decoded states.
Zucchini W., MacDonald, I.L., and Langrock, R. (2016): Hidden Markov Models for Time Series: An Introduction Using R, 2nd Edition. Chapman & Hall/CRC. doi:https://doi.org/10.1201/b20790.
# importing example data x = read.table("http://www.hmms-for-time-series.de/second/data/earthquakes.txt")$V2 # model fitting lambda = rep(10^4,2) mod = fitMod(x=x,lambda=lambda) # decoding the states stateDec(mod)# importing example data x = read.table("http://www.hmms-for-time-series.de/second/data/earthquakes.txt")$V2 # model fitting lambda = rep(10^4,2) mod = fitMod(x=x,lambda=lambda) # decoding the states stateDec(mod)