###### Example using package example data, see ?nonverbal
# specifying general model properties:
m <- 3
n_dep <- 4
q_emiss <- c(3, 2, 3, 2)
# hypothesized mean emission probabilities
prior_prob_emiss_cat <- list(matrix(c(0.10, 0.80, 0.10,
0.80, 0.10, 0.10,
0.40, 0.40, 0.20), byrow = TRUE,
nrow = m, ncol = q_emiss[1]), # vocalizing patient,
# prior belief: state 1 - much talking, state 2 -
# no talking, state 3 - mixed
matrix(c(0.30, 0.70,
0.30, 0.70,
0.30, 0.70), byrow = TRUE, nrow = m,
ncol = q_emiss[2]), # looking patient
# prior belief: all 3 states show frequent looking
# behavior
matrix(c(0.80, 0.10, 0.10,
0.10, 0.80, 0.10,
0.40, 0.40, 0.20), byrow = TRUE,
nrow = m, ncol = q_emiss[3]), # vocalizing therapist
# prior belief: state 1 - no talking, state 2 -
# frequent talking, state 3 - mixed
matrix(c(0.30, 0.70,
0.30, 0.70,
0.30, 0.70), byrow = TRUE, nrow = m,
ncol = q_emiss[4])) # looking therapist
# prior belief: all 3 states show frequent looking
# behavior
# using the function prob_to_int to obtain intercept values for the above specified
# categorical emission distributions
prior_int_emiss <- sapply(prior_prob_emiss_cat, prob_to_int)
emiss_mu0 <- rep(list(vector(mode = "list", length = m)), n_dep)
for(k in 1:n_dep){
for(i in 1:m){
emiss_mu0[[k]][[i]] <- matrix(prior_int_emiss[[k]][i,], nrow = 1)
}
}
emiss_K0 <- rep(list(c(1)), n_dep)
emiss_nu <- list(c(5), c(4), c(5), c(4))
emiss_V <- list(diag(5, q_emiss[1] - 1),
diag(4, q_emiss[2] - 1),
diag(5, q_emiss[3] - 1),
diag(4, q_emiss[4] - 1))
manual_prior_emiss <- prior_emiss_cat(gen = list(m = m, n_dep = n_dep, q_emiss = q_emiss),
emiss_mu0 = emiss_mu0, emiss_K0 = emiss_K0,
emiss_nu = emiss_nu, emiss_V = emiss_V)
# using the informative hyper-prior in a model
# specifying starting values
start_TM <- diag(.7, m)
start_TM[lower.tri(start_TM) | upper.tri(start_TM)] <- .1
start_EM <- list(matrix(c(0.05, 0.90, 0.05,
0.90, 0.05, 0.05,
0.55, 0.45, 0.05), byrow = TRUE,
nrow = m, ncol = q_emiss[1]), # vocalizing patient
matrix(c(0.1, 0.9,
0.1, 0.9,
0.1, 0.9), byrow = TRUE, nrow = m,
ncol = q_emiss[2]), # looking patient
matrix(c(0.90, 0.05, 0.05,
0.05, 0.90, 0.05,
0.55, 0.45, 0.05), byrow = TRUE,
nrow = m, ncol = q_emiss[3]), # vocalizing therapist
matrix(c(0.1, 0.9,
0.1, 0.9,
0.1, 0.9), byrow = TRUE, nrow = m,
ncol = q_emiss[4])) # looking therapist
# Note that for reasons of running time, J is set at a ridiculous low value.
# One would typically use a number of iterations J of at least 1000,
# and a burn_in of 200.
# \donttest{
out_3st_infemiss <- mHMM(s_data = nonverbal,
gen = list(m = m, n_dep = n_dep, q_emiss = q_emiss),
start_val = c(list(start_TM), start_EM),
emiss_hyp_prior = manual_prior_emiss,
mcmc = list(J = 11, burn_in = 5))
out_3st_infemiss
summary(out_3st_infemiss)
# }
# \dontshow{
# executable in < 5 sec together with the examples above
out_3st_infemiss <- mHMM(s_data = nonverbal,
gen = list(m = m, n_dep = n_dep, q_emiss = q_emiss),
start_val = c(list(start_TM), start_EM),
emiss_hyp_prior = manual_prior_emiss,
mcmc = list(J = 5, burn_in = 3))
# }
Run the code above in your browser using DataLab