forecast包仅支持ts对象?

19 篇文章 0 订阅

在使用forecast包的时候常常遇到的一个问题是其只支持ts对象(就我目前查阅到的资料来说,Rob Hyndman:The forecast package only works with ts objects),而ts对象又有诸多不便,在时间序列中常用的zoo对象貌似forecast又不支持。

在stackoverflow上查到一个问题,估计大家也经常会遇到,问题第一句话就是“I am struggling to convert azoo objects to ats object.”,相当有共鸣。贴出来,大家讨论讨论

 

原文地址:http://stackoverflow.com/questions/18856822/converting-zoo-to-ts-before-forecasting/18870931#18870931

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

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

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

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

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

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

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值