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
stats on transitions from one state to another
4 messages · Murali.Menon at avivainvestors.com, David Winsemius, Berend Hasselman
On Feb 20, 2012, at 10:11 AM, <Murali.Menon at avivainvestors.com> wrote:
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?
Add an extra column of previous states: and tabulate:
> sss <-cbind(sss, c(NA, sss[,"State"][-nrow(sss)]) )
> table(sss[,"State"], sss[,4])
0 1 2
0 3 1 1
1 1 5 1
2 2 1 4
The requested means for "V1" by transition types:
> tapply(sss[,"V1"], INDEX=interaction(sss[,"State"], sss[,4]), mean)
0.0 1.0 2.0 0.1 1.1 2.1 0.2 1.2 2.2
-0.670 -0.190 -0.255 0.390 -0.640 2.790 -1.620 1.630 0.205
The counts on which those means are based:
> tapply(sss[,"V1"], INDEX=interaction(sss[,"State"], sss[,4]), length)
0.0 1.0 2.0 0.1 1.1 2.1 0.2 1.2 2.2
3 1 2 1 5 1 1 1 4
David Winsemius, MD West Hartford, CT
On 20-02-2012, at 16:11, <Murali.Menon at avivainvestors.com> <Murali.Menon at avivainvestors.com> wrote:
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
What do you mean by previous state. I do this
x <- cbind(c(NA, x[,"State"][-nrow(x)]), x) colnames(x)[1] <- "State.prev" x
State.prev State V1 V2 [1,] NA 0 0.21 0.51 [2,] 0 2 -0.57 -1.00 [3,] 2 2 -0.59 -1.29 [4,] 2 2 0.16 2.15 [5,] 2 0 -1.62 0.39 [6,] 0 0 0.18 0.78 [7,] 0 0 -0.81 0.85 [8,] 0 1 -0.19 1.18 [9,] 1 1 -0.76 1.66 [10,] 1 1 0.74 0.90 [11,] 1 1 -1.51 -0.94 [12,] 1 2 2.79 -1.29 [13,] 2 2 0.41 -0.23 [14,] 2 1 1.63 -0.92 [15,] 1 1 -0.86 -0.21 [16,] 1 1 -0.81 1.02 [17,] 1 0 0.39 -0.77 [18,] 0 0 -1.38 -0.68 [19,] 0 2 0.06 -0.33 [20,] 2 2 0.84 0.04 The mean of V1 in state 2 when the previous state is 0 would be in my interpretation mean(c(-0.57, 0.06))
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
Your second case would be the mean of -1.62 Berend
David: Brilliant! Thanks very much.
As Berend pointed out, I was not precise in the query, sorry. Please note in the example that we have a run of three state 2 after 0, and later another run of two state 2 after 0.
0 2 2 2 .... 0 0 2 2
Whenever the state moves from 0 to 2, I want to compute the mean of the run of values in state 2.
I tried to modify David's example by doing the 'interaction' on states and runs:
y <- rle(sss[,"State"])
yy <- cbind(y$lengths, y$values, c(NA, y$values[-length(y$values)]))
colnames(yy) <- c("RunLength","State","PrevState")
tapply(yy[,"RunLength"], INDEX=interaction(yy[,"PrevState"], yy[,"State"]), c)
This gives me the list of runs for each transition-pair.
Now I need to match these lists of runs against the corresponding rows in sss of values V1, V2, and compute stats on them.
Is there an easier way?
Thanks,
Murali
-----Original Message-----
From: David Winsemius [mailto:dwinsemius at comcast.net]
Sent: 20 February 2012 15:31
To: Menon Murali
Cc: r-help at r-project.org
Subject: Re: [R] stats on transitions from one state to another
On Feb 20, 2012, at 10:11 AM, <Murali.Menon at avivainvestors.com> wrote:
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?
Add an extra column of previous states: and tabulate:
> sss <-cbind(sss, c(NA, sss[,"State"][-nrow(sss)]) )
> table(sss[,"State"], sss[,4])
0 1 2
0 3 1 1
1 1 5 1
2 2 1 4
The requested means for "V1" by transition types:
> tapply(sss[,"V1"], INDEX=interaction(sss[,"State"], sss[,4]), mean)
0.0 1.0 2.0 0.1 1.1 2.1 0.2 1.2 2.2
-0.670 -0.190 -0.255 0.390 -0.640 2.790 -1.620 1.630 0.205
The counts on which those means are based:
> tapply(sss[,"V1"], INDEX=interaction(sss[,"State"], sss[,4]), length)
0.0 1.0 2.0 0.1 1.1 2.1 0.2 1.2 2.2
3 1 2 1 5 1 1 1 4
David Winsemius, MD West Hartford, CT