Dear all, First I apologize if my question is quite simple, but i'm very newbie with R. I have vectors of the form v = c(1,1,-1,-1,-1,1,1,1,1,-1,1) (longer than this one of course). The elements are only +1 or -1. I would like to calculate : - the frequencies of -1 occurences after 2 consecutives -1 - the frequencies of +1 occurences after 2 consecutives +1 It looks probably something like : Proba( Ut+2=1 / ((Ut+1==1) && (Ut==1))) could someone please give me a little hint about how i should/could begin to proceed ? Thanks (Thanks also to the R creators/contributors, this soft seems really great !)
maybe something like this: x <- sample(c(1, -1), 100, TRUE) y <- rle(x) ## ind1 <- y$length[y$value == 1] sum(ind1[ind1 > 2] - 2) ind2 <- y$length[y$value == -1] ## sum(ind1[ind1 > 2] - 2) could be helpful. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "vincent" <vincent at 7d4.com> To: <r-help at stat.math.ethz.ch> Sent: Monday, April 25, 2005 6:03 PM Subject: [R] Proba( Ut+2=1 / ((Ut+1==1) && (Ut==1))) ?> Dear all, > > First I apologize if my question is quite simple, > but i'm very newbie with R. > > I have vectors of the form v = c(1,1,-1,-1,-1,1,1,1,1,-1,1) > (longer than this one of course). > The elements are only +1 or -1. > > I would like to calculate : > - the frequencies of -1 occurences after 2 consecutives -1 > - the frequencies of +1 occurences after 2 consecutives +1 > > It looks probably something like : > Proba( Ut+2=1 / ((Ut+1==1) && (Ut==1))) > > could someone please give me a little hint about how > i should/could begin to proceed ? > > Thanks > (Thanks also to the R creators/contributors, this soft > seems really great !) > > ______________________________________________ > 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 >
or maybe something like this x<- sample(c(1, -1), 100, TRUE); t <- p <- 0 for (i in 1: (lenght(x)-2)) { if (x[i]+x[i+1]+x[i+2] == 3) t<- t+1; if (x[i]+x[i+1]+x[i+2] == -3) p<-p+1} P1<-t/length(x); P2<-p/length(x) Tom Dimitris Rizopoulos <dimitris.rizopoulos@med.kuleuven.ac.be> wrote: maybe something like this: x <- sample(c(1, -1), 100, TRUE) y <- rle(x) ## ind1 <- y$length[y$value == 1] sum(ind1[ind1 > 2] - 2) ind2 <- y$length[y$value == -1] ## sum(ind1[ind1 > 2] - 2) could be helpful. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "vincent" To: Sent: Monday, April 25, 2005 6:03 PM Subject: [R] Proba( Ut+2=1 / ((Ut+1==1) && (Ut==1))) ?> Dear all, > > First I apologize if my question is quite simple, > but i'm very newbie with R. > > I have vectors of the form v = c(1,1,-1,-1,-1,1,1,1,1,-1,1) > (longer than this one of course). > The elements are only +1 or -1. > > I would like to calculate : > - the frequencies of -1 occurences after 2 consecutives -1 > - the frequencies of +1 occurences after 2 consecutives +1 > > It looks probably something like : > Proba( Ut+2=1 / ((Ut+1==1) && (Ut==1))) > > could someone please give me a little hint about how > i should/could begin to proceed ? > > Thanks > (Thanks also to the R creators/contributors, this soft > seems really great !) > > ______________________________________________ > R-help@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 >______________________________________________ R-help@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 __________________________________________________ [[alternative HTML version deleted]]
table() can return all the n-gram statistics, e.g.: > v <- sample(c(-1,1), 1000, rep=TRUE) > table("v_{t-2}"=v[-seq(to=length(v), len=2)], "v_{t-1}"=v[-c(1,length(v))], "v_t"=v[-(1:2)]) , , v_t = -1 v_{t-1} v_{t-2} -1 1 -1 136 134 1 131 112 , , v_t = 1 v_{t-1} v_{t-2} -1 1 -1 131 113 1 115 126 > This says that there were 136 cases in which a -1 followed two -1's (and 126 cases in which a 1 followed to 1's). If you're really only interested in particular contexts, you can do something like: > table(v[-seq(to=length(v), len=2)]==1 & v[-c(1,length(v))]==1 & v[-(1:2)]==1) FALSE TRUE 872 126 > table(v[-seq(to=length(v), len=2)]==-1 & v[-c(1,length(v))]==-1 & v[-(1:2)]==-1) FALSE TRUE 862 136 or > sum(v[-seq(to=length(v), len=2)]==-1 & v[-c(1,length(v))]==-1 & v[-(1:2)]==-1) [1] 136 > vincent wrote:> Dear all, > > First I apologize if my question is quite simple, > but i'm very newbie with R. > > I have vectors of the form v = c(1,1,-1,-1,-1,1,1,1,1,-1,1) > (longer than this one of course). > The elements are only +1 or -1. > > I would like to calculate : > - the frequencies of -1 occurences after 2 consecutives -1 > - the frequencies of +1 occurences after 2 consecutives +1 > > It looks probably something like : > Proba( Ut+2=1 / ((Ut+1==1) && (Ut==1))) > > could someone please give me a little hint about how > i should/could begin to proceed ? > > Thanks > (Thanks also to the R creators/contributors, this soft > seems really great !) > > ______________________________________________ > 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 >
These two expressions might be what you are looking for -- sum (3 =x[c(-length(x),-(length(x)-1))]+x[c(-1,-length(x))]+x[c(-1,-2)]) sum (-3 =x[c(-length(x),-(length(x)-1))]+x[c(-1,-length(x))]+x[c(-1,-2)]) Ben Fairbank -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of vincent Sent: Monday, April 25, 2005 11:03 AM To: r-help at stat.math.ethz.ch Subject: [R] Proba( Ut+2=1 / ((Ut+1==1) && (Ut==1))) ? Dear all, First I apologize if my question is quite simple, but i'm very newbie with R. I have vectors of the form v = c(1,1,-1,-1,-1,1,1,1,1,-1,1) (longer than this one of course). The elements are only +1 or -1. I would like to calculate : - the frequencies of -1 occurences after 2 consecutives -1 - the frequencies of +1 occurences after 2 consecutives +1 It looks probably something like : Proba( Ut+2=1 / ((Ut+1==1) && (Ut==1))) could someone please give me a little hint about how i should/could begin to proceed ? Thanks (Thanks also to the R creators/contributors, this soft seems really great !) ______________________________________________ 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
On 4/25/05, vincent <vincent@7d4.com> wrote:> > Dear all, > > First I apologize if my question is quite simple, > but i'm very newbie with R. > > I have vectors of the form v = c(1,1,-1,-1,-1,1,1,1,1,-1,1) > (longer than this one of course). > The elements are only +1 or -1. > > I would like to calculate : > - the frequencies of -1 occurences after 2 consecutives -1 > - the frequencies of +1 occurences after 2 consecutives +1 > > It looks probably something like : > Proba( Ut+2=1 / ((Ut+1==1) && (Ut==1))) > > could someone please give me a little hint about how > i should/could begin to proceed ?Let v be a time series. Then lag(v, -1) is the same series moved forward by one and lag(v, -2) is the same series moved forward by two. Now we just want to know if all three are equal to -1 (or +1 in the other case): v <- ts(c(1,1,-1,-1,-1,1,1,1,1,-1,1)) sum( lag(v, -2) == -1 & lag(v,-1) == -1 & v == -1 ) sum( lag(v, -2) == 1 & lag(v,-1) == 1 & v == 1 ) Using ts objects in this way has the nice property that it takes care of alignment and end effects for you automatically. [[alternative HTML version deleted]]