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:
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?
Transition matrices are used to describe the way in which transitions are made between two states. It is used when events are more or less likely depending on the previous events.
A Markov transition matrix is a square matrix describing the probabilities of moving from one state to another in a dynamic system. In each row are the probabilities of moving from the state represented by that row, to the other states. Thus the rows of a Markov transition matrix each add to one.
The first one, called the transition matrix, determines probabilities of transitions from one hidden state to another one (the next one). The second matrix, called the emission matrix, determines probabilities of observations given a hidden state.
res <- with(df, table(xt, xt1)) ## table() to form transition matrix
res/rowSums(res) ## /rowSums() to normalize by row
# xt1
# xt 1 2 4 5
# 1 0.5000000 0.0000000 0.0000000 0.5000000
# 2 0.3333333 0.3333333 0.3333333 0.0000000
# 3 0.5000000 0.5000000 0.0000000 0.0000000
# 4 0.0000000 1.0000000 0.0000000 0.0000000
# 5 0.0000000 0.5000000 0.5000000 0.0000000
## As an alternative to 2nd line above, use sweep(), which won't rely on
## implicit recycling of vector returned by rowSums(res)
sweep(res, MARGIN = 1, STATS = rowSums(res), FUN = `/`)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With