Re: [R] moving correlation coef ?

From: Sundar Dorai-Raj <sundar.dorai-raj_at_pdf.com>
Date: Thu 30 Jun 2005 - 02:50:19 EST

vincent wrote:
> Thank you for your answers.
>
> In fact, i believe my question wasn't precise enough.
> I don't want to have a moving/sliding windows over the data
> to correlate (i am already doing that).
>
> If I have 2 vectors
> X = (x1, x2, x3, ..., xt)
> Y = (y1, y2, x3, ..., yt)
> I want the most recent elements (t) to have a heavier weight
> in the correlation calculus than the older ones (1).
>
> I think that one simple way to do that is for example to
> compute cor() over XP, YP where
> XP = (x1, x2, x2, x3, x3, x3, ..., xt, xt, xt)
> YP = (y1, y2, y2, y3, y3, y3, ..., yt, yt, yt)
> ie where each element is repeated several times according
> to its freshness.
> It's quite naive ! so if there is a cleaver idea, many thanks.
>
> Thanks
> Vincent
>

Perhaps ?cov.wt will work for you? Your example would be identical to:

set.seed(1)
X <- rnorm(100); Y <- rnorm(100)
# using cov.wt
rho1 <- cov.wt(cbind(X, Y), 1:100, cor = TRUE)$cor[1, 2] # your weighting scheme
rho2 <- cor(X[rep(1:100, 1:100)], Y[rep(1:100, 1:100)]) all.equal(rho1, rho2)
# [1] TRUE HTH, --sundar



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 Received on Thu Jun 30 02:53:20 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:07 EST