From fb3fa6de4b41673925f5c2f8985270eb57839e28 Mon Sep 17 00:00:00 2001 From: Jouni Helske Date: Wed, 25 Sep 2024 21:32:42 +0300 Subject: [PATCH] alpha, stats::, docs --- R/simulate_pars.R | 16 +++++++++------- man/simulate_pars.Rd | 16 ++++++++-------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/R/simulate_pars.R b/R/simulate_pars.R index b038a5ee..46d04d60 100644 --- a/R/simulate_pars.R +++ b/R/simulate_pars.R @@ -20,12 +20,12 @@ simulate_initial_probs <- function(n_states, n_clusters = 1, alpha = 1) { n_states <- rep(n_states, length = n_clusters) if (n_clusters == 1) { - x <- rgamma(n_states, alpha) + x <- stats::rgamma(n_states, alpha) x / sum(x) } else { probs <- vector("list", n_clusters) for (i in 1:n_clusters) { - x <- rgamma(n_states[i], alpha) + x <- stats::rgamma(n_states[i], alpha) probs[[i]] <- x / sum(x) } probs @@ -33,10 +33,11 @@ simulate_initial_probs <- function(n_states, n_clusters = 1, alpha = 1) { } #' @export #' @rdname simulate_pars -simulate_transition_probs <- function(n_states, n_clusters = 1, left_right = FALSE, diag_c = 0, alpha = 1) { +simulate_transition_probs <- function(n_states, n_clusters = 1, + left_right = FALSE, diag_c = 0, alpha = 1) { n_states <- rep(n_states, length = n_clusters) if (n_clusters == 1) { - x <- matrix(rgamma(n_states^2, alpha), n_states, n_states, TRUE) + + x <- matrix(stats::rgamma(n_states^2, alpha), n_states, n_states, TRUE) + diag(diag_c, n_states) if (left_right) x[lower.tri(x)] <- 0 probs <- x / rowSums(x) @@ -44,7 +45,7 @@ simulate_transition_probs <- function(n_states, n_clusters = 1, left_right = FAL probs <- vector("list", n_clusters) for (i in seq_len(n_clusters)) { x <- matrix( - rgamma(n_states[i]^2, alpha), n_states[i], n_states[i], TRUE + stats::rgamma(n_states[i]^2, alpha), n_states[i], n_states[i], TRUE ) + diag(diag_c, n_states[i]) if (left_right) x[lower.tri(x)] <- 0 probs[[i]] <- x / rowSums(x) @@ -64,7 +65,7 @@ simulate_emission_probs <- function(n_states, n_symbols, n_clusters = 1, emiss[[i]] <- vector("list", n_channels) for (j in seq_len(n_channels)) { emiss[[i]][[j]] <- matrix( - rgamma(n_states[i] * n_symbols[j], alpha), n_states[i], n_symbols[j], + stats::rgamma(n_states[i] * n_symbols[j], alpha), n_states[i], n_symbols[j], TRUE) emiss[[i]][[j]] <- emiss[[i]][[j]] / rowSums(emiss[[i]][[j]]) } @@ -72,7 +73,8 @@ simulate_emission_probs <- function(n_states, n_symbols, n_clusters = 1, } else { for (i in seq_len(n_clusters)) { emiss[[i]] <- matrix( - rgamma(n_states[i] * n_symbols), n_states[i], n_symbols, TRUE + stats::rgamma(n_states[i] * n_symbols, alpha), n_states[i], n_symbols, + TRUE ) emiss[[i]] <- emiss[[i]] / rowSums(emiss[[i]]) } diff --git a/man/simulate_pars.Rd b/man/simulate_pars.Rd index 2ed3f068..e752c8bc 100644 --- a/man/simulate_pars.Rd +++ b/man/simulate_pars.Rd @@ -6,22 +6,27 @@ \alias{simulate_emission_probs} \title{Simulate Parameters of Hidden Markov Models} \usage{ -simulate_initial_probs(n_states, n_clusters = 1) +simulate_initial_probs(n_states, n_clusters = 1, alpha = 1) simulate_transition_probs( n_states, n_clusters = 1, left_right = FALSE, - diag_c = 0 + diag_c = 0, + alpha = 1 ) -simulate_emission_probs(n_states, n_symbols, n_clusters = 1) +simulate_emission_probs(n_states, n_symbols, n_clusters = 1, alpha = 1) } \arguments{ \item{n_states}{Number of states in each cluster.} \item{n_clusters}{Number of clusters.} +\item{alpha}{A scalar, or a vector of length S (number of states) or M +(number of symbols) defining the parameters of the Dirichlet distribution +used to simulate the probabilities.} + \item{left_right}{Constrain the transition probabilities to upper triangular. Default is \code{FALSE}.} @@ -34,8 +39,3 @@ These are helper functions for quick construction of initial values for various model building functions. Mostly useful for global optimization algorithms which do not depend on initial values. } -\seealso{ -\code{\link[=build_hmm]{build_hmm()}}, \code{\link[=build_mhmm]{build_mhmm()}}, -\code{\link[=build_mm]{build_mm()}}, \code{\link[=build_mmm]{build_mmm()}}, and \code{\link[=build_lcm]{build_lcm()}} -for constructing different types of models. -}