Hi All, Does someone have an idea of how to cleverly convert a categorical timeseries into a transition matrix? Ie, I have something like: x<- c(1,1,2,1,1,2,2,2,1,2), And I want a matrix with counts and/or probabilities:> tr <- matrix(c(2,3,2,2),2,2) > tr[,1] [,2] [1,] 2 2 [2,] 3 2 Meaning that there are two transitions from 1 to 1, two from 1 to 2, three from 2 to 1 and two from 2 to 2. Using for loops etc this is of course no problem, but I am curious whether there is a smarter solution. Any hints appreciated, Ingmar -- Ingmar Visser Department of Psychology, University of Amsterdam Roetersstraat 15, room 1009 1018 WB Amsterdam The Netherlands http://users.fmg.uva.nl/ivisser/ tel: +31-20-5256735
The following, using table, seems to work:> x <- sample(letters[1:2], 10, replace=T) > x[1] "b" "a" "a" "b" "b" "a" "a" "b" "b" "b"> table(x[-1],x[-length(x)])a b a 2 2 b 2 3 Hope this helps, Giovanni Petris> Date: Tue, 22 Mar 2005 11:20:40 -0500 > From: Ingmar Visser <i.visser at uva.nl> > Sender: r-help-bounces at stat.math.ethz.ch > Cc: > Precedence: list > User-Agent: Microsoft-Entourage/11.1.0.040913 > > Hi All, > Does someone have an idea of how to cleverly convert a categorical > timeseries into a transition matrix? > Ie, I have something like: > x<- c(1,1,2,1,1,2,2,2,1,2), > And I want a matrix with counts and/or probabilities: > > tr <- matrix(c(2,3,2,2),2,2) > > tr > [,1] [,2] > [1,] 2 2 > [2,] 3 2 > Meaning that there are two transitions from 1 to 1, two from 1 to 2, three > from 2 to 1 and two from 2 to 2. > Using for loops etc this is of course no problem, but I am curious whether > there is a smarter solution. > Any hints appreciated, Ingmar > > -- > Ingmar Visser > Department of Psychology, University of Amsterdam > Roetersstraat 15, room 1009 > 1018 WB Amsterdam > The Netherlands > http://users.fmg.uva.nl/ivisser/ > tel: +31-20-5256735 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > >
Ingmar Visser <i.visser <at> uva.nl> writes: : : Hi All, : Does someone have an idea of how to cleverly convert a categorical : timeseries into a transition matrix? : Ie, I have something like: : x<- c(1,1,2,1,1,2,2,2,1,2), : And I want a matrix with counts and/or probabilities: : > tr <- matrix(c(2,3,2,2),2,2) : > tr : [,1] [,2] : [1,] 2 2 : [2,] 3 2 : Meaning that there are two transitions from 1 to 1, two from 1 to 2, three : from 2 to 1 and two from 2 to 2. : Using for loops etc this is of course no problem, but I am curious whether : there is a smarter solution. : Any hints appreciated, Ingmar : Try this: table(from = x[-length(x)], to = x[-1]) It gives the transpose of tr in your example since there are actually three transitions from 1 to 2, not three transitions from 2 to 1.
Ingmar -- Using "embed" and "table" can do it. "Embed" creates the> temp1 <- sample(1:3, 500, replace = TRUE) > temp2 <- embed(temp1, 2) > dim(temp2)[1] 499 2> table(as.data.frame(temp2[, c(2,1)]))V2 V1 1 2 3 1 47 59 50 2 68 57 52 3 42 61 63 You need to reverse the times because embed creates rows of the form (x[t], x[t-1]). For higher dimensions you could use rev(1:ncol(x)). Hope this helps, Matt Wiener -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Ingmar Visser Sent: Tuesday, March 22, 2005 11:21 AM To: R-help at stat.math.ethz.ch Subject: [R] Convert timeseries to transition matrix Hi All, Does someone have an idea of how to cleverly convert a categorical timeseries into a transition matrix? Ie, I have something like: x<- c(1,1,2,1,1,2,2,2,1,2), And I want a matrix with counts and/or probabilities:> tr <- matrix(c(2,3,2,2),2,2) > tr[,1] [,2] [1,] 2 2 [2,] 3 2 Meaning that there are two transitions from 1 to 1, two from 1 to 2, three from 2 to 1 and two from 2 to 2. Using for loops etc this is of course no problem, but I am curious whether there is a smarter solution. Any hints appreciated, Ingmar -- Ingmar Visser Department of Psychology, University of Amsterdam Roetersstraat 15, room 1009 1018 WB Amsterdam The Netherlands http://users.fmg.uva.nl/ivisser/ tel: +31-20-5256735 ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Here is a way to do this: x <- c(1,1,2,1,1,2,2,2,1,2) y <- cbind(x,c(x[-1],NA)) # time-shifted by one aggregate(y, by=list(y[,1],y[,2]), length) -Christos Hatzis -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Ingmar Visser Sent: Tuesday, March 22, 2005 11:21 AM To: R-help at stat.math.ethz.ch Subject: [R] Convert timeseries to transition matrix Hi All, Does someone have an idea of how to cleverly convert a categorical timeseries into a transition matrix? Ie, I have something like: x<- c(1,1,2,1,1,2,2,2,1,2), And I want a matrix with counts and/or probabilities:> tr <- matrix(c(2,3,2,2),2,2) > tr[,1] [,2] [1,] 2 2 [2,] 3 2 Meaning that there are two transitions from 1 to 1, two from 1 to 2, three from 2 to 1 and two from 2 to 2. Using for loops etc this is of course no problem, but I am curious whether there is a smarter solution. Any hints appreciated, Ingmar -- Ingmar Visser Department of Psychology, University of Amsterdam Roetersstraat 15, room 1009 1018 WB Amsterdam The Netherlands http://users.fmg.uva.nl/ivisser/ tel: +31-20-5256735 ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Thanks to everyone for your quick and helpful answers! ingmar On 3/22/05 11:40 AM, "Christos Hatzis" <christos at nuverabio.com> wrote:> Here is a way to do this: > > x <- c(1,1,2,1,1,2,2,2,1,2) > y <- cbind(x,c(x[-1],NA)) # time-shifted by one > aggregate(y, by=list(y[,1],y[,2]), length) > > -Christos Hatzis > > -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Ingmar Visser > Sent: Tuesday, March 22, 2005 11:21 AM > To: R-help at stat.math.ethz.ch > Subject: [R] Convert timeseries to transition matrix > > Hi All, > Does someone have an idea of how to cleverly convert a categorical > timeseries into a transition matrix? > Ie, I have something like: > x<- c(1,1,2,1,1,2,2,2,1,2), > And I want a matrix with counts and/or probabilities: >> tr <- matrix(c(2,3,2,2),2,2) >> tr > [,1] [,2] > [1,] 2 2 > [2,] 3 2 > Meaning that there are two transitions from 1 to 1, two from 1 to 2, three > from 2 to 1 and two from 2 to 2. > Using for loops etc this is of course no problem, but I am curious whether > there is a smarter solution. > Any hints appreciated, Ingmar > > -- > Ingmar Visser > Department of Psychology, University of Amsterdam Roetersstraat 15, room > 1009 > 1018 WB Amsterdam > The Netherlands > http://users.fmg.uva.nl/ivisser/ > tel: +31-20-5256735 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >-- Ingmar Visser Department of Psychology, University of Amsterdam Roetersstraat 15, 1018 WB Amsterdam The Netherlands http://users.fmg.uva.nl/ivisser/ tel: +31-20-5256735