Skip to content

transitions in R

3 messages · Greg Trafton, Thomas Lumley, Philippe GROSJEAN

#
Hi, All.  I'd have a set of data in an array:

process <- c( 5 , 7 , 4 , 1 , 4 , 1 , 4 , 1 , 4 , 4 , 1 , 5 , 4 , ...)

and I'd like to know the number of transitions in this data.  I
calculate transitions as the number of times a number follows another
number.  thus, something like this would be a 1 deep transition:

1 --> 1 : 10% (and actual number of 1 --> 1 occurrences)
1 --> 2 : 2%
1 --> 3 : 23%
...
2 --> 1 : 2%
2 --> 2 : 8%
(etc.)

of course, you can have 2 or 3 or n deep transitions, but I'm really
only interested in 1 and 2 (and maybe 3) deep transitions.

what is the best way of calculating this info in R?

thanks!
greg
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
On Mon, 9 Jul 2001, Greg Trafton wrote:

            
One way is to just tabulate the process against an offset of itself
1 4 5 7
  1 0 4 0 0
  4 3 1 1 1
  5 1 0 0 0
  7 0 0 1 0

Or if you now want to divide each column by its sum and get %ages
1  4  5   7
  1  0 80  0   0
  4 75 20 50 100
  5 25  0  0   0
  7  0  0 50   0


	-thomas

Thomas Lumley			Asst. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
transitions <- function(vect, t1, t2) {
  # vect is the vector to test
  # t1 is the vector of all initial states of the transitions
  # t2 is the vector of all final states of the transitions
  n <- length(vect)
  nt <- length(t1)   # rem: t2 must be same length, not tested here
  v <- rep(vect, nt)
  dim(v) <- c(n, nt)
  v <- t(v)
  tests <- (v[, 1:(n-1)]==t1 & v[, 2:n]==t2)
  res <- apply(tests, 1, "sum", na.rm=T)
  res
}
[1] 1 2 1 2 0 1


...........]<(({?<...............<?}))><...............................
 ) ) ) ) )	 __               	 __
( ( ( ( ( 	|__)              	|  _
 ) ) ) ) )	|   hilippe       	|__)rosjean
( ( ( ( ( 	Marine Biol. Lab., ULB, Belgium
 ) ) ) ) )	                  	 __
( ( ( ( ( 	|\  /|            	|__)
 ) ) ) ) )	| \/ |ariculture &	|__)iostatistics
( ( ( ( ( 
 ) ) ) ) )	e-mail: phgrosje at ulb.ac.be or phgrosjean at sciviews.org
( ( ( ( ( 	SciViews project coordinator (http://www.sciviews.org)
 ) ) ) ) )      tel: 00-32-2-650.29.70 (lab), 00-32-2-673.31.33 (home)
( ( ( ( (
 ) ) ) ) )      "I'm 100% confident that p is between 0 and 1" 
( ( ( ( (                                  L. Gonick & W. Smith (1993) 
 ) ) ) ) )
.......................................................................
 
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._