Chuck (Is it fine to call you Chuck?) has far more R jutsu than I do obviously. I don't know much about pmin and factor but it might worth looking into if you want to manipulate states by names (I assume this is why one might want to use it?) generate_transition_matrix <- function(data, states) prop.table(table(head(data, -1), tail(data, -1)), 1)[states,] checkdf=data.frame(clusterNum=c(3,2,3,1,1,3,1,3,2,1,1,3,2,1,3,2)) states=c(2,3) transition_matrix= generate_transition_matrix(checkdf$clusterNum, states) transition_matrix 2017-08-28 18:09 GMT+02:00 niharika singhal <niharikasinghal1990 at gmail.com>:> yes or S_i= number of state > > M[S_i][S_j]= transition from state S_i to state S_j / number of > transition from state S_i where S_i is in the set S > For example 1 row of 3 states will be > x= s1->s1/no of transition in s1 > y= s1->s2/no of transition in s1 > z= s1->s3/no of transition in s1 > > that comes out x=((2/6) y=(1/6) z= (3/6) > s1 s2 s3 > s1 x y z > s2 > s3 > > Thanks & Regards > Niharika > > On Mon, Aug 28, 2017 at 6:03 PM, Elie Canonici Merle < > elie.canonicimerle at gmail.com> wrote: > >> Ok, I assumed you wanted to compute a matrix M for all states such that >> >> M[i][j]= transition from state i to state j / number of transition from >> state i >> >> but from what you just answered it looks like you want to compute a >> matrix M for a set of states S such that: >> M[S_i][S_j]= transition from state S_i to state S_j / number of >> transition from state S_i where S_i is in the set S >> ? >> >> 2017-08-28 17:50 GMT+02:00 niharika singhal < >> niharikasinghal1990 at gmail.com>: >> >>> Sorry I have typed wrong value for output >>> (2/6, 1/6, 3/6) >>> >>> On Mon, Aug 28, 2017 at 5:49 PM, niharika singhal < >>> niharikasinghal1990 at gmail.com> wrote: >>> >>>> Hi Elie, >>>> >>>> Thanks for the mail >>>> >>>> If the number of states will change then the data frame will also have >>>> 4 as one of the value >>>> Also I am only intersted in i and (i+1) (linear search for each state) >>>> >>>> in the above data frame for state1 ={1} the output will like >>>> (1/6, 1/6, 3/6) >>>> >>>> Your code is giving me some error but I will see if I can remove and >>>> use it. >>>> >>>> Regards >>>> Niharika >>>> >>>> On Mon, Aug 28, 2017 at 5:31 PM, Elie Canonici Merle < >>>> elie.canonicimerle at gmail.com> wrote: >>>> >>>>> Hi, >>>>> >>>>> I think you overthought this one a little bit, I don't know if this is >>>>> the kind of code you are expecting but I came up with something like that: >>>>> >>>>> generate_transition_matrix <- function(data, n_states) { >>>>> >>>>> #To be sure I imagine you should check n_states is right at this >>>>> point >>>>> >>>>> transitions <- matrix(0, n_states, n_states) >>>>> >>>>> #we could improve a little bit here because at step N+1 source is >>>>> dest from step N >>>>> #but it would not be as readable >>>>> for (k in 1:(length(data) - 1)) { >>>>> source_state <- data[k] >>>>> dest_state <- data[k+1] >>>>> transitions[source_state, dest_state] <- >>>>> transitions[source_state, dest_state] + 1 >>>>> } >>>>> >>>>> for (k in 1:n_states) >>>>> transitions[k,] <- transitions[k,] / sum(transitions[k,]) >>>>> transitions >>>>> } >>>>> >>>>> checkdf=data.frame(clusterNum=c(3,2,3,1,1,3,4,3,2,1,1,3,2,1,3,2)) >>>>> no_of_state=4 >>>>> >>>>> transition_matrix= generate_transition_matrix(checkdf$clusterNum, >>>>> no_of_state) >>>>> transition_matrix >>>>> >>>>> >>>>> >>>>> 2017-08-28 16:37 GMT+02:00 niharika singhal < >>>>> niharikasinghal1990 at gmail.com>: >>>>> >>>>>> Hello, >>>>>> >>>>>> I am trying to implement a formula >>>>>> >>>>>> aij= transition from state S_i to S_j/no of transition at state S_i >>>>>> >>>>>> >>>>>> >>>>>> Code I have written is working with three state {1,2,3 }, but if the >>>>>> number >>>>>> of states become={1,2,3,4,......n} then the code will not work, so >>>>>> can some >>>>>> help me with this. >>>>>> >>>>>> For and some rows of my data frame look like >>>>>> >>>>>> checkdf=data.frame(clusterNum=c(3,2,3,1,1,3,4,3,2,1,1,3,2,1,3,2) >>>>>> no_of_state=3 >>>>>> transition_matrix=matrix(NA,nrow=no_of_state, ncol=no_of_state) >>>>>> for(k in 1: no_of_state) >>>>>> { >>>>>> count1=0 >>>>>> count2=0 >>>>>> count3=0 >>>>>> #For last point no transition takes place >>>>>> for(j in 1: (nrow(checkdf)-1)) >>>>>> { >>>>>> >>>>>> if(checkdf$clusterNum[j]==k) >>>>>> { >>>>>> if(checkdf$clusterNum[j+1]==1){ >>>>>> count1=count1+1 >>>>>> } >>>>>> else if(checkdf$clusterNum[j+1]==2){ >>>>>> count2=count2+1 >>>>>> } >>>>>> else { >>>>>> count3=count3+1 >>>>>> } >>>>>> } >>>>>> } >>>>>> >>>>>> no_of_points=(count1+count2+count3) >>>>>> s1=count1/no_of_points >>>>>> s2=count2/no_of_points >>>>>> s3=count3/no_of_points >>>>>> transition_matrix[k,]=c(s1, s2, s3) >>>>>> >>>>>> } >>>>>> >>>>>> I know the code is not written nicely and I want to improve it. >>>>>> >>>>>> Thanks in advance >>>>>> Niharika >>>>>> >>>>>> [[alternative HTML version deleted]] >>>>>> >>>>>> ______________________________________________ >>>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >>>>>> https://stat.ethz.ch/mailman/listinfo/r-help >>>>>> PLEASE do read the posting guide http://www.R-project.org/posti >>>>>> ng-guide.html >>>>>> and provide commented, minimal, self-contained, reproducible code. >>>>>> >>>>> >>>>> >>>> >>> >> >[[alternative HTML version deleted]]

> On Aug 28, 2017, at 9:26 AM, Elie Canonici Merle <elie.canonicimerle at gmail.com> wrote: > > Chuck (Is it fine to call you Chuck?)In this forum, yes please.> I don't know much about pmin and factor but it might worth looking into if > you want to manipulate states by names (I assume this is why one might want > to use it?) >Actually is it because the OP had states 1-4 in his data. In the pre-state only 1-3 get counted. In the post-state, 4 gets rolled into 3. So, pre <- factor(data, 1:3) # 4 and higher are NA post <- pmin(3, data) # 4 and higher become 3 implements those rules, and table(pre, post) gives a 3 x 3 table that one need not subset. Chuck> generate_transition_matrix <- function(data, states) > prop.table(table(head(data, -1), tail(data, -1)), 1)[states,] > > > checkdf=data.frame(clusterNum=c(3,2,3,1,1,3,1,3,2,1,1,3,2,1,3,2)) > > states=c(2,3) > > transition_matrix= generate_transition_matrix(checkdf$clusterNum, states) > transition_matrix >