simulate_mhmm {seqHMM} | R Documentation |
Simulate Mixture Hidden Markov Models
Description
Simulate sequences of observed and hidden states given the parameters of a mixture hidden Markov model.
Usage
simulate_mhmm(
n_sequences,
initial_probs,
transition_probs,
emission_probs,
sequence_length,
formula,
data,
coefficients
)
Arguments
n_sequences |
The number of simulations. |
initial_probs |
A list containing vectors of initial state probabilities for the submodel of each cluster. |
transition_probs |
A list of matrices of transition probabilities for the submodel of each cluster. |
emission_probs |
A list which contains matrices of emission
probabilities or a list of such objects (one for each channel) for
the submodel of each cluster. Note that the matrices must have
dimensions |
sequence_length |
The length of the simulated sequences. |
formula |
Covariates as an object of class |
data |
An optional data frame, a list or an environment containing
the variables in the model. If not found in data, the variables are
taken from |
coefficients |
An optional |
Value
A list of state sequence objects of class stslist
.
See Also
build_mhmm
and fit_model
for building
and fitting mixture hidden Markov models; ssplot
for plotting
multiple sequence data sets; seqdef
for more
information on state sequence objects; and simulate_hmm
for simulating hidden Markov models.
Examples
emission_probs_1 <- matrix(c(0.75, 0.05, 0.25, 0.95), 2, 2)
emission_probs_2 <- matrix(c(0.1, 0.8, 0.9, 0.2), 2, 2)
colnames(emission_probs_1) <- colnames(emission_probs_2) <-
c("heads", "tails")
transition_probs_1 <- matrix(c(9, 0.1, 1, 9.9) / 10, 2, 2)
transition_probs_2 <- matrix(c(35, 1, 1, 35) / 36, 2, 2)
rownames(emission_probs_1) <- rownames(transition_probs_1) <-
colnames(transition_probs_1) <- c("coin 1", "coin 2")
rownames(emission_probs_2) <- rownames(transition_probs_2) <-
colnames(transition_probs_2) <- c("coin 3", "coin 4")
initial_probs_1 <- c(1, 0)
initial_probs_2 <- c(1, 0)
n <- 30
set.seed(123)
covariate_1 <- runif(n)
covariate_2 <- sample(c("A", "B"),
size = n, replace = TRUE,
prob = c(0.3, 0.7)
)
dataf <- data.frame(covariate_1, covariate_2)
coefs <- cbind(cluster_1 = c(0, 0, 0), cluster_2 = c(-1.5, 3, -0.7))
rownames(coefs) <- c("(Intercept)", "covariate_1", "covariate_2B")
sim <- simulate_mhmm(
n = n, initial_probs = list(initial_probs_1, initial_probs_2),
transition_probs = list(transition_probs_1, transition_probs_2),
emission_probs = list(emission_probs_1, emission_probs_2),
sequence_length = 20, formula = ~ covariate_1 + covariate_2,
data = dataf, coefficients = coefs
)
ssplot(sim$observations,
hidden.paths = sim$states, plots = "both",
sortv = "from.start", sort.channel = 0, type = "I"
)
hmm <- build_mhmm(sim$observations,
initial_probs = list(initial_probs_1, initial_probs_2),
transition_probs = list(transition_probs_1, transition_probs_2),
emission_probs = list(emission_probs_1, emission_probs_2),
formula = ~ covariate_1 + covariate_2,
data = dataf
)
fit <- fit_model(hmm)
fit$model
paths <- hidden_paths(fit$model)
ssplot(list(estimates = paths, true = sim$states),
sortv = "from.start",
sort.channel = 2, ylab = c("estimated paths", "true (simulated)"),
type = "I"
)