I need to multiply the matrix estado by the matrix matriz_de transition and re-assign the variable estado to the result of the multiplication, I have to do this 1000 times, and I need to repeat the simulation 100 times.
I.e Multiply the estado matrix by the matriz_de_transicion 1000 times and see if the value of estado converges, then repeat this operation 100 times.
I have tried with for loops, but the way I re-assign the values to the variable estado doesn't work since I keep getting the same results, even if when I do it manually it seems to work.
This is my code:
nombre_estados <- c("Estado 1","Estado 2","Estado 3")
matriz_de_transicion <- matrix(c(0.2,0.7,0.1,
0.3,0.7,0.0,
0.1,0.4,0.5),
byrow = T,nrow = 3, dimnames = list(nombre_estados,nombre_estados))
estado <- matrix(c(0.2,0.7,0.1),byrow = T, nrow = 1)
estado <- estado %*% matriz_de_transicion
estado # 0.26 0.67 0.07
estado <- estado %*% matriz_de_transicion
estado # 0.26 0.679 0.061
# repeat this 1000 times
Many thanks for the answers,how could I then add these final estados to the 100 x 3 matrix?. I created the 100 x 3 matrix:
datos <- matrix(c(0,0,0),byrow = F,ncol = 3, nrow = 100)
Then I tried to do a nested loop:
for(i in 1:100){
for(i in 1:1000){
estado <- estado %*% matriz_de_transicion
}
datos[,1] <- estado[1,1]
datos[,2] <- estado[1,2]
datos[,3] <- estado[1,3]
estado <- matrix(c(0.2,0.7,0.1),byrow = T, nrow = 1) # I thought this would reset the value to the initial state but I fill the 100 x 3 matrix with the same values.
}
[,1] [,2] [,3]
[1,] 0.2631579 0.6842105 0.05263158
[2,] 0.2631579 0.6842105 0.05263158
[3,] 0.2631579 0.6842105 0.05263158
[4,] 0.2631579 0.6842105 0.05263158
[5,] 0.2631579 0.6842105 0.05263158
[6,] 0.2631579 0.6842105 0.05263158
[7,] 0.2631579 0.6842105 0.05263158
[8,] 0.2631579 0.6842105 0.05263158
I'm struggling to reset the value of the estado variable,for each one of the 100 simulations I need to obtain in the end, so each one of the 1:1000 loops have the same starting value for estado.