Nothing

library(knitr) rm(list=ls())

- The markovchain package [@markovchainR] will be introduced.
- The package is intended to provide S4 classes to perform probabilistic and statistical analysis of Discrete Time Markov Chains (DTMC). See [@bremaud1999discrete] for a theoretical review of the mathematics underlying the DTMC models.
- The vignette will show: how to load the package and create a DTMC, how to manage a DTMC, how to perform basic probabilistic analysis, how to fit a DTMC.

- The package is on Cran since Summer 2013.
- It requires a recent version of R (>=3.0). Since version 0.2 parts of code have been moved to Rcpp [@RcppR].
- The package won a slot in Google Summer of Code 2015 for optimizing internals and expanding functionalities.

- The package is loaded using

#load the package library(markovchain)

- DTMC can be easily create following standard S4 classes syntax. The show method displays it.

tmA <- matrix(c(0,0.5,0.5,.5,0,.5,.5,.5,0),nrow = 3, byrow = TRUE) #define the transition matrix dtmcA <- new("markovchain",transitionMatrix=tmA, states=c("a","b","c"), name="MarkovChain A") #create the DTMC dtmcA

- Otherwise, it can also be created directly coercing a matrix.

dtmcA2<-as(tmA, "markovchain") #using coerce from matrix states(dtmcA2) #note default names assigned to states

- It is also possible to display a DTMC, using igraph package [@pkg:igraph] capabilities

plot(dtmcA)

- It is possible to access transition probabilities and to perform basic operations.
- Similarly, it is possible to access the conditional distribution of states, $Pr\left ( X_{t+1} | X_{t}=s \right )$

dtmcA[2,3] #using [ method transitionProbability(dtmcA, "b","c") #using specific S4 method conditionalDistribution(dtmcA,"b")

- It is possible to simulate states distribution after n-steps

initialState<-c(0,1,0) steps<-4 finalState<-initialState*dtmcA^steps #using power operator finalState

- As well as steady states distribution

```
steadyStates(dtmcA) #S4 method
```

- We use an example found on Mathematica Web page, [@mathematica9]

E <- matrix(0, nrow = 4, ncol = 4) E[1, 2] <- 1;E[2, 1] <- 1/3; E[2, 3] <- 2/3 E[3,2] <- 1/4; E[3, 4] <- 3/4; E[4, 3] <- 1 mcMathematica <- new("markovchain", states = c("a", "b", "c", "d"), transitionMatrix = E,name = "Mathematica")

- The summary method shows the proprieties of the DTCM

```
summary(mcMathematica)
```

The package permits to fit a DTMC estimating the transition matrix from a sequence of data. - createSequenceMatrix returns a function showing previous vs actual states from the pairs in a given sequence.

#using Alofi rainfall dataset data(rain) mysequence<-rain$rain createSequenceMatrix(mysequence)

- markovchainFit function allows to obtain the estimated transition matric and the confidence levels (using elliptic MLE hyphotesis).

myFit<-markovchainFit(data=mysequence,confidencelevel = .9,method = "mle") myFit

- See the vignettes for further fitting methods as well as for functionalities targeted on non - homogeneous Markov chains.

alofiMc<-myFit$estimate alofiMc

**Any scripts or data that you put into this service are public.**

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.