You are on page 1of 2

HAIKAL IRFAN BIN AZUAN AZIZ KASIM

1/25/2022
library(markovchain)

## Package: markovchain
## Version: 0.8.6
## Date: 2021-05-17
## BugReport: https://github.com/spedygiorgio/markovchain/issues

s<- c("0","1","2","3","4","5")
TP<- new("markovchain", states= s,
transitionMatrix=matrix(c(.5,.5,0,0,0,0,.9,0,0,0,0,.1,0,0,0,.8,0,.2,.7,0,.1,0
,.2,0,0,0,0,.1,.9,0,.9,0,0,0,.1,0), nrow=6,byrow=TRUE,dimnames=list(s,s)))

TP

## Unnamed Markov chain


## A 6 - dimensional discrete Markov Chain defined by the following states:
## 0, 1, 2, 3, 4, 5
## The transition matrix (by rows) is defined as follows:
## 0 1 2 3 4 5
## 0 0.5 0.5 0.0 0.0 0.0 0.0
## 1 0.9 0.0 0.0 0.0 0.0 0.1
## 2 0.0 0.0 0.0 0.8 0.0 0.2
## 3 0.7 0.0 0.1 0.0 0.2 0.0
## 4 0.0 0.0 0.0 0.1 0.9 0.0
## 5 0.9 0.0 0.0 0.0 0.1 0.0

steadyStates(TP)

## 0 1 2 3 4 5
## [1,] 0.6165736 0.3082868 0.0004293688 0.004293688 0.03950193 0.03091456

library(markovchain)

iniS<-matrix(c(.1,.1,.2,.3,.3,.2))

fvals<-function(TP,initialstate,n){
out<-data.frame()
names(initialstate)<-names(TP)
for(i in 0:n)
{
iteration<-initialstate*(mchain^(i))
out<-rbind(out,iteration)
}
out<-cbind(out,i=seq(0,n))
out<-out[,c(6,1:6)]
return(out)
}
fvals

## function(TP,initialstate,n){
## out<-data.frame()
## names(initialstate)<-names(TP)
## for(i in 0:n)
## {
## iteration<-initialstate*(mchain^(i))
## out<-rbind(out,iteration)
## }
## out<-cbind(out,i=seq(0,n))
## out<-out[,c(6,1:6)]
## return(out)
## }

You might also like