numsteps <- 100 A <- rbind(c(.9,.07), c(.1,.93)) #list of states on this realization. xlist(k)=1 means in state 1 at #timestep k, etc states <- rep(0,numsteps) # init states[1] <- 1 for (k in 1:(numsteps-1)) { #uniformly distributed random number - will use for transitions from #timestep k to current timestep k+1 rd <- runif(1) if (rd < A[1,states[k]]) states[k+1] <- 1 else states[k+1] <- 2 } plot(1:numsteps, states, xlab = "timestep", ylab= "state", xlab = "values of Markov chain") plot(1:numsteps, cumsum((states-1.5)*2), col = "red", main = "tracking Markov chain", pch = 16)