在预测之前将动物园转换为 ts [英] Converting zoo to ts before forecasting

查看:27
本文介绍了在预测之前将动物园转换为 ts的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力将 zoo 对象转换为 ts 对象.

I am struggling to convert a zoo objects to a ts object.

我有一个包含季度小时数据的巨大 data.frame测试",如下所示:

I have a huge data.frame "test" with quarterly hour data, which 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

我想做一些预测(主要是 ARIMA)并认为 forecast 包会是一个好主意.首先,我将数据格式化为字符.

I want to make some predictions (mostly ARIMA) and thought the forecastpackage would be a good idea for that. First of I formated the 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

因为我需要进行插值和构造滞后,所以我还使用了 zoo 包,使用日期变量作为索引,效果很好.在处理时间序列数据时,我向我推荐了 `zoo 包.

Since I needed to do an interpolation and construct lags, I also used the zoo package using the date variable as index, which worked great. The `zoo package was recommended to me while dealing 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

但后来我意识到预测似乎只适用于 ts 对象.(这是真的吗?)

But then I realized that forecasting only seems to work with ts objects. (Is that true?)

当我尝试将 zoo 对象转换为 ts 对象时,我的时间索引消失了.我认为这可能是由于没有使用正确的频率.然而,我对这个数据集的工作频率和一般的 ts 对象有些迷茫.

When I tried to convert the zoo object to a ts object, my time index disappeared. I think this might be due to not using a proper frequency. However I am somewhat lost as to what would be a working frequency for this dataset and with ts objects in general.

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

如何将这个 zoo 对象转换回可用于预测的 ts 对象?谢谢!

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

推荐答案

预测包仅适用于您怀疑的 ts 对象.

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

您可以将 test.ts 与预测包一起使用.例如

You can use test.ts with the forecast package. For example

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

这篇关于在预测之前将动物园转换为 ts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆