[R] Computing and testing transition probabilities?
Gabor Grothendieck
ggrothendieck at myway.com
Thu Oct 7 15:39:02 CEST 2004
Ajay Shah <ajayshah <at> mayin.org> writes:
:
: 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
I do not get the same answer as you but my understanding from
your description is that this is what you are looking for:
R> set.seed(5)
R> 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)
+ )
R> print(D)
names year f
1 a 81 1
2 a 82 3
3 a 83 3
4 b 81 1
5 b 83 1
6 c 81 3
7 c 82 2
8 c 83 3
9 d 82 3
10 d 83 1
R> MM <- merge(D, cbind(year = D$year + 1, D), by.x = 2:1, by.y = 1:2)
R> tab <- with(MM, table(f.x, f.y))
R> tab
f.y
f.x 1 2 3
1 0 0 1
2 0 0 1
3 1 1 1
R> prop.table(tab, margin = 1)
f.y
f.x 1 2 3
1 0.0000000 0.0000000 1.0000000
2 0.0000000 0.0000000 1.0000000
3 0.3333333 0.3333333 0.3333333
More information about the R-help
mailing list