Search code examples
rmarkov-chainsmarkov

Markov Chain previous states


I've simulated a 1000 steps in a markov chain were there are in total 6 different states(0-5) and we started in state 5. With the bar plot we can we see how many times we are in each state.

However, what i want to know is how many times we went to state 5, when the step just before it was from state 1. Since we are in total 26 times in state 1, the answer would the most be 26. Is there a way to see how many times we were in state 1 prior to going to state 5?

spec_sim <- function(x){ 
  u <- runif(1) 
    if(x==0){ 
      if(u < 0.5){ 
        y <- 3 
      } else { 
        y <- 5 
      } 
    } else if(x==1){ 
      if(u<0.1){ 
        y <- 0 
      } else if(u < 0.1 + 0.1){ 
            y <- 1
      } else if(u < 0.1 + 0.1 + 0.4){
            y <- 3
      } else {
        y <- 5}

    } else if(x==2){
        if(u<0.2){
            y <- 1
        } else if(u < 0.2 + 0.2){
            y <- 2
        } else if(u < 0.2 + 0.2 + 0.3){
            y <- 3
        } else {
            y <- 5
        } 
    } else if(x==3){
        if(u<0.3){
          y <- 2
        } else if(u < 0.3 + 0.5){
          y <- 3
        } else{
          y <- 5
        }
    } else if(x==4){
        if(u<0.4){
          y <- 3
        } else {
          y <- 4
        }
    } else if(x==5){
        if(u<0.4){
          y <- 4
        } else {
          y <- 5
        }
    }
  return(y)
}

set.seed(1) 
results <- numeric(1001)
for(i in 2:length(results)){
    results[i]<- spec_sim(results[i - 1]) 
}

results <- results[-1]

barplot(table(results), xlab="states", ylab="frequency", 
    main="1000 simuleringar av en Markovkedja")

table(results)

Thank you for putting time into my question.


Solution

  • There is another way in base R, too:

    length(which(diff(results) == 4))
    

    Very easy to understand and without knowledge of dplyr

    Explanation:

    Function diff() calculates the difference between the elements of a vector. If your results go from state 1 to state 5, the difference between the two elements is +4. So you are searching the elements, where the difference is +4. With which you get the number of the index of diff(results) == 4. And with length you can count the indices. So you get the number of changes from 1 to 5. Note that you do not get the changes from 5 to 1, because then the result is -4.

    Regards,
    J_F