Crash Introduction to markovchain R package"

Intro

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


First moves into the markovchain package

Loading the package

#load the package
library(markovchain) 

Creating a DTMC

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

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

plot(dtmcA)

Probabilistic analysis

The basic

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

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

steadyStates(dtmcA) #S4 method

Advanced

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

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.


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

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

alofiMc<-myFit$estimate
alofiMc

Bibliography {.allowframebreaks}



Try the markovchain package in your browser

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

markovchain documentation built on Aug. 24, 2018, 1:03 a.m.