Hi!
You could use a customized corellation-function (my.cor), wich omits sd=0 
and thus won't produce a warning, if sd = 0.

x.tS <- timeSeries(matrix(runif(10 * 100, -0.05, 0.05), 100, 10), 
timeSequence(length.out = 100, by = "day", to = Sys.Date()))
x.tS[5:70, 10] <- 0
# just to make it reproducible

x22 <- rollapply(as.zoo(x.tS), width = 22, FUN = function(x) {y <- cor(x);
 round(mean(y[lower.tri(y)]),3)}, by.column = FALSE, align="right")
# Your function: this produces the warnings!

my.cor <- function(x) {
cov.temp <- cov(x)[lower.tri(diag(ncol(x)))]
sd.temp <- sd(x)
sd.temp <- outer(sd.temp, sd.temp)[lower.tri(diag(length(sd.temp)))]

return(round(mean(cov.temp[sd.temp != 0] / sd.temp[sd.temp != 0]), 3))
}

x22 <- rollapply(as.zoo(x.tS), width = 22, FUN = my.cor, by.column = 
FALSE, align="right")
# no warnings!

rds

Matthias



Von:    petrovaa@gmail.com
An:     r-sig-finance@r-project.org
Datum:  30.06.2011 12:13
Betreff:        [R-SIG-Finance] Rolling Correlation Problem
Gesendet von:   r-sig-finance-bounces@r-project.org



Hello Masters of R,

I was wondering if somebody can help with a problem I am facing. I have a
zoo object consisting of 500 vectors representing constituents of a
benchmark. I want to calculate the rolling correlation on 22 window 
period.
The observations are daily return series. However, when I run  my code 

x22=rollapply(spx,width = 22, FUN = function(x) {y <- cor(x);
 round(mean(y[lower.tri(y)]),3); }, by.column = FALSE, align='right')

I get warning message such as the underneath one due to too many 0000s in
some constituents. 

Warning messages:
1: In cor(x) : the standard deviation is zero
2: In cor(x) : the standard deviation is zero
3: In cor(x) : the standard deviation is zero
4: In cor(x) : the standard deviation is zero
5: In cor(x) : the standard deviation is zero
6: In cor(x) : the standard deviation is zero
7: In cor(x) : the standard deviation is zero
8: In cor(x) : the standard deviation is zero


Do you know how to calculate the rolling correlations in the presence of 
too
many zeros?

Thanks.

AP

--
View this message in context: 
http://r.789695.n4.nabble.com/Rolling-Correlation-Problem-tp3635256p3635256.html

Sent from the Rmetrics mailing list archive at Nabble.com.

_______________________________________________
R-SIG-Finance@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions 
should go.



----------------------------------------
Raiffeisenlandesbank Niederösterreich-Wien AG  -  Firmensitz Wien -  FN 203160s - Handelsgericht Wien - DVR-Nummer: 0031585

Der Austausch von Nachrichten mit o.a. Absender via e-mail dient ausschliesslich Informationszwecken. Rechtsgeschaeftliche Erklaerungen duerfen ueber dieses Medium nicht ausgetauscht werden.
Correspondence with a.m. sender via e-mail is only for information purposes. This medium is not to be used for the exchange of legally-binding communications.
----------------------------------------

	[[alternative HTML version deleted]]

