# Total contains the length of data to simulate
Total <- 100
# number of states
N <- 2
# transition probabilities between states
TRANS <- matrix(c(0.9, 0.1, 0.3, 0.7), nrow=2, byrow=TRUE)
label <- matrix(0, Total, 1)
# Simulate initial state
label[1] <- randindx(matrix(1,ncol=N)/N, 1, 1)
# Use Markov property for the following time index
for(t in 2:Total) {
label[t] <- randindx(TRANS[label[t-1],], 1, 1)
}
plot(label)
Run the code above in your browser using DataLab