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@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