将不规则时间序列转换为规则时间序列 [英] Convert a irregular time series to a regular time series

查看:28
本文介绍了将不规则时间序列转换为规则时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将不规则时间序列转换为规则时间序列时遇到问题.下面是一个简化的例子:

I am having a problem when converting irregular time series to regular time series. Below a simplified example can be found:

require(zoo)
t <- as.character(c(1981,1984,1985))
d <- c(1,3,6)
dt <- data.frame(d,t)
t <- as.Date(t,"%Y")
z <- zoo(d,t)
plot(z)
ts.d <- as.ts(as.zooreg(z,freq=1)) # create a regular ts object
ts.d # regular time series

我想创建一个像这样 c(1981,NA,NA,1984,1985) 的常规时间序列 ts.d.

I would like to create a regular time series ts.d that looks like this c(1981,NA,NA,1984,1985).

令人惊奇的是,我第一次运行它:它工作!但是当我想再次运行它或重复它(as.ts() 行)时,它会停止工作,我会得到一个很长的时间序列:

The amazing thing is that the first time that I run this: it works! but when I want to run it again or repeat it (the as.ts()line) it stops workings and I obtain a very long time series:

ts.d # regular time series
Time Series:
Start = 4299 
End = 5760 
Frequency = 1 
  [1]  1 NA NA NA NA NA NA NA NA NA NA NA NA NA
 [15] NA NA NA NA NA NA NA NA 

等等

出了什么问题?

推荐答案

正如已经指出的 as.Date(as.character(t), "%Y") 不正确,因为它没有给出所需的月份和日期.如果我们想将年份转换为 "Date" 类,我们可以使用 zoo 的 as.yearmonas.Date(as.yearmon(t))代码>;但是,那么我们会遇到更进一步的问题,即不同年份有不同的天数,因此无法使用日期来表示年份.

As has been pointed out the as.Date(as.character(t), "%Y") is incorrect as it does not give the desired month and day. If we wanted to convert years to "Date" class we could do this as.Date(as.yearmon(t)) using zoo's as.yearmon; however, then we would have the further problem that different years have different numbers of days so there is no way to have a regular series using dates to represent years.

真的,我们一开始就不想约会.我们只想使用多年,在这种情况下,它简化为:

Really we don't want dates in the first place. We just want to work with years in which case it simplifies to just:

> z <- zoo(c(1, 3, 6), c(1981, 1984, 1985))
> 
> as.ts(z)
Time Series:
Start = 1981 
End = 1985 
Frequency = 1 
[1]  1 NA NA  3  6

或者如果我们想要安全,我们可以这样做,这将强制它是年度的,即使输入的频率偶然较低:frequency(z) <- 1;as.ts(z) 或者只是将原始 zoo 系列定义为从头开始的频率为 1:

or if we want to be safe we could do this which will force it to be annual even if the input has, by chance, a lower frequency: frequency(z) <- 1; as.ts(z) or just define the original zoo series to have a frequency of 1 right from the beginning:

> z <- zoo(c(1, 3, 6), c(1981, 1984, 1985), frequency = 1)
> as.ts(z)
Time Series:
Start = 1981 
End = 1985 
Frequency = 1 
[1]  1 NA NA  3  6

在这个例子中它没有区别,但在这种情况下 z <- zoo(c(1, 3, 6), c(1981, 1983, 1985), frequency = 1) 需要显式的 frequency 来防止它具有 0.5 的频率.

With this example it does not make a difference but in this case z <- zoo(c(1, 3, 6), c(1981, 1983, 1985), frequency = 1) the explicit frequency would be needed to prevent it from having a frequency of 0.5 .

这篇关于将不规则时间序列转换为规则时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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