G Girija
2013-Jul-26 05:37 UTC
[R] number of items to replace is not a multiple of replacement length
Hi All,
I have 5 stock values and i am calculating EWMA
followed the logic as given ind following link.[
http://www.orecastingfinancialrisk.com/3.html<http://www.forecastingfinancialrisk.com/3.html>
]
library('tseries')
returns[,1]<-returns[,1]-mean(returns[,1])
returns[,2]<-returns[,2]-mean(returns[,2])
returns[,3]<-returns[,3]-mean(returns[,3])
returns[,4]<-returns[,4]-mean(returns[,4])
returns[,5]<-returns[,5]-mean(returns[,5])
T<-length(returns[,1])
T
EWMA<-matrix(nrow=T,ncol=5)
lambda=0.94
S<-cov(returns)
S
EWMA[1,] <- S[lower.tri(S,diag=TRUE)]
*Error in EWMA[1, ] <- S[lower.tri(S, diag = TRUE)] : *
* number of items to replace is not a multiple of replacement length*
*
*
for(i in 2:T)
{
S<- lambda*S +(1-lambda)*t(returns[i])%*% returns[i]
EWMA[i,] <- S[lower.tri(S,diag=TRUE)]
}
*
*
[[alternative HTML version deleted]]
Pascal Oettli
2013-Jul-26 08:59 UTC
[R] number of items to replace is not a multiple of replacement length
Hello, Once again, the matrix EWMA has not the correct size. Did you carefully read the answer by Thomas Stewart? https://stat.ethz.ch/pipermail/r-help/attachments/20130724/c454b0f7/attachment.pl Extract of his reply: "When you expand the example to 5 stocks, there will be 15 elements (5 variances and 10 covariances)" > EWMA<-matrix(nrow=T,ncol=15)) Regards, Pascal On 26/07/2013 14:37, G Girija wrote:> Hi All, > > I have 5 stock values and i am calculating EWMA > followed the logic as given ind following link.[ > http://www.orecastingfinancialrisk.com/3.html<http://www.forecastingfinancialrisk.com/3.html> > ] > > library('tseries') > returns[,1]<-returns[,1]-mean(returns[,1]) > returns[,2]<-returns[,2]-mean(returns[,2]) > returns[,3]<-returns[,3]-mean(returns[,3]) > returns[,4]<-returns[,4]-mean(returns[,4]) > returns[,5]<-returns[,5]-mean(returns[,5]) > T<-length(returns[,1]) > T > EWMA<-matrix(nrow=T,ncol=5) > lambda=0.94 > S<-cov(returns) > S > EWMA[1,] <- S[lower.tri(S,diag=TRUE)] > > > *Error in EWMA[1, ] <- S[lower.tri(S, diag = TRUE)] : * > * number of items to replace is not a multiple of replacement length* > * > * > for(i in 2:T) > { > S<- lambda*S +(1-lambda)*t(returns[i])%*% returns[i] > EWMA[i,] <- S[lower.tri(S,diag=TRUE)] > } > * > * > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >