Skip to Content

Moving Between States in a Markov Model - How to Tell R?

I have been struggling with this problem for quite a while and any help would be much appreciated.

I am trying to write a function to calculate a transition matrix from observed data for a markov model.

My initial data I am using to build the function look something like this;

 Season                 Team State
1        1    Manchester United     1
2        1              Chelsea     1
3        1      Manchester City     1
99       5    Charlton Athletic     4
100      5              Watford     4

with 5 seasons and 4 states.

I know how I am going to calculate the transition matrix, but in order to do this I need to count the number of teams that move from state i to state j for each season.

I need code that will do something like this,

if("team x is in state i in season 1 and state j in season 2") 1 else 0

and then I could do this for each team and pair of states and repeat for all 5 seasons. However, I am having a hard time getting my head around how to tell R the thing in quotation marks. Sorry if there is a really obvious answer but I am a rubbish programmer.

Thanks so much for reading!