## Examples on univariate categorical data
# simulating data for 10 subjects with each 100 categorical observations
n_t <- 100
n <- 10
m <- 3
n_dep <- 1
q_emiss <- 4
gamma <- matrix(c(0.8, 0.1, 0.1,
0.2, 0.7, 0.1,
0.2, 0.2, 0.6), ncol = m, byrow = TRUE)
emiss_distr <- list(matrix(c(0.5, 0.5, 0.0, 0.0,
0.1, 0.1, 0.8, 0.0,
0.0, 0.0, 0.1, 0.9), nrow = m, ncol = q_emiss, byrow = TRUE))
data1 <- sim_mHMM(n_t = n_t, n = n, gen = list(m = m, n_dep = n_dep, q_emiss = q_emiss),
gamma = gamma, emiss_distr = emiss_distr, var_gamma = 1, var_emiss = 1)
head(data1$obs)
head(data1$states)
# including a covariate to predict (only) the transition probability matrix gamma
beta <- rep(list(NULL), 2)
beta[[1]] <- matrix(c(0.5, 1.0,
-0.5, 0.5,
0.0, 1.0), byrow = TRUE, ncol = 2)
xx_vec <- rep(list(NULL),2)
xx_vec[[1]] <- c(rep(0,5), rep(1,5))
data2 <- sim_mHMM(n_t = n_t, n = n, gen = list(m = m, n_dep = n_dep, q_emiss = q_emiss),
gamma = gamma, emiss_distr = emiss_distr, beta = beta, xx_vec = xx_vec,
var_gamma = 1, var_emiss = 1)
# simulating subject specific transition probability matrices and emission distributions only
n_t <- 0
n <- 5
m <- 3
n_dep <- 1
q_emiss <- 4
gamma <- matrix(c(0.8, 0.1, 0.1,
0.2, 0.7, 0.1,
0.2, 0.2, 0.6), ncol = m, byrow = TRUE)
emiss_distr <- list(matrix(c(0.5, 0.5, 0.0, 0.0,
0.1, 0.1, 0.8, 0.0,
0.0, 0.0, 0.1, 0.9), nrow = m, ncol = q_emiss, byrow = TRUE))
data3 <- sim_mHMM(n_t = n_t, n = n, gen = list(m = m, n_dep = n_dep, q_emiss = q_emiss),
gamma = gamma, emiss_distr = emiss_distr, var_gamma = 1, var_emiss = 1)
data3
data4 <- sim_mHMM(n_t = n_t, n = n, gen = list(m = m, n_dep = n_dep, q_emiss = q_emiss),
gamma = gamma, emiss_distr = emiss_distr, var_gamma = .5, var_emiss = .5)
data4
## Example on multivariate continuous data
# simulating multivariate continuous data
n_t <- 100
n <- 10
m <- 3
n_dep <- 2
gamma <- matrix(c(0.8, 0.1, 0.1,
0.2, 0.7, 0.1,
0.2, 0.2, 0.6), ncol = m, byrow = TRUE)
emiss_distr <- list(matrix(c( 50, 10,
100, 10,
150, 10), nrow = m, byrow = TRUE),
matrix(c(5, 2,
10, 5,
20, 3), nrow = m, byrow = TRUE))
data_cont <- sim_mHMM(n_t = n_t, n = n, data_distr = 'continuous',
gen = list(m = m, n_dep = n_dep),
gamma = gamma, emiss_distr = emiss_distr,
var_gamma = .5, var_emiss = c(5^2, 0.2^2))
head(data_cont$states)
head(data_cont$obs)
## Example on multivariate count data without covariates
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)
## Example on multivariate count data with covariates
# Simulate data with one covariate for each count dependent variable
n_t <- 200 # Number of observations on the dependent variable
m <- 3 # Number of hidden states
n_dep <- 3 # 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(15,
2,
5), nrow = m, byrow = TRUE),
matrix(c(50,
3,
20), nrow = m, byrow = TRUE))
# Since we have covariates, we specify inputs in the log scale:
emiss_distr_log <- lapply(emiss_distr, function(e) log(e))
# Define list of vectors of covariate values
set.seed(42)
xx_vec <- c(list(NULL),rep(list(rnorm(n_subj,mean = 0, sd = 0.1)),3))
# Define object beta with regression coefficients for the three dependent variables
beta <- rep(list(NULL), n_dep+1)
beta[[2]] <- matrix(c(1,-1,0), byrow = TRUE, ncol = 1)
beta[[3]] <- matrix(c(2,0,-2), byrow = TRUE, ncol = 1)
beta[[4]] <- matrix(c(-1,3,1), byrow = TRUE, ncol = 1)
# Calculate logvar to use on the simulating function:
logvar <- var_to_logvar(gen = list(m = m, n_dep = n_dep),
emiss_mu = emiss_distr,
var_emiss = list(rep(4, m),
rep(2, m),
rep(5, m)),
byrow = FALSE)
# Put logvar in the right format:
logvar <- lapply(logvar, function(q) matrix(q, 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_log,
xx_vec = xx_vec,
beta = beta,
var_gamma = 0.1,
var_emiss = logvar,
return_ind_par = TRUE,
log_scale = TRUE)
head(data_count$states)
head(data_count$obs)
Run the code above in your browser using DataLab