# [R] Computing and testing transition probabilities?

Spencer Graves spencer.graves at pdf.com
Tue Oct 5 18:32:26 CEST 2004

```      Regarding how to compile the table, have you considered something
like the following:

> Trans <- array(0, dim=c(3,3))
> for(i in 1:(n-1))
+   if(D\$names[i]==D\$names[i+1])
+     (Trans[D\$f[i], D\$f[i+1]] <- Trans[D\$f[i], D\$f[i+1]]+1)
> Trans> Trans <- array(0, dim=c(3,3))
> for(i in 1:(n-1))
+   if(D\$names[i]==D\$names[i+1])
+     (Trans[D\$f[i], D\$f[i+1]] <- Trans[D\$f[i], D\$f[i+1]]+1)
> Trans
> Trans <- array(0, dim=c(3,3))
> for(i in 1:(n-1))
+   if(D\$names[i]==D\$names[i+1])
+     (Trans[D\$f[i], D\$f[i+1]] <- Trans[D\$f[i], D\$f[i+1]]+1)
> Trans
[,1] [,2] [,3]
[1,]    1    0    1
[2,]    0    0    1
[3,]    1    1    1

This is not the answers you got;  I got this in R 1.9.1 under
Windows 2000.  I'm sure there are ways to do this without a for loop,
but I couldn't think of one immediately.

Regarding how to analyze, have you searched for "discrete markov
estimation" and "hidden markov estimate" from www.r-project.org ->
search -> "R site search"?  There are many possibilities.

hope this helps.  spencer graves

Ajay Shah wrote:

>Folks, I have a situation with many firms, observed for many years
>(but it's not panel data, so every now and then data for a firm just
>goes missing).
>
>Let me show you an example. There are 3 firms "a", "b" and "c". There
>are 3 years: 1981, 1982 and 1983. There's a factor f which takes
>values 1, 2 or 3.
>
>set.seed(5)
>D = data.frame(
>  names=c("a", "a", "a", "b", "b", "c", "c", "c", "d", "d"),
>  year= c( 81,  82,  83,  81,  83,  81,  82,  83,  82,  83),
>  f=    sample(1:3, 10, replace=T)
>  )
>print(D)
>
>What I'd like to do is locate situations where a firm is observed for
>two consecutive years, and put together conditional probabilities of
>state transition.
>
>Expressed as counts, putting time \$t\$ as the rows and time \$t+1\$ as
>the columms, I'd like to get the table:
>
>    1   2   3
>1           1
>2       1
>3   1   1   1
>
>For example, this says that we only observe one situation (in this
>data) where f=1 and then we got to see the next year, and in that year
>f was 3. So we have a 100% probability of going from 1 -> 3. In the
>case of f=3, we have 3 situations where we observe two consecutive
>years where the 1st is f=3, and it turns out that the outcomes are
>1,2,3 each happening once.
>
>Expressed as conditional probabilities it is:
>
>    1   2   3
>1           1
>2       1
>3 .33  .33 .33
>
>How might one do this? And then, how might one go about getting
>confidence intervals for the transition probabilities that are seen in
>this transition matrix? I'd like to test the null that the state
>variable does not change. I.e., if a firm is in f=2, under the null,
>it's just stay at f=2 in the following year. That'd be an identity
>matrix for the transition probabilities:
>
>    1   2   3
>1   1
>2       1
>3           1
>
>How does one talk about the distribution of the transition
>probabilities under this null, and thus get a test?
>
>
>

--
Spencer Graves, PhD, Senior Development Engineer
O:  (408)938-4420;  mobile:  (408)655-4567

```