Title: | Mixture of Markov Chains with Support of Higher Orders and Multiple Sequences |
---|---|
Description: | Fit mixture of Markov chains of higher orders from multiple sequences. It is also compatible with ordinary 1-component, 1-order or single-sequence Markov chains. Various utility functions are provided to derive transition patterns, transition probabilities per component and component priors. In addition, print(), predict() and component extracting/replacing methods are also defined as a convention of mixture models. |
Authors: | Xiurui Zhu [aut, cre] |
Maintainer: | Xiurui Zhu <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.3 |
Built: | 2024-11-18 05:55:13 UTC |
Source: | https://github.com/zhuxr11/markovmix |
markovmix
package Fit mixture of Markov chains of higher orders from multiple
sequences. It is also compatible with ordinary 1-component, 1-order or
single-sequence Markov chains. Various utility functions are provided
to derive transition patterns, transition probabilities per component
and component priors. In addition, print()
,
predict()
and component extracting/replacing
methods are also defined as a convention of mixture models.
Change log:
0.1.0 Xiurui Zhu - Initiate the package.
0.1.2 Xiurui Zhu - Update package documentation.
Xiurui Zhu
Operators to extract or replace components of a MarkovMix
object.
## S3 method for class 'MarkovMix' x[i] ## S3 replacement method for class 'MarkovMix' x[i] <- value
## S3 method for class 'MarkovMix' x[i] ## S3 replacement method for class 'MarkovMix' x[i] <- value
x |
|
i |
Indices specifying components to extract or replace. |
value |
Numeric matrix as soft counts for transition patterns (like |
Change log:
0.1.1 Xiurui Zhu - Initiate the functions.
0.1.2 Xiurui Zhu - Update function documentation.
Xiurui Zhu
Other MarkovMix utilities:
get_counts()
,
get_order()
,
get_prior()
,
get_prob()
,
get_states_mat()
,
get_states()
,
restate()
# Load example MarkovMix object data("markov_mix_ex") # Derive transition pattern soft counts get_counts(object = markov_mix_ex) # Derive the order of Markov chains get_order(object = markov_mix_ex) # Derive the states of Markov chains get_states(object = markov_mix_ex) # Derive state transition patterns get_states_mat(markov_mix_ex) # Derive probability matrices get_prob(markov_mix_ex) # Derive component priors get_prior(markov_mix_ex) # Combine state transition patterns and their probabilities cbind( as.data.frame(get_states_mat(markov_mix_ex)), as.data.frame(get_prob(markov_mix_ex)) ) # Extract 1 or more components markov_mix_ex[2L] markov_mix_ex[c(1L, 3L)] # Replace 1 or more components nrow_value <- length(get_states(object = markov_mix_ex, check = FALSE))^ (get_order(object = markov_mix_ex, check = FALSE) + 1L) markov_mix_ex2 <- markov_mix_ex markov_mix_ex2[2L] <- runif(nrow_value) print(markov_mix_ex2) markov_mix_ex3 <- markov_mix_ex markov_mix_ex3[c(1L, 3L)] <- matrix(runif(nrow_value * 2L), ncol = 2L) print(markov_mix_ex3)
# Load example MarkovMix object data("markov_mix_ex") # Derive transition pattern soft counts get_counts(object = markov_mix_ex) # Derive the order of Markov chains get_order(object = markov_mix_ex) # Derive the states of Markov chains get_states(object = markov_mix_ex) # Derive state transition patterns get_states_mat(markov_mix_ex) # Derive probability matrices get_prob(markov_mix_ex) # Derive component priors get_prior(markov_mix_ex) # Combine state transition patterns and their probabilities cbind( as.data.frame(get_states_mat(markov_mix_ex)), as.data.frame(get_prob(markov_mix_ex)) ) # Extract 1 or more components markov_mix_ex[2L] markov_mix_ex[c(1L, 3L)] # Replace 1 or more components nrow_value <- length(get_states(object = markov_mix_ex, check = FALSE))^ (get_order(object = markov_mix_ex, check = FALSE) + 1L) markov_mix_ex2 <- markov_mix_ex markov_mix_ex2[2L] <- runif(nrow_value) print(markov_mix_ex2) markov_mix_ex3 <- markov_mix_ex markov_mix_ex3[c(1L, 3L)] <- matrix(runif(nrow_value * 2L), ncol = 2L) print(markov_mix_ex3)
fit_markov_mix
fits mixture of Markov chains. It supports high-order Markov chains,
multiple sequences and mixture components with cluster probabilities.
fit_markov_mix( seq_list, order. = 1L, states = NULL, clusters = NULL, verbose = TRUE )
fit_markov_mix( seq_list, order. = 1L, states = NULL, clusters = NULL, verbose = TRUE )
seq_list |
Sequence list containing vectors of the same class. |
order. |
Integer (1L) indicating the order of the Markov chain. |
states |
|
clusters |
|
verbose |
Logical (1L) indicating whether additional messages should be printed. |
An object of class MarkovMix
.
Change log:
0.1.0 Xiurui Zhu - Initiate the function.
Xiurui Zhu
# Generate a list of integer sequences of different lengths with 4 states test_states <- seq_len(4L) test_maxlen <- 10L set.seed(1111L) test_seq <- purrr::map( seq_len(100L), ~ sample(test_states, sample.int(test_maxlen, 1L), replace = TRUE) ) # Fit a 1-order Markov chain markov_fit <- fit_markov_mix( seq_list = test_seq, order. = 1L, states = test_states ) print(markov_fit) # Fit a mixture of 2-order Markov chain with 3 components test_n_comp <- 3L test_clusters <- matrix( runif(length(test_seq) * test_n_comp), nrow = length(test_seq), ncol = test_n_comp ) markov_mix_fit <- fit_markov_mix( seq_list = test_seq, order. = 2L, states = test_states, clusters = test_clusters ) print(markov_mix_fit)
# Generate a list of integer sequences of different lengths with 4 states test_states <- seq_len(4L) test_maxlen <- 10L set.seed(1111L) test_seq <- purrr::map( seq_len(100L), ~ sample(test_states, sample.int(test_maxlen, 1L), replace = TRUE) ) # Fit a 1-order Markov chain markov_fit <- fit_markov_mix( seq_list = test_seq, order. = 1L, states = test_states ) print(markov_fit) # Fit a mixture of 2-order Markov chain with 3 components test_n_comp <- 3L test_clusters <- matrix( runif(length(test_seq) * test_n_comp), nrow = length(test_seq), ncol = test_n_comp ) markov_mix_fit <- fit_markov_mix( seq_list = test_seq, order. = 2L, states = test_states, clusters = test_clusters ) print(markov_mix_fit)
get_counts
gets transition pattern counts from MarkovMix
object.
get_counts(object, check = TRUE)
get_counts(object, check = TRUE)
object |
|
check |
Logical (1L) indicating whether to check |
A numeric matrix indicates transition pattern (soft) counts, where each row corresponds to a transition pattern and each column corresponds to a component.
Change log:
0.1.2 Xiurui Zhu - Initiate the function.
Xiurui Zhu
Other MarkovMix utilities:
Extract.MarkovMix
,
get_order()
,
get_prior()
,
get_prob()
,
get_states_mat()
,
get_states()
,
restate()
# Load example MarkovMix object data("markov_mix_ex") # Derive transition pattern soft counts get_counts(object = markov_mix_ex) # Derive the order of Markov chains get_order(object = markov_mix_ex) # Derive the states of Markov chains get_states(object = markov_mix_ex) # Derive state transition patterns get_states_mat(markov_mix_ex) # Derive probability matrices get_prob(markov_mix_ex) # Derive component priors get_prior(markov_mix_ex) # Combine state transition patterns and their probabilities cbind( as.data.frame(get_states_mat(markov_mix_ex)), as.data.frame(get_prob(markov_mix_ex)) ) # Extract 1 or more components markov_mix_ex[2L] markov_mix_ex[c(1L, 3L)] # Replace 1 or more components nrow_value <- length(get_states(object = markov_mix_ex, check = FALSE))^ (get_order(object = markov_mix_ex, check = FALSE) + 1L) markov_mix_ex2 <- markov_mix_ex markov_mix_ex2[2L] <- runif(nrow_value) print(markov_mix_ex2) markov_mix_ex3 <- markov_mix_ex markov_mix_ex3[c(1L, 3L)] <- matrix(runif(nrow_value * 2L), ncol = 2L) print(markov_mix_ex3)
# Load example MarkovMix object data("markov_mix_ex") # Derive transition pattern soft counts get_counts(object = markov_mix_ex) # Derive the order of Markov chains get_order(object = markov_mix_ex) # Derive the states of Markov chains get_states(object = markov_mix_ex) # Derive state transition patterns get_states_mat(markov_mix_ex) # Derive probability matrices get_prob(markov_mix_ex) # Derive component priors get_prior(markov_mix_ex) # Combine state transition patterns and their probabilities cbind( as.data.frame(get_states_mat(markov_mix_ex)), as.data.frame(get_prob(markov_mix_ex)) ) # Extract 1 or more components markov_mix_ex[2L] markov_mix_ex[c(1L, 3L)] # Replace 1 or more components nrow_value <- length(get_states(object = markov_mix_ex, check = FALSE))^ (get_order(object = markov_mix_ex, check = FALSE) + 1L) markov_mix_ex2 <- markov_mix_ex markov_mix_ex2[2L] <- runif(nrow_value) print(markov_mix_ex2) markov_mix_ex3 <- markov_mix_ex markov_mix_ex3[c(1L, 3L)] <- matrix(runif(nrow_value * 2L), ncol = 2L) print(markov_mix_ex3)
get_order
gets the order of Markov chains from MarkovMix
object.
get_order(object, check = TRUE)
get_order(object, check = TRUE)
object |
|
check |
Logical (1L) indicating whether to check |
An integer as the order of Markov chains.
Change log:
0.1.2 Xiurui Zhu - Initiate the function.
Xiurui Zhu
Other MarkovMix utilities:
Extract.MarkovMix
,
get_counts()
,
get_prior()
,
get_prob()
,
get_states_mat()
,
get_states()
,
restate()
# Load example MarkovMix object data("markov_mix_ex") # Derive transition pattern soft counts get_counts(object = markov_mix_ex) # Derive the order of Markov chains get_order(object = markov_mix_ex) # Derive the states of Markov chains get_states(object = markov_mix_ex) # Derive state transition patterns get_states_mat(markov_mix_ex) # Derive probability matrices get_prob(markov_mix_ex) # Derive component priors get_prior(markov_mix_ex) # Combine state transition patterns and their probabilities cbind( as.data.frame(get_states_mat(markov_mix_ex)), as.data.frame(get_prob(markov_mix_ex)) ) # Extract 1 or more components markov_mix_ex[2L] markov_mix_ex[c(1L, 3L)] # Replace 1 or more components nrow_value <- length(get_states(object = markov_mix_ex, check = FALSE))^ (get_order(object = markov_mix_ex, check = FALSE) + 1L) markov_mix_ex2 <- markov_mix_ex markov_mix_ex2[2L] <- runif(nrow_value) print(markov_mix_ex2) markov_mix_ex3 <- markov_mix_ex markov_mix_ex3[c(1L, 3L)] <- matrix(runif(nrow_value * 2L), ncol = 2L) print(markov_mix_ex3)
# Load example MarkovMix object data("markov_mix_ex") # Derive transition pattern soft counts get_counts(object = markov_mix_ex) # Derive the order of Markov chains get_order(object = markov_mix_ex) # Derive the states of Markov chains get_states(object = markov_mix_ex) # Derive state transition patterns get_states_mat(markov_mix_ex) # Derive probability matrices get_prob(markov_mix_ex) # Derive component priors get_prior(markov_mix_ex) # Combine state transition patterns and their probabilities cbind( as.data.frame(get_states_mat(markov_mix_ex)), as.data.frame(get_prob(markov_mix_ex)) ) # Extract 1 or more components markov_mix_ex[2L] markov_mix_ex[c(1L, 3L)] # Replace 1 or more components nrow_value <- length(get_states(object = markov_mix_ex, check = FALSE))^ (get_order(object = markov_mix_ex, check = FALSE) + 1L) markov_mix_ex2 <- markov_mix_ex markov_mix_ex2[2L] <- runif(nrow_value) print(markov_mix_ex2) markov_mix_ex3 <- markov_mix_ex markov_mix_ex3[c(1L, 3L)] <- matrix(runif(nrow_value * 2L), ncol = 2L) print(markov_mix_ex3)
get_prior
gets component priors from MarkovMix
object,
normalized to sum up to 1.
get_prior(object, check = TRUE)
get_prior(object, check = TRUE)
object |
|
check |
Logical (1L) indicating whether to check |
A numeric vector indicates component priors.
Change log:
0.1.0 Xiurui Zhu - Initiate the function.
Xiurui Zhu
Other MarkovMix utilities:
Extract.MarkovMix
,
get_counts()
,
get_order()
,
get_prob()
,
get_states_mat()
,
get_states()
,
restate()
# Load example MarkovMix object data("markov_mix_ex") # Derive transition pattern soft counts get_counts(object = markov_mix_ex) # Derive the order of Markov chains get_order(object = markov_mix_ex) # Derive the states of Markov chains get_states(object = markov_mix_ex) # Derive state transition patterns get_states_mat(markov_mix_ex) # Derive probability matrices get_prob(markov_mix_ex) # Derive component priors get_prior(markov_mix_ex) # Combine state transition patterns and their probabilities cbind( as.data.frame(get_states_mat(markov_mix_ex)), as.data.frame(get_prob(markov_mix_ex)) ) # Extract 1 or more components markov_mix_ex[2L] markov_mix_ex[c(1L, 3L)] # Replace 1 or more components nrow_value <- length(get_states(object = markov_mix_ex, check = FALSE))^ (get_order(object = markov_mix_ex, check = FALSE) + 1L) markov_mix_ex2 <- markov_mix_ex markov_mix_ex2[2L] <- runif(nrow_value) print(markov_mix_ex2) markov_mix_ex3 <- markov_mix_ex markov_mix_ex3[c(1L, 3L)] <- matrix(runif(nrow_value * 2L), ncol = 2L) print(markov_mix_ex3)
# Load example MarkovMix object data("markov_mix_ex") # Derive transition pattern soft counts get_counts(object = markov_mix_ex) # Derive the order of Markov chains get_order(object = markov_mix_ex) # Derive the states of Markov chains get_states(object = markov_mix_ex) # Derive state transition patterns get_states_mat(markov_mix_ex) # Derive probability matrices get_prob(markov_mix_ex) # Derive component priors get_prior(markov_mix_ex) # Combine state transition patterns and their probabilities cbind( as.data.frame(get_states_mat(markov_mix_ex)), as.data.frame(get_prob(markov_mix_ex)) ) # Extract 1 or more components markov_mix_ex[2L] markov_mix_ex[c(1L, 3L)] # Replace 1 or more components nrow_value <- length(get_states(object = markov_mix_ex, check = FALSE))^ (get_order(object = markov_mix_ex, check = FALSE) + 1L) markov_mix_ex2 <- markov_mix_ex markov_mix_ex2[2L] <- runif(nrow_value) print(markov_mix_ex2) markov_mix_ex3 <- markov_mix_ex markov_mix_ex3[c(1L, 3L)] <- matrix(runif(nrow_value * 2L), ncol = 2L) print(markov_mix_ex3)
get_prob
gets probability matrix from MarkovMix
object.
It normalizes each column in the count matrix to sum up to 1.
get_prob(object, check = TRUE)
get_prob(object, check = TRUE)
object |
|
check |
Logical (1L) indicating whether to check |
A numeric matrix indicating probabilities of each state transition pattern in each component.
Change log:
0.1.0 Xiurui Zhu - Initiate the function.
Xiurui Zhu
Other MarkovMix utilities:
Extract.MarkovMix
,
get_counts()
,
get_order()
,
get_prior()
,
get_states_mat()
,
get_states()
,
restate()
# Load example MarkovMix object data("markov_mix_ex") # Derive transition pattern soft counts get_counts(object = markov_mix_ex) # Derive the order of Markov chains get_order(object = markov_mix_ex) # Derive the states of Markov chains get_states(object = markov_mix_ex) # Derive state transition patterns get_states_mat(markov_mix_ex) # Derive probability matrices get_prob(markov_mix_ex) # Derive component priors get_prior(markov_mix_ex) # Combine state transition patterns and their probabilities cbind( as.data.frame(get_states_mat(markov_mix_ex)), as.data.frame(get_prob(markov_mix_ex)) ) # Extract 1 or more components markov_mix_ex[2L] markov_mix_ex[c(1L, 3L)] # Replace 1 or more components nrow_value <- length(get_states(object = markov_mix_ex, check = FALSE))^ (get_order(object = markov_mix_ex, check = FALSE) + 1L) markov_mix_ex2 <- markov_mix_ex markov_mix_ex2[2L] <- runif(nrow_value) print(markov_mix_ex2) markov_mix_ex3 <- markov_mix_ex markov_mix_ex3[c(1L, 3L)] <- matrix(runif(nrow_value * 2L), ncol = 2L) print(markov_mix_ex3)
# Load example MarkovMix object data("markov_mix_ex") # Derive transition pattern soft counts get_counts(object = markov_mix_ex) # Derive the order of Markov chains get_order(object = markov_mix_ex) # Derive the states of Markov chains get_states(object = markov_mix_ex) # Derive state transition patterns get_states_mat(markov_mix_ex) # Derive probability matrices get_prob(markov_mix_ex) # Derive component priors get_prior(markov_mix_ex) # Combine state transition patterns and their probabilities cbind( as.data.frame(get_states_mat(markov_mix_ex)), as.data.frame(get_prob(markov_mix_ex)) ) # Extract 1 or more components markov_mix_ex[2L] markov_mix_ex[c(1L, 3L)] # Replace 1 or more components nrow_value <- length(get_states(object = markov_mix_ex, check = FALSE))^ (get_order(object = markov_mix_ex, check = FALSE) + 1L) markov_mix_ex2 <- markov_mix_ex markov_mix_ex2[2L] <- runif(nrow_value) print(markov_mix_ex2) markov_mix_ex3 <- markov_mix_ex markov_mix_ex3[c(1L, 3L)] <- matrix(runif(nrow_value * 2L), ncol = 2L) print(markov_mix_ex3)
get_states
gets the states of Markov chains from MarkovMix
object.
get_states(object, check = TRUE)
get_states(object, check = TRUE)
object |
|
check |
Logical (1L) indicating whether to check |
A vector as the states used in Markov chains.
Change log:
0.1.2 Xiurui Zhu - Initiate the function.
Xiurui Zhu
Other MarkovMix utilities:
Extract.MarkovMix
,
get_counts()
,
get_order()
,
get_prior()
,
get_prob()
,
get_states_mat()
,
restate()
# Load example MarkovMix object data("markov_mix_ex") # Derive transition pattern soft counts get_counts(object = markov_mix_ex) # Derive the order of Markov chains get_order(object = markov_mix_ex) # Derive the states of Markov chains get_states(object = markov_mix_ex) # Derive state transition patterns get_states_mat(markov_mix_ex) # Derive probability matrices get_prob(markov_mix_ex) # Derive component priors get_prior(markov_mix_ex) # Combine state transition patterns and their probabilities cbind( as.data.frame(get_states_mat(markov_mix_ex)), as.data.frame(get_prob(markov_mix_ex)) ) # Extract 1 or more components markov_mix_ex[2L] markov_mix_ex[c(1L, 3L)] # Replace 1 or more components nrow_value <- length(get_states(object = markov_mix_ex, check = FALSE))^ (get_order(object = markov_mix_ex, check = FALSE) + 1L) markov_mix_ex2 <- markov_mix_ex markov_mix_ex2[2L] <- runif(nrow_value) print(markov_mix_ex2) markov_mix_ex3 <- markov_mix_ex markov_mix_ex3[c(1L, 3L)] <- matrix(runif(nrow_value * 2L), ncol = 2L) print(markov_mix_ex3)
# Load example MarkovMix object data("markov_mix_ex") # Derive transition pattern soft counts get_counts(object = markov_mix_ex) # Derive the order of Markov chains get_order(object = markov_mix_ex) # Derive the states of Markov chains get_states(object = markov_mix_ex) # Derive state transition patterns get_states_mat(markov_mix_ex) # Derive probability matrices get_prob(markov_mix_ex) # Derive component priors get_prior(markov_mix_ex) # Combine state transition patterns and their probabilities cbind( as.data.frame(get_states_mat(markov_mix_ex)), as.data.frame(get_prob(markov_mix_ex)) ) # Extract 1 or more components markov_mix_ex[2L] markov_mix_ex[c(1L, 3L)] # Replace 1 or more components nrow_value <- length(get_states(object = markov_mix_ex, check = FALSE))^ (get_order(object = markov_mix_ex, check = FALSE) + 1L) markov_mix_ex2 <- markov_mix_ex markov_mix_ex2[2L] <- runif(nrow_value) print(markov_mix_ex2) markov_mix_ex3 <- markov_mix_ex markov_mix_ex3[c(1L, 3L)] <- matrix(runif(nrow_value * 2L), ncol = 2L) print(markov_mix_ex3)
get_states_mat
gets state transition patterns from MarkovMix
object.
The number of columns is the order of the (mixture of) Markov chain(s) plus 1 (the destination state).
Each column is arranged in the ascending order of the states.
The last column serves as the destination state and iterates the fastest.
get_states_mat(object, check = TRUE)
get_states_mat(object, check = TRUE)
object |
|
check |
Logical (1L) indicating whether to check |
A matrix indicating the state transition patterns.
Change log:
0.1.0 Xiurui Zhu - Initiate the function.
Xiurui Zhu
Other MarkovMix utilities:
Extract.MarkovMix
,
get_counts()
,
get_order()
,
get_prior()
,
get_prob()
,
get_states()
,
restate()
# Load example MarkovMix object data("markov_mix_ex") # Derive transition pattern soft counts get_counts(object = markov_mix_ex) # Derive the order of Markov chains get_order(object = markov_mix_ex) # Derive the states of Markov chains get_states(object = markov_mix_ex) # Derive state transition patterns get_states_mat(markov_mix_ex) # Derive probability matrices get_prob(markov_mix_ex) # Derive component priors get_prior(markov_mix_ex) # Combine state transition patterns and their probabilities cbind( as.data.frame(get_states_mat(markov_mix_ex)), as.data.frame(get_prob(markov_mix_ex)) ) # Extract 1 or more components markov_mix_ex[2L] markov_mix_ex[c(1L, 3L)] # Replace 1 or more components nrow_value <- length(get_states(object = markov_mix_ex, check = FALSE))^ (get_order(object = markov_mix_ex, check = FALSE) + 1L) markov_mix_ex2 <- markov_mix_ex markov_mix_ex2[2L] <- runif(nrow_value) print(markov_mix_ex2) markov_mix_ex3 <- markov_mix_ex markov_mix_ex3[c(1L, 3L)] <- matrix(runif(nrow_value * 2L), ncol = 2L) print(markov_mix_ex3)
# Load example MarkovMix object data("markov_mix_ex") # Derive transition pattern soft counts get_counts(object = markov_mix_ex) # Derive the order of Markov chains get_order(object = markov_mix_ex) # Derive the states of Markov chains get_states(object = markov_mix_ex) # Derive state transition patterns get_states_mat(markov_mix_ex) # Derive probability matrices get_prob(markov_mix_ex) # Derive component priors get_prior(markov_mix_ex) # Combine state transition patterns and their probabilities cbind( as.data.frame(get_states_mat(markov_mix_ex)), as.data.frame(get_prob(markov_mix_ex)) ) # Extract 1 or more components markov_mix_ex[2L] markov_mix_ex[c(1L, 3L)] # Replace 1 or more components nrow_value <- length(get_states(object = markov_mix_ex, check = FALSE))^ (get_order(object = markov_mix_ex, check = FALSE) + 1L) markov_mix_ex2 <- markov_mix_ex markov_mix_ex2[2L] <- runif(nrow_value) print(markov_mix_ex2) markov_mix_ex3 <- markov_mix_ex markov_mix_ex3[c(1L, 3L)] <- matrix(runif(nrow_value * 2L), ncol = 2L) print(markov_mix_ex3)
A mixture of 2-order Markov chain fit from 100 random sequences with 4 states (A, B, C, D) and 3 components.
markov_mix_ex
markov_mix_ex
A MarkovMix
object.
Change log:
0.1.0 Xiurui Zhu - Initiate the dataset.
Xiurui Zhu
An object of class MarkovMix
is a list containing the following components:
Numeric matrix containing soft counts of sub-sequence patterns in each component. For (non-mixture) Markov chains, the matrix contains only 1 column and counts are actually integers, but they are still stored as numeric values.
Integer (1L) as the order of (mixture) Markov chain(s).
Vector as the states in the (mixture) Markov chain(s).
Change log:
0.1.0 Xiurui Zhu - Initiate the class.
Xiurui Zhu
predict.MarkovMix
predicts probabilities with MarkovMix
object
and new sequence list. NA values are returned for sequences with no valid sub-sequences
to distinguish them from those that are truly not observed (probabilities = 0)
in the transition matrices.
## S3 method for class 'MarkovMix' predict(object, newdata, aggregate. = TRUE, ...)
## S3 method for class 'MarkovMix' predict(object, newdata, aggregate. = TRUE, ...)
object |
|
newdata |
Sequence list containing vectors of the same class. |
aggregate. |
Logical (1L) indicating whether probabilities
from each component should be weighted mean by component priors
( |
... |
Currently ignored for this method. |
For aggregate. = TRUE
, a numeric vector of probabilities.
For aggregate. = TRUE
, a numeric matrix of probabilities
from each component.
Change log:
0.1.0 Xiurui Zhu - Initiate the function.
Xiurui Zhu
Other MarkovMix methods:
print.MarkovMix()
# Load example MarkovMix object data("markov_mix_ex") # Generate a new list of sequences set.seed(2222L) new_maxlen <- 8L new_seq_list <- purrr::map( seq_len(50L), ~ sample(get_states(object = markov_mix_ex, check = FALSE), sample.int(new_maxlen, 1L), replace = TRUE) ) # Predict the probabilities of sequences predict(markov_mix_ex, newdata = new_seq_list) # Predict the probabilities of sequences from each component predict(markov_mix_ex, newdata = new_seq_list, aggregate. = FALSE)
# Load example MarkovMix object data("markov_mix_ex") # Generate a new list of sequences set.seed(2222L) new_maxlen <- 8L new_seq_list <- purrr::map( seq_len(50L), ~ sample(get_states(object = markov_mix_ex, check = FALSE), sample.int(new_maxlen, 1L), replace = TRUE) ) # Predict the probabilities of sequences predict(markov_mix_ex, newdata = new_seq_list) # Predict the probabilities of sequences from each component predict(markov_mix_ex, newdata = new_seq_list, aggregate. = FALSE)
print.MarkovMix
prints MarkovMix
object in a user-friendly form,
including component priors and transition matrices.
## S3 method for class 'MarkovMix' print(x, sep = "->", print_max = 20L, print_min = 10L, ...)
## S3 method for class 'MarkovMix' print(x, sep = "->", print_max = 20L, print_min = 10L, ...)
x |
|
sep |
Character (1L) used as separator between states in the row names of transition matrix. |
print_max , print_min
|
Integers as the numbers of rows to print each transition matrix.
See |
... |
Currently ignored for this method. |
Input x
, invisibly.
Change log:
0.1.0 Xiurui Zhu - Initiate the function.
Xiurui Zhu
Other MarkovMix methods:
predict.MarkovMix()
# Generate a list of integer sequences of different lengths with 4 states test_states <- seq_len(4L) test_maxlen <- 10L set.seed(1111L) test_seq <- purrr::map( seq_len(100L), ~ sample(test_states, sample.int(test_maxlen, 1L), replace = TRUE) ) # Fit a 1-order Markov chain markov_fit <- fit_markov_mix( seq_list = test_seq, order. = 1L, states = test_states ) print(markov_fit) # Fit a mixture of 2-order Markov chain with 3 components test_n_comp <- 3L test_clusters <- matrix( runif(length(test_seq) * test_n_comp), nrow = length(test_seq), ncol = test_n_comp ) markov_mix_fit <- fit_markov_mix( seq_list = test_seq, order. = 2L, states = test_states, clusters = test_clusters ) print(markov_mix_fit)
# Generate a list of integer sequences of different lengths with 4 states test_states <- seq_len(4L) test_maxlen <- 10L set.seed(1111L) test_seq <- purrr::map( seq_len(100L), ~ sample(test_states, sample.int(test_maxlen, 1L), replace = TRUE) ) # Fit a 1-order Markov chain markov_fit <- fit_markov_mix( seq_list = test_seq, order. = 1L, states = test_states ) print(markov_fit) # Fit a mixture of 2-order Markov chain with 3 components test_n_comp <- 3L test_clusters <- matrix( runif(length(test_seq) * test_n_comp), nrow = length(test_seq), ncol = test_n_comp ) markov_mix_fit <- fit_markov_mix( seq_list = test_seq, order. = 2L, states = test_states, clusters = test_clusters ) print(markov_mix_fit)
restate
reorganizes states in MarkovMix
object with a function.
restate(.object, .fun, .check = TRUE, ...)
restate(.object, .fun, .check = TRUE, ...)
.object |
|
.fun |
Function to process each column in state transition patterns as factors,
such as those in |
.check |
Logical (1L) indicating whether to check |
... |
Additional arguments passed on to |
A MarkovMix
object with modified states and count matrix.
Change log:
0.1.0 Xiurui Zhu - Initiate the function.
Xiurui Zhu
Other MarkovMix utilities:
Extract.MarkovMix
,
get_counts()
,
get_order()
,
get_prior()
,
get_prob()
,
get_states_mat()
,
get_states()
# Load example MarkovMix object data("markov_mix_ex") # Reverse states (using function) markov_mix_new1 <- restate( .object = markov_mix_ex, .fun = forcats::fct_rev ) print(markov_mix_new1) # Reorder states by hand (using function name with additional arguments) markov_mix_new2 <- restate( .object = markov_mix_ex, .fun = "levels<-", value = c("B", "D", "C", "A") ) print(markov_mix_new2) # Merge state D into C (using purrr-style lambda function) markov_mix_new3 <- restate( .object = markov_mix_ex, .fun = ~ forcats::fct_recode(.x, "C" = "D") ) print(markov_mix_new3)
# Load example MarkovMix object data("markov_mix_ex") # Reverse states (using function) markov_mix_new1 <- restate( .object = markov_mix_ex, .fun = forcats::fct_rev ) print(markov_mix_new1) # Reorder states by hand (using function name with additional arguments) markov_mix_new2 <- restate( .object = markov_mix_ex, .fun = "levels<-", value = c("B", "D", "C", "A") ) print(markov_mix_new2) # Merge state D into C (using purrr-style lambda function) markov_mix_new3 <- restate( .object = markov_mix_ex, .fun = ~ forcats::fct_recode(.x, "C" = "D") ) print(markov_mix_new3)