Btibert3
Btibert3

Reputation: 40146

Extract Date forecast package R

I am using the forecast package and saved the results of

t <- data.frame(forecast(prod.arima, h=4))

to a data frame. The row names represent my dates, which when printed look like

> t
         Point.Forecast    Lo.80    Hi.80    Lo.95     Hi.95
2010.856       812.9849 630.5707 995.3992 534.0064 1091.9634
2010.876       670.3363 485.1885 855.4840 387.1772  953.4953
2010.895       769.4848 584.2552 954.7145 486.2005 1052.7691
2010.914       692.8951 507.6630 878.1272 409.6070  976.1832

I am not close (yet) to getting this completed, but I am hoping to automate a LaTex report where I will print the forecasts to a report page and a plot of the forecast. I figure I can pass the dataframe to a LaTex table, but I need the dates to be formatted in a way where the end user will understand that they represent week ends (set to Sundays).

I converted my original time series with "readable" dates to a time series object using

ts(prod, start = 2009 +(31+28+31+5)/365, f=52 )

where the start of my timeseries was on April 5th, 2009 and represented weekly datapoints.

Any help you can provide will be very much appreciated. Needless to say I am pretty new to R.

Upvotes: 4

Views: 2607

Answers (1)

IRTFM
IRTFM

Reputation: 263342

I can think of two ways: one would be to use a function as.Date.cal.yr from the Epi package. Another would be to multiply those dates by the number of seconds in a year, and then pass that to as.POSIXct (but I haven't tested that one.) Of those two the first seems the least painful:

require(Epi)
as.Date.cal.yr(as.numeric(row.names(t)))
# [1] "2010-11-10" "2010-11-17" "2010-11-24" "2010-12-01"
as.POSIXlt(as.Date.cal.yr(as.numeric(row.names(t))))$wday
#  [1] 3 3 3 3
paste(weekdays(as.Date.cal.yr(as.numeric(row.names(t)))) , 
         as.Date.cal.yr(as.numeric(row.names(t))), sep=",")
 #   [1] "Wednesday,2010-11-10" "Wednesday,2010-11-17" "Wednesday,2010-11-24"
 #   [4] "Wednesday,2010-12-01"

So I'm not getting Sundays, since those would be wday==0's in the POSIXlt system. See the help for DateTimeClasses.

As for the LaTeX part of the question, install the Hmisc package and use function latex().

Upvotes: 5

Related Questions