0

I'm new to ts, and xts object.

When dealing with time series data, I encountered the problem

require(quantmod)
require(forecast)
ticker <- "^GSPC"
getSymbols(ticker, src="yahoo", to = "2013-12-31")
prices <- GSPC[,6]  # First get data using package quantmod
# then forecasting using package forecast
prices.ts <- as.ts(prices)
prices.ets <- ets(prices.ts)
prices.fore <- forecast(prices.ets, h=10)
# then plot
plot(prices.fore, xaxt = "n")

My problems are :

1 . When I tried to save the GSPC with date in a csv file. I searched and tried this

write.zoo((GSPC, file = "GSPC.csv", sep = ",", qmethod = "double"))

The error message: Error: unexpected ',' in "write.zoo((GSPC," , I checked the syntax, it seems to be correct, and I tried other combinations. All failed with the similar error message.

also I tried index(GSPC) to get the date.

and then cbind(index(GSPC), GSPC[, 6]). It also failed..

Error message: Error in merge.xts(..., all = all, fill = fill, suffixes = suffixes) : dims [product 1762] do not match the length of object [3524]

but when I checked the length

> length(GSPC[,6])
[1] 1762
> length(index(GSPC))
[1] 1762

2 . the plot is like this

enter image description here

there's no x-lab and y- lab. I tried the methods of accepted answer posted here, . but failed.

Especially, I don't get the purpose of the following code. It seems to change the appearance of the plot, but it doesn't change the appearance at all. I don't know whether I lose some points.

a = seq(as.Date("2011-11-01"), by="weeks", length=11)
axis(1, at = decimal_date(a), labels = format(a, "%Y %b %d"), cex.axis=0.6)
abline(v = decimal_date(a), col='grey', lwd=0.5)

Also, I want to plot from as.Date("2013-01-01").

Could you please give some suggestions?

Thanks a lot!

Community
  • 1
  • 1
Bigchao
  • 1,746
  • 3
  • 15
  • 31
  • Instead of saying "it didn't work" or "it failed" please tell us what the error messages were. Also, saying "not as expected" is meaningless unless you also describe what you expected. – SlowLearner Feb 20 '14 at 08:05
  • Sure, @SlowLearner, I'll put the error message immediately. – Bigchao Feb 20 '14 at 08:16
  • @SlowLearner, hey, I modified the post, and gave some details of error message. Could you give some suggestion? Thanks a lot! – Bigchao Feb 20 '14 at 08:32

1 Answers1

1
  1. You have additional parenthesis. Use

    write.zoo(GSPC, file = "GSPC.csv", sep = ",", qmethod = "double")

    I don't know what you are trying to achieve with your index and cbind commands. index does not give the data. And if you want the 6th column of GSPC just use GSPC[,6].

  2. It looks like you have some non-standard plotting dimensions. Start a new graphics window and you will reset them to defaults. But you won't get xlab and ylab unless you specify them explicitly. And you won't get an x-axis because you have set xaxt="n"

  3. The questions about the last code block do not seem to relate to your data at all.

Rob Hyndman
  • 30,301
  • 7
  • 73
  • 85
  • Hey, Rob. Thanks a lot for your comments! For the plotting part, I put a link, in the original post the accepted answer and all other answer have such similar code block, which I don't understand why.. It seems that the plot cannot be done using ggplot2. could you please take a look and give some suggestions about how to deal with the plots? How to specify them explicitly? Thanks a lot! – Bigchao Feb 20 '14 at 12:07