What would be the common way of estimating MC transition matrix given the timeseries?
Is there R function for doing that?
What would be the common way of estimating MC transition matrix given the timeseries?
Is there R function for doing that?
Since the time series is discrete valued, you can estimate the transition probabilities by the sample proportions. Let $Y_{t}$ be the state of the process at time $t$, ${\bf P}$ be the transition matrix then
$$ {\bf P}_{ij} = P(Y_{t} = j | Y_{t-1} = i) $$
Since this is a markov chain, this probability depends only on $Y_{t-1}$, so it can be estimated by the sample proportion. Let $n_{ik}$ be the number of times that the process moved from state $i$ to $k$. Then,
$$ \hat{{\bf P}}_{ij} = \frac{ n_{ij} }{ \sum_{k=1}^{m} n_{ik} } $$
where $m$ is the number of possible states ($m=5$ in your case). The denominator, $\sum_{k=1}^{m} n_{ik}$, is the total number of movements out of state $i$. Estimating the entries in this way actually corresponds to the maximum likelihood estimator of the transition matrix, viewing the outcomes as multinomial, conditioned on $Y_{t-1}$.
Edit: This does assume that you have the time series observed at evenly spaced intervals. Otherwise, the transition probabilities would also depend on the time lag (even if they are still markovian).
It is very, with the hypothesis that your time series is stationary :
To simplify the excellent answer of Macro
Here you have your time series with 5 states : A, B, C, D, E
AAAEDDDCBEEEDBADBECADAAAACCCDDE
You just have to count first the transitions : - leaving A : 9 transitions Among those 9 transitions, 5 are A->A, 0 A->B, 1 A->C, 2 A->D, 1 A->E So the first line of your transition probability matrix is [5/9 0 1/9 2/9 1/9]
You do that counting for each state, and then obtain your 5x5 matrix.
the function markovchainFit from markovchain package deals with your problem.