[R] stats on transitions from one state to another

Murali.Menon at avivainvestors.com Murali.Menon at avivainvestors.com
Mon Feb 20 16:11:04 CET 2012


Folks,

I'm trying to get stats from a matrix for each transition from one state to another.

I have a matrix x as below.

structure(c(0, 2, 2, 2, 0, 0, 0, 1, 1, 1, 1, 2, 2, 1, 1, 1, 0, 
0, 2, 2, 0.21, -0.57, -0.59, 0.16, -1.62, 0.18, -0.81, -0.19, 
-0.76, 0.74, -1.51, 2.79, 0.41, 1.63, -0.86, -0.81, 0.39, -1.38, 
0.06, 0.84, 0.51, -1, -1.29, 2.15, 0.39, 0.78, 0.85, 1.18, 1.66, 
0.9, -0.94, -1.29, -0.23, -0.92, -0.21, 1.02, -0.77, -0.68, -0.33, 
0.04), .Dim = c(20L, 3L), .Dimnames = list(NULL, c("State", "V1", 
"V2")))

Is it possible to get, say, mean values of each variable in state 1 when the previous state was 0, in state 2 when the previous state was 0, and so on with all available transitions between states 0, 1, 2?

In the above case, mean of V1 in state 2 when previous state was 0 would be 

mean(c(-0.57, -0.59, 0.16, 0.06, 0.84)) = -0.02

while the mean of V1 in state 0 when previous state was 2 would be:

mean(c(1.62, 0.18, -0.81)) = 0.33

If I try something like

by(x[, 2:3], x[, 1], FUN = colMeans)

I get the means for each state. I'm not sure how to get the split by transition?

Thanks,

Murali



More information about the R-help mailing list