Skip to content

Convert timeseries to transition matrix

1 message · Wiener, Matthew

#
Ingmar --

Using "embed" and "table" can do it.  "Embed" creates the
[1] 499   2
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:
[,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