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.