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 !)
Proba( Ut+2=1 / ((Ut+1==1) && (Ut==1))) ?
5 messages · vincent, Dimitris Rizopoulos, Tony Plate +1 more
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
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
An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20050425/711e6b45/attachment.pl