plotting with dates

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

plotting with dates

Eric Thungstom
How do I get this to plot with dates ? The x-axis is coming through with
numbers.

require(quantmod)
require(forecast)
getSymbols("^GSPC", from='2006-01-06', to=Sys.Date())
z <- Ad(GSPC)
y <-forecast(z)
summary(y)
plot(y)

        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: plotting with dates

Kenton Russell

Based on my very limited experience with ts, it does not handle daily data very well, which leaves us three options: change data to weekly, which I think is probably not acceptablez <- to.weekly(Ad(GSPC))[,4]
z.ts<-ts(as.vector(z),start=as.numeric(c(format(index(z)[1],"%Y"),format(index(z)[1],"%m"))),
frequency=52)
y <-forecast(z.ts)
summary(y)
plot(y)
or assume 365 days (not good for leap years and results are very strange)require(quantmod)
require(forecast)
getSymbols("^GSPC", from='2006-01-06', to=Sys.Date())
z <- Ad(GSPC)
z.ts<-ts(as.vector(z),start=as.numeric(c(format(index(z)[1],"%Y"),format(index(z)[1],"%m"),format(index(z)[1],"%d"))),
frequency=365)
y <-forecast(z.ts)
summary(y)
plot(y) or convert to xts and then plot (ugly example but should get you started)y.xts <- as.xts(cbind(
  c(y$x,y$upper[,1]),
  c(y$x,y$upper[,2]),
  c(y$x,y$lower[,1]),
  c(y$x,y$lower[,2]),
  c(y$x,rep(y$x[NROW(y$x)],NROW(y$upper)))),
 c(index(z),index(last(z))+1:NROW(y$upper)))
plot.zoo(y.xts,screens=1,col=c(2:5,1))    Kenttimelyportfolio.blogspot.com
 > Date: Sat, 26 May 2012 14:51:54 -0400

> From: [hidden email]
> To: [hidden email]
> Subject: [R-SIG-Finance] plotting with dates
>
> How do I get this to plot with dates ? The x-axis is coming through with
> numbers.
>
> require(quantmod)
> require(forecast)
> getSymbols("^GSPC", from='2006-01-06', to=Sys.Date())
> z <- Ad(GSPC)
> y <-forecast(z)
> summary(y)
> plot(y)
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> [hidden email] 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.
     
        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: plotting with dates

braverock
In reply to this post by Eric Thungstom
On 05/26/2012 01:51 PM, Eric Thungstom wrote:

> How do I get this to plot with dates ? The x-axis is coming through with
> numbers.
>
> require(quantmod)
> require(forecast)
> getSymbols("^GSPC", from='2006-01-06', to=Sys.Date())
> z<- Ad(GSPC)
> y<-forecast(z)
> summary(y)
> plot(y)

The forecast object is indexed to the same data, they're just a mess.

I suggest extracting the slots you want from the forecast object.

e.g.

z$fitted<-as.vector(y$fitted)
chart_Series(z$fitted)
require(PerformanceAnalytics)
chart.TimeSeries(z)
chart.TimeSeries(z$fitted)


--
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock

_______________________________________________
[hidden email] 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.