2
votes

I have a CSV file with the format

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

and I want to use the forecast package to predict the next values of this time series. The forecast package only accepts a ts object, but so far all my attempts to create one failed. I tried to

1) Use zoo package

df = read.zoo(data_file, sep=';', dec=',', format="%Y-%m-%d", header=T)

but the data is truncated at the decimal point.

2) Use the zoo package with xts

df = read.zoo(datafile, sep=';', dec=',', format="%Y-%m-%d", header=T)
df_ts = ts(df)

The dates are nowhere to be seen, the index is just a sequence of numbers, like

1 123.22 22.77 99

3) Use read.csv and ts

df = read.zoo(datafile, sep=';', dec=',', format="%Y-%m-%d", header=T)
df_ts = ts(df)

4) Try using xts

df = read.csv(data_file, sep=';', header=T, dec=',')
tt = as.xts(df[,-1],order.by = as.Date(as.character(df[,1]), format = "%Y-%m-%d"))
forecast(tt) 
Error in `tsp<-`(`*tmp*`, value = tsp.y) : 
  invalid time series parameters specified

the result looses all information about the date, including the ref_date column, and now the forecast package gives nonsense as result.

What is the correct approach to create the object that the forecast library is waiting and can generate a forecast, maintaining the dates, including in the plots?

1
Sorry about that. - Ivan
Why do you think truncation occurs with the first method? I tried it on the sample and it didn't seem to loose precision. It did complain about having duplicate dates. What version of zoo did you use? I tested with zoo_1.7-14 . - MrFlick
Same version as yours. I don“t have repeated dates, fixing it now. - Ivan
The read.zoo command with the data shown works for me but note that it produces a zoo object, not a data.frame which the name df might suggests. - G. Grothendieck
So what is the best procedure to convert a CSV file with ISO dates to a object (dataframe? zoo? ts?) that can be used with the forecast package, that generates a graph with dates, not integer indexes? - Ivan

1 Answers

1
votes

I have been wrestling CSV data into ZOO/XTS objects and sympathize -- painful.

Suggest using as_xts() in the tidyquant package

as_xts(read_csv(file),ref_date)

You may need to coerce the resulting coredata() in the XTS object back to numeric.