[R] From ts to xts
Giuseppe Vittucci
ignatiusreilly75 at yahoo.it
Mon Dec 6 15:45:05 CET 2010
I have the following problem.
After having created an xts importing data from a csv with:
data <- read.csv(mib.csv, header = TRUE, dec = ".", sep="\t")
dates <- as.POSIXct(strptime(data[,1],format="%m/%d/%Y"))
mib <- xts(data[,c(2:6)],order.by=dates))
I work out weekly log returns:
p <- apply.weekly(mib$Close,first)
r <- diff(p,lag=1,differences=1,log=TRUE)
fit an arma:
armafit <- arima(r, order = c(2, 0, 2))
and compute forecast:
f <- predict(armafit,n.ahead=5)
The fit is: f$pred, however it is a ts object, and if I try:
xp <- as.xts(f$pred)
the program returns the following error
Error in as.xts.ts(f$pred) : could not convert index to appropriate type
Calls: as.xts -> as.xts.ts
And I have also tried the following:
r_pred <- xts(f$pred, end(p)+index(f$pred))
that works, but the object merging the series with:
x <- merge(p,r,fr)
looks like this:
p r r_pred
2006-07-03 00:00:00 36666.00 NA NA
2006-07-10 00:00:00 36500.20 -0.0045321551 NA
2006-07-17 00:00:00 35802.60 -0.0192972236 NA
...
2010-09-16 00:00:00 20853.70 0.0057660912 NA
2010-09-25 05:00:01 NA NA 0.005320179
2010-09-25 06:00:01 NA NA -0.003516013
2010-09-25 07:00:01 NA NA -0.003299932
2010-09-25 08:00:01 NA NA -0.001210222
2010-09-25 09:00:01 NA NA -0.003233607
Is there a way to compute the right intervals?
Thanks
More information about the R-help
mailing list