---
title: "Crash Introduction to markovchain R package"
author: "Giorgio Alfredo Spedicato, Ph.D C.Stat ACAS"
date: "`r Sys.Date()`"
output:
  beamer_presentation:
    incremental: no
    keep_tex: yes
    toc: yes
    fig_width: 7
    fig_height: 6
    fig_caption: false
    includes:
      in_header: mystyles.sty
font_adjustmen: -1
bibliography: markovchainBiblio.bib
vignette: >
  %\VignetteIndexEntry{Crash Introduction to markovchain R package} 
  %\VignetteEngine{knitr::rmarkdown}  
  %VignetteEncoding{UTF-8}
---

<style type="text/css">
body, td {
   font-size: 14px;
}
code.r{
  font-size: 10px;
}
pre {
  font-size: 8px
}
</style>



# Intro

```{r setup, include=FALSE}
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.

---

# First moves into the markovchain package

## Loading the package

- The package is loaded using 

```{r load, echo=TRUE, warning=FALSE}
#load the package
library(markovchain) 
```

---

## Creating a DTMC

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

```{r create, echo=TRUE, tidy=FALSE, message=FALSE,tidy=FALSE}
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.

```{r create2, echo=TRUE, tidy=FALSE}
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

```{r plot, echo=TRUE}
plot(dtmcA)
```

---

# Probabilistic analysis

## The basic

- 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 )$

```{r proprieties, echo=TRUE, tidy=FALSE}
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

```{r transitions, echo=TRUE, tidy=FALSE}
initialState<-c(0,1,0)
steps<-4
finalState<-initialState*dtmcA^steps #using power operator
finalState
```

---
  
- As well as steady states distribution

```{r steadystate, echo=TRUE, tidy=FALSE}
steadyStates(dtmcA) #S4 method
```

---

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

```{r mathematicaMc, echo=TRUE, tidy=FALSE}
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

```{r summary, echo=TRUE, tidy=FALSE}
summary(mcMathematica)
```

---
  
# Estimation and simulation
  
 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.

---
  
```{r fitIntro, echo=TRUE, tidy=FALSE}
#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).

```{r mlfFit, echo=TRUE, tidy=FALSE}
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.

```{r mlfFit2, echo=TRUE, tidy=FALSE}
alofiMc<-myFit$estimate
alofiMc
```

# Bibliography {.allowframebreaks}
