Convert zoo to ts before forecasting

I am trying to convert zoo objects to ts object.

I have a huge data.frame "test" with quarterly data that looks like this:

 date <- c("2010-07-04 09:45:00", "2010-07-04 10:00:00", "2010-07-04 10:15:00", "2010-07-04 10:30:00", "2010-07-04 10:45:00", "2010-07-04 11:00:00") nrv <- c("-147.241", "-609.778", "-432.289", "-340.418", "-73.96" , "-533.108") tt <- c("3510.7", "3608.5", "3835.7", "4003.7", "4018.8", "4411.9") test <- data.frame(date,nrv,tt) test 

I want to make some forecasts (mostly ARIMA ) and thought that the forecast package would be a good idea for this. First, I generated data away from characters.

 test$date <- strptime(test$date,format="%Y-%m-%d %H:%M") test$nrv <- as.numeric(as.character(test$nrv)) test$tt <- as.numeric(as.character(test$tt)) str(test) #date is POSIXlt object 

Since I needed to interpolate and build lags, I also used the zoo package, using the date variable as an index, which worked fine. The `zoo package was recommended to me when working with time series data.

 library(zoo) test.zoo <- zoo(test[,2:3],test[,1]) test.zoo #date is now the Index and and the zoo objects works nicely 

But then I realized that forecasting only works with ts objects. (It's true?)

When I tried to convert the zoo object to the ts object, my time index disappeared. I think this may be due to the wrong frequency. However, I am somewhat lost as to what will be the operating frequency for this dataset and with ts objects in general.

 test.ts <- as.ts(test.zoo) test.ts 

How to convert this zoo object back to a ts object that I can use for forecasting? Thanks!

+6
source share
2 answers

I had the same problem and decided to use it with the zooreg function. step1: use zooreg to convert the zoo object to a non-zoo, but ts is a similar object step2: use the ts function to further convert to the ts object

+2
source

The forecast package only works with ts objects, as you suspected.

You can use test.ts with the forecast package. for instance

 plot(forecast(test.ts[,1])) 
+1
source

Source: https://habr.com/ru/post/954013/


All Articles