Forecast using R-frames, zoos and real dates based on results

I have a CSV with values ​​like

ref_date;wings;airfoil;turbines
2015-03-31;123,22;22,77;99,0
2015-04-30;123,22;28,77;99,0
2015-05-31;123,22;22,177;02,0
2015-06-30;56,288;22,77;99,0

      

What I read in the dataframe and converted it to a time series using

df_agg = aggregate(df$wings, by=list(date=df$ref_date), FUN=mean)
df_agg['date'] = as.Date(df_agg[['date']], format='%Y-%m-%d')
tt = xts(df_agg[,c('x')], order.by=df_agg$date)

      

So now I have an object tt

, a xts

. To use a package forecast

, I have to convert it to an object ts

, so I use zoo

:

pred = forecast(zoo(tt))

df_pred = as.data.frame(pred)

      

But in order to be able to get the result with a column with dates instead of a sequence of numbers, I need to return it with

zoo(df_pred, as.Date(as.numeric(rownames(df_pred))))

      

and now I have a result like

             Forecast  Lo 80    Hi 80    Lo 95    Hi 95
2015-07-30   12          10      15      11       14
2015-08-31   13.4        11      15.4    11.2     13

      

Is there a way to do the same without having to navigate between the dataframe, xts, zoo, ts and zoo over and over again?

In Python, it would be something like

 from statsmodels.api import sm
 df = pd.read_csv(file_csv)
 df.index = pd.to_datetime(df.date, format='%Y-%m-%d')
 y_pred = sm.ARIMA(df)

      

+3


source to share


1 answer


1) zoo The version of the forecast package version has as.ts.forecast

, and the development version of the zoo (to become the zoo version 1.8.0) has an extended as.zoo.ts

one which by default applies yearmon / yearqtr for the ts series with frequencies 4 and 12. Together they would allow writing the question code much more compact. Since the forecast package does not have explicit support for non-ts time series, define a simple one-line zoo method for forecasting. Then read the data with read.zoo

. To keep this offline data usage in the note at the end we use text=Lines

, but in fact it will be replaced with something like myfile.dat

. The command read.zoo

also applies the yearmon class to the index and aggregates it using the value. Finally runforecast

and then convert the output to the zoo class. This entire sequence involves only one transformation, and the only reason that was needed is that the forecast only supports ts.

library(forecast)
library(zoo)

as.zoo.forecast <- function(x, ...) as.zoo(as.ts(x))

z <- read.zoo(text = Lines, header = TRUE, sep = ";", dec = ",", 
  FUN = as.yearmon, aggregate = mean)
f <- forecast(z$wings)
as.zoo(f)

      

Until these new package versions are released, you can run the code below above. Once the new forecast and zoo packs are released, the code above will suffice.

# taken from development verison of forecast package
as.ts.forecast <- function(x, ...){
   df <- ts(as.matrix(forecast:::as.data.frame.forecast(x)))
   tsp(df) <- tsp(x$mean)
   return(df)
}

# can use this until devel version of zoo released
as.zoo.ts <- function(x, ...) {
  z <- zoo:::as.zoo.ts(x)
  if (frequency(z) == 4) time(z) <- as.yearqtr(time(z))
  if (frequency(z) == 12) time(z) <- as.yearmon(time(z))
  z
}

      



2) ts Another possibility is to use the class "ts"

only using the zoo to read the data. Using the above z

and as.ts.forecast

(which, as mentioned, can be omitted after the pkg forecast version is current):

tt <- as.ts(z)
f <- forecast(tt[, "wings"])
as.ts(f)

      

Note: Above we used this input:

Lines <- "ref_date;wings;airfoil;turbines
2015-03-31;123,22;22,77;99,0
2015-04-30;123,22;28,77;99,0
2015-05-31;123,22;22,177;02,0
2015-06-30;56,288;22,77;99,0"

      

+4


source







All Articles