### Summarize Mathematica Example Markov Chain Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Provides a summary of the Markov chain created from the Mathematica example. This helps in understanding its structure and basic properties. ```r summary(mathematicaMc) ``` -------------------------------- ### Create Markov Chain from Mathematica Example Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Defines a Markov chain object based on an example from the Mathematica website. This is often used for comparative analysis or demonstrating specific properties. ```r mathematicaMatr <- markovchain:::zeros(5) mathematicaMatr[1,] <- c(0, 1/3, 0, 2/3, 0) mathematicaMatr[2,] <- c(1/2, 0, 0, 0, 1/2) mathematicaMatr[3,] <- c(0, 0, 1/2, 1/2, 0) mathematicaMatr[4,] <- c(0, 0, 1/2, 1/2, 0) mathematicaMatr[5,] <- c(0, 0, 0, 0, 1) statesNames <- letters[1:5] mathematicaMc <- new("markovchain", transitionMatrix = mathematicaMatr, name = "Mathematica MC", states = statesNames) ``` -------------------------------- ### Install markovchain from GitHub (Development Version) Source: https://cran.r-project.org/web/packages/markovchain/readme/README.html Install the development version of the markovchain package directly from GitHub using the devtools package. This is useful for accessing the latest features or bug fixes. ```r devtools::install_github('spedygiorgio/markovchain') ``` -------------------------------- ### Applying Pseudo-Bayesian Estimation to Weather Example (Small Sample) Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Applies the pseudoBayesEstimator function to a small sample of data from a true Markov chain and compares the estimated matrix to the true matrix. Requires prior setup of true and apriori Markov chains and data generation. ```r trueMc<-as(matrix(c(0.1, .9,.7,.3),nrow = 2, byrow = 2),"markovchain") aprioriMc<-as(matrix(c(0.5, .5,.5,.5),nrow = 2, byrow = 2),"markovchain") smallSample<-rmarkovchain(n=20,object = trueMc) smallSampleRawTransitions<-createSequenceMatrix(stringchar = smallSample) pseudoBayesEstimator( raw = smallSampleRawTransitions, apriori = aprioriMc@transitionMatrix ) - trueMc@transitionMatrix ``` -------------------------------- ### Applying Pseudo-Bayesian Estimation to Weather Example (Bigger Sample) Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Applies the pseudoBayesEstimator function to a larger sample of data from a true Markov chain and compares the estimated matrix to the true matrix. Requires prior setup of true and apriori Markov chains and data generation. ```r biggerSample<-rmarkovchain(n=100,object = trueMc) biggerSampleRawTransitions<-createSequenceMatrix(stringchar = biggerSample) pseudoBayesEstimator( raw = biggerSampleRawTransitions, apriori = aprioriMc@transitionMatrix ) - trueMc@transitionMatrix ``` -------------------------------- ### Applying Pseudo-Bayesian Estimation to Weather Example (Big Sample) Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Applies the pseudoBayesEstimator function to a very large sample of data from a true Markov chain and compares the estimated matrix to the true matrix. Requires prior setup of true and apriori Markov chains and data generation. ```r bigSample<-rmarkovchain(n=1000,object = trueMc) bigSampleRawTransitions<-createSequenceMatrix(stringchar = bigSample) pseudoBayesEstimator( raw = bigSampleRawTransitions, apriori = aprioriMc@transitionMatrix ) - trueMc@transitionMatrix ``` -------------------------------- ### Load Example Data Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Loads the 'kullback' dataset, which contains a list of two matrices representing raw transitions between two states. This data is often used for contingency table tests. ```R data(kullback) ``` -------------------------------- ### Install markovchain from CRAN Source: https://cran.r-project.org/web/packages/markovchain/readme/README.html Use this command to install the latest stable release of the markovchain package from the Comprehensive R Archive Network (CRAN). ```r install.packages('markovchain') ``` -------------------------------- ### Create and Simulate a Weather Markov Chain Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Defines states and a transition matrix for weather conditions and simulates 365 days of weather starting from a sunny state. ```R weatherStates <- c("sunny", "cloudy", "rain") byRow <- TRUE weatherMatrix <- matrix(data = c(0.7, 0.2, 0.1, 0.3, 0.4, 0.3, 0.2, 0.4, 0.4), byrow = byRow, nrow = 3, dimnames = list(weatherStates, weatherStates)) mcWeather <- new("markovchain", states = weatherStates, byrow = byRow, transitionMatrix = weatherMatrix, name = "Weather") weathersOfDays <- rmarkovchain(n = 365, object = mcWeather, t0 = "sunny") ``` -------------------------------- ### transitionProbability Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html This is a convenience function to get transition probabilities. ```APIDOC ## transitionProbability ### Description This is a convenience function to get transition probabilities. ### Usage ``` transitionProbability(object, t0, t1) ## S4 method for signature 'markovchain' transitionProbability(object, t0, t1) ``` ### Arguments `object` | A `markovchain` object. `t0` | Initial state. `t1` | Subsequent state. ### Value Numeric Vector ### Author(s) Giorgio Spedicato ### References A First Course in Probability (8th Edition), Sheldon Ross, Prentice Hall 2010 ### See Also `markovchain` ### Examples ```R statesNames <- c("a", "b", "c") markovB <- new("markovchain", states = statesNames, transitionMatrix = matrix(c(0.2, 0.5, 0.3, 0, 1, 0, 0.1, 0.8, 0.1), nrow = 3, byrow = TRUE, dimnames=list(statesNames,statesNames)), name = "A markovchain Object" ) transitionProbability(markovB,"b", "c") ``` ``` -------------------------------- ### Set and Get Markovchain Object Name Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Demonstrates how to retrieve the current name of a markovchain object and how to assign a new name to it. ```r name(mcWeather) name(mcWeather) <- "New Name" name(mcWeather) ``` -------------------------------- ### Verify Empirical vs. Theoretical Transition Matrix Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Compares an empirical transition matrix derived from data against a theoretical transition matrix. This is useful for hypothesis testing on Markov chain models. The example uses a sequence of numerical states. ```R sequence<-c(0,1,2,2,1,0,0,0,0,0,0,1,2,2,2,1,0,0,1,0,0,0,0,0,0,1,1, 2,0,0,2,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,2,1,0, 0,2,1,0,0,0,0,0,0,1,1,1,2,2,0,0,2,1,1,1,1,2,1,1,1,1,1,1,1,1,1,0,2, 0,1,1,0,0,0,1,2,2,0,0,0,0,0,0,2,2,2,1,1,1,1,0,1,1,1,1,0,0,2,1,1, 0,0,0,0,0,2,2,1,1,1,1,1,2,1,2,0,0,0,1,2,2,2,0,0,0,1,1) mc=matrix(c(5/8,1/4,1/8,1/4,1/2,1/4,1/4,3/8,3/8),byrow=TRUE, nrow=3) rownames(mc)<-colnames(mc)<-0:2; theoreticalMc<-as(mc, "markovchain") verifyEmpiricalToTheoretical(data=sequence,object=theoreticalMc) ``` -------------------------------- ### Simulate a Year of Weather States Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Generates 365 daily weather states starting from 'sunny' using the mcWeather DTMC. Displays the first 30 simulated states. ```R weathersOfDays <- rmarkovchain(n = 365, object = mcWeather, t0 = "sunny") weathersOfDays[1:30] ``` -------------------------------- ### Compare Long-Term Forecasts to Steady State Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Illustrates that long-term predictions (e.g., 7 days) converge to the steady-state probabilities, regardless of the initial weather state. This requires defining different starting state vectors. ```R R0 <- t(as.matrix(c(1, 0, 0))) R7 <- R0 * (mcWP ^ 7); R7 S0 <- t(as.matrix(c(0, 0, 1))) S7 <- S0 * (mcWP ^ 7); S7 ``` -------------------------------- ### Calculate first passage probability in a Markov chain Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Demonstrates calculating the probability of first passage to a specific state within a given number of steps (`n`) starting from an initial state. Uses a `markovchain` object. ```R simpleMc <- new("markovchain", states = c("a", "b"), transitionMatrix = matrix(c(0.4, 0.6, .3, .7), nrow = 2, byrow = TRUE)) firstPassage(simpleMc, "b", 20) ``` -------------------------------- ### Periodicity Analysis with a Larger Matrix Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Demonstrates periodicity analysis for a Markov chain defined by a larger transition matrix. This example is useful for understanding how the 'period' function behaves with more complex state transitions. ```R # periodicity analysis B <- matrix(c(0, 0, 1/2, 1/4, 1/4, 0, 0, 0, 0, 1/3, 0, 2/3, 0, 0, 0, 0, 0, 0, 0, 1/3, 2/3, 0, 0, 0, 0, 0, 1/2, 1/2, 0, 0, 0, 0, 0, 3/4, 1/4, 1/2, 1/2, 0, 0, 0, 0, 0, 1/4, 3/4, 0, 0, 0, 0, 0), byrow = TRUE, ncol = 7) mcB <- new("markovchain", transitionMatrix = B) period(mcB) ``` -------------------------------- ### Forecast Weather States After 7 Days Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Computes the probability distribution of weather states one week (7 days) in the future, starting from a 'nice' day. This demonstrates long-term forecasting capabilities. ```R W7 <- W0 * (mcWP ^ 7) W7 ``` -------------------------------- ### Generate Markov Chain List for Age Sequence Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Creates a list of Markov chain objects for a sequence of ages, starting from a specified age. This is used for non-homogeneous transitions across ages. ```r getFullTransitionTable<-function(age){ ageSequence<-seq(from=age, to=120) k=1 myList=list() for ( i in ageSequence) { mc_age_i<-getMc4Age(age = i) myList[[k]]<-mc_age_i k=k+1 } myMarkovChainList<-new("markovchainList", markovchains = myList, name = paste("TransitionsSinceAge", age, sep = "")) return(myMarkovChainList) } transitionsSince100<-getFullTransitionTable(age=100) ``` -------------------------------- ### Calculate Committor Probability Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Calculates the committor probability, which is the probability of reaching a target state before another specified state, given a starting state. This example calculates the probability of rain before sunny, starting from cloudy. ```R committorAB(mcWeather,3,1) ``` -------------------------------- ### Create and Analyze a Markov Chain Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Demonstrates how to create a new Markov chain object with specified states and a transition matrix, and then calculate its absorption probabilities. ```R m <- matrix(c(1/2, 1/2, 0, 1/2, 1/2, 0, 0, 1/2, 1/2), ncol = 3, byrow = TRUE) mc <- new("markovchain", states = letters[1:3], transitionMatrix = m) absorptionProbabilities(mc) ``` -------------------------------- ### Create and Analyze a Markov Chain Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Demonstrates the creation of a new Markov chain object using a transition matrix and calculating its hitting probabilities. ```R M <- markovchain:::zeros(5) M[1,1] <- M[5,5] <- 1 M[2,1] <- M[2,3] <- 1/2 M[3,2] <- M[3,4] <- 1/2 M[4,2] <- M[4,5] <- 1/2 mc <- new("markovchain", transitionMatrix = M) hittingProbabilities(mc) ``` -------------------------------- ### Create Markov Chains Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Demonstrates the creation of several discrete-time Markov chain objects using different methods and transition matrices. ```R # create some markov chains statesNames=c("a","b") mcA<-new("markovchain", transitionMatrix=matrix(c(0.7,0.3,0.1,0.9),byrow=TRUE, nrow=2, dimnames=list(statesNames,statesNames))) statesNames=c("a","b","c") mcB<-new("markovchain", states=statesNames, transitionMatrix= matrix(c(0.2,0.5,0.3,0,1,0,0.1,0.8,0.1), nrow=3, byrow=TRUE, dimnames=list(statesNames, statesNames))) statesNames=c("a","b","c","d") matrice<-matrix(c(0.25,0.75,0,0,0.4,0.6,0,0,0,0,0.1,0.9,0,0,0.7,0.3), nrow=4, byrow=TRUE) mcC<-new("markovchain", states=statesNames, transitionMatrix=matrice) mcD<-new("markovchain", transitionMatrix=matrix(c(0,1,0,1), nrow=2,byrow=TRUE)) #operations with S4 methods mcA^2 steadyStates(mcB) absorbingStates(mcB) markovchainSequence(n=20, markovchain=mcC, include=TRUE) ``` -------------------------------- ### Create a markovchain object (short way) Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Initialize a 'markovchain' object using a more concise syntax. This method leverages the S4 initialize method for efficient object creation. ```r mcWeather <- new("markovchain", states = c("sunny", "cloudy", "rain"), transitionMatrix = matrix(data = c(0.70, 0.2, 0.1, 0.3, 0.4, 0.3, 0.2, 0.45, 0.35), byrow = byRow, nrow = 3), name = "Weather") ``` -------------------------------- ### Create and Analyze a Markov Chain Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Demonstrates how to create a new Markov chain object and calculate its mean recurrence time. ```R m <- matrix(1 / 10 * c(6,3,1, 2,3,5, 4,1,5), ncol = 3, byrow = TRUE) pc <- new("markovchain", states = c("s","c","r"), transitionMatrix = m) meanRecurrenceTime(mc) ``` -------------------------------- ### meanNumVisits Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Calculates the expected number of visits to each state, given a starting state. The element (i, j) in the output matrix represents the expected number of visits to state j if the chain starts at state i. ```APIDOC ## meanNumVisits ### Description Given a markovchain object, this function calculates a matrix where the element (i, j) represents the expect number of visits to the state j if the chain starts at i. ### Usage ```R meanNumVisits(object) ``` ### Arguments * `object` (`markovchain-class` object): The Markov chain object. ### Value A matrix with the expected number of visits to each state. ### Examples ```R M <- markovchain:::zeros(5) M[1,1] <- M[5,5] <- 1 M[2,1] <- M[2,3] <- 1/2 M[3,2] <- M[3,4] <- 1/2 M[4,2] <- M[4,5] <- 1/2 mc <- new("markovchain", transitionMatrix = M) meanNumVisits(mc) ``` ``` -------------------------------- ### Create and Analyze a Markov Chain Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Demonstrates creating a new Markov chain object and calling various accessor functions to retrieve properties like communicating classes, recurrent classes, absorbing states, transient states, and recurrent states. Also shows how to obtain the canonical form of the chain. ```R statesNames <- c("a", "b", "c") mc <- new("markovchain", states = statesNames, transitionMatrix = matrix(c(0.2, 0.5, 0.3, 0, 1, 0, 0.1, 0.8, 0.1), nrow = 3, byrow = TRUE, dimnames = list(statesNames, statesNames)) ) communicatingClasses(mc) recurrentClasses(mc) recurrentClasses(mc) absorbingStates(mc) transientStates(mc) recurrentStates(mc) canonicForm(mc) ``` -------------------------------- ### Get Conditional Distribution from a State Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Calculates the conditional probability distribution of transitions from a specified state ('b' in this case). ```R conditionalDistribution(simpleMc, "b") ``` -------------------------------- ### Create a Markov Chain Object Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Demonstrates how to create a new markovchain object with specified states and a transition matrix. This is a foundational step for using other functions in the package. ```R statesNames <- c("a", "b", "c") markovB <- new("markovchain", states = statesNames, transitionMatrix = matrix(c(0.2, 0.5, 0.3, 0, 1, 0, 0.1, 0.8, 0.1), nrow = 3, byrow = TRUE, dimnames = list(statesNames, statesNames) )) firstPassageMultiple(markovB,"a",c("b","c"),4) ``` -------------------------------- ### Get States and Dimension of Markovchain Object Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Retrieves the names of the states and the dimensions of the transition matrix from a markovchain object. ```r states(mcWeather) names(mcWeather) dim(mcWeather) ``` -------------------------------- ### Prepare Inferred Hyper-parameter Matrices Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Prepares hyper-parameter matrices inferred from a transition matrix and from a data sample, ready for use in markovchainFit. ```R hyperMatrix3 <- inferHyperparam(transMatr = weatherMatrix, scale = c(10, 10, 10)) hyperMatrix3 <- hyperMatrix3$scaledInference hyperMatrix4 <- inferHyperparam(data = weathersOfDays[1:15]) hyperMatrix4 <- hyperMatrix4$dataInference ``` -------------------------------- ### is.CTMCirreducible Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Checks if a Continuous-Time Markov Chain (CTMC) object is irreducible. An irreducible CTMC means that it is possible to get from any state to any other state. ```APIDOC ## is.CTMCirreducible ### Description This function verifies whether a CTMC object is irreducible ### Usage ```R is.CTMCirreducible(ctmc) ``` ### Arguments `ctmc` | a ctmc-class object ### Value a boolean value as described above. ### Author(s) Vandit Jain ### References Continuous-Time Markov Chains, Karl Sigman, Columbia University ### Examples ```R energyStates <- c("sigma", "sigma_star") byRow <- TRUE gen <- matrix(data = c(-3, 3, 1, -1), nrow = 2, byrow = byRow, dimnames = list(energyStates, energyStates)) molecularCTMC <- new("ctmc", states = energyStates, byrow = byRow, generator = gen, name = "Molecular Transition Model") is.CTMCirreducible(molecularCTMC) ``` ``` -------------------------------- ### Create HOMMC Object Source: https://cran.r-project.org/web/packages/markovchain/vignettes/higher_order_markov_chains.Rmd Demonstrates how to initialize an object of the 'hommc' class. This involves defining states, transition probability matrices (P), and their corresponding weights (Lambda). ```r states <- c('a', 'b') P <- array(dim = c(2, 2, 4), dimnames = list(states, states)) P[, , 1] <- matrix(c(1/3, 2/3, 1, 0), byrow = FALSE, nrow = 2, ncol = 2) P[, , 2] <- matrix(c(0, 1, 1, 0), byrow = FALSE, nrow = 2, ncol = 2) P[, , 3] <- matrix(c(2/3, 1/3, 0, 1), byrow = FALSE, nrow = 2, ncol = 2) P[, , 4] <- matrix(c(1/2, 1/2, 1/2, 1/2), byrow = FALSE, nrow = 2, ncol = 2) Lambda <- c(.8, .2, .3, .7) hob <- new("hommc", order = 1, Lambda = Lambda, P = P, states = states, byrow = FALSE, name = "FOMMC") hob ``` -------------------------------- ### Create a Simple Markov Chain Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Constructs a basic homogeneous Markov chain with two states 'a' and 'b' and a specified transition matrix. ```R transMatr<-matrix(c(0.4,0.6,.3,.7),nrow=2,byrow=TRUE) simpleMc<-new("markovchain", states=c("a","b"), transitionMatrix=transMatr, name="simpleMc") ``` -------------------------------- ### Convert Markovchain to igraph Object Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Converts a markovchain object to an igraph object for visualization or network analysis. Requires the 'igraph' package to be installed. ```R mcIgraph <- as(mcWeather, "igraph") ``` -------------------------------- ### meanRecurrenceTime Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Computes the expected time to return to a recurrent state, assuming the Markov chain starts in that state. Only recurrent states are included in the output. ```APIDOC ## meanRecurrenceTime ### Description Computes the expected time to return to a recurrent state in case the Markov chain starts there. ### Usage ```R meanRecurrenceTime(object) ``` ### Arguments * `object` (`markovchain` object): The Markov chain object. ### Value For a Markov chain, this outputs a named vector with the expected time to first return to a state when the chain starts there. States present in the vector are only the recurrent ones. If the matrix is ergodic (i.e., irreducible), then all states are present in the output, and their order is the same as the order of states for the Markov chain. ``` -------------------------------- ### Fit a Continuous-Time Markov Chain (CTMC) from data Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Illustrates fitting a CTMC model using the `ctmcFit` function. This function takes transition data and times to estimate the CTMC parameters via maximum likelihood. ```R data <- list(c("a", "b", "c", "a", "b", "a", "c", "b", "c"), c(0, 0.8, 2.1, 2.4, 4, 5, 5.9, 8.2, 9)) ctmcFit(data) ``` -------------------------------- ### Get Transition Probabilities Between States Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html A convenience function to retrieve the transition probabilities between two specified states (t0 and t1) of a markovchain object. ```r statesNames <- c("a", "b", "c") markovB <- new("markovchain", states = statesNames, transitionMatrix = matrix(c(0.2, 0.5, 0.3, 0, 1, 0, 0.1, 0.8, 0.1), nrow = 3, byrow = TRUE, dimnames=list(statesNames,statesNames)), name = "A markovchain Object" ) transitionProbability(markovB,"b", "c") ``` -------------------------------- ### Get States of Markov Chain Object Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Returns the names of the states for a given Markov chain object. This is an S4 method for the 'markovchain' class. ```R names(x) ``` -------------------------------- ### Get Name of Markov Chain Object Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Retrieves the name associated with a Markov chain object. This is a method for S4 objects of class 'markovchain'. ```R statesNames <- c("a", "b", "c") markovB <- new("markovchain", states = statesNames, transitionMatrix = matrix(c(0.2, 0.5, 0.3, 0, 1, 0, 0.1, 0.8, 0.1), nrow = 3, byrow = TRUE, dimnames=list(statesNames,statesNames)), name = "A markovchain Object" ) name(markovB) ``` -------------------------------- ### Create Markovchain Object from Matrix Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Demonstrates how to create a markovchain object from a transition probability matrix and check if it is regular. Ensure the matrix is properly formatted and named. ```R P <- matrix(c(0.5, 0.25, 0.25, 0.5, 0, 0.5, 0.25, 0.25, 0.5), nrow = 3) colnames(P) <- rownames(P) <- c("R","N","S") ciao <- as(P, "markovchain") is.regular(ciao) ``` -------------------------------- ### Get states from a Markov chain object Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Retrieves the character vector representing the states of a discrete `markovchain` object. This is a direct accessor for the 'states' slot. ```R states(object) ``` -------------------------------- ### Create a markovchain object (long way) Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Initialize a 'markovchain' object by explicitly defining states, transition matrix, and other properties. This method is useful for clarity and when all parameters need to be specified individually. ```r weatherStates <- c("sunny", "cloudy", "rain") byRow <- TRUE weatherMatrix <- matrix(data = c(0.70, 0.2, 0.1, 0.3, 0.4, 0.3, 0.2, 0.45, 0.35), byrow = byRow, nrow = 3, dimnames = list(weatherStates, weatherStates)) pcWeather <- new("markovchain", states = weatherStates, byrow = byRow, transitionMatrix = weatherMatrix, name = "Weather") ``` -------------------------------- ### Fit Markov Chain with Permuted Hyper-parameters Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Demonstrates fitting a Markov chain model using MAP estimation with permuted hyper-parameters, showing that the results are consistent. ```R hyperMatrix2<- hyperMatrix[c(2,3,1), c(2,3,1)] markovchainFit(weathersOfDays[1:200], method = "map", confidencelevel = 0.92, hyperparam = hyperMatrix2) predictiveDistribution(weathersOfDays[1:200], weathersOfDays[201:365],hyperparam = hyperMatrix2) ``` -------------------------------- ### Create and Summarize a Markov Chain Object Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Constructs a Markov chain object from a transition matrix and displays a summary. This is useful for initial analysis and understanding the chain's properties. ```r P <- markovchain:::zeros(10) P[1, c(1, 3)] <- 1/2; P[2, 2] <- 1/3; P[2,7] <- 2/3; P[3, 1] <- 1; P[4, 5] <- 1; P[5, c(4, 5, 9)] <- 1/3; P[6, 6] <- 1; P[7, 7] <- 1/4; P[7,9] <- 3/4; P[8, c(3, 4, 8, 10)] <- 1/4; P[9, 2] <- 1; P[10, c(2, 5, 10)] <- 1/3; rownames(P) <- letters[1:10] colnames(P) <- letters[1:10] probMc <- new("markovchain", transitionMatrix = P, name = "Probability MC") summary(probMc) ``` -------------------------------- ### Load Health Insurance Data Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Loads health insurance transition data from a file. Ensure the 'markovchain' package is installed and the data file exists in the specified path. ```r ltcDemoPath<-system.file("extdata", "ltdItaData.txt", package = "markovchain") ltcDemo<-read.table(file = ltcDemoPath, header=TRUE, sep = ";", dec = ".") head(ltcDemo) ``` -------------------------------- ### Plot Markovchain Object using igraph Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Generates a plot of the Markov chain using the igraph package. Requires the igraph package to be installed. Allows customization of graph layout. ```r if (requireNamespace("igraph", quietly = TRUE)) { library(igraph) plot(mcWeather,layout = layout.fruchterman.reingold) } else { message("igraph unavailable") } ``` -------------------------------- ### Identify Recurrent States in Drunkard's Walk Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Identifies the recurrent states in the Drunkard's walk Markov chain. In this specific example, these are the absorbing states (home and bar). ```R recurrentStates(drunkMc) ``` -------------------------------- ### Verify Empirical vs. Theoretical Transition Matrix Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Verifies if an empirical transition matrix is consistent with a theoretical one. Requires a sequence of states and the theoretical transition matrix object. ```r sequence<-c(0,1,2,2,1,0,0,0,0,0,0,1,2,2,2,1,0,0,1,0,0,0,0,0,0,1,1, 2,0,0,2,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,2,1,0, 0,2,1,0,0,0,0,0,0,1,1,1,2,2,0,0,2,1,1,1,1,2,1,1,1,1,1,1,1,1,1,0,2, 0,1,1,0,0,0,1,2,2,0,0,0,0,0,0,2,2,2,1,1,1,1,0,1,1,1,1,0,0,2,1,1, 0,0,0,0,0,2,2,1,1,1,1,1,2,1,2,0,0,0,1,2,2,2,0,0,0,1,1) mc=matrix(c(5/8,1/4,1/8,1/4,1/2,1/4,1/4,3/8,3/8),byrow=TRUE, nrow=3) rownames(mc)<-colnames(mc)<-0:2; theoreticalMc<-as(mc, "markovchain") verifyEmpiricalToTheoretical(data=sequence,object=theoreticalMc) ``` -------------------------------- ### Fit Higher Order Markov Chain (Order 3) Source: https://cran.r-project.org/web/packages/markovchain/vignettes/higher_order_markov_chains.Rmd Fits a third-order Markov chain to a categorical sequence. Requires the Rsolnp package. The 'rain' dataset is used as an example. ```r if (requireNamespace("Rsolnp", quietly = TRUE)) { library(Rsolnp) data(rain) fitHigherOrder(rain$rain, 3) } ``` -------------------------------- ### Create a default markovchain object Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Initialize a default 'markovchain' object. This creates a basic Markov chain with default settings, useful for initial testing or as a placeholder. ```r defaultMc <- new("markovchain") ``` -------------------------------- ### Fit Higher Order Markov Chain (Order 2) Source: https://cran.r-project.org/web/packages/markovchain/vignettes/higher_order_markov_chains.Rmd Fits a second-order Markov chain to a categorical sequence. Requires the Rsolnp package. The 'rain' dataset is used as an example. ```r if (requireNamespace("Rsolnp", quietly = TRUE)) { library(Rsolnp) data(rain) fitHigherOrder(rain$rain, 2) } ``` -------------------------------- ### Verify Mean First Passage Time using PDF Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Verifies the mean first passage time by averaging the first passage probability density function over a large number of steps. Requires a pre-calculated first passage PDF. ```R firstPassagePdF.long <- firstPassage(object = mcWeather, state = "sunny", n = 100) sum(firstPassagePdF.long[,"rain"] * 1:100) ``` -------------------------------- ### Convert Craigsendi Data to Markov Chain and Calculate Steady States Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Converts the 'craigsendi' table data into a Markov chain object and then calculates its steady-state distribution. ```R data(craigsendi) csMc<-as(craigsendi, "markovchain") steadyStates(csMc) ``` -------------------------------- ### Compute Mean Recurrence Time Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Computes the expected time to return to a recurrent state, given the Markov chain starts in that state. Outputs a named vector for recurrent states only. ```R meanRecurrenceTime(object) ``` -------------------------------- ### Compute Mean Number of Visits Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Calculates a matrix where element (i, j) is the expected number of visits to state j starting from state i. Assumes a Markov chain defined by columns. ```R meanNumVisits(object) ``` ```R M <- markovchain:::zeros(5) M[1,1] <- M[5,5] <- 1 M[2,1] <- M[2,3] <- 1/2 M[3,2] <- M[3,4] <- 1/2 M[4,2] <- M[4,5] <- 1/2 mc <- new("markovchain", transitionMatrix = M) meanNumVisits(mc) ``` -------------------------------- ### Create Markov Chain for Hitting Probability Test Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Initializes a 5x5 transition matrix and creates a new markovchain object for testing hitting probabilities. The matrix is set up with specific probabilities to create a test case. ```R M <- markovchain:::zeros(5) M[1,1] <- M[5,5] <- 1 M[2,1] <- M[2,3] <- 1/2 M[3,2] <- M[3,4] <- 1/2 M[4,2] <- M[4,5] <- 1/2 hittingTest <- new("markovchain", transitionMatrix = M) ``` -------------------------------- ### Plot CTMC Generator Matrix using 'diagram' Package Source: https://cran.r-project.org/web/packages/markovchain/vignettes/gsoc_2017_additions.html Plots the CTMC generator matrix as a directed graph using the 'diagram' package. This option is available if the 'ctmcd' package is installed. ```R if(requireNamespace(package='ctmcd', quietly = TRUE)) { plot(molecularCTMC,package = "diagram") } else { print("diagram package unavailable") } ``` -------------------------------- ### Create Markov Chain from Credit Rating Matrix Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Initializes a markovchain object using the predefined credit rating transition matrix. This allows for analysis of credit rating transitions. ```R creditMc <- new("markovchain", transitionMatrix = creditMatrix, name = "S&P Matrix") absorbingStates(creditMc) ``` -------------------------------- ### Load preproglucacon Dataset Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Loads the preproglucacon dataset from the markovchain package. ```r data("preproglucacon", package = "markovchain") ``` -------------------------------- ### Periodicity Analysis of a Markov Chain Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Illustrates how to analyze the periodicity of a Markov chain. It shows how to create a Markov chain object and use the 'is.irreducible' and 'period' functions to determine if the chain is irreducible and its periodicity. ```R # periodicity analysis A <- matrix(c(0, 1, 0, 0, 0.5, 0, 0.5, 0, 0, 0.5, 0, 0.5, 0, 0, 1, 0), nrow = 4, ncol = 4, byrow = TRUE) mcA <- new("markovchain", states = c("a", "b", "c", "d"), transitionMatrix = A, name = "A") is.irreducible(mcA) #true period(mcA) #2 ``` -------------------------------- ### Simulate Patient States from Semi-Homogeneous Chain Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Simulates 5 patient states starting from 'H', including the initial state, from the mcCCRC chain. Displays the first 10 simulated states. ```R patientStates <- rmarkovchain(n = 5, object = mcCCRC, t0 = "H", include.t0 = TRUE) patientStates[1:10,] ``` -------------------------------- ### Fit Discrete Markov Chain with NA Sequences Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Demonstrates fitting a discrete-time Markov chain when the input sequence contains NA values. NA transitions are ignored during the fitting process. ```R na.sequence <- c("a", NA, "a", "b") # There will be only a (a,b) transition na.sequenceMatr <- createSequenceMatrix(na.sequence, sanitize = FALSE) mcFitMLE <- markovchainFit(data = na.sequence) ``` -------------------------------- ### Calculate Hitting Probabilities for mcWeather Chain Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Calculates the hitting probabilities for the pre-defined `mcWeather` Markov chain object. This example shows the function's output for a different, pre-existing Markov chain. ```R hittingProbabilities(mcWeather) ``` -------------------------------- ### Load and Inspect Sales Data Source: https://cran.r-project.org/web/packages/markovchain/vignettes/higher_order_markov_chains.Rmd Loads the 'sales' dataset and displays the first few rows to understand the sales demand patterns for different products. ```r data(sales) head(sales) ``` -------------------------------- ### Calculate Expected Disabled Period Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Simulates numerous life trajectories for a person starting at a specific age and state, then calculates the average time spent in the 'disabled' state. Requires the 'markovchain' package. ```r transitionsSince80<-getFullTransitionTable(age=80) lifeTrajectories<-rmarkovchain(n=1e3, object=transitionsSince80, what="matrix",t0="A",include.t0=TRUE) temp<-matrix(0,nrow=nrow(lifeTrajectories), ncol = ncol(lifeTrajectories)) temp[lifeTrajectories=="I"]<-1 expected_period_disabled<-mean(rowSums((temp))) expected_period_disabled ``` -------------------------------- ### Load the markovchain package Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Load the markovchain package into the R session to access its functions and classes. ```r library("markovchain") ``` -------------------------------- ### Calculate Joint Probability Distribution of State Visits Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Computes the joint probability distribution of the number of visits to various states within the first N steps of a DTMC, starting from a specified initial state. ```R transMatr<-matrix(c(0.4,0.6,.3,.7),nrow=2,byrow=TRUE) simpleMc<-new("markovchain", states=c("a","b"), transitionMatrix=transMatr, name="simpleMc") noofVisitsDist(simpleMc,5,"a") ``` -------------------------------- ### Define Transition Matrix and Markov Chain Object Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Initializes a transition matrix for a telephone company's customer/non-customer status and creates a markovchain object. ```R statesNames <- c("customer", "non customer") P <- markovchain:::zeros(2); P[1, 1] <- .9; P[1, 2] <- .1; P[2, 2] <- .95; P[2, 1] <- .05; rownames(P) <- statesNames; colnames(P) <- statesNames mcP <- new("markovchain", transitionMatrix = P, name = "Telephone company") M <- markovchain:::zeros(2); M[1, 1] <- -20; M[1, 2] <- -30; M[2, 1] <- -40; M[2, 2] <- 0 ``` -------------------------------- ### Forecast Weather States After 1, 2, and 3 Days Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Calculates the probability distribution of weather states for the next 1, 2, and 3 days, given the current state is 'nice'. Requires the initial state vector and the Markov chain object. ```R W0 <- t(as.matrix(c(0, 1, 0))) W1 <- W0 * mcWP; W1 W2 <- W0 * (mcWP ^ 2); W2 W3 <- W0 * (mcWP ^ 3); W3 ``` -------------------------------- ### noofVisitsDist Source: https://cran.r-project.org/web/packages/markovchain/refman/markovchain.html Returns a joint probability density function (pdf) of the number of visits to various states of a Discrete Time Markov Chain (DTMC) within the first N steps, starting from a specified initial state. ```APIDOC ## noofVisitsDist ### Description This function would return a joint pdf of the number of visits to the various states of the DTMC during the first N steps. ### Usage ``` oofVisitsDist(markovchain,N,state) ``` ### Arguments `markovchain` | a markovchain-class object `N` | no of steps `state` | the initial state ### Details This function would return a joint pdf of the number of visits to the various states of the DTMC during the first N steps. ### Value a numeric vector depicting the above described probability density function. ### Author(s) Vandit Jain ``` -------------------------------- ### Fit Markov Chain Model to Protein Sequence Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Models transition probabilities between bases in a protein sequence using the preproglucacon dataset. The resulting transition matrix is estimated and displayed. ```r mcProtein <- markovchainFit(preproglucacon$preproglucacon, name = "Preproglucacon MC")$estimate mcProtein ``` -------------------------------- ### Fit Higher Order Multivariate Markov Chain Source: https://cran.r-project.org/web/packages/markovchain/vignettes/higher_order_markov_chains.Rmd Fits a multivariate Markov chain of a specified order using the `fitHighOrderMultivarMC` function. Requires the `Rsolnp` package to be installed. The `Norm` parameter specifies the norm to use for optimization. ```r if (requireNamespace("Rsolnp", quietly = TRUE)) { object <- fitHighOrderMultivarMC(sales, order = 8, Norm = 2) } ``` -------------------------------- ### Fit Markov Chain using Bootstrap Estimation Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Fits a Markov chain using a bootstrap approach to estimate the variability of transition probabilities. Requires specifying the number of bootstrap samples (nboot). Outputs the estimated transition matrix and its standard error. ```r weatherFittedBOOT <- markovchainFit(data = weathersOfDays, method = "bootstrap", nboot = 20) weatherFittedBOOT$estimate weatherFittedBOOT$standardError ``` -------------------------------- ### Convert Markovchain to Data Frame and Back Source: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.Rmd Demonstrates converting a markovchain object to a data frame and then back to a markovchain object. This is useful for inspecting or manipulating the transition data. ```R mcDf <- as(mcWeather, "data.frame") mcNew <- as(mcDf, "markovchain") mcDf ```