Consider the following dataframe:
df = data.frame(cusip = paste("A", 1:10, sep = ""), xt = c(1,2,3,2,3,5,2,4,5,1), xt1 = c(1,4,2,1,1,4,2,2,2,5))
The data is divided in five states, which are quantiles in reality: 1,2,3,4,5. The first column of the dataframe represents the state at time t, and the second column is the state at time t+1.
I would like to compute a sort of a transition matrix for the five states. The meaning of the matrix would be as follows:
- (Row, Col) = (1,1) : % of cusips that were in quantile 1 at time t, and stayed at 1 in time t+1
- (Row, Col) = (1,2) : % of cusips that were in quantile 1 at t, and became quantile 2 at t+1
- etc...
I am really not sure how to do this in an efficient way. I have the feeling the answer is trivial, but I just can't get my head around it.
Could anyone please help?