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

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

问题描述

当将不规律的时间序列转换为常规时间序列时,我遇到了问题。以下简单示例可以找到:

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

我想创建一个定期的时间序列ts.d,看起来像这样c(1981,NA,NA,1984,1985)。

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类,我们可以这样做 as.Date(as.yearmon(t))使用动物园的 as.yearmon ;然而,我们还有另外一个问题,即不同的年份有不同的天数,所以没有办法使用日期来代表年份。

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

或者如果我们想要安全,我们可以做到这一点,这将迫使它是年度的,即使输入有机会较低的频率:频率(z)<-1; as.ts(z)或者只是从一开始就定义原始动物园系列的频率为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)将需要显式频率它的频率为 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天全站免登陆