0

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.

J_F
  • 9,956
  • 2
  • 31
  • 55
PeterNiklas
  • 75
  • 1
  • 9

2 Answers2

3

Your code did not run for me, but here is an example that does what you ask:

library(dplyr)
df <- data.frame(state=c(1,5,3,5,4,5,2,5,2,1,5))
df <- mutate(df, state_diff= state - lag(state))

which(df$state==5 & df$state_diff == 4)

length(which(df$state==5 & df$state_diff == 4))

EDIT:

This should now work with your fixed code:

df <- data.frame(results)
df<- mutate(df, results_diff = results - lag(results))
length(which(df$results==5 & df$results_diff == 4))
which(df$results==5 & df$results_diff == 4)
mkt
  • 437
  • 7
  • 20
  • Hello, I must have copied the code wrongly, i wrote the same markov chain here http://stackoverflow.com/questions/38058756/value-of-a-bar-in-a-graph maybe it works if u take it from there? – PeterNiklas Jun 29 '16 at 14:05
1

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

J_F
  • 9,956
  • 2
  • 31
  • 55
  • Hello, length(which(diff(results) == 4)) Just to make sure, is this the amount of times we were in state 1 before going to state 5 directly after? Can you explain the code? For instance why is it == 4? – PeterNiklas Jun 29 '16 at 14:20