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:> 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? >One way is to just tabulate the process against an offset of itself> process <- c( 5 , 7 , 4 , 1 , 4 , 1 , 4 , 1 , 4 , 4 , 1 , 5 , 4 ) > n<-length(process) > table(process[-1],process[-n])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> tt<-table(process[-1],process[-n]) > sweep(tt,2,apply(tt,2,sum),"/")*1001 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>From owner-r-help at stat.math.ethz.ch Mon Jul 9 16:43:54 2001 >Received: from stat.math.ethz.ch (majordom at hypatia.ethz.ch [129.132.58.23]) > by cattell.psych.upenn.edu (8.9.1/8.9.1/SAS.05) with ESMTP id QAA10528 > for <baron at cattell.psych.upenn.edu>; Mon, 9 Jul 2001 16:43:54 -0400 (EDT) >Received: by stat.math.ethz.ch (8.9.1/8.9.1) id VAA22595 > for r-help-gang-use; Mon, 9 Jul 2001 21:58:06 +0200 (MET DST) >Received: (from daemon at localhost) > by stat.math.ethz.ch (8.9.1/8.9.1) id VAA22574 > for <r-help at hypatia.math.ethz.ch>; Mon, 9 Jul 2001 21:57:58 +0200 (MET DST) >Received: from viz.itd.nrl.navy.mil(132.250.80.179) > via SMTP by hypatia, id smtpdAAAa005WR; Mon Jul 9 21:57:48 2001 >Received: (from trafton at localhost) > by viz.itd.nrl.navy.mil (8.11.2/8.11.2) id f69Jvfq17493; > Mon, 9 Jul 2001 15:57:41 -0400 >From: Greg Trafton <trafton at itd.nrl.navy.mil> >MIME-Version: 1.0 >Content-Type: text/plain; charset=us-ascii >Content-Transfer-Encoding: 7bit >Message-ID: <15178.3253.53696.310509 at viz.itd.nrl.navy.mil> >Date: Mon, 9 Jul 2001 15:57:41 -0400 >To: r-help at stat.math.ethz.ch >Subject: [R] transitions in R >X-Mailer: VM 6.89 under 21.1 (patch 14) "Cuyahoga Valley" XEmacs Lucid >Sender: owner-r-help at stat.math.ethz.ch >Precedence: SfS-bulk >Status: R >Content-Length: 985 > >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?For the one-deep try plength <- length(process) table(process[1:(plength-1)],process[2:plength]) To get proportions, table(process[1:(plength-1)],process[2:plength])/(plength-1) Multiply by 100 to get percent, and use round() if needed. I think you can extend this to more depth and get bigger tables. Jon Baron -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>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! >gregtransitions <- 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 }> p <- c(1,2,3,2,1,1,2,1,3) > transitions(p, c(1,1,1,2,2,2), c(1,2,3,1,2,3))[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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._