###### Example using simulated data
# specifying general model properties:
n_t <- 200 # Number of observations on the dependent variable
m <- 3 # Number of hidden states
n_dep <- 2 # Number of dependent variables
n_subj <- 30 # Number of subjects
gamma <- matrix(c(0.9, 0.05, 0.05,
0.2, 0.7, 0.1,
0.2,0.3, 0.5), ncol = m, byrow = TRUE)
emiss_distr <- list(matrix(c(20,
10,
5), nrow = m, byrow = TRUE),
matrix(c(50,
3,
20), nrow = m, byrow = TRUE))
# Define between subject variance to use on the simulating function:
# here, the variance is varied over states within the dependent variable.
var_emiss <- list(matrix(c(5.0, 3.0, 1.5), nrow = m),
matrix(c(5.0, 5.0, 5.0), nrow = m))
# Simulate count data:
data_count <- sim_mHMM(n_t = n_t,
n = n_subj,
data_distr = "count",
gen = list(m = m, n_dep = n_dep),
gamma = gamma,
emiss_distr = emiss_distr,
var_gamma = 0.1,
var_emiss = var_emiss,
return_ind_par = TRUE)
# Transition probabilities
start_gamma <- diag(0.8, m)
start_gamma[lower.tri(start_gamma) | upper.tri(start_gamma)] <- (1 - diag(start_gamma)) / (m - 1)
# Emission distribution
start_emiss <- list(matrix(c(20,10, 5), nrow = m, byrow = TRUE),
matrix(c(50, 3,20), nrow = m, byrow = TRUE))
# Specify hyper-prior for the count emission distribution
manual_prior_emiss <- prior_emiss_count(
gen = list(m = m, n_dep = n_dep),
emiss_mu0 = list(matrix(c(20, 10, 5), byrow = TRUE, ncol = m),
matrix(c(50, 3, 20), byrow = TRUE, ncol = m)),
emiss_K0 = rep(list(0.1),n_dep),
emiss_nu = rep(list(0.1),n_dep),
emiss_V = rep(list(rep(10, m)),n_dep)
)
# Specify the desired values for the sampler
manual_emiss_sampler <- pd_RW_emiss_count(gen = list(m = m, n_dep = n_dep),
emiss_scalar = rep(list(2.38),n_dep),
emiss_w = rep(list(0.1), n_dep))
# Run model
# 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.
out_3st_count_RWemiss <- mHMM(s_data = data_count$obs,
data_distr = 'count',
gen = list(m = m, n_dep = n_dep),
start_val = c(list(start_gamma), start_emiss),
emiss_hyp_prior = manual_prior_emiss,
emiss_sampler = manual_emiss_sampler,
mcmc = list(J = 11, burn_in = 5),
show_progress = TRUE)
# Examine acceptance rates over dependent variable, individual, and states:
lapply(out_3st_count_RWemiss$emiss_naccept, function(e) e/out_3st_count_RWemiss$input$J)
# Finally, take the average acceptance rate by dependent variable and state:
lapply(out_3st_count_RWemiss$emiss_naccept, function(e) colMeans(e/out_3st_count_RWemiss$input$J))
Run the code above in your browser using DataLab