Learn R Programming

LaMa (version 3.1.0)

tpm_g: Build all transition probability matrices of an inhomogeneous HMM

Description

In an HMM, we often model the influence of covariates on the state process by linking them to the transition probability matrix. Most commonly, this is done by specifying a linear predictor $$ \eta_{ij}^{(t)} = \beta^{(ij)}_0 + \beta^{(ij)}_1 z_{t1} + \dots + \beta^{(ij)}_p z_{tp} $$ for each off-diagonal element (\(i \neq j\)) of the transition probability matrix and then applying the inverse multinomial logistic link (also known as softmax) to each row. This function efficiently calculates all transition probability matrices for a given design matrix Z and parameter matrix beta.

Usage

tpm_g(Z, beta, Eta = NULL, byrow = FALSE, ref = NULL, ad = NULL, report = TRUE)

Value

array of transition probability matrices of dimension c(nStates, nStates, nObs)

Arguments

Z

Covariate design matrix with or without intercept column, i.e. of dimension c(nObs, p) or c(nObs, p+1). If Z has only p columns, an intercept column is added automatically.

beta

Matrix of coefficients for the off-diagonal elements of the transition probability matrix of dimension c(nStates * (nStates-1), p+1). The first column contains the intercepts.

Eta

optional pre-calculated matrix of linear predictors of dimension c(nObs, nStates * (nStates-1)). If provided, no Z and beta are necessary and will be ignored.

byrow

logical indicating if each transition probability matrix should be filled by row. Defaults to FALSE, but should be set to TRUE if one wants to work with a matrix of beta parameters returned by popular HMM packages like moveHMM, momentuHMM, or hmmTMB.

ref

optional integer vector of length nStates giving, for each row, the column index of the reference state (its predictor is fixed to 0). Defaults to the diagonal (ref = 1:nStates).

ad

logical; whether to use automatic differentiation. Determined automatically — for debugging only.

report

logical; if TRUE (default), the coefficient matrix beta is reported from the fitted model, if Z is provided. Requires ad = TRUE.

Details

Deprecated: please use tpm(beta, Z) for all model types instead.

See Also

Other transition probability matrix functions: generator(), generator_g(), tpm(), tpm_ct(), tpm_emb(), tpm_emb_g(), tpm_g2(), tpm_p()

Examples

Run this code
## inhomogeneous Markov chain
# t.p.m. depends on covariates
z1 <- runif(100); z2 <- runif(100) # 2 covariates
Z <- cbind(1, z1, z2) # design matrix
beta0 <- c(-2, -2); beta1 = c(1, -2); beta2 = c(2, -1) # coefficients for intercept and covariates
beta <- cbind(beta0, beta1, beta2) # coefficient matrix; with intercepts!
#' Gamma <- tpm(beta, Z) # array with 100 slices

Run the code above in your browser using DataLab