1

I´m trying to calculate the next state from a given probability vector extracted from a transition matrix.

probs <- structure(c(0.876896837675484, 0.101918293545303, 0.0189210190005101, 
0.00220982524291829, 5.40245357842536e-05), .Names = c("State 1", 
"State 2", "State 3", "State 4", "State 5"))

Where probs stands for the probability vector, the next step is to calculate the output given a binomial distribution.

set.seed(1)
rbinom(n = 5,size = 1,prob = probs)

Where n is number of observations (in this case state 1 --> State 5), size is number of trials (1 success or 0 failed) and prob is the probability of success of each observation and the function returns the following output:

1,0,0,0,0

So I'm assuming that the more probable ouput is the state 1 after one step, but sometimes I get the following:

1,0,1,0,0

But that wouldn't be possible because the person should only go to a certain state from his previous step.

Anyone has an idea on how to solve this?

Thanks for your time

Solved: I should have used rmultinom instead of rbinom

1 Answers1

0

The rbinom function does not compute transitions in a Markov process. It delivers a random draw from a universe of possibilities with a particular probability. If you wanted a single run of a 5-state transition process where there were 5 items in state-1 at the start, you would create a vector of starting values c(5,0,0,0,0). Then you would specify a transition matrix where the probabilities for all possible transitions were specified. It would need to be a 5x5 matrix in this case. Your vector of probabilities could be the first row, which would then be the probabilities for going from state-1 to any other state in the next interval in time. But you also need to state what the probabilities for transitions from the other states might be. If they are absorbing states then their i-th element would be 1 so this might be the matrix:

  trans <- matrix( c(0.876896837675484, 0.101918293545303, 0.0189210190005101, 0.00220982524291829, 5.40245357842536e-05,

                     0,1,0,0,0,
                     0,0,1,0,0,
                     0,0,0,1,0,
                     0,0,0,0,1), nrow=5, byrow=TRUE, dimnames= list( from=c("State 1", 
"State 2", "State 3", "State 4", "State 5") , to = c("State 1", 
"State 2", "State 3", "State 4", "State 5") ))

trans
         to
from        State 1   State 2    State 3     State 4      State 5
  State 1 0.8768968 0.1019183 0.01892102 0.002209825 5.402454e-05
  State 2 0.0000000 1.0000000 0.00000000 0.000000000 0.000000e+00
  State 3 0.0000000 0.0000000 1.00000000 0.000000000 0.000000e+00
  State 4 0.0000000 0.0000000 0.00000000 1.000000000 0.000000e+00
  State 5 0.0000000 0.0000000 0.00000000 0.000000000 1.000000e+00

Now you could apply this to a starting (or successive) vector of counts. Then that result would be the next state in a sequence. It can be done with either a deterministic method of by making random draws.

IRTFM
  • 258,963
  • 21
  • 364
  • 487