library(markovchain)
library(diagram)
In Example 11.7, find the probability that the grandson of a man from Harvard went to Harvard.
school <- c("Harvard", "Yale", "Dartmouth")
prob <- matrix(c(1.0, 0.0, 0.0, 0.3, 0.4, 0.3, 0.2, 0.1, 0.7), byrow = T, ncol = 3, dimnames = list(school, school))
prob
## Harvard Yale Dartmouth
## Harvard 1.0 0.0 0.0
## Yale 0.3 0.4 0.3
## Dartmouth 0.2 0.1 0.7
chain <- new("markovchain", states = school, byrow= T, transitionMatrix = prob, name = "Schools")
chain
## Schools
## A 3 - dimensional discrete Markov Chain defined by the following states:
## Harvard, Yale, Dartmouth
## The transition matrix (by rows) is defined as follows:
## Harvard Yale Dartmouth
## Harvard 1.0 0.0 0.0
## Yale 0.3 0.4 0.3
## Dartmouth 0.2 0.1 0.7
plotmat(prob, pos=c(1,2), lwd=1, box.lwd=1, cex.txt = 0.5, box.size =0.1, box.type ="circle", box.prop = 0.5, box.col = "light yellow", arr.length = .1, arr.width =.1, self.cex = .4, self.shifty=-0.1, self.shiftx=.13, main="grandfather's son")
chain^2
## Schools^2
## A 3 - dimensional discrete Markov Chain defined by the following states:
## Harvard, Yale, Dartmouth
## The transition matrix (by rows) is defined as follows:
## Harvard Yale Dartmouth
## Harvard 1.00 0.00 0.00
## Yale 0.48 0.19 0.33
## Dartmouth 0.37 0.11 0.52
school <- c("Harvard", "Yale", "Dartmouth")
prob <- matrix(c(1.0, 0.0, 0.0, 0.48, 0.19, 0.33, 0.37, 0.11, 0.52), byrow = T, ncol = 3, dimnames = list(school, school))
plotmat(prob, pos=c(1,2), lwd=1, box.lwd=1, cex.txt = 0.5, box.size =0.1, box.type ="circle", box.prop = 0.5, box.col = "light yellow", arr.length = .1, arr.width =.1, self.cex = .4, self.shifty=-0.1, self.shiftx=.13, main="grandfather grandson")
ref: https://dataconomy.com/2018/03/an-introduction-to-markov-chains-using-r/