# Re: [R] Proba( Ut+2=1 / ((Ut+1==1) && (Ut==1))) ?

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Tue 26 Apr 2005 - 06:10:47 EST

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)))
>
> 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]]

R-help@stat.math.ethz.ch mailing list