forecast package + ggplot2

398 views
Skip to first unread message

Hasan Diwan

unread,
Apr 2, 2012, 10:00:09 AM4/2/12
to ggp...@googlegroups.com
Trying to plot an arima model (from the forecast CRAN package) using ggplot2, one generated using auto.arima. This object is of class "Arima" and can't be coerced into a data.frame directly. Any idea how to force this conversion? Many thanks! -- H

R. Michael Weylandt

unread,
Apr 2, 2012, 10:06:29 AM4/2/12
to Hasan Diwan, ggp...@googlegroups.com
It seems to work well enough on the forecasted points:

example(auto.arima) # Creates an Arima object called "fit"
as.data.frame(forecast(fit,h=20))

But what exactly are you trying to plot? There's a lot more in the
Arima object than it would make sense to transfer to a single graph.

Hope this helps,

Michael

> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

Jean-Olivier Irisson

unread,
Apr 2, 2012, 10:25:27 AM4/2/12
to Hasan Diwan, ggp...@googlegroups.com
On 2012-Apr-02, at 16:00 , Hasan Diwan wrote:
>
> Trying to plot an arima model (from the forecast CRAN package) using ggplot2, one generated using auto.arima. This object is of class "Arima" and can't be coerced into a data.frame directly. Any idea how to force this conversion? Many thanks! -- H

The canonical way would be to create a fortify.arima() function that extracts the relevant information from the arima object (and original data) and outputs a data.frame. I don't know of such a function.

See fortify.lm() for an example (and the naming conventions).

Jean-Olivier Irisson
---
Observatoire Océanologique
Station Zoologique, B.P. 28, Chemin du Lazaret
06230 Villefranche-sur-Mer
Tel: +33 04 93 76 38 04
Mob: +33 06 21 05 19 90
http://jo.irisson.com/

Hasan Diwan

unread,
Apr 2, 2012, 10:32:46 AM4/2/12
to ggp...@googlegroups.com
> On 2012-Apr-02, at 16:00 , Hasan Diwan wrote:
>>
>> Trying to plot an arima model (from the forecast CRAN package) using ggplot2, one generated using auto.arima. This object is of class "Arima" and can't be coerced into a data.frame directly. Any idea how to force this conversion? Many thanks! -- H
Sorted, I had:
fit <- auto.arima(as.ts(rnorm(100))
fitDF <- as.data.frame(fit)
What I needed was:
fit <- auto.arima(as.ts(rnorm(100))
fitDF <- as.data.frame(forecast(fit, h=10))
Sorry for the silly question. -- H
--
Sent from my mobile device
Envoyait de mon portable

fd

unread,
Apr 2, 2012, 3:28:12 PM4/2/12
to ggplot2
I dealt with this issue recently and did a blog post about it.

For an overview of a function to extract all the relevant data (fits,
confidence intervals, training data, et cetera) (my own quick and
dirty fortify()) see:

http://davenportspatialanalytics.squarespace.com/blog/2012/3/14/plotting-forecast-objects-in-ggplot-part-1-extracting-the-da.html

For an example of how to implement with simulated data, see:

http://davenportspatialanalytics.squarespace.com/blog/2012/3/21/plotting-forecast-objects-in-ggplot-part-2-visualize-observa.html

Hope that helps.

F
Reply all
Reply to author
Forward
0 new messages